/* |
/* |
* jQuery UI Position @VERSION |
* jQuery UI Position @VERSION |
* |
* |
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about) |
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about) |
* Dual licensed under the MIT or GPL Version 2 licenses. |
* Dual licensed under the MIT or GPL Version 2 licenses. |
* http://jquery.org/license |
* http://jquery.org/license |
* |
* |
* http://docs.jquery.com/UI/Position |
* http://docs.jquery.com/UI/Position |
*/ |
*/ |
(function( $, undefined ) { |
(function( $, undefined ) { |
|
|
$.ui = $.ui || {}; |
$.ui = $.ui || {}; |
|
|
var horizontalPositions = /left|center|right/, |
var horizontalPositions = /left|center|right/, |
verticalPositions = /top|center|bottom/, |
verticalPositions = /top|center|bottom/, |
center = "center", |
center = "center", |
_position = $.fn.position, |
_position = $.fn.position, |
_offset = $.fn.offset; |
_offset = $.fn.offset; |
|
|
$.fn.position = function( options ) { |
$.fn.position = function( options ) { |
if ( !options || !options.of ) { |
if ( !options || !options.of ) { |
return _position.apply( this, arguments ); |
return _position.apply( this, arguments ); |
} |
} |
|
|
// make a copy, we don't want to modify arguments |
// make a copy, we don't want to modify arguments |
options = $.extend( {}, options ); |
options = $.extend( {}, options ); |
|
|
var target = $( options.of ), |
var target = $( options.of ), |
targetElem = target[0], |
targetElem = target[0], |
collision = ( options.collision || "flip" ).split( " " ), |
collision = ( options.collision || "flip" ).split( " " ), |
offset = options.offset ? options.offset.split( " " ) : [ 0, 0 ], |
offset = options.offset ? options.offset.split( " " ) : [ 0, 0 ], |
targetWidth, |
targetWidth, |
targetHeight, |
targetHeight, |
basePosition; |
basePosition; |
|
|
if ( targetElem.nodeType === 9 ) { |
if ( targetElem.nodeType === 9 ) { |
targetWidth = target.width(); |
targetWidth = target.width(); |
targetHeight = target.height(); |
targetHeight = target.height(); |
basePosition = { top: 0, left: 0 }; |
basePosition = { top: 0, left: 0 }; |
} else if ( $.isWindow( targetElem ) ) { |
} else if ( $.isWindow( targetElem ) ) { |
targetWidth = target.width(); |
targetWidth = target.width(); |
targetHeight = target.height(); |
targetHeight = target.height(); |
basePosition = { top: target.scrollTop(), left: target.scrollLeft() }; |
basePosition = { top: target.scrollTop(), left: target.scrollLeft() }; |
} else if ( targetElem.preventDefault ) { |
} else if ( targetElem.preventDefault ) { |
// force left top to allow flipping |
// force left top to allow flipping |
options.at = "left top"; |
options.at = "left top"; |
targetWidth = targetHeight = 0; |
targetWidth = targetHeight = 0; |
basePosition = { top: options.of.pageY, left: options.of.pageX }; |
basePosition = { top: options.of.pageY, left: options.of.pageX }; |
} else { |
} else { |
targetWidth = target.outerWidth(); |
targetWidth = target.outerWidth(); |
targetHeight = target.outerHeight(); |
targetHeight = target.outerHeight(); |
basePosition = target.offset(); |
basePosition = target.offset(); |
} |
} |
|
|
// force my and at to have valid horizontal and veritcal positions |
// force my and at to have valid horizontal and veritcal positions |
// if a value is missing or invalid, it will be converted to center |
// if a value is missing or invalid, it will be converted to center |
$.each( [ "my", "at" ], function() { |
$.each( [ "my", "at" ], function() { |
var pos = ( options[this] || "" ).split( " " ); |
var pos = ( options[this] || "" ).split( " " ); |
if ( pos.length === 1) { |
if ( pos.length === 1) { |
pos = horizontalPositions.test( pos[0] ) ? |
pos = horizontalPositions.test( pos[0] ) ? |
pos.concat( [center] ) : |
pos.concat( [center] ) : |
verticalPositions.test( pos[0] ) ? |
verticalPositions.test( pos[0] ) ? |
[ center ].concat( pos ) : |
[ center ].concat( pos ) : |
[ center, center ]; |
[ center, center ]; |
} |
} |
pos[ 0 ] = horizontalPositions.test( pos[0] ) ? pos[ 0 ] : center; |
pos[ 0 ] = horizontalPositions.test( pos[0] ) ? pos[ 0 ] : center; |
pos[ 1 ] = verticalPositions.test( pos[1] ) ? pos[ 1 ] : center; |
pos[ 1 ] = verticalPositions.test( pos[1] ) ? pos[ 1 ] : center; |
options[ this ] = pos; |
options[ this ] = pos; |
}); |
}); |
|
|
// normalize collision option |
// normalize collision option |
if ( collision.length === 1 ) { |
if ( collision.length === 1 ) { |
collision[ 1 ] = collision[ 0 ]; |
collision[ 1 ] = collision[ 0 ]; |
} |
} |
|
|
// normalize offset option |
// normalize offset option |
offset[ 0 ] = parseInt( offset[0], 10 ) || 0; |
offset[ 0 ] = parseInt( offset[0], 10 ) || 0; |
if ( offset.length === 1 ) { |
if ( offset.length === 1 ) { |
offset[ 1 ] = offset[ 0 ]; |
offset[ 1 ] = offset[ 0 ]; |
} |
} |
offset[ 1 ] = parseInt( offset[1], 10 ) || 0; |
offset[ 1 ] = parseInt( offset[1], 10 ) || 0; |
|
|
if ( options.at[0] === "right" ) { |
if ( options.at[0] === "right" ) { |
basePosition.left += targetWidth; |
basePosition.left += targetWidth; |
} else if ( options.at[0] === center ) { |
} else if ( options.at[0] === center ) { |
basePosition.left += targetWidth / 2; |
basePosition.left += targetWidth / 2; |
} |
} |
|
|
if ( options.at[1] === "bottom" ) { |
if ( options.at[1] === "bottom" ) { |
basePosition.top += targetHeight; |
basePosition.top += targetHeight; |
} else if ( options.at[1] === center ) { |
} else if ( options.at[1] === center ) { |
basePosition.top += targetHeight / 2; |
basePosition.top += targetHeight / 2; |
} |
} |
|
|
basePosition.left += offset[ 0 ]; |
basePosition.left += offset[ 0 ]; |
basePosition.top += offset[ 1 ]; |
basePosition.top += offset[ 1 ]; |
|
|
return this.each(function() { |
return this.each(function() { |
var elem = $( this ), |
var elem = $( this ), |
elemWidth = elem.outerWidth(), |
elemWidth = elem.outerWidth(), |
elemHeight = elem.outerHeight(), |
elemHeight = elem.outerHeight(), |
marginLeft = parseInt( $.curCSS( this, "marginLeft", true ) ) || 0, |
marginLeft = parseInt( $.curCSS( this, "marginLeft", true ) ) || 0, |
marginTop = parseInt( $.curCSS( this, "marginTop", true ) ) || 0, |
marginTop = parseInt( $.curCSS( this, "marginTop", true ) ) || 0, |
collisionWidth = elemWidth + marginLeft + |
collisionWidth = elemWidth + marginLeft + |
( parseInt( $.curCSS( this, "marginRight", true ) ) || 0 ), |
( parseInt( $.curCSS( this, "marginRight", true ) ) || 0 ), |
collisionHeight = elemHeight + marginTop + |
collisionHeight = elemHeight + marginTop + |
( parseInt( $.curCSS( this, "marginBottom", true ) ) || 0 ), |
( parseInt( $.curCSS( this, "marginBottom", true ) ) || 0 ), |
position = $.extend( {}, basePosition ), |
position = $.extend( {}, basePosition ), |
collisionPosition; |
collisionPosition; |
|
|
if ( options.my[0] === "right" ) { |
if ( options.my[0] === "right" ) { |
position.left -= elemWidth; |
position.left -= elemWidth; |
} else if ( options.my[0] === center ) { |
} else if ( options.my[0] === center ) { |
position.left -= elemWidth / 2; |
position.left -= elemWidth / 2; |
} |
} |
|
|
if ( options.my[1] === "bottom" ) { |
if ( options.my[1] === "bottom" ) { |
position.top -= elemHeight; |
position.top -= elemHeight; |
} else if ( options.my[1] === center ) { |
} else if ( options.my[1] === center ) { |
position.top -= elemHeight / 2; |
position.top -= elemHeight / 2; |
} |
} |
|
|
// prevent fractions (see #5280) |
// prevent fractions (see #5280) |
position.left = Math.round( position.left ); |
position.left = Math.round( position.left ); |
position.top = Math.round( position.top ); |
position.top = Math.round( position.top ); |
|
|
collisionPosition = { |
collisionPosition = { |
left: position.left - marginLeft, |
left: position.left - marginLeft, |
top: position.top - marginTop |
top: position.top - marginTop |
}; |
}; |
|
|
$.each( [ "left", "top" ], function( i, dir ) { |
$.each( [ "left", "top" ], function( i, dir ) { |
if ( $.ui.position[ collision[i] ] ) { |
if ( $.ui.position[ collision[i] ] ) { |
$.ui.position[ collision[i] ][ dir ]( position, { |
$.ui.position[ collision[i] ][ dir ]( position, { |
targetWidth: targetWidth, |
targetWidth: targetWidth, |
targetHeight: targetHeight, |
targetHeight: targetHeight, |
elemWidth: elemWidth, |
elemWidth: elemWidth, |
elemHeight: elemHeight, |
elemHeight: elemHeight, |
collisionPosition: collisionPosition, |
collisionPosition: collisionPosition, |
collisionWidth: collisionWidth, |
collisionWidth: collisionWidth, |
collisionHeight: collisionHeight, |
collisionHeight: collisionHeight, |
offset: offset, |
offset: offset, |
my: options.my, |
my: options.my, |
at: options.at |
at: options.at |
}); |
}); |
} |
} |
}); |
}); |
|
|
if ( $.fn.bgiframe ) { |
if ( $.fn.bgiframe ) { |
elem.bgiframe(); |
elem.bgiframe(); |
} |
} |
elem.offset( $.extend( position, { using: options.using } ) ); |
elem.offset( $.extend( position, { using: options.using } ) ); |
}); |
}); |
}; |
}; |
|
|
$.ui.position = { |
$.ui.position = { |
fit: { |
fit: { |
left: function( position, data ) { |
left: function( position, data ) { |
var win = $( window ), |
var win = $( window ), |
over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft(); |
over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft(); |
position.left = over > 0 ? position.left - over : Math.max( position.left - data.collisionPosition.left, position.left ); |
position.left = over > 0 ? position.left - over : Math.max( position.left - data.collisionPosition.left, position.left ); |
}, |
}, |
top: function( position, data ) { |
top: function( position, data ) { |
var win = $( window ), |
var win = $( window ), |
over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop(); |
over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop(); |
position.top = over > 0 ? position.top - over : Math.max( position.top - data.collisionPosition.top, position.top ); |
position.top = over > 0 ? position.top - over : Math.max( position.top - data.collisionPosition.top, position.top ); |
} |
} |
}, |
}, |
|
|
flip: { |
flip: { |
left: function( position, data ) { |
left: function( position, data ) { |
if ( data.at[0] === center ) { |
if ( data.at[0] === center ) { |
return; |
return; |
} |
} |
var win = $( window ), |
var win = $( window ), |
over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft(), |
over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft(), |
myOffset = data.my[ 0 ] === "left" ? |
myOffset = data.my[ 0 ] === "left" ? |
-data.elemWidth : |
-data.elemWidth : |
data.my[ 0 ] === "right" ? |
data.my[ 0 ] === "right" ? |
data.elemWidth : |
data.elemWidth : |
0, |
0, |
atOffset = data.at[ 0 ] === "left" ? |
atOffset = data.at[ 0 ] === "left" ? |
data.targetWidth : |
data.targetWidth : |
-data.targetWidth, |
-data.targetWidth, |
offset = -2 * data.offset[ 0 ]; |
offset = -2 * data.offset[ 0 ]; |
position.left += data.collisionPosition.left < 0 ? |
position.left += data.collisionPosition.left < 0 ? |
myOffset + atOffset + offset : |
myOffset + atOffset + offset : |
over > 0 ? |
over > 0 ? |
myOffset + atOffset + offset : |
myOffset + atOffset + offset : |
0; |
0; |
}, |
}, |
top: function( position, data ) { |
top: function( position, data ) { |
if ( data.at[1] === center ) { |
if ( data.at[1] === center ) { |
return; |
return; |
} |
} |
var win = $( window ), |
var win = $( window ), |
over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop(), |
over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop(), |
myOffset = data.my[ 1 ] === "top" ? |
myOffset = data.my[ 1 ] === "top" ? |
-data.elemHeight : |
-data.elemHeight : |
data.my[ 1 ] === "bottom" ? |
data.my[ 1 ] === "bottom" ? |
data.elemHeight : |
data.elemHeight : |
0, |
0, |
atOffset = data.at[ 1 ] === "top" ? |
atOffset = data.at[ 1 ] === "top" ? |
data.targetHeight : |
data.targetHeight : |
-data.targetHeight, |
-data.targetHeight, |
offset = -2 * data.offset[ 1 ]; |
offset = -2 * data.offset[ 1 ]; |
position.top += data.collisionPosition.top < 0 ? |
position.top += data.collisionPosition.top < 0 ? |
myOffset + atOffset + offset : |
myOffset + atOffset + offset : |
over > 0 ? |
over > 0 ? |
myOffset + atOffset + offset : |
myOffset + atOffset + offset : |
0; |
0; |
} |
} |
} |
} |
}; |
}; |
|
|
// offset setter from jQuery 1.4 |
// offset setter from jQuery 1.4 |
if ( !$.offset.setOffset ) { |
if ( !$.offset.setOffset ) { |
$.offset.setOffset = function( elem, options ) { |
$.offset.setOffset = function( elem, options ) { |
// set position first, in-case top/left are set even on static elem |
// set position first, in-case top/left are set even on static elem |
if ( /static/.test( $.curCSS( elem, "position" ) ) ) { |
if ( /static/.test( $.curCSS( elem, "position" ) ) ) { |
elem.style.position = "relative"; |
elem.style.position = "relative"; |
} |
} |
var curElem = $( elem ), |
var curElem = $( elem ), |
curOffset = curElem.offset(), |
curOffset = curElem.offset(), |
curTop = parseInt( $.curCSS( elem, "top", true ), 10 ) || 0, |
curTop = parseInt( $.curCSS( elem, "top", true ), 10 ) || 0, |
curLeft = parseInt( $.curCSS( elem, "left", true ), 10) || 0, |
curLeft = parseInt( $.curCSS( elem, "left", true ), 10) || 0, |
props = { |
props = { |
top: (options.top - curOffset.top) + curTop, |
top: (options.top - curOffset.top) + curTop, |
left: (options.left - curOffset.left) + curLeft |
left: (options.left - curOffset.left) + curLeft |
}; |
}; |
|
|
if ( 'using' in options ) { |
if ( 'using' in options ) { |
options.using.call( elem, props ); |
options.using.call( elem, props ); |
} else { |
} else { |
curElem.css( props ); |
curElem.css( props ); |
} |
} |
}; |
}; |
|
|
$.fn.offset = function( options ) { |
$.fn.offset = function( options ) { |
var elem = this[ 0 ]; |
var elem = this[ 0 ]; |
if ( !elem || !elem.ownerDocument ) { return null; } |
if ( !elem || !elem.ownerDocument ) { return null; } |
if ( options ) { |
if ( options ) { |
return this.each(function() { |
return this.each(function() { |
$.offset.setOffset( this, options ); |
$.offset.setOffset( this, options ); |
}); |
}); |
} |
} |
return _offset.call( this ); |
return _offset.call( this ); |
}; |
}; |
} |
} |
|
|
}( jQuery )); |
}( jQuery )); |
|
|