1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*
* jQuery UI Position @VERSION
*
* Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* http://docs.jquery.com/UI/Position
*/
(function($) {
$.ui = $.ui || {};
var horizontalPositions = /left|center|right/,
horizontalDefault = 'center',
verticalPositions = /top|center|bottom/,
verticalDefault = 'center',
_position = $.fn.position;
$.fn.position = function(options) {
if (!options || !options.of) {
return _position.apply(this, arguments);
}
options = $.extend({}, $.ui.position.defaults, options);
var target = $(options.of),
collision = (options.collision || 'flip').split(' '),
offset = options.offset ? options.offset.split(' ') : [0, 0],
targetWidth,
targetHeight,
basePosition;
if (options.of == document) {
targetWidth = target.width();
targetHeight = target.height();
basePosition = { top: 0, left: 0 };
} else if (options.of == window) {
targetWidth = target.width();
targetHeight = target.height();
basePosition = { top: target.scrollTop(), left: target.scrollLeft() };
} else if (options.of.preventDefault) {
targetWidth = targetHeight = 0;
basePosition = { top: options.of.pageY, left: options.of.pageX };
} else {
targetWidth = target.outerWidth();
targetHeight = target.outerHeight();
basePosition = target.offset();
}
// force my and at to have valid horizontal and veritcal positions
// if a value is missing or invalid, it will be converted to center
$.each(['my', 'at'], function() {
var pos = options[this].split(' ');
pos = pos.length == 1
? horizontalPositions.test(pos[0])
? pos.concat([verticalDefault])
: verticalPositions.test(pos[0])
? [horizontalDefault].concat(pos)
: [horizontalDefault, verticalDefault]
: pos;
pos[0] = horizontalPositions.test(pos[0]) ? pos[0] : horizontalDefault;
pos[1] = verticalPositions.test(pos[1]) ? pos[1] : verticalDefault;
options[this] = pos;
});
// normalize collision option
if (collision.length == 1) {
collision[1] = collision[0];
}
// normalize offset option
offset[0] = parseInt(offset[0], 10) || 0;
if (offset.length == 1) {
offset[1] = offset[0];
}
offset[1] = parseInt(offset[1], 10) || 0;
switch (options.at[0]) {
case 'right':
basePosition.left += targetWidth;
break;
case horizontalDefault:
basePosition.left += targetWidth / 2;
break;
}
switch (options.at[1]) {
case 'bottom':
basePosition.top += targetHeight;
break;
case verticalDefault:
basePosition.top += targetHeight / 2;
break;
}
basePosition.left += offset[0];
basePosition.top += offset[1];
return this.each(function() {
var elem = $(this),
elemWidth = elem.outerWidth(),
elemHeight = elem.outerHeight(),
position = $.extend({}, basePosition),
over,
myOffset,
atOffset;
switch (options.my[0]) {
case 'right':
position.left -= elemWidth;
break;
case horizontalDefault:
position.left -= elemWidth / 2;
break;
}
switch (options.my[1]) {
case 'bottom':
position.top -= elemHeight;
break;
case verticalDefault:
position.top -= elemHeight / 2;
break;
}
$.each(['left', 'top'], function(i, dir) {
($.ui.position[collision[i]] &&
$.ui.position[collision[i]][dir](position, {
targetWidth: targetWidth,
targetHeight: targetHeight,
elemWidth: elemWidth,
elemHeight: elemHeight,
offset: offset,
my: options.my,
at: options.at
}));
});
(options.stackfix && $.fn.stackfix && elem.stackfix());
// the by function is passed the offset values, not the position values
// we'll need the logic from the .offset() setter to be accessible for
// us to calculate the position values to make the by option more useful
($.isFunction(options.by) ? options.by.call(this, position) : elem.offset(position));
});
};
$.ui.position = {
defaults:{
stackFix: true
},
fit: {
left: function(position, data) {
var over = position.left + data.elemWidth - $(window).width() - $(window).scrollLeft();
position.left = over > 0 ? position.left - over : Math.max(0, position.left);
},
top: function(position, data) {
var over = position.top + data.elemHeight - $(window).height() - $(window).scrollTop();
position.top = over > 0 ? position.top - over : Math.max(0, position.top);
}
},
flip: {
left: function(position, data) {
if (data.at[0] == 'center')
return;
var over = position.left + data.elemWidth - $(window).width() - $(window).scrollLeft(),
myOffset = data.my[0] == 'left' ? -data.elemWidth : data.my[0] == 'right' ? data.elemWidth : 0,
offset = -2 * data.offset[0];
position.left += position.left < 0 ? myOffset + data.targetWidth + offset : over > 0 ? myOffset - data.targetWidth + offset : 0;
},
top: function(position, data) {
if (data.at[1] == 'center')
return;
var over = position.top + data.elemHeight - $(window).height() - $(window).scrollTop(),
myOffset = data.my[1] == 'top' ? -data.elemHeight : data.my[1] == 'bottom' ? data.elemHeight : 0,
atOffset = data.at[1] == 'top' ? data.targetHeight : -data.targetHeight,
offset = -2 * data.offset[1];
position.top += position.top < 0 ? myOffset + data.targetHeight + offset : over > 0 ? myOffset + atOffset + offset : 0;
}
}
};
// the following functionality is planned for jQuery 1.4
// based on http://plugins.jquery.com/files/offset.js.txt
$.fn.extend({
_offset: $.fn.offset,
offset: function(newOffset) {
return !newOffset ? this._offset() : this.each(function() {
var elem = $(this),
// we need to convert static positioning to relative positioning
isRelative = /relative|static/.test(elem.css('position')),
hide = elem.css('display') == 'none';
(isRelative && elem.css('position', 'relative'));
(hide && elem.show());
var offset = elem.offset(),
delta = {
left : parseInt(elem.css('left'), 10),
top: parseInt(elem.css('top'), 10)
};
// in case of 'auto'
delta.left = !isNaN(delta.left)
? delta.left
: isRelative
? 0
: this.offsetLeft;
delta.top = !isNaN(delta.top)
? delta.top
: isRelative
? 0
: this.offsetTop;
// allow setting only left or only top
if (newOffset.left || newOffset.left === 0) {
elem.css('left', newOffset.left - offset.left + delta.left);
}
if (newOffset.top || newOffset.top === 0) {
elem.css('top', newOffset.top - offset.top + delta.top);
}
(hide && elem.hide());
});
}
});
})(jQuery);
|