@@ -13,6 +13,9 @@ $.ui = $.ui || {};
1313
1414var horizontalPositions = / l e f t | c e n t e r | r i g h t / ,
1515 verticalPositions = / t o p | c e n t e r | b o t t o m / ,
16+ roffset = / [ + - ] \d + % ? / ,
17+ rposition = / ^ \w + / ,
18+ rpercent = / % $ / ,
1619 center = "center" ,
1720 _position = $ . fn . position ;
1821
@@ -27,7 +30,8 @@ $.fn.position = function( options ) {
2730 var target = $ ( options . of ) ,
2831 targetElem = target [ 0 ] ,
2932 collision = ( options . collision || "flip" ) . split ( " " ) ,
30- offset = options . offset ? options . offset . split ( " " ) : [ 0 , 0 ] ,
33+ offsets = { } ,
34+ atOffset ,
3135 targetWidth ,
3236 targetHeight ,
3337 basePosition ;
@@ -54,7 +58,10 @@ $.fn.position = function( options ) {
5458 // force my and at to have valid horizontal and vertical positions
5559 // if a value is missing or invalid, it will be converted to center
5660 $ . each ( [ "my" , "at" ] , function ( ) {
57- var pos = ( options [ this ] || "" ) . split ( " " ) ;
61+ var pos = ( options [ this ] || "" ) . split ( " " ) ,
62+ horizontalOffset ,
63+ verticalOffset ;
64+
5865 if ( pos . length === 1 ) {
5966 pos = horizontalPositions . test ( pos [ 0 ] ) ?
6067 pos . concat ( [ center ] ) :
@@ -64,21 +71,27 @@ $.fn.position = function( options ) {
6471 }
6572 pos [ 0 ] = horizontalPositions . test ( pos [ 0 ] ) ? pos [ 0 ] : center ;
6673 pos [ 1 ] = verticalPositions . test ( pos [ 1 ] ) ? pos [ 1 ] : center ;
67- options [ this ] = pos ;
74+
75+ // calculate offsets
76+ horizontalOffset = roffset . exec ( pos [ 0 ] ) ;
77+ verticalOffset = roffset . exec ( pos [ 1 ] ) ;
78+ offsets [ this ] = [
79+ horizontalOffset ? horizontalOffset [ 0 ] : 0 ,
80+ verticalOffset ? verticalOffset [ 0 ] : 0
81+ ] ;
82+
83+ // reduce to just the positions without the offsets
84+ options [ this ] = [
85+ rposition . exec ( pos [ 0 ] ) [ 0 ] ,
86+ rposition . exec ( pos [ 1 ] ) [ 0 ]
87+ ] ;
6888 } ) ;
6989
7090 // normalize collision option
7191 if ( collision . length === 1 ) {
7292 collision [ 1 ] = collision [ 0 ] ;
7393 }
7494
75- // normalize offset option
76- offset [ 0 ] = parseInt ( offset [ 0 ] , 10 ) || 0 ;
77- if ( offset . length === 1 ) {
78- offset [ 1 ] = offset [ 0 ] ;
79- }
80- offset [ 1 ] = parseInt ( offset [ 1 ] , 10 ) || 0 ;
81-
8295 if ( options . at [ 0 ] === "right" ) {
8396 basePosition . left += targetWidth ;
8497 } else if ( options . at [ 0 ] === center ) {
@@ -91,8 +104,14 @@ $.fn.position = function( options ) {
91104 basePosition . top += targetHeight / 2 ;
92105 }
93106
94- basePosition . left += offset [ 0 ] ;
95- basePosition . top += offset [ 1 ] ;
107+ atOffset = [
108+ parseInt ( offsets . at [ 0 ] , 10 ) *
109+ ( rpercent . test ( offsets . at [ 0 ] ) ? targetWidth / 100 : 1 ) ,
110+ parseInt ( offsets . at [ 1 ] , 10 ) *
111+ ( rpercent . test ( offsets . at [ 1 ] ) ? targetHeight / 100 : 1 )
112+ ] ;
113+ basePosition . left += atOffset [ 0 ] ,
114+ basePosition . top += atOffset [ 1 ] ;
96115
97116 return this . each ( function ( ) {
98117 var elem = $ ( this ) ,
@@ -105,6 +124,12 @@ $.fn.position = function( options ) {
105124 collisionHeight = elemHeight + marginTop +
106125 ( parseInt ( $ . curCSS ( this , "marginBottom" , true ) ) || 0 ) ,
107126 position = $ . extend ( { } , basePosition ) ,
127+ myOffset = [
128+ parseInt ( offsets . my [ 0 ] , 10 ) *
129+ ( rpercent . test ( offsets . my [ 0 ] ) ? elem . outerWidth ( ) / 100 : 1 ) ,
130+ parseInt ( offsets . my [ 1 ] , 10 ) *
131+ ( rpercent . test ( offsets . my [ 1 ] ) ? elem . outerHeight ( ) / 100 : 1 )
132+ ] ,
108133 collisionPosition ;
109134
110135 if ( options . my [ 0 ] === "right" ) {
@@ -119,6 +144,9 @@ $.fn.position = function( options ) {
119144 position . top -= elemHeight / 2 ;
120145 }
121146
147+ position . left += myOffset [ 0 ] ;
148+ position . top += myOffset [ 1 ] ;
149+
122150 // prevent fractions (see #5280)
123151 position . left = Math . round ( position . left ) ;
124152 position . top = Math . round ( position . top ) ;
@@ -138,7 +166,7 @@ $.fn.position = function( options ) {
138166 collisionPosition : collisionPosition ,
139167 collisionWidth : collisionWidth ,
140168 collisionHeight : collisionHeight ,
141- offset : offset ,
169+ offset : [ atOffset [ 0 ] + myOffset [ 0 ] , atOffset [ 1 ] + myOffset [ 1 ] ] ,
142170 my : options . my ,
143171 at : options . at
144172 } ) ;
@@ -212,4 +240,40 @@ $.ui.position = {
212240 }
213241} ;
214242
243+ // DEPRECATED
244+ if ( $ . uiBackCompat !== false ) {
245+ // offset option
246+ ( function ( $ ) {
247+ var _position = $ . fn . position ;
248+ $ . fn . position = function ( options ) {
249+ if ( ! options || ! ( "offset" in options ) ) {
250+ return _position . call ( this , options ) ;
251+ }
252+ var offset = options . offset . split ( " " ) ,
253+ at = options . at . split ( " " ) ;
254+ if ( offset . length === 1 ) {
255+ offset [ 1 ] = offset [ 0 ] ;
256+ }
257+ if ( / ^ \d / . test ( offset [ 0 ] ) ) {
258+ offset [ 0 ] = "+" + offset [ 0 ] ;
259+ }
260+ if ( / ^ \d / . test ( offset [ 1 ] ) ) {
261+ offset [ 1 ] = "+" + offset [ 1 ] ;
262+ }
263+ if ( at . length === 1 ) {
264+ if ( / l e f t | c e n t e r | r i g h t / . test ( at [ 0 ] ) ) {
265+ at [ 1 ] = "center" ;
266+ } else {
267+ at [ 1 ] = at [ 0 ] ;
268+ at [ 0 ] = "center" ;
269+ }
270+ }
271+ return _position . call ( this , $ . extend ( options , {
272+ at : at [ 0 ] + offset [ 0 ] + " " + at [ 1 ] + offset [ 1 ] ,
273+ offset : undefined
274+ } ) ) ;
275+ }
276+ } ( jQuery ) ) ;
277+ }
278+
215279} ( jQuery ) ) ;
0 commit comments