@@ -13,6 +13,9 @@ $.ui = $.ui || {};
13
13
14
14
var horizontalPositions = / l e f t | c e n t e r | r i g h t / ,
15
15
verticalPositions = / t o p | c e n t e r | b o t t o m / ,
16
+ roffset = / [ + - ] \d + % ? / ,
17
+ rposition = / ^ \w + / ,
18
+ rpercent = / % $ / ,
16
19
center = "center" ,
17
20
_position = $ . fn . position ;
18
21
@@ -27,7 +30,8 @@ $.fn.position = function( options ) {
27
30
var target = $ ( options . of ) ,
28
31
targetElem = target [ 0 ] ,
29
32
collision = ( options . collision || "flip" ) . split ( " " ) ,
30
- offset = options . offset ? options . offset . split ( " " ) : [ 0 , 0 ] ,
33
+ offsets = { } ,
34
+ atOffset ,
31
35
targetWidth ,
32
36
targetHeight ,
33
37
basePosition ;
@@ -54,7 +58,10 @@ $.fn.position = function( options ) {
54
58
// force my and at to have valid horizontal and vertical positions
55
59
// if a value is missing or invalid, it will be converted to center
56
60
$ . each ( [ "my" , "at" ] , function ( ) {
57
- var pos = ( options [ this ] || "" ) . split ( " " ) ;
61
+ var pos = ( options [ this ] || "" ) . split ( " " ) ,
62
+ horizontalOffset ,
63
+ verticalOffset ;
64
+
58
65
if ( pos . length === 1 ) {
59
66
pos = horizontalPositions . test ( pos [ 0 ] ) ?
60
67
pos . concat ( [ center ] ) :
@@ -64,21 +71,27 @@ $.fn.position = function( options ) {
64
71
}
65
72
pos [ 0 ] = horizontalPositions . test ( pos [ 0 ] ) ? pos [ 0 ] : center ;
66
73
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
+ ] ;
68
88
} ) ;
69
89
70
90
// normalize collision option
71
91
if ( collision . length === 1 ) {
72
92
collision [ 1 ] = collision [ 0 ] ;
73
93
}
74
94
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
-
82
95
if ( options . at [ 0 ] === "right" ) {
83
96
basePosition . left += targetWidth ;
84
97
} else if ( options . at [ 0 ] === center ) {
@@ -91,8 +104,14 @@ $.fn.position = function( options ) {
91
104
basePosition . top += targetHeight / 2 ;
92
105
}
93
106
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 ] ;
96
115
97
116
return this . each ( function ( ) {
98
117
var elem = $ ( this ) ,
@@ -105,6 +124,12 @@ $.fn.position = function( options ) {
105
124
collisionHeight = elemHeight + marginTop +
106
125
( parseInt ( $ . curCSS ( this , "marginBottom" , true ) ) || 0 ) ,
107
126
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
+ ] ,
108
133
collisionPosition ;
109
134
110
135
if ( options . my [ 0 ] === "right" ) {
@@ -119,6 +144,9 @@ $.fn.position = function( options ) {
119
144
position . top -= elemHeight / 2 ;
120
145
}
121
146
147
+ position . left += myOffset [ 0 ] ;
148
+ position . top += myOffset [ 1 ] ;
149
+
122
150
// prevent fractions (see #5280)
123
151
position . left = Math . round ( position . left ) ;
124
152
position . top = Math . round ( position . top ) ;
@@ -138,7 +166,7 @@ $.fn.position = function( options ) {
138
166
collisionPosition : collisionPosition ,
139
167
collisionWidth : collisionWidth ,
140
168
collisionHeight : collisionHeight ,
141
- offset : offset ,
169
+ offset : [ atOffset [ 0 ] + myOffset [ 0 ] , atOffset [ 1 ] + myOffset [ 1 ] ] ,
142
170
my : options . my ,
143
171
at : options . at
144
172
} ) ;
@@ -212,4 +240,40 @@ $.ui.position = {
212
240
}
213
241
} ;
214
242
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
+
215
279
} ( jQuery ) ) ;
0 commit comments