annotate core/assets/vendor/jquery-once/jquery.once.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 /*!
Chris@0 2 * jQuery Once v2.2.0 - http://github.com/robloach/jquery-once
Chris@0 3 * @license MIT, GPL-2.0
Chris@0 4 * http://opensource.org/licenses/MIT
Chris@0 5 * http://opensource.org/licenses/GPL-2.0
Chris@0 6 */
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Universal Module Definition
Chris@0 10 *
Chris@0 11 * jQuery Once has a dependency on jQuery, so we wrap the code with a UMD
Chris@0 12 * pattern in order to allow loading jQuery and jQuery Once through a module
Chris@0 13 * definition like CommonJS, AMD, or through a global object.
Chris@0 14 *
Chris@0 15 * @see {@link http://github.com/umdjs/umd}
Chris@0 16 */
Chris@0 17 (function (factory) {
Chris@0 18 'use strict';
Chris@0 19
Chris@0 20 if (typeof exports === 'object') {
Chris@0 21 // CommonJS
Chris@0 22 factory(require('jquery'));
Chris@0 23 } else if (typeof define === 'function' && define.amd) {
Chris@0 24 // AMD
Chris@0 25 /* globals define */
Chris@0 26 define(['jquery'], factory);
Chris@0 27 } else {
Chris@0 28 // Global object
Chris@0 29 /* globals jQuery */
Chris@0 30 factory(jQuery);
Chris@0 31 }
Chris@0 32 })(function ($) {
Chris@0 33 'use strict';
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Ensures that the given ID is valid, returning 'once' if one is not given.
Chris@0 37 *
Chris@0 38 * @param {string} [id=once]
Chris@0 39 * A string representing the ID to check. Defaults to `'once'`.
Chris@0 40 *
Chris@0 41 * @returns The valid ID name.
Chris@0 42 *
Chris@0 43 * @throws TypeError when an ID is provided, but not a string.
Chris@0 44 * @private
Chris@0 45 */
Chris@0 46 var checkId = function (id) {
Chris@0 47 id = id || 'once';
Chris@0 48 if (typeof id !== 'string') {
Chris@0 49 throw new TypeError('The jQuery Once id parameter must be a string');
Chris@0 50 }
Chris@0 51 return id;
Chris@0 52 };
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Filter elements that have yet to be processed by the given data ID.
Chris@0 56 *
Chris@0 57 * @param {string} [id=once]
Chris@0 58 * The data ID used to determine whether the given elements have already
Chris@0 59 * been processed or not. Defaults to `'once'`.
Chris@0 60 *
Chris@0 61 * @returns jQuery collection of elements that have now run once by
Chris@0 62 * the given ID.
Chris@0 63 *
Chris@0 64 * @example
Chris@0 65 * ``` javascript
Chris@0 66 * // The following will change the color of each paragraph to red, just once
Chris@0 67 * // for the 'changecolor' key.
Chris@0 68 * $('p').once('changecolor').css('color', 'red');
Chris@0 69 *
Chris@0 70 * // .once() will return a set of elements that yet to have the once ID
Chris@0 71 * // associated with them. You can return to the original collection set by
Chris@0 72 * // using .end().
Chris@0 73 * $('p')
Chris@0 74 * .once('changecolorblue')
Chris@0 75 * .css('color', 'blue')
Chris@0 76 * .end()
Chris@0 77 * .css('color', 'red');
Chris@0 78 *
Chris@0 79 * // To execute a function on the once set, you can use jQuery's each().
Chris@0 80 * $('div.calendar').once().each(function () {
Chris@0 81 * // Since there is no once ID provided here, the key will be 'once'.
Chris@0 82 * });
Chris@0 83 * ```
Chris@0 84 *
Chris@0 85 * @see removeOnce
Chris@0 86 * @see findOnce
Chris@0 87 * @this jQuery
Chris@0 88 *
Chris@0 89 * @global
Chris@0 90 * @public
Chris@0 91 */
Chris@0 92 $.fn.once = function (id) {
Chris@0 93 // Build the jQuery Once data name from the provided ID.
Chris@0 94 var name = 'jquery-once-' + checkId(id);
Chris@0 95
Chris@0 96 // Find elements that don't have the jQuery Once data applied to them yet.
Chris@0 97 return this.filter(function () {
Chris@0 98 return $(this).data(name) !== true;
Chris@0 99 }).data(name, true);
Chris@0 100 };
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Removes the once data from elements, based on the given ID.
Chris@0 104 *
Chris@0 105 * @param {string} [id=once]
Chris@0 106 * A string representing the name of the data ID which should be used when
Chris@0 107 * filtering the elements. This only filters elements that have already been
Chris@0 108 * processed by the once function. The ID should be the same ID that was
Chris@0 109 * originally passed to the once() function. Defaults to `'once'`.
Chris@0 110 *
Chris@0 111 * @returns jQuery collection of elements that were acted upon to remove their
Chris@0 112 * once data.
Chris@0 113 *
Chris@0 114 * @example
Chris@0 115 * ``` javascript
Chris@0 116 * // Remove once data with the 'changecolor' ID. The result set is the
Chris@0 117 * // elements that had their once data removed.
Chris@0 118 * $('p').removeOnce('changecolor').css('color', '');
Chris@0 119 *
Chris@0 120 * // Any jQuery function can be performed on the result set.
Chris@0 121 * $('div.calendar').removeOnce().each(function () {
Chris@0 122 * // Remove the calendar behavior.
Chris@0 123 * });
Chris@0 124 * ```
Chris@0 125 *
Chris@0 126 * @see once
Chris@0 127 * @this jQuery
Chris@0 128 *
Chris@0 129 * @global
Chris@0 130 * @public
Chris@0 131 */
Chris@0 132 $.fn.removeOnce = function (id) {
Chris@0 133 // Filter through the elements to find the once'd elements.
Chris@0 134 return this.findOnce(id).removeData('jquery-once-' + checkId(id));
Chris@0 135 };
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Filters elements that have already been processed once.
Chris@0 139 *
Chris@0 140 * @param {string} [id=once]
Chris@0 141 * A string representing the name of the data id which should be used when
Chris@0 142 * filtering the elements. This only filters elements that have already
Chris@0 143 * been processed by the once function. The id should be the same id that
Chris@0 144 * was originally passed to the once() function. Defaults to 'once'.
Chris@0 145 *
Chris@0 146 * @returns jQuery collection of elements that have been run once.
Chris@0 147 *
Chris@0 148 * @example
Chris@0 149 * ``` javascript
Chris@0 150 * // Find all elements that have been changecolor'ed once.
Chris@0 151 * $('p').findOnce('changecolor').each(function () {
Chris@0 152 * // This function is called for all elements that has already once'd.
Chris@0 153 * });
Chris@0 154 *
Chris@0 155 * // Find all elements that have been acted on with the default 'once' key.
Chris@0 156 * $('p').findOnce().each(function () {
Chris@0 157 * // This function is called for all elements that have been acted on with
Chris@0 158 * // a 'once' action.
Chris@0 159 * });
Chris@0 160 * ```
Chris@0 161 *
Chris@0 162 * @see once
Chris@0 163 * @this jQuery
Chris@0 164 *
Chris@0 165 * @global
Chris@0 166 * @public
Chris@0 167 */
Chris@0 168 $.fn.findOnce = function (id) {
Chris@0 169 // Filter the elements by which do have the data.
Chris@0 170 var name = 'jquery-once-' + checkId(id);
Chris@0 171
Chris@0 172 return this.filter(function () {
Chris@0 173 return $(this).data(name) === true;
Chris@0 174 });
Chris@0 175 };
Chris@0 176 });