scratch – Diff between revs 58 and 125

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 58 Rev 125
1 define( [ 1 define([
2 "./core", 2 "./core",
-   3 "./var/strundefined",
3 "./core/access", 4 "./core/access",
4 "./var/document", -  
5 "./var/documentElement", -  
6 "./css/var/rnumnonpx", 5 "./css/var/rnumnonpx",
7 "./css/curCSS", 6 "./css/curCSS",
8 "./css/addGetHookIf", 7 "./css/addGetHookIf",
9 "./css/support", 8 "./css/support",
10   9  
11 "./core/init", 10 "./core/init",
12 "./css", 11 "./css",
13 "./selector" // contains 12 "./selector" // contains
14 ], function( jQuery, access, document, documentElement, rnumnonpx, curCSS, addGetHookIf, support ) { 13 ], function( jQuery, strundefined, access, rnumnonpx, curCSS, addGetHookIf, support ) {
15   14  
16 "use strict"; 15 var docElem = window.document.documentElement;
17   16  
18 /** 17 /**
19 * Gets a window from an element 18 * Gets a window from an element
20 */ 19 */
21 function getWindow( elem ) { 20 function getWindow( elem ) {
22 return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 && elem.defaultView; 21 return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 && elem.defaultView;
23 } 22 }
24   23  
25 jQuery.offset = { 24 jQuery.offset = {
26 setOffset: function( elem, options, i ) { 25 setOffset: function( elem, options, i ) {
27 var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition, 26 var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
28 position = jQuery.css( elem, "position" ), 27 position = jQuery.css( elem, "position" ),
29 curElem = jQuery( elem ), 28 curElem = jQuery( elem ),
30 props = {}; 29 props = {};
31   30  
32 // Set position first, in-case top/left are set even on static elem 31 // Set position first, in-case top/left are set even on static elem
33 if ( position === "static" ) { 32 if ( position === "static" ) {
34 elem.style.position = "relative"; 33 elem.style.position = "relative";
35 } 34 }
36   35  
37 curOffset = curElem.offset(); 36 curOffset = curElem.offset();
38 curCSSTop = jQuery.css( elem, "top" ); 37 curCSSTop = jQuery.css( elem, "top" );
39 curCSSLeft = jQuery.css( elem, "left" ); 38 curCSSLeft = jQuery.css( elem, "left" );
40 calculatePosition = ( position === "absolute" || position === "fixed" ) && 39 calculatePosition = ( position === "absolute" || position === "fixed" ) &&
41 ( curCSSTop + curCSSLeft ).indexOf( "auto" ) > -1; 40 ( curCSSTop + curCSSLeft ).indexOf("auto") > -1;
42   41  
43 // Need to be able to calculate position if either 42 // Need to be able to calculate position if either
44 // top or left is auto and position is either absolute or fixed 43 // top or left is auto and position is either absolute or fixed
45 if ( calculatePosition ) { 44 if ( calculatePosition ) {
46 curPosition = curElem.position(); 45 curPosition = curElem.position();
47 curTop = curPosition.top; 46 curTop = curPosition.top;
48 curLeft = curPosition.left; 47 curLeft = curPosition.left;
49   48  
50 } else { 49 } else {
51 curTop = parseFloat( curCSSTop ) || 0; 50 curTop = parseFloat( curCSSTop ) || 0;
52 curLeft = parseFloat( curCSSLeft ) || 0; 51 curLeft = parseFloat( curCSSLeft ) || 0;
53 } 52 }
54   53  
55 if ( jQuery.isFunction( options ) ) { 54 if ( jQuery.isFunction( options ) ) {
56   -  
57 // Use jQuery.extend here to allow modification of coordinates argument (gh-1848) -  
58 options = options.call( elem, i, jQuery.extend( {}, curOffset ) ); 55 options = options.call( elem, i, curOffset );
59 } 56 }
60   57  
61 if ( options.top != null ) { 58 if ( options.top != null ) {
62 props.top = ( options.top - curOffset.top ) + curTop; 59 props.top = ( options.top - curOffset.top ) + curTop;
63 } 60 }
64 if ( options.left != null ) { 61 if ( options.left != null ) {
65 props.left = ( options.left - curOffset.left ) + curLeft; 62 props.left = ( options.left - curOffset.left ) + curLeft;
66 } 63 }
67   64  
68 if ( "using" in options ) { 65 if ( "using" in options ) {
69 options.using.call( elem, props ); 66 options.using.call( elem, props );
70   67  
71 } else { 68 } else {
72 curElem.css( props ); 69 curElem.css( props );
73 } 70 }
74 } 71 }
75 }; 72 };
76   73  
77 jQuery.fn.extend( { 74 jQuery.fn.extend({
78 offset: function( options ) { -  
79   -  
80 // Preserve chaining for setter 75 offset: function( options ) {
81 if ( arguments.length ) { 76 if ( arguments.length ) {
82 return options === undefined ? 77 return options === undefined ?
83 this : 78 this :
84 this.each( function( i ) { 79 this.each(function( i ) {
85 jQuery.offset.setOffset( this, options, i ); 80 jQuery.offset.setOffset( this, options, i );
86 } ); 81 });
87 } 82 }
88   83  
89 var docElem, win, rect, doc, 84 var docElem, win,
-   85 elem = this[ 0 ],
-   86 box = { top: 0, left: 0 },
90 elem = this[ 0 ]; 87 doc = elem && elem.ownerDocument;
91   88  
92 if ( !elem ) { 89 if ( !doc ) {
93 return; 90 return;
94 } 91 }
95   -  
96 // Support: IE <=11 only -  
97 // Running getBoundingClientRect on a -  
98 // disconnected node in IE throws an error -  
99 if ( !elem.getClientRects().length ) { -  
100 return { top: 0, left: 0 }; -  
101 } -  
102   -  
103 rect = elem.getBoundingClientRect(); -  
104   -  
105 // Make sure element is not hidden (display: none) -  
106 if ( rect.width || rect.height ) { -  
107 doc = elem.ownerDocument; -  
108 win = getWindow( doc ); 92  
109 docElem = doc.documentElement; -  
110   93 docElem = doc.documentElement;
111 return { 94  
112 top: rect.top + win.pageYOffset - docElem.clientTop, 95 // Make sure it's not a disconnected DOM node
113 left: rect.left + win.pageXOffset - docElem.clientLeft 96 if ( !jQuery.contains( docElem, elem ) ) {
114 }; 97 return box;
-   98 }
-   99  
-   100 // Support: BlackBerry 5, iOS 3 (original iPhone)
-   101 // If we don't have gBCR, just use 0,0 rather than error
-   102 if ( typeof elem.getBoundingClientRect !== strundefined ) {
115 } 103 box = elem.getBoundingClientRect();
-   104 }
-   105 win = getWindow( doc );
-   106 return {
116   107 top: box.top + win.pageYOffset - docElem.clientTop,
117 // Return zeros for disconnected and hidden elements (gh-2310) 108 left: box.left + win.pageXOffset - docElem.clientLeft
118 return rect; 109 };
119 }, 110 },
120   111  
121 position: function() { 112 position: function() {
122 if ( !this[ 0 ] ) { 113 if ( !this[ 0 ] ) {
123 return; 114 return;
124 } 115 }
125   116  
126 var offsetParent, offset, 117 var offsetParent, offset,
127 elem = this[ 0 ], 118 elem = this[ 0 ],
128 parentOffset = { top: 0, left: 0 }; 119 parentOffset = { top: 0, left: 0 };
129   120  
130 // Fixed elements are offset from window (parentOffset = {top:0, left: 0}, -  
131 // because it is its only offset parent 121 // Fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is its only offset parent
132 if ( jQuery.css( elem, "position" ) === "fixed" ) { -  
133   122 if ( jQuery.css( elem, "position" ) === "fixed" ) {
134 // Assume getBoundingClientRect is there when computed position is fixed 123 // Assume getBoundingClientRect is there when computed position is fixed
135 offset = elem.getBoundingClientRect(); 124 offset = elem.getBoundingClientRect();
136   125  
137 } else { 126 } else {
138   -  
139 // Get *real* offsetParent 127 // Get *real* offsetParent
140 offsetParent = this.offsetParent(); 128 offsetParent = this.offsetParent();
141   129  
142 // Get correct offsets 130 // Get correct offsets
143 offset = this.offset(); 131 offset = this.offset();
144 if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) { 132 if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
145 parentOffset = offsetParent.offset(); 133 parentOffset = offsetParent.offset();
146 } 134 }
147   135  
148 // Add offsetParent borders 136 // Add offsetParent borders
149 parentOffset = { -  
150 top: parentOffset.top + jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ), 137 parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
151 left: parentOffset.left + jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true ) 138 parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
152 }; -  
153 } 139 }
154   140  
155 // Subtract parent offsets and element margins 141 // Subtract parent offsets and element margins
156 return { 142 return {
157 top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ), 143 top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
158 left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true ) 144 left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
159 }; 145 };
160 }, 146 },
161   -  
162 // This method will return documentElement in the following cases: -  
163 // 1) For the element inside the iframe without offsetParent, this method will return -  
164 // documentElement of the parent window -  
165 // 2) For the hidden or detached element -  
166 // 3) For body or html element, i.e. in case of the html node - it will return itself -  
167 // -  
168 // but those exceptions were never presented as a real life use-cases -  
169 // and might be considered as more preferable results. -  
170 // -  
171 // This logic, however, is not guaranteed and can change at any point in the future 147  
172 offsetParent: function() { 148 offsetParent: function() {
173 return this.map( function() { 149 return this.map(function() {
174 var offsetParent = this.offsetParent; 150 var offsetParent = this.offsetParent || docElem;
175   151  
176 while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) { 152 while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position" ) === "static" ) ) {
177 offsetParent = offsetParent.offsetParent; 153 offsetParent = offsetParent.offsetParent;
178 } 154 }
179   155  
180 return offsetParent || documentElement; 156 return offsetParent || docElem;
181 } ); 157 });
182 } 158 }
183 } ); 159 });
184   160  
185 // Create scrollLeft and scrollTop methods 161 // Create scrollLeft and scrollTop methods
186 jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) { 162 jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
187 var top = "pageYOffset" === prop; 163 var top = "pageYOffset" === prop;
188   164  
189 jQuery.fn[ method ] = function( val ) { 165 jQuery.fn[ method ] = function( val ) {
190 return access( this, function( elem, method, val ) { 166 return access( this, function( elem, method, val ) {
191 var win = getWindow( elem ); 167 var win = getWindow( elem );
192   168  
193 if ( val === undefined ) { 169 if ( val === undefined ) {
194 return win ? win[ prop ] : elem[ method ]; 170 return win ? win[ prop ] : elem[ method ];
195 } 171 }
196   172  
197 if ( win ) { 173 if ( win ) {
198 win.scrollTo( 174 win.scrollTo(
199 !top ? val : win.pageXOffset, 175 !top ? val : window.pageXOffset,
200 top ? val : win.pageYOffset 176 top ? val : window.pageYOffset
201 ); 177 );
202   178  
203 } else { 179 } else {
204 elem[ method ] = val; 180 elem[ method ] = val;
205 } 181 }
206 }, method, val, arguments.length ); 182 }, method, val, arguments.length, null );
207 }; 183 };
208 } ); 184 });
209   185  
210 // Support: Safari <=7 - 9.1, Chrome <=37 - 49 186 // Support: Safari<7+, Chrome<37+
211 // Add the top/left cssHooks using jQuery.fn.position 187 // Add the top/left cssHooks using jQuery.fn.position
212 // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084 188 // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
213 // Blink bug: https://bugs.chromium.org/p/chromium/issues/detail?id=589347 189 // Blink bug: https://code.google.com/p/chromium/issues/detail?id=229280
214 // getComputedStyle returns percent when specified for top/left/bottom/right; 190 // getComputedStyle returns percent when specified for top/left/bottom/right;
215 // rather than make the css module depend on the offset module, just check for it here 191 // rather than make the css module depend on the offset module, just check for it here
216 jQuery.each( [ "top", "left" ], function( i, prop ) { 192 jQuery.each( [ "top", "left" ], function( i, prop ) {
217 jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition, 193 jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
218 function( elem, computed ) { 194 function( elem, computed ) {
219 if ( computed ) { 195 if ( computed ) {
220 computed = curCSS( elem, prop ); 196 computed = curCSS( elem, prop );
221   -  
222 // If curCSS returns percentage, fallback to offset 197 // If curCSS returns percentage, fallback to offset
223 return rnumnonpx.test( computed ) ? 198 return rnumnonpx.test( computed ) ?
224 jQuery( elem ).position()[ prop ] + "px" : 199 jQuery( elem ).position()[ prop ] + "px" :
225 computed; 200 computed;
226 } 201 }
227 } 202 }
228 ); 203 );
229 } ); 204 });
230   205  
231 return jQuery; 206 return jQuery;
232 } ); 207 });
233   208