[SPIP] +spip v3.0.17
[lhc/web/clavette_www.git] / www / plugins-dist / jquery_ui / prive / javascript / ui / jquery.ui.position.js
1 /*!
2 * jQuery UI Position 1.8.21
3 *
4 * Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
5 * Dual licensed under the MIT or GPL Version 2 licenses.
6 * http://jquery.org/license
7 *
8 * http://docs.jquery.com/UI/Position
9 */
10 (function( $, undefined ) {
11
12 $.ui = $.ui || {};
13
14 var horizontalPositions = /left|center|right/,
15 verticalPositions = /top|center|bottom/,
16 center = "center",
17 support = {},
18 _position = $.fn.position,
19 _offset = $.fn.offset;
20
21 $.fn.position = function( options ) {
22 if ( !options || !options.of ) {
23 return _position.apply( this, arguments );
24 }
25
26 // make a copy, we don't want to modify arguments
27 options = $.extend( {}, options );
28
29 var target = $( options.of ),
30 targetElem = target[0],
31 collision = ( options.collision || "flip" ).split( " " ),
32 offset = options.offset ? options.offset.split( " " ) : [ 0, 0 ],
33 targetWidth,
34 targetHeight,
35 basePosition;
36
37 if ( targetElem.nodeType === 9 ) {
38 targetWidth = target.width();
39 targetHeight = target.height();
40 basePosition = { top: 0, left: 0 };
41 // TODO: use $.isWindow() in 1.9
42 } else if ( targetElem.setTimeout ) {
43 targetWidth = target.width();
44 targetHeight = target.height();
45 basePosition = { top: target.scrollTop(), left: target.scrollLeft() };
46 } else if ( targetElem.preventDefault ) {
47 // force left top to allow flipping
48 options.at = "left top";
49 targetWidth = targetHeight = 0;
50 basePosition = { top: options.of.pageY, left: options.of.pageX };
51 } else {
52 targetWidth = target.outerWidth();
53 targetHeight = target.outerHeight();
54 basePosition = target.offset();
55 }
56
57 // force my and at to have valid horizontal and veritcal positions
58 // if a value is missing or invalid, it will be converted to center
59 $.each( [ "my", "at" ], function() {
60 var pos = ( options[this] || "" ).split( " " );
61 if ( pos.length === 1) {
62 pos = horizontalPositions.test( pos[0] ) ?
63 pos.concat( [center] ) :
64 verticalPositions.test( pos[0] ) ?
65 [ center ].concat( pos ) :
66 [ center, center ];
67 }
68 pos[ 0 ] = horizontalPositions.test( pos[0] ) ? pos[ 0 ] : center;
69 pos[ 1 ] = verticalPositions.test( pos[1] ) ? pos[ 1 ] : center;
70 options[ this ] = pos;
71 });
72
73 // normalize collision option
74 if ( collision.length === 1 ) {
75 collision[ 1 ] = collision[ 0 ];
76 }
77
78 // normalize offset option
79 offset[ 0 ] = parseInt( offset[0], 10 ) || 0;
80 if ( offset.length === 1 ) {
81 offset[ 1 ] = offset[ 0 ];
82 }
83 offset[ 1 ] = parseInt( offset[1], 10 ) || 0;
84
85 if ( options.at[0] === "right" ) {
86 basePosition.left += targetWidth;
87 } else if ( options.at[0] === center ) {
88 basePosition.left += targetWidth / 2;
89 }
90
91 if ( options.at[1] === "bottom" ) {
92 basePosition.top += targetHeight;
93 } else if ( options.at[1] === center ) {
94 basePosition.top += targetHeight / 2;
95 }
96
97 basePosition.left += offset[ 0 ];
98 basePosition.top += offset[ 1 ];
99
100 return this.each(function() {
101 var elem = $( this ),
102 elemWidth = elem.outerWidth(),
103 elemHeight = elem.outerHeight(),
104 marginLeft = parseInt( $.curCSS( this, "marginLeft", true ) ) || 0,
105 marginTop = parseInt( $.curCSS( this, "marginTop", true ) ) || 0,
106 collisionWidth = elemWidth + marginLeft +
107 ( parseInt( $.curCSS( this, "marginRight", true ) ) || 0 ),
108 collisionHeight = elemHeight + marginTop +
109 ( parseInt( $.curCSS( this, "marginBottom", true ) ) || 0 ),
110 position = $.extend( {}, basePosition ),
111 collisionPosition;
112
113 if ( options.my[0] === "right" ) {
114 position.left -= elemWidth;
115 } else if ( options.my[0] === center ) {
116 position.left -= elemWidth / 2;
117 }
118
119 if ( options.my[1] === "bottom" ) {
120 position.top -= elemHeight;
121 } else if ( options.my[1] === center ) {
122 position.top -= elemHeight / 2;
123 }
124
125 // prevent fractions if jQuery version doesn't support them (see #5280)
126 if ( !support.fractions ) {
127 position.left = Math.round( position.left );
128 position.top = Math.round( position.top );
129 }
130
131 collisionPosition = {
132 left: position.left - marginLeft,
133 top: position.top - marginTop
134 };
135
136 $.each( [ "left", "top" ], function( i, dir ) {
137 if ( $.ui.position[ collision[i] ] ) {
138 $.ui.position[ collision[i] ][ dir ]( position, {
139 targetWidth: targetWidth,
140 targetHeight: targetHeight,
141 elemWidth: elemWidth,
142 elemHeight: elemHeight,
143 collisionPosition: collisionPosition,
144 collisionWidth: collisionWidth,
145 collisionHeight: collisionHeight,
146 offset: offset,
147 my: options.my,
148 at: options.at
149 });
150 }
151 });
152
153 if ( $.fn.bgiframe ) {
154 elem.bgiframe();
155 }
156 elem.offset( $.extend( position, { using: options.using } ) );
157 });
158 };
159
160 $.ui.position = {
161 fit: {
162 left: function( position, data ) {
163 var win = $( window ),
164 over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft();
165 position.left = over > 0 ? position.left - over : Math.max( position.left - data.collisionPosition.left, position.left );
166 },
167 top: function( position, data ) {
168 var win = $( window ),
169 over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop();
170 position.top = over > 0 ? position.top - over : Math.max( position.top - data.collisionPosition.top, position.top );
171 }
172 },
173
174 flip: {
175 left: function( position, data ) {
176 if ( data.at[0] === center ) {
177 return;
178 }
179 var win = $( window ),
180 over = data.collisionPosition.left + data.collisionWidth - win.width() - win.scrollLeft(),
181 myOffset = data.my[ 0 ] === "left" ?
182 -data.elemWidth :
183 data.my[ 0 ] === "right" ?
184 data.elemWidth :
185 0,
186 atOffset = data.at[ 0 ] === "left" ?
187 data.targetWidth :
188 -data.targetWidth,
189 offset = -2 * data.offset[ 0 ];
190 position.left += data.collisionPosition.left < 0 ?
191 myOffset + atOffset + offset :
192 over > 0 ?
193 myOffset + atOffset + offset :
194 0;
195 },
196 top: function( position, data ) {
197 if ( data.at[1] === center ) {
198 return;
199 }
200 var win = $( window ),
201 over = data.collisionPosition.top + data.collisionHeight - win.height() - win.scrollTop(),
202 myOffset = data.my[ 1 ] === "top" ?
203 -data.elemHeight :
204 data.my[ 1 ] === "bottom" ?
205 data.elemHeight :
206 0,
207 atOffset = data.at[ 1 ] === "top" ?
208 data.targetHeight :
209 -data.targetHeight,
210 offset = -2 * data.offset[ 1 ];
211 position.top += data.collisionPosition.top < 0 ?
212 myOffset + atOffset + offset :
213 over > 0 ?
214 myOffset + atOffset + offset :
215 0;
216 }
217 }
218 };
219
220 // offset setter from jQuery 1.4
221 if ( !$.offset.setOffset ) {
222 $.offset.setOffset = function( elem, options ) {
223 // set position first, in-case top/left are set even on static elem
224 if ( /static/.test( $.curCSS( elem, "position" ) ) ) {
225 elem.style.position = "relative";
226 }
227 var curElem = $( elem ),
228 curOffset = curElem.offset(),
229 curTop = parseInt( $.curCSS( elem, "top", true ), 10 ) || 0,
230 curLeft = parseInt( $.curCSS( elem, "left", true ), 10) || 0,
231 props = {
232 top: (options.top - curOffset.top) + curTop,
233 left: (options.left - curOffset.left) + curLeft
234 };
235
236 if ( 'using' in options ) {
237 options.using.call( elem, props );
238 } else {
239 curElem.css( props );
240 }
241 };
242
243 $.fn.offset = function( options ) {
244 var elem = this[ 0 ];
245 if ( !elem || !elem.ownerDocument ) { return null; }
246 if ( options ) {
247 if ( $.isFunction( options ) ) {
248 return this.each(function( i ) {
249 $( this ).offset( options.call( this, i, $( this ).offset() ) );
250 });
251 }
252 return this.each(function() {
253 $.offset.setOffset( this, options );
254 });
255 }
256 return _offset.call( this );
257 };
258 }
259
260 // fraction support test (older versions of jQuery don't support fractions)
261 (function () {
262 var body = document.getElementsByTagName( "body" )[ 0 ],
263 div = document.createElement( "div" ),
264 testElement, testElementParent, testElementStyle, offset, offsetTotal;
265
266 //Create a "fake body" for testing based on method used in jQuery.support
267 testElement = document.createElement( body ? "div" : "body" );
268 testElementStyle = {
269 visibility: "hidden",
270 width: 0,
271 height: 0,
272 border: 0,
273 margin: 0,
274 background: "none"
275 };
276 if ( body ) {
277 $.extend( testElementStyle, {
278 position: "absolute",
279 left: "-1000px",
280 top: "-1000px"
281 });
282 }
283 for ( var i in testElementStyle ) {
284 testElement.style[ i ] = testElementStyle[ i ];
285 }
286 testElement.appendChild( div );
287 testElementParent = body || document.documentElement;
288 testElementParent.insertBefore( testElement, testElementParent.firstChild );
289
290 div.style.cssText = "position: absolute; left: 10.7432222px; top: 10.432325px; height: 30px; width: 201px;";
291
292 offset = $( div ).offset( function( _, offset ) {
293 return offset;
294 }).offset();
295
296 testElement.innerHTML = "";
297 testElementParent.removeChild( testElement );
298
299 offsetTotal = offset.top + offset.left + ( body ? 2000 : 0 );
300 support.fractions = offsetTotal > 21 && offsetTotal < 22;
301 })();
302
303 }( jQuery ));