core.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. define([
  2. "./var/arr",
  3. "./var/slice",
  4. "./var/concat",
  5. "./var/push",
  6. "./var/indexOf",
  7. "./var/class2type",
  8. "./var/toString",
  9. "./var/hasOwn",
  10. "./var/support"
  11. ], function( arr, slice, concat, push, indexOf, class2type, toString, hasOwn, support ) {
  12. var
  13. // Use the correct document accordingly with window argument (sandbox)
  14. document = window.document,
  15. version = "@VERSION",
  16. // Define a local copy of jQuery
  17. jQuery = function( selector, context ) {
  18. // The jQuery object is actually just the init constructor 'enhanced'
  19. // Need init if jQuery is called (just allow error to be thrown if not included)
  20. return new jQuery.fn.init( selector, context );
  21. },
  22. // Support: Android<4.1
  23. // Make sure we trim BOM and NBSP
  24. rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,
  25. // Matches dashed string for camelizing
  26. rmsPrefix = /^-ms-/,
  27. rdashAlpha = /-([\da-z])/gi,
  28. // Used by jQuery.camelCase as callback to replace()
  29. fcamelCase = function( all, letter ) {
  30. return letter.toUpperCase();
  31. };
  32. jQuery.fn = jQuery.prototype = {
  33. // The current version of jQuery being used
  34. jquery: version,
  35. constructor: jQuery,
  36. // Start with an empty selector
  37. selector: "",
  38. // The default length of a jQuery object is 0
  39. length: 0,
  40. toArray: function() {
  41. return slice.call( this );
  42. },
  43. // Get the Nth element in the matched element set OR
  44. // Get the whole matched element set as a clean array
  45. get: function( num ) {
  46. return num != null ?
  47. // Return just the one element from the set
  48. ( num < 0 ? this[ num + this.length ] : this[ num ] ) :
  49. // Return all the elements in a clean array
  50. slice.call( this );
  51. },
  52. // Take an array of elements and push it onto the stack
  53. // (returning the new matched element set)
  54. pushStack: function( elems ) {
  55. // Build a new jQuery matched element set
  56. var ret = jQuery.merge( this.constructor(), elems );
  57. // Add the old object onto the stack (as a reference)
  58. ret.prevObject = this;
  59. ret.context = this.context;
  60. // Return the newly-formed element set
  61. return ret;
  62. },
  63. // Execute a callback for every element in the matched set.
  64. // (You can seed the arguments with an array of args, but this is
  65. // only used internally.)
  66. each: function( callback, args ) {
  67. return jQuery.each( this, callback, args );
  68. },
  69. map: function( callback ) {
  70. return this.pushStack( jQuery.map(this, function( elem, i ) {
  71. return callback.call( elem, i, elem );
  72. }));
  73. },
  74. slice: function() {
  75. return this.pushStack( slice.apply( this, arguments ) );
  76. },
  77. first: function() {
  78. return this.eq( 0 );
  79. },
  80. last: function() {
  81. return this.eq( -1 );
  82. },
  83. eq: function( i ) {
  84. var len = this.length,
  85. j = +i + ( i < 0 ? len : 0 );
  86. return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] );
  87. },
  88. end: function() {
  89. return this.prevObject || this.constructor(null);
  90. },
  91. // For internal use only.
  92. // Behaves like an Array's method, not like a jQuery method.
  93. push: push,
  94. sort: arr.sort,
  95. splice: arr.splice
  96. };
  97. jQuery.extend = jQuery.fn.extend = function() {
  98. var options, name, src, copy, copyIsArray, clone,
  99. target = arguments[0] || {},
  100. i = 1,
  101. length = arguments.length,
  102. deep = false;
  103. // Handle a deep copy situation
  104. if ( typeof target === "boolean" ) {
  105. deep = target;
  106. // Skip the boolean and the target
  107. target = arguments[ i ] || {};
  108. i++;
  109. }
  110. // Handle case when target is a string or something (possible in deep copy)
  111. if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
  112. target = {};
  113. }
  114. // Extend jQuery itself if only one argument is passed
  115. if ( i === length ) {
  116. target = this;
  117. i--;
  118. }
  119. for ( ; i < length; i++ ) {
  120. // Only deal with non-null/undefined values
  121. if ( (options = arguments[ i ]) != null ) {
  122. // Extend the base object
  123. for ( name in options ) {
  124. src = target[ name ];
  125. copy = options[ name ];
  126. // Prevent never-ending loop
  127. if ( target === copy ) {
  128. continue;
  129. }
  130. // Recurse if we're merging plain objects or arrays
  131. if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
  132. if ( copyIsArray ) {
  133. copyIsArray = false;
  134. clone = src && jQuery.isArray(src) ? src : [];
  135. } else {
  136. clone = src && jQuery.isPlainObject(src) ? src : {};
  137. }
  138. // Never move original objects, clone them
  139. target[ name ] = jQuery.extend( deep, clone, copy );
  140. // Don't bring in undefined values
  141. } else if ( copy !== undefined ) {
  142. target[ name ] = copy;
  143. }
  144. }
  145. }
  146. }
  147. // Return the modified object
  148. return target;
  149. };
  150. jQuery.extend({
  151. // Unique for each copy of jQuery on the page
  152. expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "" ),
  153. // Assume jQuery is ready without the ready module
  154. isReady: true,
  155. error: function( msg ) {
  156. throw new Error( msg );
  157. },
  158. noop: function() {},
  159. isFunction: function( obj ) {
  160. return jQuery.type(obj) === "function";
  161. },
  162. isArray: Array.isArray,
  163. isWindow: function( obj ) {
  164. return obj != null && obj === obj.window;
  165. },
  166. isNumeric: function( obj ) {
  167. // parseFloat NaNs numeric-cast false positives (null|true|false|"")
  168. // ...but misinterprets leading-number strings, particularly hex literals ("0x...")
  169. // subtraction forces infinities to NaN
  170. // adding 1 corrects loss of precision from parseFloat (#15100)
  171. return !jQuery.isArray( obj ) && (obj - parseFloat( obj ) + 1) >= 0;
  172. },
  173. isPlainObject: function( obj ) {
  174. // Not plain objects:
  175. // - Any object or value whose internal [[Class]] property is not "[object Object]"
  176. // - DOM nodes
  177. // - window
  178. if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
  179. return false;
  180. }
  181. if ( obj.constructor &&
  182. !hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
  183. return false;
  184. }
  185. // If the function hasn't returned already, we're confident that
  186. // |obj| is a plain object, created by {} or constructed with new Object
  187. return true;
  188. },
  189. isEmptyObject: function( obj ) {
  190. var name;
  191. for ( name in obj ) {
  192. return false;
  193. }
  194. return true;
  195. },
  196. type: function( obj ) {
  197. if ( obj == null ) {
  198. return obj + "";
  199. }
  200. // Support: Android<4.0, iOS<6 (functionish RegExp)
  201. return typeof obj === "object" || typeof obj === "function" ?
  202. class2type[ toString.call(obj) ] || "object" :
  203. typeof obj;
  204. },
  205. // Evaluates a script in a global context
  206. globalEval: function( code ) {
  207. var script,
  208. indirect = eval;
  209. code = jQuery.trim( code );
  210. if ( code ) {
  211. // If the code includes a valid, prologue position
  212. // strict mode pragma, execute code by injecting a
  213. // script tag into the document.
  214. if ( code.indexOf("use strict") === 1 ) {
  215. script = document.createElement("script");
  216. script.text = code;
  217. document.head.appendChild( script ).parentNode.removeChild( script );
  218. } else {
  219. // Otherwise, avoid the DOM node creation, insertion
  220. // and removal by using an indirect global eval
  221. indirect( code );
  222. }
  223. }
  224. },
  225. // Convert dashed to camelCase; used by the css and data modules
  226. // Support: IE9-11+
  227. // Microsoft forgot to hump their vendor prefix (#9572)
  228. camelCase: function( string ) {
  229. return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
  230. },
  231. nodeName: function( elem, name ) {
  232. return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase();
  233. },
  234. // args is for internal usage only
  235. each: function( obj, callback, args ) {
  236. var value,
  237. i = 0,
  238. length = obj.length,
  239. isArray = isArraylike( obj );
  240. if ( args ) {
  241. if ( isArray ) {
  242. for ( ; i < length; i++ ) {
  243. value = callback.apply( obj[ i ], args );
  244. if ( value === false ) {
  245. break;
  246. }
  247. }
  248. } else {
  249. for ( i in obj ) {
  250. value = callback.apply( obj[ i ], args );
  251. if ( value === false ) {
  252. break;
  253. }
  254. }
  255. }
  256. // A special, fast, case for the most common use of each
  257. } else {
  258. if ( isArray ) {
  259. for ( ; i < length; i++ ) {
  260. value = callback.call( obj[ i ], i, obj[ i ] );
  261. if ( value === false ) {
  262. break;
  263. }
  264. }
  265. } else {
  266. for ( i in obj ) {
  267. value = callback.call( obj[ i ], i, obj[ i ] );
  268. if ( value === false ) {
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. return obj;
  275. },
  276. // Support: Android<4.1
  277. trim: function( text ) {
  278. return text == null ?
  279. "" :
  280. ( text + "" ).replace( rtrim, "" );
  281. },
  282. // results is for internal usage only
  283. makeArray: function( arr, results ) {
  284. var ret = results || [];
  285. if ( arr != null ) {
  286. if ( isArraylike( Object(arr) ) ) {
  287. jQuery.merge( ret,
  288. typeof arr === "string" ?
  289. [ arr ] : arr
  290. );
  291. } else {
  292. push.call( ret, arr );
  293. }
  294. }
  295. return ret;
  296. },
  297. inArray: function( elem, arr, i ) {
  298. return arr == null ? -1 : indexOf.call( arr, elem, i );
  299. },
  300. merge: function( first, second ) {
  301. var len = +second.length,
  302. j = 0,
  303. i = first.length;
  304. for ( ; j < len; j++ ) {
  305. first[ i++ ] = second[ j ];
  306. }
  307. first.length = i;
  308. return first;
  309. },
  310. grep: function( elems, callback, invert ) {
  311. var callbackInverse,
  312. matches = [],
  313. i = 0,
  314. length = elems.length,
  315. callbackExpect = !invert;
  316. // Go through the array, only saving the items
  317. // that pass the validator function
  318. for ( ; i < length; i++ ) {
  319. callbackInverse = !callback( elems[ i ], i );
  320. if ( callbackInverse !== callbackExpect ) {
  321. matches.push( elems[ i ] );
  322. }
  323. }
  324. return matches;
  325. },
  326. // arg is for internal usage only
  327. map: function( elems, callback, arg ) {
  328. var value,
  329. i = 0,
  330. length = elems.length,
  331. isArray = isArraylike( elems ),
  332. ret = [];
  333. // Go through the array, translating each of the items to their new values
  334. if ( isArray ) {
  335. for ( ; i < length; i++ ) {
  336. value = callback( elems[ i ], i, arg );
  337. if ( value != null ) {
  338. ret.push( value );
  339. }
  340. }
  341. // Go through every key on the object,
  342. } else {
  343. for ( i in elems ) {
  344. value = callback( elems[ i ], i, arg );
  345. if ( value != null ) {
  346. ret.push( value );
  347. }
  348. }
  349. }
  350. // Flatten any nested arrays
  351. return concat.apply( [], ret );
  352. },
  353. // A global GUID counter for objects
  354. guid: 1,
  355. // Bind a function to a context, optionally partially applying any
  356. // arguments.
  357. proxy: function( fn, context ) {
  358. var tmp, args, proxy;
  359. if ( typeof context === "string" ) {
  360. tmp = fn[ context ];
  361. context = fn;
  362. fn = tmp;
  363. }
  364. // Quick check to determine if target is callable, in the spec
  365. // this throws a TypeError, but we will just return undefined.
  366. if ( !jQuery.isFunction( fn ) ) {
  367. return undefined;
  368. }
  369. // Simulated bind
  370. args = slice.call( arguments, 2 );
  371. proxy = function() {
  372. return fn.apply( context || this, args.concat( slice.call( arguments ) ) );
  373. };
  374. // Set the guid of unique handler to the same of original handler, so it can be removed
  375. proxy.guid = fn.guid = fn.guid || jQuery.guid++;
  376. return proxy;
  377. },
  378. now: Date.now,
  379. // jQuery.support is not used in Core but other projects attach their
  380. // properties to it so it needs to exist.
  381. support: support
  382. });
  383. // Populate the class2type map
  384. jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
  385. class2type[ "[object " + name + "]" ] = name.toLowerCase();
  386. });
  387. function isArraylike( obj ) {
  388. var length = obj.length,
  389. type = jQuery.type( obj );
  390. if ( type === "function" || jQuery.isWindow( obj ) ) {
  391. return false;
  392. }
  393. if ( obj.nodeType === 1 && length ) {
  394. return true;
  395. }
  396. return type === "array" || length === 0 ||
  397. typeof length === "number" && length > 0 && ( length - 1 ) in obj;
  398. }
  399. return jQuery;
  400. });