Advertisement
yambroskin

Untitled

Apr 21st, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery.extend({
  2.  
  3.     isArray: Array.isArray || function (obj) {
  4.         return jQuery.type(obj) === "array";
  5.     },
  6.  
  7.     isWindow: function (obj) {
  8.         /* jshint eqeqeq: false */
  9.         return obj != null && obj == obj.window;
  10.     },
  11.  
  12.     isNumeric: function (obj) {
  13.         // parseFloat NaNs numeric-cast false positives (null|true|false|"")
  14.         // ...but misinterprets leading-number strings, particularly hex literals ("0x...")
  15.         // subtraction forces infinities to NaN
  16.         // adding 1 corrects loss of precision from parseFloat (#15100)
  17.         return !jQuery.isArray(obj) && (obj - parseFloat(obj) + 1) >= 0;
  18.     }
  19. });
  20.  
  21. var rnotwhite = (/\S+/g);
  22.  
  23. // String to Object options format cache
  24. var optionsCache = {};
  25.  
  26. // Convert String-formatted options into Object-formatted ones and store in cache
  27. function createOptions(options) {
  28.     var object = optionsCache[options] = {};
  29.     jQuery.each(options.match(rnotwhite) || [], function (_, flag) {
  30.         object[flag] = true;
  31.     });
  32.     return object;
  33. }
  34.  
  35. jQuery.Callbacks = function (options) {
  36.  
  37.     // Convert options from String-formatted to Object-formatted if needed
  38.     // (we check in cache first)
  39.     options = typeof options === "string" ?
  40.         (optionsCache[options] || createOptions(options)) :
  41.         jQuery.extend({}, options);
  42.  
  43.     var // Flag to know if list is currently firing
  44.         firing,
  45.         // Last fire value (for non-forgettable lists)
  46.         memory,
  47.         // Flag to know if list was already fired
  48.         fired,
  49.         // End of the loop when firing
  50.         firingLength,
  51.         // Index of currently firing callback (modified by remove if needed)
  52.         firingIndex,
  53.         // First callback to fire (used internally by add and fireWith)
  54.         firingStart,
  55.         // Actual callback list
  56.         list = [],
  57.         // Stack of fire calls for repeatable lists
  58.         stack = !options.once && [],
  59.         // Fire callbacks
  60.         fire = function (data) {
  61.             memory = options.memory && data;
  62.             fired = true;
  63.             firingIndex = firingStart || 0;
  64.             firingStart = 0;
  65.             firingLength = list.length;
  66.             firing = true;
  67.             for (; list && firingIndex < firingLength; firingIndex++) {
  68.                 if (list[firingIndex].apply(data[0], data[1]) === false && options.stopOnFalse) {
  69.                     memory = false; // To prevent further calls using add
  70.                     break;
  71.                 }
  72.             }
  73.             firing = false;
  74.             if (list) {
  75.                 if (stack) {
  76.                     if (stack.length) {
  77.                         fire(stack.shift());
  78.                     }
  79.                 } else if (memory) {
  80.                     list = [];
  81.                 } else {
  82.                     self.disable();
  83.                 }
  84.             }
  85.         },
  86.         // Actual Callbacks object
  87.         self = {
  88.             // Add a callback or a collection of callbacks to the list
  89.             add: function () {
  90.                 if (list) {
  91.                     // First, we save the current length
  92.                     var start = list.length;
  93.                     (function add(args) {
  94.                         jQuery.each(args, function (_, arg) {
  95.                             var type = jQuery.type(arg);
  96.                             if (type === "function") {
  97.                                 if (!options.unique || !self.has(arg)) {
  98.                                     list.push(arg);
  99.                                 }
  100.                             } else if (arg && arg.length && type !== "string") {
  101.                                 // Inspect recursively
  102.                                 add(arg);
  103.                             }
  104.                         });
  105.                     })(arguments);
  106.                     // Do we need to add the callbacks to the
  107.                     // current firing batch?
  108.                     if (firing) {
  109.                         firingLength = list.length;
  110.                         // With memory, if we're not firing then
  111.                         // we should call right away
  112.                     } else if (memory) {
  113.                         firingStart = start;
  114.                         fire(memory);
  115.                     }
  116.                 }
  117.                 return this;
  118.             },
  119.             // Remove a callback from the list
  120.             remove: function () {
  121.                 if (list) {
  122.                     jQuery.each(arguments, function (_, arg) {
  123.                         var index;
  124.                         while ((index = jQuery.inArray(arg, list, index)) > -1) {
  125.                             list.splice(index, 1);
  126.                             // Handle firing indexes
  127.                             if (firing) {
  128.                                 if (index <= firingLength) {
  129.                                     firingLength--;
  130.                                 }
  131.                                 if (index <= firingIndex) {
  132.                                     firingIndex--;
  133.                                 }
  134.                             }
  135.                         }
  136.                     });
  137.                 }
  138.                 return this;
  139.             },
  140.             // Check if a given callback is in the list.
  141.             // If no argument is given, return whether or not list has callbacks attached.
  142.             has: function (fn) {
  143.                 return fn ? jQuery.inArray(fn, list) > -1 : !!(list && list.length);
  144.             },
  145.             // Remove all callbacks from the list
  146.             empty: function () {
  147.                 list = [];
  148.                 firingLength = 0;
  149.                 return this;
  150.             },
  151.             // Have the list do nothing anymore
  152.             disable: function () {
  153.                 list = stack = memory = undefined;
  154.                 return this;
  155.             },
  156.             // Is it disabled?
  157.             disabled: function () {
  158.                 return !list;
  159.             },
  160.             // Lock the list in its current state
  161.             lock: function () {
  162.                 stack = undefined;
  163.                 if (!memory) {
  164.                     self.disable();
  165.                 }
  166.                 return this;
  167.             },
  168.             // Is it locked?
  169.             locked: function () {
  170.                 return !stack;
  171.             },
  172.             // Call all callbacks with the given context and arguments
  173.             fireWith: function (context, args) {
  174.                 if (list && (!fired || stack)) {
  175.                     args = args || [];
  176.                     args = [context, args.slice ? args.slice() : args];
  177.                     if (firing) {
  178.                         stack.push(args);
  179.                     } else {
  180.                         fire(args);
  181.                     }
  182.                 }
  183.                 return this;
  184.             },
  185.             // Call all the callbacks with the given arguments
  186.             fire: function () {
  187.                 self.fireWith(this, arguments);
  188.                 return this;
  189.             },
  190.             // To know if the callbacks have already been called at least once
  191.             fired: function () {
  192.                 return !!fired;
  193.             }
  194.         };
  195.  
  196.     return self;
  197. };
  198.  
  199.  
  200. jQuery.extend({
  201.  
  202.     Deferred: function (func) {
  203.         var tuples = [
  204.                 // action, add listener, listener list, final state
  205.                 ["resolve", "done", jQuery.Callbacks("once memory"), "resolved"],
  206.                 ["reject", "fail", jQuery.Callbacks("once memory"), "rejected"],
  207.                 ["notify", "progress", jQuery.Callbacks("memory")]
  208.         ],
  209.             state = "pending",
  210.             promise = {
  211.                 state: function () {
  212.                     return state;
  213.                 },
  214.                 always: function () {
  215.                     deferred.done(arguments).fail(arguments);
  216.                     return this;
  217.                 },
  218.                 then: function ( /* fnDone, fnFail, fnProgress */) {
  219.                     var fns = arguments;
  220.                     return jQuery.Deferred(function (newDefer) {
  221.                         jQuery.each(tuples, function (i, tuple) {
  222.                             var fn = jQuery.isFunction(fns[i]) && fns[i];
  223.                             // deferred[ done | fail | progress ] for forwarding actions to newDefer
  224.                             deferred[tuple[1]](function () {
  225.                                 var returned = fn && fn.apply(this, arguments);
  226.                                 if (returned && jQuery.isFunction(returned.promise)) {
  227.                                     returned.promise()
  228.                                         .done(newDefer.resolve)
  229.                                         .fail(newDefer.reject)
  230.                                         .progress(newDefer.notify);
  231.                                 } else {
  232.                                     newDefer[tuple[0] + "With"](this === promise ? newDefer.promise() : this, fn ? [returned] : arguments);
  233.                                 }
  234.                             });
  235.                         });
  236.                         fns = null;
  237.                     }).promise();
  238.                 },
  239.                 // Get a promise for this deferred
  240.                 // If obj is provided, the promise aspect is added to the object
  241.                 promise: function (obj) {
  242.                     return obj != null ? jQuery.extend(obj, promise) : promise;
  243.                 }
  244.             },
  245.             deferred = {};
  246.  
  247.         // Keep pipe for back-compat
  248.         promise.pipe = promise.then;
  249.  
  250.         // Add list-specific methods
  251.         jQuery.each(tuples, function (i, tuple) {
  252.             var list = tuple[2],
  253.                 stateString = tuple[3];
  254.  
  255.             // promise[ done | fail | progress ] = list.add
  256.             promise[tuple[1]] = list.add;
  257.  
  258.             // Handle state
  259.             if (stateString) {
  260.                 list.add(function () {
  261.                     // state = [ resolved | rejected ]
  262.                     state = stateString;
  263.  
  264.                     // [ reject_list | resolve_list ].disable; progress_list.lock
  265.                 }, tuples[i ^ 1][2].disable, tuples[2][2].lock);
  266.             }
  267.  
  268.             // deferred[ resolve | reject | notify ]
  269.             deferred[tuple[0]] = function () {
  270.                 deferred[tuple[0] + "With"](this === deferred ? promise : this, arguments);
  271.                 return this;
  272.             };
  273.             deferred[tuple[0] + "With"] = list.fireWith;
  274.         });
  275.  
  276.         // Make the deferred a promise
  277.         promise.promise(deferred);
  278.  
  279.         // Call given func if any
  280.         if (func) {
  281.             func.call(deferred, deferred);
  282.         }
  283.  
  284.         // All done!
  285.         return deferred;
  286.     },
  287.  
  288.     // Deferred helper
  289.     when: function (subordinate /* , ..., subordinateN */) {
  290.         var i = 0,
  291.             resolveValues = slice.call(arguments),
  292.             length = resolveValues.length,
  293.  
  294.             // the count of uncompleted subordinates
  295.             remaining = length !== 1 || (subordinate && jQuery.isFunction(subordinate.promise)) ? length : 0,
  296.  
  297.             // the master Deferred. If resolveValues consist of only a single Deferred, just use that.
  298.             deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
  299.  
  300.             // Update function for both resolve and progress values
  301.             updateFunc = function (i, contexts, values) {
  302.                 return function (value) {
  303.                     contexts[i] = this;
  304.                     values[i] = arguments.length > 1 ? slice.call(arguments) : value;
  305.                     if (values === progressValues) {
  306.                         deferred.notifyWith(contexts, values);
  307.  
  308.                     } else if (!(--remaining)) {
  309.                         deferred.resolveWith(contexts, values);
  310.                     }
  311.                 };
  312.             },
  313.  
  314.             progressValues, progressContexts, resolveContexts;
  315.  
  316.         // add listeners to Deferred subordinates; treat others as resolved
  317.         if (length > 1) {
  318.             progressValues = new Array(length);
  319.             progressContexts = new Array(length);
  320.             resolveContexts = new Array(length);
  321.             for (; i < length; i++) {
  322.                 if (resolveValues[i] && jQuery.isFunction(resolveValues[i].promise)) {
  323.                     resolveValues[i].promise()
  324.                         .done(updateFunc(i, resolveContexts, resolveValues))
  325.                         .fail(deferred.reject)
  326.                         .progress(updateFunc(i, progressContexts, progressValues));
  327.                 } else {
  328.                     --remaining;
  329.                 }
  330.             }
  331.         }
  332.  
  333.         // if we're not waiting on anything, resolve the master
  334.         if (!remaining) {
  335.             deferred.resolveWith(resolveContexts, resolveValues);
  336.         }
  337.  
  338.         return deferred.promise();
  339.     }
  340. });
  341.  
  342.  
  343. jQuery.fn.extend({
  344.  
  345.     on: function (types, selector, data, fn, /*INTERNAL*/ one) {
  346.         var type, origFn;
  347.  
  348.         // Types can be a map of types/handlers
  349.         if (typeof types === "object") {
  350.             // ( types-Object, selector, data )
  351.             if (typeof selector !== "string") {
  352.                 // ( types-Object, data )
  353.                 data = data || selector;
  354.                 selector = undefined;
  355.             }
  356.             for (type in types) {
  357.                 this.on(type, selector, data, types[type], one);
  358.             }
  359.             return this;
  360.         }
  361.  
  362.         if (data == null && fn == null) {
  363.             // ( types, fn )
  364.             fn = selector;
  365.             data = selector = undefined;
  366.         } else if (fn == null) {
  367.             if (typeof selector === "string") {
  368.                 // ( types, selector, fn )
  369.                 fn = data;
  370.                 data = undefined;
  371.             } else {
  372.                 // ( types, data, fn )
  373.                 fn = data;
  374.                 data = selector;
  375.                 selector = undefined;
  376.             }
  377.         }
  378.         if (fn === false) {
  379.             fn = returnFalse;
  380.         } else if (!fn) {
  381.             return this;
  382.         }
  383.  
  384.         if (one === 1) {
  385.             origFn = fn;
  386.             fn = function (event) {
  387.                 // Can use an empty set, since event contains the info
  388.                 jQuery().off(event);
  389.                 return origFn.apply(this, arguments);
  390.             };
  391.             // Use same guid so caller can remove using origFn
  392.             fn.guid = origFn.guid || (origFn.guid = jQuery.guid++);
  393.         }
  394.         return this.each(function () {
  395.             jQuery.event.add(this, types, fn, data, selector);
  396.         });
  397.     },
  398.     one: function (types, selector, data, fn) {
  399.         return this.on(types, selector, data, fn, 1);
  400.     },
  401.     off: function (types, selector, fn) {
  402.         var handleObj, type;
  403.         if (types && types.preventDefault && types.handleObj) {
  404.             // ( event )  dispatched jQuery.Event
  405.             handleObj = types.handleObj;
  406.             jQuery(types.delegateTarget).off(
  407.                 handleObj.namespace ? handleObj.origType + "." + handleObj.namespace : handleObj.origType,
  408.                 handleObj.selector,
  409.                 handleObj.handler
  410.             );
  411.             return this;
  412.         }
  413.         if (typeof types === "object") {
  414.             // ( types-object [, selector] )
  415.             for (type in types) {
  416.                 this.off(type, selector, types[type]);
  417.             }
  418.             return this;
  419.         }
  420.         if (selector === false || typeof selector === "function") {
  421.             // ( types [, fn] )
  422.             fn = selector;
  423.             selector = undefined;
  424.         }
  425.         if (fn === false) {
  426.             fn = returnFalse;
  427.         }
  428.         return this.each(function () {
  429.             jQuery.event.remove(this, types, fn, selector);
  430.         });
  431.     },
  432.  
  433.     trigger: function (type, data) {
  434.         return this.each(function () {
  435.             jQuery.event.trigger(type, data, this);
  436.         });
  437.     },
  438.     triggerHandler: function (type, data) {
  439.         var elem = this[0];
  440.         if (elem) {
  441.             return jQuery.event.trigger(type, data, elem, true);
  442.         }
  443.     }
  444. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement