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