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