1 | /*
|
---|
2 | * jQuery UI Position 1.8rc1
|
---|
3 | *
|
---|
4 | * Copyright (c) 2010 AUTHORS.txt (http://jqueryui.com/about)
|
---|
5 | * Dual licensed under the MIT (MIT-LICENSE.txt)
|
---|
6 | * and GPL (GPL-LICENSE.txt) licenses.
|
---|
7 | *
|
---|
8 | * http://docs.jquery.com/UI/Position
|
---|
9 | */
|
---|
10 | (function($) {
|
---|
11 |
|
---|
12 | $.ui = $.ui || {};
|
---|
13 |
|
---|
14 | var horizontalPositions = /left|center|right/,
|
---|
15 | horizontalDefault = 'center',
|
---|
16 | verticalPositions = /top|center|bottom/,
|
---|
17 | verticalDefault = 'center',
|
---|
18 | _position = $.fn.position;
|
---|
19 |
|
---|
20 | $.fn.position = function(options) {
|
---|
21 | if (!options || !options.of) {
|
---|
22 | return _position.apply(this, arguments);
|
---|
23 | }
|
---|
24 |
|
---|
25 | // make a copy, we don't want to modify arguments
|
---|
26 | options = $.extend({}, options);
|
---|
27 |
|
---|
28 | var target = $(options.of),
|
---|
29 | collision = (options.collision || 'flip').split(' '),
|
---|
30 | offset = options.offset ? options.offset.split(' ') : [0, 0],
|
---|
31 | targetWidth,
|
---|
32 | targetHeight,
|
---|
33 | basePosition;
|
---|
34 |
|
---|
35 | if (options.of.nodeType === 9) {
|
---|
36 | targetWidth = target.width();
|
---|
37 | targetHeight = target.height();
|
---|
38 | basePosition = { top: 0, left: 0 };
|
---|
39 | } else if (options.of.scrollTo && options.of.document) {
|
---|
40 | targetWidth = target.width();
|
---|
41 | targetHeight = target.height();
|
---|
42 | basePosition = { top: target.scrollTop(), left: target.scrollLeft() };
|
---|
43 | } else if (options.of.preventDefault) {
|
---|
44 | // force left top to allow flipping
|
---|
45 | options.at = 'left top';
|
---|
46 | targetWidth = targetHeight = 0;
|
---|
47 | basePosition = { top: options.of.pageY, left: options.of.pageX };
|
---|
48 | } else {
|
---|
49 | targetWidth = target.outerWidth();
|
---|
50 | targetHeight = target.outerHeight();
|
---|
51 | basePosition = target.offset();
|
---|
52 | }
|
---|
53 |
|
---|
54 | // force my and at to have valid horizontal and veritcal positions
|
---|
55 | // if a value is missing or invalid, it will be converted to center
|
---|
56 | $.each(['my', 'at'], function() {
|
---|
57 | var pos = (options[this] || '').split(' ');
|
---|
58 | pos = pos.length == 1
|
---|
59 | ? horizontalPositions.test(pos[0])
|
---|
60 | ? pos.concat([verticalDefault])
|
---|
61 | : verticalPositions.test(pos[0])
|
---|
62 | ? [horizontalDefault].concat(pos)
|
---|
63 | : [horizontalDefault, verticalDefault]
|
---|
64 | : pos;
|
---|
65 | pos[0] = horizontalPositions.test(pos[0]) ? pos[0] : horizontalDefault;
|
---|
66 | pos[1] = verticalPositions.test(pos[1]) ? pos[1] : verticalDefault;
|
---|
67 | options[this] = pos;
|
---|
68 | });
|
---|
69 |
|
---|
70 | // normalize collision option
|
---|
71 | if (collision.length == 1) {
|
---|
72 | collision[1] = collision[0];
|
---|
73 | }
|
---|
74 |
|
---|
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 | switch (options.at[0]) {
|
---|
83 | case 'right':
|
---|
84 | basePosition.left += targetWidth;
|
---|
85 | break;
|
---|
86 | case horizontalDefault:
|
---|
87 | basePosition.left += targetWidth / 2;
|
---|
88 | break;
|
---|
89 | }
|
---|
90 |
|
---|
91 | switch (options.at[1]) {
|
---|
92 | case 'bottom':
|
---|
93 | basePosition.top += targetHeight;
|
---|
94 | break;
|
---|
95 | case verticalDefault:
|
---|
96 | basePosition.top += targetHeight / 2;
|
---|
97 | break;
|
---|
98 | }
|
---|
99 |
|
---|
100 | basePosition.left += offset[0];
|
---|
101 | basePosition.top += offset[1];
|
---|
102 |
|
---|
103 | return this.each(function() {
|
---|
104 | var elem = $(this),
|
---|
105 | elemWidth = elem.outerWidth(),
|
---|
106 | elemHeight = elem.outerHeight(),
|
---|
107 | position = $.extend({}, basePosition),
|
---|
108 | over,
|
---|
109 | myOffset,
|
---|
110 | atOffset;
|
---|
111 |
|
---|
112 | switch (options.my[0]) {
|
---|
113 | case 'right':
|
---|
114 | position.left -= elemWidth;
|
---|
115 | break;
|
---|
116 | case horizontalDefault:
|
---|
117 | position.left -= elemWidth / 2;
|
---|
118 | break;
|
---|
119 | }
|
---|
120 |
|
---|
121 | switch (options.my[1]) {
|
---|
122 | case 'bottom':
|
---|
123 | position.top -= elemHeight;
|
---|
124 | break;
|
---|
125 | case verticalDefault:
|
---|
126 | position.top -= elemHeight / 2;
|
---|
127 | break;
|
---|
128 | }
|
---|
129 |
|
---|
130 | $.each(['left', 'top'], function(i, dir) {
|
---|
131 | ($.ui.position[collision[i]] &&
|
---|
132 | $.ui.position[collision[i]][dir](position, {
|
---|
133 | targetWidth: targetWidth,
|
---|
134 | targetHeight: targetHeight,
|
---|
135 | elemWidth: elemWidth,
|
---|
136 | elemHeight: elemHeight,
|
---|
137 | offset: offset,
|
---|
138 | my: options.my,
|
---|
139 | at: options.at
|
---|
140 | }));
|
---|
141 | });
|
---|
142 |
|
---|
143 | ($.fn.bgiframe && elem.bgiframe());
|
---|
144 | elem.offset($.extend(position, { using: options.using }));
|
---|
145 | });
|
---|
146 | };
|
---|
147 |
|
---|
148 | $.ui.position = {
|
---|
149 | fit: {
|
---|
150 | left: function(position, data) {
|
---|
151 | var over = position.left + data.elemWidth - $(window).width() - $(window).scrollLeft();
|
---|
152 | position.left = over > 0 ? position.left - over : Math.max(0, position.left);
|
---|
153 | },
|
---|
154 | top: function(position, data) {
|
---|
155 | var over = position.top + data.elemHeight - $(window).height() - $(window).scrollTop();
|
---|
156 | position.top = over > 0 ? position.top - over : Math.max(0, position.top);
|
---|
157 | }
|
---|
158 | },
|
---|
159 |
|
---|
160 | flip: {
|
---|
161 | left: function(position, data) {
|
---|
162 | if (data.at[0] == 'center')
|
---|
163 | return;
|
---|
164 | var over = position.left + data.elemWidth - $(window).width() - $(window).scrollLeft(),
|
---|
165 | myOffset = data.my[0] == 'left' ? -data.elemWidth : data.my[0] == 'right' ? data.elemWidth : 0,
|
---|
166 | offset = -2 * data.offset[0];
|
---|
167 | position.left += position.left < 0 ? myOffset + data.targetWidth + offset : over > 0 ? myOffset - data.targetWidth + offset : 0;
|
---|
168 | },
|
---|
169 | top: function(position, data) {
|
---|
170 | if (data.at[1] == 'center')
|
---|
171 | return;
|
---|
172 | var over = position.top + data.elemHeight - $(window).height() - $(window).scrollTop(),
|
---|
173 | myOffset = data.my[1] == 'top' ? -data.elemHeight : data.my[1] == 'bottom' ? data.elemHeight : 0,
|
---|
174 | atOffset = data.at[1] == 'top' ? data.targetHeight : -data.targetHeight,
|
---|
175 | offset = -2 * data.offset[1];
|
---|
176 | position.top += position.top < 0 ? myOffset + data.targetHeight + offset : over > 0 ? myOffset + atOffset + offset : 0;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | };
|
---|
180 |
|
---|
181 | // offset setter from jQuery 1.4
|
---|
182 | if (!$.offset.setOffset) {
|
---|
183 | $.offset.setOffset = function( elem, options ) {
|
---|
184 | // set position first, in-case top/left are set even on static elem
|
---|
185 | if ( /static/.test( jQuery.curCSS( elem, 'position' ) ) ) {
|
---|
186 | elem.style.position = 'relative';
|
---|
187 | }
|
---|
188 | var curElem = jQuery( elem ),
|
---|
189 | curOffset = curElem.offset(),
|
---|
190 | curTop = parseInt( jQuery.curCSS( elem, 'top', true ), 10 ) || 0,
|
---|
191 | curLeft = parseInt( jQuery.curCSS( elem, 'left', true ), 10) || 0,
|
---|
192 | props = {
|
---|
193 | top: (options.top - curOffset.top) + curTop,
|
---|
194 | left: (options.left - curOffset.left) + curLeft
|
---|
195 | };
|
---|
196 |
|
---|
197 | if ( 'using' in options ) {
|
---|
198 | options.using.call( elem, props );
|
---|
199 | } else {
|
---|
200 | curElem.css( props );
|
---|
201 | }
|
---|
202 | };
|
---|
203 |
|
---|
204 | var _offset = $.fn.offset;
|
---|
205 | $.fn.offset = function( options ) {
|
---|
206 | var elem = this[0];
|
---|
207 | if ( !elem || !elem.ownerDocument ) { return null; }
|
---|
208 | if ( options ) {
|
---|
209 | return this.each(function() {
|
---|
210 | $.offset.setOffset( this, options );
|
---|
211 | });
|
---|
212 | }
|
---|
213 | return _offset.call(this);
|
---|
214 | };
|
---|
215 | }
|
---|
216 |
|
---|
217 | })(jQuery);
|
---|