2013-02-03 20:27:55 +00:00
|
|
|
var data_user, data_priv,
|
|
|
|
rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/,
|
2011-09-07 14:13:22 +00:00
|
|
|
rmultiDash = /([A-Z])/g;
|
2013-01-26 01:44:55 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
function Data() {
|
|
|
|
// Nodes|Objects
|
|
|
|
this.owners = [];
|
|
|
|
// Data objects
|
|
|
|
this.cache = [];
|
|
|
|
}
|
2013-01-04 01:39:15 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
Data.index = function( array, node ) {
|
|
|
|
return array.indexOf( node );
|
|
|
|
};
|
2010-12-19 21:33:53 +00:00
|
|
|
|
2013-01-04 01:39:15 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
Data.prototype = {
|
|
|
|
add: function( owner ) {
|
2013-02-03 20:31:23 +00:00
|
|
|
return (this.cache[ this.owners.push( owner ) - 1 ] = {});
|
2013-02-03 20:27:55 +00:00
|
|
|
},
|
|
|
|
set: function( owner, data, value ) {
|
|
|
|
var prop,
|
|
|
|
index = Data.index( this.owners, owner );
|
|
|
|
|
|
|
|
// If there is no entry for this "owner", create one inline
|
|
|
|
// and set the index as though an owner entry had always existed
|
|
|
|
if ( index === -1 ) {
|
|
|
|
this.add( owner );
|
|
|
|
index = this.owners.length - 1;
|
2009-12-07 04:00:31 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
// Handle: [ owner, key, value ] args
|
|
|
|
if ( typeof data === "string" ) {
|
|
|
|
this.cache[ index ][ data ] = value;
|
2013-01-04 01:39:15 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// Handle: [ owner, { properties } ] args
|
2012-12-31 20:09:45 +00:00
|
|
|
} else {
|
2013-02-03 20:27:55 +00:00
|
|
|
// In the case where there was actually no "owner" entry and
|
|
|
|
// this.add( owner ) was called to create one, there will be
|
|
|
|
// a corresponding empty plain object in the cache.
|
|
|
|
if ( jQuery.isEmptyObject( this.cache[ index ] ) ) {
|
|
|
|
this.cache[ index ] = data;
|
2009-11-25 17:09:53 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// Otherwise, copy the properties one-by-one to the cache object
|
|
|
|
} else {
|
|
|
|
for ( prop in data ) {
|
|
|
|
this.cache[ index ][ prop ] = data[ prop ];
|
|
|
|
}
|
|
|
|
}
|
2011-01-09 21:52:33 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
get: function( owner, key ) {
|
|
|
|
var cache,
|
|
|
|
index = Data.index( this.owners, owner );
|
|
|
|
|
|
|
|
// A valid cache is found, or needs to be created.
|
|
|
|
// New entries will be added and return the current
|
|
|
|
// empty data object to be used as a return reference
|
|
|
|
// return this.add( owner );
|
|
|
|
// This logic was required by expectations made of the
|
|
|
|
// old data system.
|
|
|
|
cache = index === -1 ?
|
|
|
|
this.add( owner ) : this.cache[ index ];
|
|
|
|
|
|
|
|
return key === undefined ?
|
|
|
|
cache : cache[ key ];
|
|
|
|
},
|
|
|
|
access: function( owner, key, value ) {
|
|
|
|
if ( value === undefined && (key && typeof key !== "object") ) {
|
|
|
|
// Assume this is a request to read the cached data
|
|
|
|
return this.get( owner, key );
|
|
|
|
} else {
|
2011-07-11 01:42:40 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// If only an owner was specified, return the entire
|
|
|
|
// cache object.
|
|
|
|
if ( key === undefined ) {
|
|
|
|
return this.get( owner );
|
|
|
|
}
|
2013-01-04 01:39:15 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// Allow setting or extending (existing objects) with an
|
|
|
|
// object of properties, or a key and val
|
|
|
|
this.set( owner, key, value );
|
|
|
|
return value !== undefined ? value : key;
|
2013-01-04 01:39:15 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
// Otherwise, this is a read request.
|
|
|
|
return this.get( owner, key );
|
|
|
|
},
|
|
|
|
remove: function( owner, key ) {
|
|
|
|
var i, l, name,
|
|
|
|
camel = jQuery.camelCase,
|
|
|
|
index = Data.index( this.owners, owner ),
|
|
|
|
cache = this.cache[ index ];
|
2013-01-04 01:39:15 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
if ( key === undefined ) {
|
|
|
|
cache = {};
|
|
|
|
} else {
|
|
|
|
if ( cache ) {
|
|
|
|
// Support array or space separated string of keys
|
|
|
|
if ( !Array.isArray( key ) ) {
|
|
|
|
// Try the string as a key before any manipulation
|
|
|
|
//
|
|
|
|
|
|
|
|
if ( key in cache ) {
|
|
|
|
name = [ key ];
|
2012-10-16 15:15:41 +00:00
|
|
|
} else {
|
2013-02-03 20:27:55 +00:00
|
|
|
// Split the camel cased version by spaces unless a key with the spaces exists
|
|
|
|
name = camel( key );
|
|
|
|
name = name in cache ?
|
|
|
|
[ name ] : name.split(" ");
|
2012-10-16 15:15:41 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
} else {
|
|
|
|
// If "name" is an array of keys...
|
|
|
|
// When data is initially created, via ("key", "val") signature,
|
|
|
|
// keys will be converted to camelCase.
|
|
|
|
// Since there is no way to tell _how_ a key was added, remove
|
|
|
|
// both plain key and camelCase key. #12786
|
|
|
|
// This will only penalize the array argument path.
|
|
|
|
name = key.concat( key.map( camel ) );
|
2012-10-16 15:15:41 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
i = 0;
|
|
|
|
l = name.length;
|
2011-01-09 21:52:33 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
for ( ; i < l; i++ ) {
|
|
|
|
delete cache[ name[i] ];
|
|
|
|
}
|
2013-01-04 01:39:15 +00:00
|
|
|
}
|
2011-01-09 21:52:33 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
this.cache[ index ] = cache;
|
|
|
|
},
|
|
|
|
hasData: function( owner ) {
|
|
|
|
var index = Data.index( this.owners, owner );
|
2012-12-31 23:06:38 +00:00
|
|
|
|
2013-02-03 22:55:28 +00:00
|
|
|
return index !== -1 && jQuery.isEmptyObject( this.cache[ index ] );
|
2013-02-03 20:27:55 +00:00
|
|
|
},
|
|
|
|
discard: function( owner ) {
|
|
|
|
var index = Data.index( this.owners, owner );
|
2013-01-04 01:39:15 +00:00
|
|
|
|
2013-02-03 23:04:53 +00:00
|
|
|
if ( index !== -1 ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
this.owners.splice( index, 1 );
|
|
|
|
this.cache.splice( index, 1 );
|
2012-12-31 23:06:38 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
return this;
|
2012-10-16 15:15:41 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This will be used by remove()/cleanData() in manipulation to sever
|
|
|
|
// remaining references to node objects. One day we'll replace the dual
|
|
|
|
// arrays with a WeakMap and this won't be an issue.
|
|
|
|
// (Splices the data objects out of the internal cache arrays)
|
|
|
|
function data_discard( owner ) {
|
|
|
|
data_user.discard( owner );
|
|
|
|
data_priv.discard( owner );
|
|
|
|
}
|
2011-08-05 13:43:58 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// These may be used throughout the jQuery core codebase
|
|
|
|
data_user = new Data();
|
|
|
|
data_priv = new Data();
|
2011-11-06 22:24:44 +00:00
|
|
|
|
|
|
|
|
2012-10-16 15:15:41 +00:00
|
|
|
jQuery.extend({
|
2013-02-03 20:27:55 +00:00
|
|
|
// This is no longer relevant to jQuery core, but must remain
|
|
|
|
// supported for the sake of jQuery 1.9.x API surface compatibility.
|
|
|
|
acceptData: function() {
|
|
|
|
return true;
|
|
|
|
},
|
2012-10-16 15:15:41 +00:00
|
|
|
// Unique for each copy of jQuery on the page
|
|
|
|
// Non-digits removed to match rinlinejQuery
|
2012-11-26 08:20:43 +00:00
|
|
|
expando: "jQuery" + ( core_version + Math.random() ).replace( /\D/g, "" ),
|
2011-01-09 21:52:33 +00:00
|
|
|
|
2012-10-16 15:15:41 +00:00
|
|
|
hasData: function( elem ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
return data_user.hasData( elem ) || data_priv.hasData( elem );
|
2012-10-16 15:15:41 +00:00
|
|
|
},
|
2012-07-06 13:22:44 +00:00
|
|
|
|
2012-10-16 15:15:41 +00:00
|
|
|
data: function( elem, name, data ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
return data_user.access( elem, name, data );
|
2012-10-16 15:15:41 +00:00
|
|
|
},
|
2012-07-06 13:22:44 +00:00
|
|
|
|
2012-10-16 15:15:41 +00:00
|
|
|
removeData: function( elem, name ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
return data_user.remove( elem, name );
|
2010-09-24 20:24:07 +00:00
|
|
|
},
|
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// TODO: Replace all calls to _data and _removeData with direct
|
|
|
|
// calls to
|
|
|
|
//
|
|
|
|
// data_priv.access( elem, name, data );
|
|
|
|
//
|
|
|
|
// data_priv.remove( elem, name );
|
|
|
|
//
|
2011-01-09 21:52:33 +00:00
|
|
|
_data: function( elem, name, data ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
return data_priv.access( elem, name, data );
|
2012-10-16 15:15:41 +00:00
|
|
|
},
|
2013-01-26 01:44:55 +00:00
|
|
|
|
2012-12-31 20:09:45 +00:00
|
|
|
_removeData: function( elem, name ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
return data_priv.remove( elem, name );
|
2009-11-25 17:09:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
jQuery.fn.extend({
|
2009-12-22 00:58:13 +00:00
|
|
|
data: function( key, value ) {
|
2012-10-31 17:55:51 +00:00
|
|
|
var attrs, name,
|
2011-12-06 20:25:38 +00:00
|
|
|
elem = this[0],
|
|
|
|
i = 0,
|
2011-09-19 20:13:57 +00:00
|
|
|
data = null;
|
2010-10-17 15:42:53 +00:00
|
|
|
|
2011-12-06 20:25:38 +00:00
|
|
|
// Gets all values
|
|
|
|
if ( key === undefined ) {
|
2010-10-17 15:42:53 +00:00
|
|
|
if ( this.length ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
data = data_user.get( elem );
|
2010-10-17 15:42:53 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
if ( elem.nodeType === 1 && !data_priv.get( elem, "hasDataAttrs" ) ) {
|
2012-10-31 17:55:51 +00:00
|
|
|
attrs = elem.attributes;
|
|
|
|
for ( ; i < attrs.length; i++ ) {
|
|
|
|
name = attrs[i].name;
|
2010-12-19 21:33:53 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
if ( name.indexOf( "data-" ) === 0 ) {
|
|
|
|
name = jQuery.camelCase( name.substring(5) );
|
2011-12-06 20:25:38 +00:00
|
|
|
dataAttr( elem, name, data[ name ] );
|
2010-12-03 07:19:39 +00:00
|
|
|
}
|
2010-10-17 15:42:53 +00:00
|
|
|
}
|
2013-02-03 20:27:55 +00:00
|
|
|
data_priv.set( elem, "hasDataAttrs", true );
|
2010-10-17 15:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2011-12-06 20:25:38 +00:00
|
|
|
}
|
2009-12-09 21:16:18 +00:00
|
|
|
|
2011-12-06 20:25:38 +00:00
|
|
|
// Sets multiple values
|
|
|
|
if ( typeof key === "object" ) {
|
2009-12-09 21:16:18 +00:00
|
|
|
return this.each(function() {
|
2013-02-03 20:27:55 +00:00
|
|
|
data_user.set( this, key );
|
2009-12-09 21:16:18 +00:00
|
|
|
});
|
2009-11-25 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 20:25:38 +00:00
|
|
|
return jQuery.access( this, function( value ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
var data,
|
|
|
|
camelKey = jQuery.camelCase( key );
|
2011-12-06 20:25:38 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// Get the Data...
|
2011-12-06 20:25:38 +00:00
|
|
|
if ( value === undefined ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
|
|
|
|
// Attempt to get data from the cache
|
|
|
|
// with the key as-is
|
|
|
|
data = data_user.get( elem, key );
|
|
|
|
if ( data !== undefined ) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to "discover" the data in
|
|
|
|
// HTML5 custom data-* attrs
|
|
|
|
data = dataAttr( elem, key, undefined );
|
|
|
|
if ( data !== undefined ) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// As a last resort, attempt to find
|
|
|
|
// the data by checking AGAIN, but with
|
|
|
|
// a camelCased key.
|
|
|
|
data = data_user.get( elem, camelKey );
|
|
|
|
if ( data !== undefined ) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We tried really hard, but the data doesn't exist.
|
|
|
|
return undefined;
|
2009-11-27 19:28:42 +00:00
|
|
|
}
|
2010-07-27 17:45:32 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// Set the data...
|
2011-12-06 20:25:38 +00:00
|
|
|
this.each(function() {
|
2013-02-03 20:27:55 +00:00
|
|
|
// First, attempt to store a copy or reference of any
|
|
|
|
// data that might've been store with a camelCased key.
|
|
|
|
var data = data_user.get( this, camelKey );
|
|
|
|
|
|
|
|
// For HTML5 data-* attribute interop, we have to
|
|
|
|
// store property names with dashes in a camelCase form.
|
|
|
|
// This might not apply to all properties...*
|
|
|
|
data_user.set( this, camelKey, value );
|
|
|
|
|
|
|
|
// *... In the case of properties that might ACTUALLY
|
|
|
|
// have dashes, we need to also store a copy of that
|
|
|
|
// unchanged property.
|
|
|
|
if ( /-/.test( key ) && data !== undefined ) {
|
|
|
|
data_user.set( this, key, value );
|
|
|
|
}
|
2009-11-25 17:09:53 +00:00
|
|
|
});
|
2012-11-06 14:53:00 +00:00
|
|
|
}, null, value, arguments.length > 1, null, true );
|
2009-11-25 17:09:53 +00:00
|
|
|
},
|
|
|
|
|
2009-12-22 00:58:13 +00:00
|
|
|
removeData: function( key ) {
|
|
|
|
return this.each(function() {
|
2013-02-03 20:27:55 +00:00
|
|
|
data_user.remove( this, key );
|
2009-11-25 17:09:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2010-09-08 17:54:33 +00:00
|
|
|
|
2010-10-17 15:42:53 +00:00
|
|
|
function dataAttr( elem, key, data ) {
|
2013-02-03 20:27:55 +00:00
|
|
|
var name;
|
|
|
|
|
2010-10-17 15:42:53 +00:00
|
|
|
// If nothing was found internally, try to fetch any
|
|
|
|
// data from the HTML5 data-* attribute
|
|
|
|
if ( data === undefined && elem.nodeType === 1 ) {
|
2011-09-07 14:13:22 +00:00
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase();
|
2011-04-10 19:17:00 +00:00
|
|
|
data = elem.getAttribute( name );
|
2010-10-17 15:42:53 +00:00
|
|
|
|
|
|
|
if ( typeof data === "string" ) {
|
|
|
|
try {
|
|
|
|
data = data === "true" ? true :
|
2013-02-03 20:27:55 +00:00
|
|
|
data === "false" ? false :
|
|
|
|
data === "null" ? null :
|
|
|
|
// Only convert to a number if it doesn't change the string
|
|
|
|
+data + "" === data ? +data :
|
|
|
|
rbrace.test( data ) ?
|
|
|
|
JSON.parse( data ) : data;
|
2010-10-17 15:42:53 +00:00
|
|
|
} catch( e ) {}
|
|
|
|
|
|
|
|
// Make sure we set the data so it isn't changed later
|
2013-02-03 20:27:55 +00:00
|
|
|
data_user.set( elem, key, data );
|
2010-10-17 15:42:53 +00:00
|
|
|
} else {
|
|
|
|
data = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|