1/** 2 * jQuery Once Plugin v1.2 3 * http://plugins.jquery.com/project/once 4 * 5 * Dual licensed under the MIT and GPL licenses: 6 * http://www.opensource.org/licenses/mit-license.php 7 * http://www.gnu.org/licenses/gpl.html 8 */ 9 10(function ($) { 11 var cache = {}, uuid = 0; 12 13 /** 14 * Filters elements by whether they have not yet been processed. 15 * 16 * @param id 17 * (Optional) If this is a string, then it will be used as the CSS class 18 * name that is applied to the elements for determining whether it has 19 * already been processed. The elements will get a class in the form of 20 * "id-processed". 21 * 22 * If the id parameter is a function, it will be passed off to the fn 23 * parameter and the id will become a unique identifier, represented as a 24 * number. 25 * 26 * When the id is neither a string or a function, it becomes a unique 27 * identifier, depicted as a number. The element's class will then be 28 * represented in the form of "jquery-once-#-processed". 29 * 30 * Take note that the id must be valid for usage as an element's class name. 31 * @param fn 32 * (Optional) If given, this function will be called for each element that 33 * has not yet been processed. The function's return value follows the same 34 * logic as $.each(). Returning true will continue to the next matched 35 * element in the set, while returning false will entirely break the 36 * iteration. 37 */ 38 $.fn.once = function (id, fn) { 39 if (typeof id != 'string') { 40 // Generate a numeric ID if the id passed can't be used as a CSS class. 41 if (!(id in cache)) { 42 cache[id] = ++uuid; 43 } 44 // When the fn parameter is not passed, we interpret it from the id. 45 if (!fn) { 46 fn = id; 47 } 48 id = 'jquery-once-' + cache[id]; 49 } 50 // Remove elements from the set that have already been processed. 51 var name = id + '-processed'; 52 var elements = this.not('.' + name).addClass(name); 53 54 return $.isFunction(fn) ? elements.each(fn) : elements; 55 }; 56 57 /** 58 * Filters elements that have been processed once already. 59 * 60 * @param id 61 * A required string representing the name of the class which should be used 62 * when filtering the elements. This only filters elements that have already 63 * been processed by the once function. The id should be the same id that 64 * was originally passed to the once() function. 65 * @param fn 66 * (Optional) If given, this function will be called for each element that 67 * has not yet been processed. The function's return value follows the same 68 * logic as $.each(). Returning true will continue to the next matched 69 * element in the set, while returning false will entirely break the 70 * iteration. 71 */ 72 $.fn.removeOnce = function (id, fn) { 73 var name = id + '-processed'; 74 var elements = this.filter('.' + name).removeClass(name); 75 76 return $.isFunction(fn) ? elements.each(fn) : elements; 77 }; 78})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.