Chris@0: /*! Chris@0: * jQuery Once v2.2.0 - http://github.com/robloach/jquery-once Chris@0: * @license MIT, GPL-2.0 Chris@0: * http://opensource.org/licenses/MIT Chris@0: * http://opensource.org/licenses/GPL-2.0 Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Universal Module Definition Chris@0: * Chris@0: * jQuery Once has a dependency on jQuery, so we wrap the code with a UMD Chris@0: * pattern in order to allow loading jQuery and jQuery Once through a module Chris@0: * definition like CommonJS, AMD, or through a global object. Chris@0: * Chris@0: * @see {@link http://github.com/umdjs/umd} Chris@0: */ Chris@0: (function (factory) { Chris@0: 'use strict'; Chris@0: Chris@0: if (typeof exports === 'object') { Chris@0: // CommonJS Chris@0: factory(require('jquery')); Chris@0: } else if (typeof define === 'function' && define.amd) { Chris@0: // AMD Chris@0: /* globals define */ Chris@0: define(['jquery'], factory); Chris@0: } else { Chris@0: // Global object Chris@0: /* globals jQuery */ Chris@0: factory(jQuery); Chris@0: } Chris@0: })(function ($) { Chris@0: 'use strict'; Chris@0: Chris@0: /** Chris@0: * Ensures that the given ID is valid, returning 'once' if one is not given. Chris@0: * Chris@0: * @param {string} [id=once] Chris@0: * A string representing the ID to check. Defaults to `'once'`. Chris@0: * Chris@0: * @returns The valid ID name. Chris@0: * Chris@0: * @throws TypeError when an ID is provided, but not a string. Chris@0: * @private Chris@0: */ Chris@0: var checkId = function (id) { Chris@0: id = id || 'once'; Chris@0: if (typeof id !== 'string') { Chris@0: throw new TypeError('The jQuery Once id parameter must be a string'); Chris@0: } Chris@0: return id; Chris@0: }; Chris@0: Chris@0: /** Chris@0: * Filter elements that have yet to be processed by the given data ID. Chris@0: * Chris@0: * @param {string} [id=once] Chris@0: * The data ID used to determine whether the given elements have already Chris@0: * been processed or not. Defaults to `'once'`. Chris@0: * Chris@0: * @returns jQuery collection of elements that have now run once by Chris@0: * the given ID. Chris@0: * Chris@0: * @example Chris@0: * ``` javascript Chris@0: * // The following will change the color of each paragraph to red, just once Chris@0: * // for the 'changecolor' key. Chris@0: * $('p').once('changecolor').css('color', 'red'); Chris@0: * Chris@0: * // .once() will return a set of elements that yet to have the once ID Chris@0: * // associated with them. You can return to the original collection set by Chris@0: * // using .end(). Chris@0: * $('p') Chris@0: * .once('changecolorblue') Chris@0: * .css('color', 'blue') Chris@0: * .end() Chris@0: * .css('color', 'red'); Chris@0: * Chris@0: * // To execute a function on the once set, you can use jQuery's each(). Chris@0: * $('div.calendar').once().each(function () { Chris@0: * // Since there is no once ID provided here, the key will be 'once'. Chris@0: * }); Chris@0: * ``` Chris@0: * Chris@0: * @see removeOnce Chris@0: * @see findOnce Chris@0: * @this jQuery Chris@0: * Chris@0: * @global Chris@0: * @public Chris@0: */ Chris@0: $.fn.once = function (id) { Chris@0: // Build the jQuery Once data name from the provided ID. Chris@0: var name = 'jquery-once-' + checkId(id); Chris@0: Chris@0: // Find elements that don't have the jQuery Once data applied to them yet. Chris@0: return this.filter(function () { Chris@0: return $(this).data(name) !== true; Chris@0: }).data(name, true); Chris@0: }; Chris@0: Chris@0: /** Chris@0: * Removes the once data from elements, based on the given ID. Chris@0: * Chris@0: * @param {string} [id=once] Chris@0: * A string representing the name of the data ID which should be used when Chris@0: * filtering the elements. This only filters elements that have already been Chris@0: * processed by the once function. The ID should be the same ID that was Chris@0: * originally passed to the once() function. Defaults to `'once'`. Chris@0: * Chris@0: * @returns jQuery collection of elements that were acted upon to remove their Chris@0: * once data. Chris@0: * Chris@0: * @example Chris@0: * ``` javascript Chris@0: * // Remove once data with the 'changecolor' ID. The result set is the Chris@0: * // elements that had their once data removed. Chris@0: * $('p').removeOnce('changecolor').css('color', ''); Chris@0: * Chris@0: * // Any jQuery function can be performed on the result set. Chris@0: * $('div.calendar').removeOnce().each(function () { Chris@0: * // Remove the calendar behavior. Chris@0: * }); Chris@0: * ``` Chris@0: * Chris@0: * @see once Chris@0: * @this jQuery Chris@0: * Chris@0: * @global Chris@0: * @public Chris@0: */ Chris@0: $.fn.removeOnce = function (id) { Chris@0: // Filter through the elements to find the once'd elements. Chris@0: return this.findOnce(id).removeData('jquery-once-' + checkId(id)); Chris@0: }; Chris@0: Chris@0: /** Chris@0: * Filters elements that have already been processed once. Chris@0: * Chris@0: * @param {string} [id=once] Chris@0: * A string representing the name of the data id which should be used when Chris@0: * filtering the elements. This only filters elements that have already Chris@0: * been processed by the once function. The id should be the same id that Chris@0: * was originally passed to the once() function. Defaults to 'once'. Chris@0: * Chris@0: * @returns jQuery collection of elements that have been run once. Chris@0: * Chris@0: * @example Chris@0: * ``` javascript Chris@0: * // Find all elements that have been changecolor'ed once. Chris@0: * $('p').findOnce('changecolor').each(function () { Chris@0: * // This function is called for all elements that has already once'd. Chris@0: * }); Chris@0: * Chris@0: * // Find all elements that have been acted on with the default 'once' key. Chris@0: * $('p').findOnce().each(function () { Chris@0: * // This function is called for all elements that have been acted on with Chris@0: * // a 'once' action. Chris@0: * }); Chris@0: * ``` Chris@0: * Chris@0: * @see once Chris@0: * @this jQuery Chris@0: * Chris@0: * @global Chris@0: * @public Chris@0: */ Chris@0: $.fn.findOnce = function (id) { Chris@0: // Filter the elements by which do have the data. Chris@0: var name = 'jquery-once-' + checkId(id); Chris@0: Chris@0: return this.filter(function () { Chris@0: return $(this).data(name) === true; Chris@0: }); Chris@0: }; Chris@0: });