jquery.once.js 5.2 KB

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