Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery.extend({
- isArray: Array.isArray || function (obj) {
- return jQuery.type(obj) === "array";
- },
- isWindow: function (obj) {
- /* jshint eqeqeq: false */
- return obj != null && obj == obj.window;
- },
- isNumeric: function (obj) {
- // parseFloat NaNs numeric-cast false positives (null|true|false|"")
- // ...but misinterprets leading-number strings, particularly hex literals ("0x...")
- // subtraction forces infinities to NaN
- // adding 1 corrects loss of precision from parseFloat (#15100)
- return !jQuery.isArray(obj) && (obj - parseFloat(obj) + 1) >= 0;
- }
- });
- var rnotwhite = (/\S+/g);
- // String to Object options format cache
- var optionsCache = {};
- // Convert String-formatted options into Object-formatted ones and store in cache
- function createOptions(options) {
- var object = optionsCache[options] = {};
- jQuery.each(options.match(rnotwhite) || [], function (_, flag) {
- object[flag] = true;
- });
- return object;
- }
- jQuery.Callbacks = function (options) {
- // Convert options from String-formatted to Object-formatted if needed
- // (we check in cache first)
- options = typeof options === "string" ?
- (optionsCache[options] || createOptions(options)) :
- jQuery.extend({}, options);
- var // Flag to know if list is currently firing
- firing,
- // Last fire value (for non-forgettable lists)
- memory,
- // Flag to know if list was already fired
- fired,
- // End of the loop when firing
- firingLength,
- // Index of currently firing callback (modified by remove if needed)
- firingIndex,
- // First callback to fire (used internally by add and fireWith)
- firingStart,
- // Actual callback list
- list = [],
- // Stack of fire calls for repeatable lists
- stack = !options.once && [],
- // Fire callbacks
- fire = function (data) {
- memory = options.memory && data;
- fired = true;
- firingIndex = firingStart || 0;
- firingStart = 0;
- firingLength = list.length;
- firing = true;
- for (; list && firingIndex < firingLength; firingIndex++) {
- if (list[firingIndex].apply(data[0], data[1]) === false && options.stopOnFalse) {
- memory = false; // To prevent further calls using add
- break;
- }
- }
- firing = false;
- if (list) {
- if (stack) {
- if (stack.length) {
- fire(stack.shift());
- }
- } else if (memory) {
- list = [];
- } else {
- self.disable();
- }
- }
- },
- // Actual Callbacks object
- self = {
- // Add a callback or a collection of callbacks to the list
- add: function () {
- if (list) {
- // First, we save the current length
- var start = list.length;
- (function add(args) {
- jQuery.each(args, function (_, arg) {
- var type = jQuery.type(arg);
- if (type === "function") {
- if (!options.unique || !self.has(arg)) {
- list.push(arg);
- }
- } else if (arg && arg.length && type !== "string") {
- // Inspect recursively
- add(arg);
- }
- });
- })(arguments);
- // Do we need to add the callbacks to the
- // current firing batch?
- if (firing) {
- firingLength = list.length;
- // With memory, if we're not firing then
- // we should call right away
- } else if (memory) {
- firingStart = start;
- fire(memory);
- }
- }
- return this;
- },
- // Remove a callback from the list
- remove: function () {
- if (list) {
- jQuery.each(arguments, function (_, arg) {
- var index;
- while ((index = jQuery.inArray(arg, list, index)) > -1) {
- list.splice(index, 1);
- // Handle firing indexes
- if (firing) {
- if (index <= firingLength) {
- firingLength--;
- }
- if (index <= firingIndex) {
- firingIndex--;
- }
- }
- }
- });
- }
- return this;
- },
- // Check if a given callback is in the list.
- // If no argument is given, return whether or not list has callbacks attached.
- has: function (fn) {
- return fn ? jQuery.inArray(fn, list) > -1 : !!(list && list.length);
- },
- // Remove all callbacks from the list
- empty: function () {
- list = [];
- firingLength = 0;
- return this;
- },
- // Have the list do nothing anymore
- disable: function () {
- list = stack = memory = undefined;
- return this;
- },
- // Is it disabled?
- disabled: function () {
- return !list;
- },
- // Lock the list in its current state
- lock: function () {
- stack = undefined;
- if (!memory) {
- self.disable();
- }
- return this;
- },
- // Is it locked?
- locked: function () {
- return !stack;
- },
- // Call all callbacks with the given context and arguments
- fireWith: function (context, args) {
- if (list && (!fired || stack)) {
- args = args || [];
- args = [context, args.slice ? args.slice() : args];
- if (firing) {
- stack.push(args);
- } else {
- fire(args);
- }
- }
- return this;
- },
- // Call all the callbacks with the given arguments
- fire: function () {
- self.fireWith(this, arguments);
- return this;
- },
- // To know if the callbacks have already been called at least once
- fired: function () {
- return !!fired;
- }
- };
- return self;
- };
- jQuery.extend({
- Deferred: function (func) {
- var tuples = [
- // action, add listener, listener list, final state
- ["resolve", "done", jQuery.Callbacks("once memory"), "resolved"],
- ["reject", "fail", jQuery.Callbacks("once memory"), "rejected"],
- ["notify", "progress", jQuery.Callbacks("memory")]
- ],
- state = "pending",
- promise = {
- state: function () {
- return state;
- },
- always: function () {
- deferred.done(arguments).fail(arguments);
- return this;
- },
- then: function ( /* fnDone, fnFail, fnProgress */) {
- var fns = arguments;
- return jQuery.Deferred(function (newDefer) {
- jQuery.each(tuples, function (i, tuple) {
- var fn = jQuery.isFunction(fns[i]) && fns[i];
- // deferred[ done | fail | progress ] for forwarding actions to newDefer
- deferred[tuple[1]](function () {
- var returned = fn && fn.apply(this, arguments);
- if (returned && jQuery.isFunction(returned.promise)) {
- returned.promise()
- .done(newDefer.resolve)
- .fail(newDefer.reject)
- .progress(newDefer.notify);
- } else {
- newDefer[tuple[0] + "With"](this === promise ? newDefer.promise() : this, fn ? [returned] : arguments);
- }
- });
- });
- fns = null;
- }).promise();
- },
- // Get a promise for this deferred
- // If obj is provided, the promise aspect is added to the object
- promise: function (obj) {
- return obj != null ? jQuery.extend(obj, promise) : promise;
- }
- },
- deferred = {};
- // Keep pipe for back-compat
- promise.pipe = promise.then;
- // Add list-specific methods
- jQuery.each(tuples, function (i, tuple) {
- var list = tuple[2],
- stateString = tuple[3];
- // promise[ done | fail | progress ] = list.add
- promise[tuple[1]] = list.add;
- // Handle state
- if (stateString) {
- list.add(function () {
- // state = [ resolved | rejected ]
- state = stateString;
- // [ reject_list | resolve_list ].disable; progress_list.lock
- }, tuples[i ^ 1][2].disable, tuples[2][2].lock);
- }
- // deferred[ resolve | reject | notify ]
- deferred[tuple[0]] = function () {
- deferred[tuple[0] + "With"](this === deferred ? promise : this, arguments);
- return this;
- };
- deferred[tuple[0] + "With"] = list.fireWith;
- });
- // Make the deferred a promise
- promise.promise(deferred);
- // Call given func if any
- if (func) {
- func.call(deferred, deferred);
- }
- // All done!
- return deferred;
- },
- // Deferred helper
- when: function (subordinate /* , ..., subordinateN */) {
- var i = 0,
- resolveValues = slice.call(arguments),
- length = resolveValues.length,
- // the count of uncompleted subordinates
- remaining = length !== 1 || (subordinate && jQuery.isFunction(subordinate.promise)) ? length : 0,
- // the master Deferred. If resolveValues consist of only a single Deferred, just use that.
- deferred = remaining === 1 ? subordinate : jQuery.Deferred(),
- // Update function for both resolve and progress values
- updateFunc = function (i, contexts, values) {
- return function (value) {
- contexts[i] = this;
- values[i] = arguments.length > 1 ? slice.call(arguments) : value;
- if (values === progressValues) {
- deferred.notifyWith(contexts, values);
- } else if (!(--remaining)) {
- deferred.resolveWith(contexts, values);
- }
- };
- },
- progressValues, progressContexts, resolveContexts;
- // add listeners to Deferred subordinates; treat others as resolved
- if (length > 1) {
- progressValues = new Array(length);
- progressContexts = new Array(length);
- resolveContexts = new Array(length);
- for (; i < length; i++) {
- if (resolveValues[i] && jQuery.isFunction(resolveValues[i].promise)) {
- resolveValues[i].promise()
- .done(updateFunc(i, resolveContexts, resolveValues))
- .fail(deferred.reject)
- .progress(updateFunc(i, progressContexts, progressValues));
- } else {
- --remaining;
- }
- }
- }
- // if we're not waiting on anything, resolve the master
- if (!remaining) {
- deferred.resolveWith(resolveContexts, resolveValues);
- }
- return deferred.promise();
- }
- });
- jQuery.fn.extend({
- on: function (types, selector, data, fn, /*INTERNAL*/ one) {
- var type, origFn;
- // Types can be a map of types/handlers
- if (typeof types === "object") {
- // ( types-Object, selector, data )
- if (typeof selector !== "string") {
- // ( types-Object, data )
- data = data || selector;
- selector = undefined;
- }
- for (type in types) {
- this.on(type, selector, data, types[type], one);
- }
- return this;
- }
- if (data == null && fn == null) {
- // ( types, fn )
- fn = selector;
- data = selector = undefined;
- } else if (fn == null) {
- if (typeof selector === "string") {
- // ( types, selector, fn )
- fn = data;
- data = undefined;
- } else {
- // ( types, data, fn )
- fn = data;
- data = selector;
- selector = undefined;
- }
- }
- if (fn === false) {
- fn = returnFalse;
- } else if (!fn) {
- return this;
- }
- if (one === 1) {
- origFn = fn;
- fn = function (event) {
- // Can use an empty set, since event contains the info
- jQuery().off(event);
- return origFn.apply(this, arguments);
- };
- // Use same guid so caller can remove using origFn
- fn.guid = origFn.guid || (origFn.guid = jQuery.guid++);
- }
- return this.each(function () {
- jQuery.event.add(this, types, fn, data, selector);
- });
- },
- one: function (types, selector, data, fn) {
- return this.on(types, selector, data, fn, 1);
- },
- off: function (types, selector, fn) {
- var handleObj, type;
- if (types && types.preventDefault && types.handleObj) {
- // ( event ) dispatched jQuery.Event
- handleObj = types.handleObj;
- jQuery(types.delegateTarget).off(
- handleObj.namespace ? handleObj.origType + "." + handleObj.namespace : handleObj.origType,
- handleObj.selector,
- handleObj.handler
- );
- return this;
- }
- if (typeof types === "object") {
- // ( types-object [, selector] )
- for (type in types) {
- this.off(type, selector, types[type]);
- }
- return this;
- }
- if (selector === false || typeof selector === "function") {
- // ( types [, fn] )
- fn = selector;
- selector = undefined;
- }
- if (fn === false) {
- fn = returnFalse;
- }
- return this.each(function () {
- jQuery.event.remove(this, types, fn, selector);
- });
- },
- trigger: function (type, data) {
- return this.each(function () {
- jQuery.event.trigger(type, data, this);
- });
- },
- triggerHandler: function (type, data) {
- var elem = this[0];
- if (elem) {
- return jQuery.event.trigger(type, data, elem, true);
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement