2015-08-16 06:59:58 +00:00
|
|
|
define( [
|
2013-08-15 18:15:49 +00:00
|
|
|
"./core",
|
2013-09-09 15:26:21 +00:00
|
|
|
"./core/access",
|
2017-11-22 19:43:44 +00:00
|
|
|
"./core/camelCase",
|
2014-07-17 17:25:59 +00:00
|
|
|
"./data/var/dataPriv",
|
|
|
|
"./data/var/dataUser"
|
2017-11-22 19:43:44 +00:00
|
|
|
], function( jQuery, access, camelCase, dataPriv, dataUser ) {
|
2013-08-15 18:15:49 +00:00
|
|
|
|
2016-04-25 18:25:08 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-04-25 22:26:36 +00:00
|
|
|
// Implementation Summary
|
|
|
|
//
|
|
|
|
// 1. Enforce API surface and semantic compatibility with 1.9.x branch
|
|
|
|
// 2. Improve the module's maintainability by reducing the storage
|
|
|
|
// paths to a single mechanism.
|
|
|
|
// 3. Use the same single mechanism to support "private" and "user" data.
|
|
|
|
// 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData)
|
|
|
|
// 5. Avoid exposing implementation details on user objects (eg. expando properties)
|
|
|
|
// 6. Provide a clear path for implementation upgrade to WeakMap in 2014
|
2013-02-11 17:39:44 +00:00
|
|
|
|
2013-10-11 08:09:54 +00:00
|
|
|
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
|
2014-12-15 08:06:09 +00:00
|
|
|
rmultiDash = /[A-Z]/g;
|
2013-01-26 01:44:55 +00:00
|
|
|
|
2016-07-15 17:42:25 +00:00
|
|
|
function getData( data ) {
|
|
|
|
if ( data === "true" ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( data === "false" ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( data === "null" ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only convert to a number if it doesn't change the string
|
|
|
|
if ( data === +data + "" ) {
|
|
|
|
return +data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( rbrace.test( data ) ) {
|
|
|
|
return JSON.parse( data );
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2013-09-09 16:13:41 +00:00
|
|
|
function dataAttr( elem, key, data ) {
|
|
|
|
var name;
|
|
|
|
|
|
|
|
// If nothing was found internally, try to fetch any
|
|
|
|
// data from the HTML5 data-* attribute
|
|
|
|
if ( data === undefined && elem.nodeType === 1 ) {
|
2014-12-15 08:06:09 +00:00
|
|
|
name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
|
2013-09-09 16:13:41 +00:00
|
|
|
data = elem.getAttribute( name );
|
|
|
|
|
|
|
|
if ( typeof data === "string" ) {
|
|
|
|
try {
|
2016-07-15 17:42:25 +00:00
|
|
|
data = getData( data );
|
2014-07-17 17:25:59 +00:00
|
|
|
} catch ( e ) {}
|
2013-09-09 16:13:41 +00:00
|
|
|
|
|
|
|
// Make sure we set the data so it isn't changed later
|
2014-07-17 17:25:59 +00:00
|
|
|
dataUser.set( elem, key, data );
|
2013-09-09 16:13:41 +00:00
|
|
|
} else {
|
|
|
|
data = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.extend( {
|
2012-10-16 15:15:41 +00:00
|
|
|
hasData: function( elem ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
return dataUser.hasData( elem ) || dataPriv.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 ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
return dataUser.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 ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
dataUser.remove( elem, name );
|
2010-09-24 20:24:07 +00:00
|
|
|
},
|
|
|
|
|
2013-04-08 19:39:20 +00:00
|
|
|
// TODO: Now that all calls to _data and _removeData have been replaced
|
2014-07-17 17:25:59 +00:00
|
|
|
// with direct calls to dataPriv methods, these can be deprecated.
|
2011-01-09 21:52:33 +00:00
|
|
|
_data: function( elem, name, data ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
return dataPriv.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 ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
dataPriv.remove( elem, name );
|
2009-11-25 17:09:53 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-11-25 17:09:53 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.fn.extend( {
|
2009-12-22 00:58:13 +00:00
|
|
|
data: function( key, value ) {
|
2013-11-05 20:03:28 +00:00
|
|
|
var i, name, data,
|
2013-04-08 19:31:47 +00:00
|
|
|
elem = this[ 0 ],
|
2013-09-28 15:57:46 +00:00
|
|
|
attrs = elem && elem.attributes;
|
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 ) {
|
2014-07-17 17:25:59 +00:00
|
|
|
data = dataUser.get( elem );
|
2010-10-17 15:42:53 +00:00
|
|
|
|
2014-07-17 17:25:59 +00:00
|
|
|
if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
|
2013-09-28 15:57:46 +00:00
|
|
|
i = attrs.length;
|
|
|
|
while ( i-- ) {
|
2010-12-19 21:33:53 +00:00
|
|
|
|
2019-04-29 20:56:09 +00:00
|
|
|
// Support: IE 11+
|
2014-04-30 14:43:39 +00:00
|
|
|
// The attrs elements can be null (#14894)
|
|
|
|
if ( attrs[ i ] ) {
|
|
|
|
name = attrs[ i ].name;
|
|
|
|
if ( name.indexOf( "data-" ) === 0 ) {
|
2017-11-22 19:43:44 +00:00
|
|
|
name = camelCase( name.slice( 5 ) );
|
2014-04-30 14:43:39 +00:00
|
|
|
dataAttr( elem, name, data[ name ] );
|
|
|
|
}
|
2010-12-03 07:19:39 +00:00
|
|
|
}
|
2010-10-17 15:42:53 +00:00
|
|
|
}
|
2014-07-17 17:25:59 +00:00
|
|
|
dataPriv.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" ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
return this.each( function() {
|
2014-07-17 17:25:59 +00:00
|
|
|
dataUser.set( this, key );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-11-25 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2013-09-09 15:26:21 +00:00
|
|
|
return access( this, function( value ) {
|
2015-05-04 13:36:58 +00:00
|
|
|
var data;
|
2011-12-06 20:25:38 +00:00
|
|
|
|
2013-03-04 00:40:33 +00:00
|
|
|
// The calling jQuery object (element matches) is not empty
|
2013-04-08 19:31:47 +00:00
|
|
|
// (and therefore has an element appears at this[ 0 ]) and the
|
2013-03-04 00:40:33 +00:00
|
|
|
// `value` parameter was not undefined. An empty jQuery object
|
2013-04-08 19:31:47 +00:00
|
|
|
// will result in `undefined` for elem = this[ 0 ] which will
|
2013-03-04 00:40:33 +00:00
|
|
|
// throw an exception if an attempt to read a data cache is made.
|
|
|
|
if ( elem && value === undefined ) {
|
2013-03-02 00:02:57 +00:00
|
|
|
|
|
|
|
// Attempt to get data from the cache
|
2015-05-04 13:36:58 +00:00
|
|
|
// The key will always be camelCased in Data
|
|
|
|
data = dataUser.get( elem, key );
|
2013-03-02 00:02:57 +00:00
|
|
|
if ( data !== undefined ) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2013-02-03 20:27:55 +00:00
|
|
|
// Attempt to "discover" the data in
|
|
|
|
// HTML5 custom data-* attrs
|
2015-05-04 13:36:58 +00:00
|
|
|
data = dataAttr( elem, key );
|
2013-02-03 20:27:55 +00:00
|
|
|
if ( data !== undefined ) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We tried really hard, but the data doesn't exist.
|
2013-04-08 19:31:47 +00:00
|
|
|
return;
|
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...
|
2015-08-16 06:59:58 +00:00
|
|
|
this.each( function() {
|
2015-05-04 13:36:58 +00:00
|
|
|
|
|
|
|
// We always store the camelCased key
|
|
|
|
dataUser.set( this, key, value );
|
2015-08-16 06:59:58 +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 ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
return this.each( function() {
|
2014-07-17 17:25:59 +00:00
|
|
|
dataUser.remove( this, key );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-11-25 17:09:53 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-09-08 17:54:33 +00:00
|
|
|
|
2013-09-09 01:25:27 +00:00
|
|
|
return jQuery;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|