Debian Codegraph @

ui/position.js

raw symbols SCIP
1 /*!
2 * jQuery UI Position 1.14.2
3 * https://jqueryui.com
4 *
5 * Copyright OpenJS Foundation and other contributors
6 * Released under the MIT license.
7 * https://jquery.org/license
8 *
9 * https://api.jqueryui.com/position/
10 */
11
12 //>>label: Position
13 //>>group: Core
14 //>>description: Positions elements relative to other elements.
15 //>>docs: https://api.jqueryui.com/position/
16 //>>demos: https://jqueryui.com/position/
17
18 ( function( factory ) {
19 "use strict";
20
21 if ( typeof define === "function" && define.amd ) {
22
23 // AMD. Register as an anonymous module.
24 define( [ "jquery", "./version" ], factory );
25 } else {
26
27 // Browser globals
28 factory( jQuery );
29 }
30 } )( function( $ ) {
31 "use strict";
32
33 ( function() {
34 var cachedScrollbarWidth,
35 max = Math.max,
36 abs = Math.abs,
37 rhorizontal = /left|center|right/,
38 rvertical = /top|center|bottom/,
39 roffset = /[\+\-]\d+(\.[\d]+)?%?/,
40 rposition = /^\w+/,
41 rpercent = /%$/,
42 _position = $.fn.position;
43
44 function getOffsets( offsets, width, height ) {
45 return [
46 parseFloat( offsets[ 0 ] ) * ( rpercent.test( offsets[ 0 ] ) ? width / 100 : 1 ),
47 parseFloat( offsets[ 1 ] ) * ( rpercent.test( offsets[ 1 ] ) ? height / 100 : 1 )
48 ];
49 }
50
51 function parseCss( element, property ) {
52 return parseInt( $.css( element, property ), 10 ) || 0;
53 }
54
55 function isWindow( obj ) {
56 return obj != null && obj === obj.window;
57 }
58
59 function getDimensions( elem ) {
60 var raw = elem[ 0 ];
61 if ( raw.nodeType === 9 ) {
62 return {
63 width: elem.width(),
64 height: elem.height(),
65 offset: { top: 0, left: 0 }
66 };
67 }
68 if ( isWindow( raw ) ) {
69 return {
70 width: elem.width(),
71 height: elem.height(),
72 offset: { top: elem.scrollTop(), left: elem.scrollLeft() }
73 };
74 }
75 if ( raw.preventDefault ) {
76 return {
77 width: 0,
78 height: 0,
79 offset: { top: raw.pageY, left: raw.pageX }
80 };
81 }
82 return {
83 width: elem.outerWidth(),
84 height: elem.outerHeight(),
85 offset: elem.offset()
86 };
87 }
88
89 $.position = {
90 scrollbarWidth: function() {
91 if ( cachedScrollbarWidth !== undefined ) {
92 return cachedScrollbarWidth;
93 }
94 var w1, w2,
95 div = $( "<div style=" +
96 "'display:block;position:absolute;width:200px;height:200px;overflow:hidden;'>" +
97 "<div style='height:300px;width:auto;'></div></div>" ),
98 innerDiv = div.children()[ 0 ];
99
100 $( "body" ).append( div );
101 w1 = innerDiv.offsetWidth;
102 div.css( "overflow", "scroll" );
103
104 w2 = innerDiv.offsetWidth;
105
106 if ( w1 === w2 ) {
107 w2 = div[ 0 ].clientWidth;
108 }
109
110 div.remove();
111
112 return ( cachedScrollbarWidth = w1 - w2 );
113 },
114 getScrollInfo: function( within ) {
115 var overflowX = within.isWindow || within.isDocument ? "" :
116 within.element.css( "overflow-x" ),
117 overflowY = within.isWindow || within.isDocument ? "" :
118 within.element.css( "overflow-y" ),
119 hasOverflowX = overflowX === "scroll" ||
120 ( overflowX === "auto" && within.width < within.element[ 0 ].scrollWidth ),
121 hasOverflowY = overflowY === "scroll" ||
122 ( overflowY === "auto" && within.height < within.element[ 0 ].scrollHeight );
123 return {
124 width: hasOverflowY ? $.position.scrollbarWidth() : 0,
125 height: hasOverflowX ? $.position.scrollbarWidth() : 0
126 };
127 },
128 getWithinInfo: function( element ) {
129 var withinElement = $( element || window ),
130 isElemWindow = isWindow( withinElement[ 0 ] ),
131 isDocument = !!withinElement[ 0 ] && withinElement[ 0 ].nodeType === 9,
132 hasOffset = !isElemWindow && !isDocument;
133 return {
134 element: withinElement,
135 isWindow: isElemWindow,
136 isDocument: isDocument,
137 offset: hasOffset ? $( element ).offset() : { left: 0, top: 0 },
138 scrollLeft: withinElement.scrollLeft(),
139 scrollTop: withinElement.scrollTop(),
140 width: withinElement.outerWidth(),
141 height: withinElement.outerHeight()
142 };
143 }
144 };
145
146 $.fn.position = function( options ) {
147 if ( !options || !options.of ) {
148 return _position.apply( this, arguments );
149 }
150
151 // Make a copy, we don't want to modify arguments
152 options = $.extend( {}, options );
153
154 var atOffset, targetWidth, targetHeight, targetOffset, basePosition, dimensions,
155
156 // Make sure string options are treated as CSS selectors
157 target = typeof options.of === "string" ?
158 $( document ).find( options.of ) :
159 $( options.of ),
160
161 within = $.position.getWithinInfo( options.within ),
162 scrollInfo = $.position.getScrollInfo( within ),
163 collision = ( options.collision || "flip" ).split( " " ),
164 offsets = {};
165
166 dimensions = getDimensions( target );
167 if ( target[ 0 ].preventDefault ) {
168
169 // Force left top to allow flipping
170 options.at = "left top";
171 }
172 targetWidth = dimensions.width;
173 targetHeight = dimensions.height;
174 targetOffset = dimensions.offset;
175
176 // Clone to reuse original targetOffset later
177 basePosition = $.extend( {}, targetOffset );
178
179 // Force my and at to have valid horizontal and vertical positions
180 // if a value is missing or invalid, it will be converted to center
181 $.each( [ "my", "at" ], function() {
182 var pos = ( options[ this ] || "" ).split( " " ),
183 horizontalOffset,
184 verticalOffset;
185
186 if ( pos.length === 1 ) {
187 pos = rhorizontal.test( pos[ 0 ] ) ?
188 pos.concat( [ "center" ] ) :
189 rvertical.test( pos[ 0 ] ) ?
190 [ "center" ].concat( pos ) :
191 [ "center", "center" ];
192 }
193 pos[ 0 ] = rhorizontal.test( pos[ 0 ] ) ? pos[ 0 ] : "center";
194 pos[ 1 ] = rvertical.test( pos[ 1 ] ) ? pos[ 1 ] : "center";
195
196 // Calculate offsets
197 horizontalOffset = roffset.exec( pos[ 0 ] );
198 verticalOffset = roffset.exec( pos[ 1 ] );
199 offsets[ this ] = [
200 horizontalOffset ? horizontalOffset[ 0 ] : 0,
201 verticalOffset ? verticalOffset[ 0 ] : 0
202 ];
203
204 // Reduce to just the positions without the offsets
205 options[ this ] = [
206 rposition.exec( pos[ 0 ] )[ 0 ],
207 rposition.exec( pos[ 1 ] )[ 0 ]
208 ];
209 } );
210
211 // Normalize collision option
212 if ( collision.length === 1 ) {
213 collision[ 1 ] = collision[ 0 ];
214 }
215
216 if ( options.at[ 0 ] === "right" ) {
217 basePosition.left += targetWidth;
218 } else if ( options.at[ 0 ] === "center" ) {
219 basePosition.left += targetWidth / 2;
220 }
221
222 if ( options.at[ 1 ] === "bottom" ) {
223 basePosition.top += targetHeight;
224 } else if ( options.at[ 1 ] === "center" ) {
225 basePosition.top += targetHeight / 2;
226 }
227
228 atOffset = getOffsets( offsets.at, targetWidth, targetHeight );
229 basePosition.left += atOffset[ 0 ];
230 basePosition.top += atOffset[ 1 ];
231
232 return this.each( function() {
233 var collisionPosition, using,
234 elem = $( this ),
235 elemWidth = elem.outerWidth(),
236 elemHeight = elem.outerHeight(),
237 marginLeft = parseCss( this, "marginLeft" ),
238 marginTop = parseCss( this, "marginTop" ),
239 collisionWidth = elemWidth + marginLeft + parseCss( this, "marginRight" ) +
240 scrollInfo.width,
241 collisionHeight = elemHeight + marginTop + parseCss( this, "marginBottom" ) +
242 scrollInfo.height,
243 position = $.extend( {}, basePosition ),
244 myOffset = getOffsets( offsets.my, elem.outerWidth(), elem.outerHeight() );
245
246 if ( options.my[ 0 ] === "right" ) {
247 position.left -= elemWidth;
248 } else if ( options.my[ 0 ] === "center" ) {
249 position.left -= elemWidth / 2;
250 }
251
252 if ( options.my[ 1 ] === "bottom" ) {
253 position.top -= elemHeight;
254 } else if ( options.my[ 1 ] === "center" ) {
255 position.top -= elemHeight / 2;
256 }
257
258 position.left += myOffset[ 0 ];
259 position.top += myOffset[ 1 ];
260
261 collisionPosition = {
262 marginLeft: marginLeft,
263 marginTop: marginTop
264 };
265
266 $.each( [ "left", "top" ], function( i, dir ) {
267 if ( $.ui.position[ collision[ i ] ] ) {
268 $.ui.position[ collision[ i ] ][ dir ]( position, {
269 targetWidth: targetWidth,
270 targetHeight: targetHeight,
271 elemWidth: elemWidth,
272 elemHeight: elemHeight,
273 collisionPosition: collisionPosition,
274 collisionWidth: collisionWidth,
275 collisionHeight: collisionHeight,
276 offset: [ atOffset[ 0 ] + myOffset[ 0 ], atOffset [ 1 ] + myOffset[ 1 ] ],
277 my: options.my,
278 at: options.at,
279 within: within,
280 elem: elem
281 } );
282 }
283 } );
284
285 if ( options.using ) {
286
287 // Adds feedback as second argument to using callback, if present
288 using = function( props ) {
289 var left = targetOffset.left - position.left,
290 right = left + targetWidth - elemWidth,
291 top = targetOffset.top - position.top,
292 bottom = top + targetHeight - elemHeight,
293 feedback = {
294 target: {
295 element: target,
296 left: targetOffset.left,
297 top: targetOffset.top,
298 width: targetWidth,
299 height: targetHeight
300 },
301 element: {
302 element: elem,
303 left: position.left,
304 top: position.top,
305 width: elemWidth,
306 height: elemHeight
307 },
308 horizontal: right < 0 ? "left" : left > 0 ? "right" : "center",
309 vertical: bottom < 0 ? "top" : top > 0 ? "bottom" : "middle"
310 };
311 if ( targetWidth < elemWidth && abs( left + right ) < targetWidth ) {
312 feedback.horizontal = "center";
313 }
314 if ( targetHeight < elemHeight && abs( top + bottom ) < targetHeight ) {
315 feedback.vertical = "middle";
316 }
317 if ( max( abs( left ), abs( right ) ) > max( abs( top ), abs( bottom ) ) ) {
318 feedback.important = "horizontal";
319 } else {
320 feedback.important = "vertical";
321 }
322 options.using.call( this, props, feedback );
323 };
324 }
325
326 elem.offset( $.extend( position, { using: using } ) );
327 } );
328 };
329
330 $.ui.position = {
331 fit: {
332 left: function( position, data ) {
333 var within = data.within,
334 withinOffset = within.isWindow ? within.scrollLeft : within.offset.left,
335 outerWidth = within.width,
336 collisionPosLeft = position.left - data.collisionPosition.marginLeft,
337 overLeft = withinOffset - collisionPosLeft,
338 overRight = collisionPosLeft + data.collisionWidth - outerWidth - withinOffset,
339 newOverRight;
340
341 // Element is wider than within
342 if ( data.collisionWidth > outerWidth ) {
343
344 // Element is initially over the left side of within
345 if ( overLeft > 0 && overRight <= 0 ) {
346 newOverRight = position.left + overLeft + data.collisionWidth - outerWidth -
347 withinOffset;
348 position.left += overLeft - newOverRight;
349
350 // Element is initially over right side of within
351 } else if ( overRight > 0 && overLeft <= 0 ) {
352 position.left = withinOffset;
353
354 // Element is initially over both left and right sides of within
355 } else {
356 if ( overLeft > overRight ) {
357 position.left = withinOffset + outerWidth - data.collisionWidth;
358 } else {
359 position.left = withinOffset;
360 }
361 }
362
363 // Too far left -> align with left edge
364 } else if ( overLeft > 0 ) {
365 position.left += overLeft;
366
367 // Too far right -> align with right edge
368 } else if ( overRight > 0 ) {
369 position.left -= overRight;
370
371 // Adjust based on position and margin
372 } else {
373 position.left = max( position.left - collisionPosLeft, position.left );
374 }
375 },
376 top: function( position, data ) {
377 var within = data.within,
378 withinOffset = within.isWindow ? within.scrollTop : within.offset.top,
379 outerHeight = data.within.height,
380 collisionPosTop = position.top - data.collisionPosition.marginTop,
381 overTop = withinOffset - collisionPosTop,
382 overBottom = collisionPosTop + data.collisionHeight - outerHeight - withinOffset,
383 newOverBottom;
384
385 // Element is taller than within
386 if ( data.collisionHeight > outerHeight ) {
387
388 // Element is initially over the top of within
389 if ( overTop > 0 && overBottom <= 0 ) {
390 newOverBottom = position.top + overTop + data.collisionHeight - outerHeight -
391 withinOffset;
392 position.top += overTop - newOverBottom;
393
394 // Element is initially over bottom of within
395 } else if ( overBottom > 0 && overTop <= 0 ) {
396 position.top = withinOffset;
397
398 // Element is initially over both top and bottom of within
399 } else {
400 if ( overTop > overBottom ) {
401 position.top = withinOffset + outerHeight - data.collisionHeight;
402 } else {
403 position.top = withinOffset;
404 }
405 }
406
407 // Too far up -> align with top
408 } else if ( overTop > 0 ) {
409 position.top += overTop;
410
411 // Too far down -> align with bottom edge
412 } else if ( overBottom > 0 ) {
413 position.top -= overBottom;
414
415 // Adjust based on position and margin
416 } else {
417 position.top = max( position.top - collisionPosTop, position.top );
418 }
419 }
420 },
421 flip: {
422 left: function( position, data ) {
423 var within = data.within,
424 withinOffset = within.offset.left + within.scrollLeft,
425 outerWidth = within.width,
426 offsetLeft = within.isWindow ? within.scrollLeft : within.offset.left,
427 collisionPosLeft = position.left - data.collisionPosition.marginLeft,
428 overLeft = collisionPosLeft - offsetLeft,
429 overRight = collisionPosLeft + data.collisionWidth - outerWidth - offsetLeft,
430 myOffset = data.my[ 0 ] === "left" ?
431 -data.elemWidth :
432 data.my[ 0 ] === "right" ?
433 data.elemWidth :
434 0,
435 atOffset = data.at[ 0 ] === "left" ?
436 data.targetWidth :
437 data.at[ 0 ] === "right" ?
438 -data.targetWidth :
439 0,
440 offset = -2 * data.offset[ 0 ],
441 newOverRight,
442 newOverLeft;
443
444 if ( overLeft < 0 ) {
445 newOverRight = position.left + myOffset + atOffset + offset + data.collisionWidth -
446 outerWidth - withinOffset;
447 if ( newOverRight < 0 || newOverRight < abs( overLeft ) ) {
448 position.left += myOffset + atOffset + offset;
449 }
450 } else if ( overRight > 0 ) {
451 newOverLeft = position.left - data.collisionPosition.marginLeft + myOffset +
452 atOffset + offset - offsetLeft;
453 if ( newOverLeft > 0 || abs( newOverLeft ) < overRight ) {
454 position.left += myOffset + atOffset + offset;
455 }
456 }
457 },
458 top: function( position, data ) {
459 var within = data.within,
460 withinOffset = within.offset.top + within.scrollTop,
461 outerHeight = within.height,
462 offsetTop = within.isWindow ? within.scrollTop : within.offset.top,
463 collisionPosTop = position.top - data.collisionPosition.marginTop,
464 overTop = collisionPosTop - offsetTop,
465 overBottom = collisionPosTop + data.collisionHeight - outerHeight - offsetTop,
466 top = data.my[ 1 ] === "top",
467 myOffset = top ?
468 -data.elemHeight :
469 data.my[ 1 ] === "bottom" ?
470 data.elemHeight :
471 0,
472 atOffset = data.at[ 1 ] === "top" ?
473 data.targetHeight :
474 data.at[ 1 ] === "bottom" ?
475 -data.targetHeight :
476 0,
477 offset = -2 * data.offset[ 1 ],
478 newOverTop,
479 newOverBottom;
480 if ( overTop < 0 ) {
481 newOverBottom = position.top + myOffset + atOffset + offset + data.collisionHeight -
482 outerHeight - withinOffset;
483 if ( newOverBottom < 0 || newOverBottom < abs( overTop ) ) {
484 position.top += myOffset + atOffset + offset;
485 }
486 } else if ( overBottom > 0 ) {
487 newOverTop = position.top - data.collisionPosition.marginTop + myOffset + atOffset +
488 offset - offsetTop;
489 if ( newOverTop > 0 || abs( newOverTop ) < overBottom ) {
490 position.top += myOffset + atOffset + offset;
491 }
492 }
493 }
494 },
495 flipfit: {
496 left: function() {
497 $.ui.position.flip.left.apply( this, arguments );
498 $.ui.position.fit.left.apply( this, arguments );
499 },
500 top: function() {
501 $.ui.position.flip.top.apply( this, arguments );
502 $.ui.position.fit.top.apply( this, arguments );
503 }
504 }
505 };
506
507 } )();
508
509 return $.ui.position;
510
511 } );