Advertisement
Guest User

Untitled

a guest
Jul 6th, 2011
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * jQuery UI Position @VERSION
  3.  *
  4.  * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
  5.  * Dual licensed under the MIT or GPL Version 2 licenses.
  6.  * http://jquery.org/license
  7.  *
  8.  * http://docs.jquery.com/UI/Position
  9.  */
  10. (function( $, undefined ) {
  11.  
  12. $.ui = $.ui || {};
  13.  
  14. var rhorizontal = /left|center|right/,
  15.     rvertical = /top|center|bottom/,
  16.     roffset = /[+-]\d+%?/,
  17.     rposition = /^\w+/,
  18.     rpercent = /%$/,
  19.     center = "center",
  20.     _position = $.fn.position;
  21.  
  22. $.position = {
  23.     scrollbarWidth: function() {
  24.         var w1, w2,
  25.             div = $( "<div style='display:block;width:50px;height:50px;overflow:hidden;'><div style='height:100px;width:auto;'></div></div>" ),
  26.             innerDiv = div.children()[0];
  27.  
  28.         $( "body" ).append( div );
  29.         w1 = innerDiv.offsetWidth;
  30.         div.css( "overflow", "scroll" );
  31.  
  32.         w2 = innerDiv.offsetWidth;
  33.  
  34.         if ( w1 === w2 ) {
  35.             w2 = div[0].clientWidth;
  36.         }
  37.  
  38.         div.remove();
  39.  
  40.         return w1 - w2;
  41.     },
  42.     getScrollInfo: function( within ) {
  43.         var that = within[0],
  44.             scrollHeight = within.height() < that.scrollHeight,
  45.             scrollWidth = within.width() < that.scrollWidth,
  46.             scrollbarWidth = $.position.scrollbarWidth();
  47.  
  48.         return {
  49.             height: scrollHeight ? scrollbarWidth : 0,
  50.             width : scrollWidth ? scrollbarWidth : 0
  51.         };
  52.     }
  53. };
  54.  
  55. $.fn.position = function( options ) {
  56.     if ( !options || !options.of ) {
  57.         return _position.apply( this, arguments );
  58.     }
  59.  
  60.     // make a copy, we don't want to modify arguments
  61.     options = $.extend( {}, options );
  62.  
  63.     var target = $( options.of ),
  64.         within  = $( options.within || window ),
  65.         targetElem = target[0],
  66.         collision = ( options.collision || "flip" ).split( " " ),
  67.         offsets = {},
  68.         atOffset,
  69.         targetWidth,
  70.         targetHeight,
  71.         basePosition;
  72.  
  73.     if ( targetElem.nodeType === 9 ) {
  74.         targetWidth = target.width();
  75.         targetHeight = target.height();
  76.         basePosition = { top: 0, left: 0 };
  77.     } else if ( $.isWindow( targetElem ) ) {
  78.         targetWidth = target.width();
  79.         targetHeight = target.height();
  80.         basePosition = { top: target.scrollTop(), left: target.scrollLeft() };
  81.     } else if ( targetElem.preventDefault ) {
  82.         // force left top to allow flipping
  83.         options.at = "left top";
  84.         targetWidth = targetHeight = 0;
  85.         basePosition = { top: options.of.pageY, left: options.of.pageX };
  86.     } else {
  87.         targetWidth = target.outerWidth();
  88.         targetHeight = target.outerHeight();
  89.         basePosition = target.offset();
  90.     }
  91.  
  92.     // force my and at to have valid horizontal and vertical positions
  93.     // if a value is missing or invalid, it will be converted to center
  94.     $.each( [ "my", "at" ], function() {
  95.         var pos = ( options[ this ] || "" ).split( " " ),
  96.             horizontalOffset,
  97.             verticalOffset;
  98.  
  99.         if ( pos.length === 1) {
  100.             pos = rhorizontal.test( pos[ 0 ] ) ?
  101.                 pos.concat( [ center ] ) :
  102.                 rvertical.test( pos[ 0 ] ) ?
  103.                     [ center ].concat( pos ) :
  104.                     [ center, center ];
  105.         }
  106.         pos[ 0 ] = rhorizontal.test( pos[ 0 ] ) ? pos[ 0 ] : center;
  107.         pos[ 1 ] = rvertical.test( pos[ 1 ] ) ? pos[ 1 ] : center;
  108.  
  109.         // calculate offsets
  110.         horizontalOffset = roffset.exec( pos[ 0 ] );
  111.         verticalOffset = roffset.exec( pos[ 1 ] );
  112.         offsets[ this ] = [
  113.             horizontalOffset ? horizontalOffset[ 0 ] : 0,
  114.             verticalOffset ? verticalOffset[ 0 ] : 0
  115.         ];
  116.  
  117.         // reduce to just the positions without the offsets
  118.         options[ this ] = [
  119.             rposition.exec( pos[ 0 ] )[ 0 ],
  120.             rposition.exec( pos[ 1 ] )[ 0 ]
  121.         ];
  122.     });
  123.  
  124.     // normalize collision option
  125.     if ( collision.length === 1 ) {
  126.         collision[ 1 ] = collision[ 0 ];
  127.     }
  128.  
  129.     if ( options.at[ 0 ] === "right" ) {
  130.         basePosition.left += targetWidth;
  131.     } else if ( options.at[ 0 ] === center ) {
  132.         basePosition.left += targetWidth / 2;
  133.     }
  134.  
  135.     if ( options.at[ 1 ] === "bottom" ) {
  136.         basePosition.top += targetHeight;
  137.     } else if ( options.at[ 1 ] === center ) {
  138.         basePosition.top += targetHeight / 2;
  139.     }
  140.  
  141.     atOffset = [
  142.         parseInt( offsets.at[ 0 ], 10 ) *
  143.             ( rpercent.test( offsets.at[ 0 ] ) ? targetWidth / 100 : 1 ),
  144.         parseInt( offsets.at[ 1 ], 10 ) *
  145.             ( rpercent.test( offsets.at[ 1 ] ) ? targetHeight / 100 : 1 )
  146.     ];
  147.     basePosition.left += atOffset[ 0 ];
  148.     basePosition.top += atOffset[ 1 ];
  149.  
  150.     return this.each(function() {
  151.         var elem = $( this ),
  152.             elemWidth = elem.outerWidth(),
  153.             elemHeight = elem.outerHeight(),
  154.             marginLeft = parseInt( $.curCSS( this, "marginLeft", true ) ) || 0,
  155.             marginTop = parseInt( $.curCSS( this, "marginTop", true ) ) || 0,
  156.             scrollInfo = $.position.getScrollInfo( within ),
  157.             collisionWidth = elemWidth + marginLeft +
  158.                 ( parseInt( $.curCSS( this, "marginRight", true ) ) || 0 ) + scrollInfo.width,
  159.             collisionHeight = elemHeight + marginTop +
  160.                 ( parseInt( $.curCSS( this, "marginBottom", true ) ) || 0 ) + scrollInfo.height,
  161.             position = $.extend( {}, basePosition ),
  162.             myOffset = [
  163.                 parseInt( offsets.my[ 0 ], 10 ) *
  164.                     ( rpercent.test( offsets.my[ 0 ] ) ? elem.outerWidth() / 100 : 1 ),
  165.                 parseInt( offsets.my[ 1 ], 10 ) *
  166.                     ( rpercent.test( offsets.my[ 1 ] ) ? elem.outerHeight() / 100 : 1 )
  167.             ],
  168.             collisionPosition;
  169.  
  170.         if ( options.my[ 0 ] === "right" ) {
  171.             position.left -= elemWidth;
  172.         } else if ( options.my[ 0 ] === center ) {
  173.             position.left -= elemWidth / 2;
  174.         }
  175.  
  176.         if ( options.my[ 1 ] === "bottom" ) {
  177.             position.top -= elemHeight;
  178.         } else if ( options.my[ 1 ] === center ) {
  179.             position.top -= elemHeight / 2;
  180.         }
  181.  
  182.         position.left += myOffset[ 0 ];
  183.         position.top += myOffset[ 1 ];
  184.  
  185.         // prevent fractions (see #5280)
  186.         position.left = Math.round( position.left );
  187.         position.top = Math.round( position.top );
  188.  
  189.         collisionPosition = {
  190.             left: position.left - marginLeft,
  191.             top: position.top - marginTop
  192.         };
  193.  
  194.         $.each( [ "left", "top" ], function( i, dir ) {
  195.             if ( $.ui.position[ collision[ i ] ] ) {
  196.                 $.ui.position[ collision[ i ] ][ dir ]( position, {
  197.                     targetWidth: targetWidth,
  198.                     targetHeight: targetHeight,
  199.                     elemWidth: elemWidth,
  200.                     elemHeight: elemHeight,
  201.                     collisionPosition: collisionPosition,
  202.                     collisionWidth: collisionWidth,
  203.                     collisionHeight: collisionHeight,
  204.                     offset: [ atOffset[ 0 ] + myOffset[ 0 ], atOffset [ 1 ] + myOffset[ 1 ] ],
  205.                     my: options.my,
  206.                     at: options.at,
  207.                     within: within,
  208.                     el : elem
  209.                 });
  210.             }
  211.         });
  212.  
  213.         if ( $.fn.bgiframe ) {
  214.             elem.bgiframe();
  215.         }
  216.         elem.offset( $.extend( position, { using: options.using } ) );
  217.     });
  218. };
  219.  
  220. $.ui.position = {
  221.     fit: {
  222.         left: function( position, data ) {
  223.             var within = data.within,
  224.                 win = $( window ),
  225.                 isWindow = $.isWindow( data.within[0] ),
  226.                 withinOffset = isWindow ? win.scrollLeft() : within.offset().left,
  227.                 outerWidth = isWindow ? win.width() : within.outerWidth(),
  228.                 overLeft = withinOffset - data.collisionPosition.left,
  229.                 overRight = data.collisionPosition.left + data.collisionWidth - outerWidth - withinOffset;
  230.  
  231.             // element is wider than window or too far left -> align with left edge
  232.             if ( data.collisionWidth > outerWidth || overLeft > 0 ) {
  233.                 position.left += overLeft;
  234.             // too far right -> align with right edge
  235.             } else if ( overRight > 0 ) {
  236.                 position.left -= overRight;
  237.             // adjust based on position and margin
  238.             } else {
  239.                 position.left = Math.max( position.left - data.collisionPosition.left, position.left );
  240.             }
  241.         },
  242.         top: function( position, data ) {
  243.             var within = data.within,
  244.                 win = $( window ),
  245.                 isWindow = $.isWindow( data.within[0] ),
  246.                 withinOffset = isWindow ? win.scrollTop() : within.offset().top,
  247.                 outerHeight = isWindow ? win.height() : within.outerHeight(),
  248.                 overTop = withinOffset - data.collisionPosition.top,
  249.                 overBottom = data.collisionPosition.top + data.collisionHeight - outerHeight - withinOffset;
  250.  
  251.             // element is taller than window or too far up -> align with top edge
  252.             if ( data.collisionHeight > outerHeight || overTop > 0 ) {
  253.                 position.top += overTop;
  254.             // too far down -> align with bottom edge
  255.             } else if ( overBottom > 0 ) {
  256.                 position.top -= overBottom;
  257.             // adjust based on position and margin
  258.             } else {
  259.                 position.top = Math.max( position.top - data.collisionPosition.top, position.top );
  260.             }
  261.         }
  262.     },
  263.     flip: {
  264.         left: function( position, data ) {
  265.             if ( data.at[ 0 ] === center ) {
  266.                 return;
  267.             }
  268.  
  269.             var within = data.within,
  270.                 win = $( window ),
  271.                 isWindow = $.isWindow( data.within[0] ),
  272.                 withinOffset = isWindow ? 0 : within.offset().left,
  273.                 outerWidth = isWindow ? within.width() : within.outerWidth(),
  274.                 overLeft = data.collisionPosition.left - withinOffset,
  275.                 overRight = data.collisionPosition.left + data.collisionWidth - outerWidth - withinOffset,
  276.                 left = data.my[ 0 ] === "left",
  277.                 myOffset = data.my[ 0 ] === "left" ?
  278.                     -data.elemWidth :
  279.                     data.my[ 0 ] === "right" ?
  280.                         data.elemWidth :
  281.                         0,
  282.                 atOffset = data.at[ 0 ] === "left" ?
  283.                     data.targetWidth :
  284.                     -data.targetWidth,
  285.                 offset = -2 * data.offset[ 0 ];
  286.             if ( overLeft < 0 || overRight > 0 ) {
  287.                 data.el.addClass( 'ui-flipped-' + ( overLeft < 0 ? 'right' : 'left' ) );
  288.                 position.left += myOffset + atOffset + offset;
  289.             }
  290.         },
  291.         top: function( position, data ) {
  292.             if ( data.at[ 1 ] === center ) {
  293.                 return;
  294.             }
  295.             var within = data.within,
  296.                 win = $( window ),
  297.                 isWindow = $.isWindow( data.within[0] ),
  298.                 withinOffset = isWindow ? 0 : within.offset().top,
  299.                 outerHeight = isWindow ? within.height() : within.outerHeight(),
  300.                 overTop = data.collisionPosition.top - withinOffset,
  301.                 overBottom = data.collisionPosition.top + data.collisionHeight - outerHeight - withinOffset,
  302.                 top = data.my[ 1 ] === "top",
  303.                 myOffset = top ?
  304.                     -data.elemHeight :
  305.                     data.my[ 1 ] === "bottom" ?
  306.                         data.elemHeight :
  307.                         0,
  308.                 atOffset = data.at[ 1 ] === "top" ?
  309.                     data.targetHeight :
  310.                     -data.targetHeight,
  311.                 offset = -2 * data.offset[ 1 ];
  312.             if ( overTop < 0 || overBottom > 0 ) {
  313.                 data.el.addClass( 'ui-flipped-' + ( overLeft < 0 ? 'bottom' : 'top' ) );
  314.                 position.top += myOffset + atOffset + offset;
  315.             }
  316.         }
  317.     }
  318. };
  319.  
  320. // DEPRECATED
  321. if ( $.uiBackCompat !== false ) {
  322.     // offset option
  323.     (function( $ ) {
  324.         var _position = $.fn.position;
  325.         $.fn.position = function( options ) {
  326.             if ( !options || !options.offset ) {
  327.                 return _position.call( this, options );
  328.             }
  329.             var offset = options.offset.split( " " ),
  330.                 at = options.at.split( " " );
  331.             if ( offset.length === 1 ) {
  332.                 offset[ 1 ] = offset[ 0 ];
  333.             }
  334.             if ( /^\d/.test( offset[ 0 ] ) ) {
  335.                 offset[ 0 ] = "+" + offset[ 0 ];
  336.             }
  337.             if ( /^\d/.test( offset[ 1 ] ) ) {
  338.                 offset[ 1 ] = "+" + offset[ 1 ];
  339.             }
  340.             if ( at.length === 1 ) {
  341.                 if ( /left|center|right/.test( at[ 0 ] ) ) {
  342.                     at[ 1 ] = "center";
  343.                 } else {
  344.                     at[ 1 ] = at[ 0 ];
  345.                     at[ 0 ] = "center";
  346.                 }
  347.             }
  348.             return _position.call( this, $.extend( options, {
  349.                 at: at[ 0 ] + offset[ 0 ] + " " + at[ 1 ] + offset[ 1 ],
  350.                 offset: undefined
  351.             } ) );
  352.         }
  353.     }( jQuery ) );
  354. }
  355.  
  356. }( jQuery ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement