ffffng/app/bower_components/jquery/src/data.js

180 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-05-13 13:25:33 +02:00
define( [
2014-05-12 20:08:19 +02:00
"./core",
"./core/access",
2017-05-13 13:25:33 +02:00
"./data/var/dataPriv",
"./data/var/dataUser"
], function( jQuery, access, dataPriv, dataUser ) {
"use strict";
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02: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
2014-05-12 20:08:19 +02:00
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
2017-05-13 13:25:33 +02:00
rmultiDash = /[A-Z]/g;
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;
}
2014-05-12 20:08:19 +02: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 ) {
2017-05-13 13:25:33 +02:00
name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
2014-05-12 20:08:19 +02:00
data = elem.getAttribute( name );
if ( typeof data === "string" ) {
try {
2017-05-13 13:25:33 +02:00
data = getData( data );
} catch ( e ) {}
2014-05-12 20:08:19 +02:00
// Make sure we set the data so it isn't changed later
2017-05-13 13:25:33 +02:00
dataUser.set( elem, key, data );
2014-05-12 20:08:19 +02:00
} else {
data = undefined;
}
}
return data;
}
2017-05-13 13:25:33 +02:00
jQuery.extend( {
2014-05-12 20:08:19 +02:00
hasData: function( elem ) {
2017-05-13 13:25:33 +02:00
return dataUser.hasData( elem ) || dataPriv.hasData( elem );
2014-05-12 20:08:19 +02:00
},
data: function( elem, name, data ) {
2017-05-13 13:25:33 +02:00
return dataUser.access( elem, name, data );
2014-05-12 20:08:19 +02:00
},
removeData: function( elem, name ) {
2017-05-13 13:25:33 +02:00
dataUser.remove( elem, name );
2014-05-12 20:08:19 +02:00
},
// TODO: Now that all calls to _data and _removeData have been replaced
2017-05-13 13:25:33 +02:00
// with direct calls to dataPriv methods, these can be deprecated.
2014-05-12 20:08:19 +02:00
_data: function( elem, name, data ) {
2017-05-13 13:25:33 +02:00
return dataPriv.access( elem, name, data );
2014-05-12 20:08:19 +02:00
},
_removeData: function( elem, name ) {
2017-05-13 13:25:33 +02:00
dataPriv.remove( elem, name );
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
jQuery.fn.extend( {
2014-05-12 20:08:19 +02:00
data: function( key, value ) {
var i, name, data,
elem = this[ 0 ],
attrs = elem && elem.attributes;
// Gets all values
if ( key === undefined ) {
if ( this.length ) {
2017-05-13 13:25:33 +02:00
data = dataUser.get( elem );
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
2014-05-12 20:08:19 +02:00
i = attrs.length;
while ( i-- ) {
2017-05-13 13:25:33 +02:00
// Support: IE 11 only
2014-05-12 20:08:19 +02:00
// The attrs elements can be null (#14894)
if ( attrs[ i ] ) {
name = attrs[ i ].name;
if ( name.indexOf( "data-" ) === 0 ) {
2017-05-13 13:25:33 +02:00
name = jQuery.camelCase( name.slice( 5 ) );
2014-05-12 20:08:19 +02:00
dataAttr( elem, name, data[ name ] );
}
}
}
2017-05-13 13:25:33 +02:00
dataPriv.set( elem, "hasDataAttrs", true );
2014-05-12 20:08:19 +02:00
}
}
return data;
}
// Sets multiple values
if ( typeof key === "object" ) {
2017-05-13 13:25:33 +02:00
return this.each( function() {
dataUser.set( this, key );
} );
2014-05-12 20:08:19 +02:00
}
return access( this, function( value ) {
2017-05-13 13:25:33 +02:00
var data;
2014-05-12 20:08:19 +02:00
// The calling jQuery object (element matches) is not empty
// (and therefore has an element appears at this[ 0 ]) and the
// `value` parameter was not undefined. An empty jQuery object
// will result in `undefined` for elem = this[ 0 ] which will
// throw an exception if an attempt to read a data cache is made.
if ( elem && value === undefined ) {
// Attempt to get data from the cache
2017-05-13 13:25:33 +02:00
// The key will always be camelCased in Data
data = dataUser.get( elem, key );
2014-05-12 20:08:19 +02:00
if ( data !== undefined ) {
return data;
}
// Attempt to "discover" the data in
// HTML5 custom data-* attrs
2017-05-13 13:25:33 +02:00
data = dataAttr( elem, key );
2014-05-12 20:08:19 +02:00
if ( data !== undefined ) {
return data;
}
// We tried really hard, but the data doesn't exist.
return;
}
// Set the data...
2017-05-13 13:25:33 +02:00
this.each( function() {
// We always store the camelCased key
dataUser.set( this, key, value );
} );
2014-05-12 20:08:19 +02:00
}, null, value, arguments.length > 1, null, true );
},
removeData: function( key ) {
2017-05-13 13:25:33 +02:00
return this.each( function() {
dataUser.remove( this, key );
} );
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
return jQuery;
2017-05-13 13:25:33 +02:00
} );