1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/bower_components/jquery/src/data.js

188 lines
5 KiB
JavaScript
Raw Normal View History

2016-01-09 13:36:35 -05:00
define( [
2015-06-26 11:53:49 -04:00
"./core",
"./core/access",
2016-01-09 13:36:35 -05:00
"./data/var/dataPriv",
"./data/var/dataUser"
], function( jQuery, access, dataPriv, dataUser ) {
2015-06-26 11:53:49 -04: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
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
2016-01-09 13:36:35 -05:00
rmultiDash = /[A-Z]/g;
2015-06-26 11:53:49 -04: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 ) {
2016-01-09 13:36:35 -05:00
name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
2015-06-26 11:53:49 -04:00
data = elem.getAttribute( name );
if ( typeof data === "string" ) {
try {
data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
2016-01-09 13:36:35 -05:00
2015-06-26 11:53:49 -04:00
// Only convert to a number if it doesn't change the string
+data + "" === data ? +data :
rbrace.test( data ) ? jQuery.parseJSON( data ) :
data;
2016-01-09 13:36:35 -05:00
} catch ( e ) {}
2015-06-26 11:53:49 -04:00
// Make sure we set the data so it isn't changed later
2016-01-09 13:36:35 -05:00
dataUser.set( elem, key, data );
2015-06-26 11:53:49 -04:00
} else {
data = undefined;
}
}
return data;
}
2016-01-09 13:36:35 -05:00
jQuery.extend( {
2015-06-26 11:53:49 -04:00
hasData: function( elem ) {
2016-01-09 13:36:35 -05:00
return dataUser.hasData( elem ) || dataPriv.hasData( elem );
2015-06-26 11:53:49 -04:00
},
data: function( elem, name, data ) {
2016-01-09 13:36:35 -05:00
return dataUser.access( elem, name, data );
2015-06-26 11:53:49 -04:00
},
removeData: function( elem, name ) {
2016-01-09 13:36:35 -05:00
dataUser.remove( elem, name );
2015-06-26 11:53:49 -04:00
},
// TODO: Now that all calls to _data and _removeData have been replaced
2016-01-09 13:36:35 -05:00
// with direct calls to dataPriv methods, these can be deprecated.
2015-06-26 11:53:49 -04:00
_data: function( elem, name, data ) {
2016-01-09 13:36:35 -05:00
return dataPriv.access( elem, name, data );
2015-06-26 11:53:49 -04:00
},
_removeData: function( elem, name ) {
2016-01-09 13:36:35 -05:00
dataPriv.remove( elem, name );
2015-06-26 11:53:49 -04:00
}
2016-01-09 13:36:35 -05:00
} );
2015-06-26 11:53:49 -04:00
2016-01-09 13:36:35 -05:00
jQuery.fn.extend( {
2015-06-26 11:53:49 -04: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 ) {
2016-01-09 13:36:35 -05:00
data = dataUser.get( elem );
2015-06-26 11:53:49 -04:00
2016-01-09 13:36:35 -05:00
if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
2015-06-26 11:53:49 -04:00
i = attrs.length;
while ( i-- ) {
// Support: IE11+
// The attrs elements can be null (#14894)
if ( attrs[ i ] ) {
name = attrs[ i ].name;
if ( name.indexOf( "data-" ) === 0 ) {
2016-01-09 13:36:35 -05:00
name = jQuery.camelCase( name.slice( 5 ) );
2015-06-26 11:53:49 -04:00
dataAttr( elem, name, data[ name ] );
}
}
}
2016-01-09 13:36:35 -05:00
dataPriv.set( elem, "hasDataAttrs", true );
2015-06-26 11:53:49 -04:00
}
}
return data;
}
// Sets multiple values
if ( typeof key === "object" ) {
2016-01-09 13:36:35 -05:00
return this.each( function() {
dataUser.set( this, key );
} );
2015-06-26 11:53:49 -04:00
}
return access( this, function( value ) {
2016-01-09 13:36:35 -05:00
var data, camelKey;
2015-06-26 11:53:49 -04: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 ) {
2016-01-09 13:36:35 -05:00
2015-06-26 11:53:49 -04:00
// Attempt to get data from the cache
// with the key as-is
2016-01-09 13:36:35 -05:00
data = dataUser.get( elem, key ) ||
// Try to find dashed key if it exists (gh-2779)
// This is for 2.2.x only
dataUser.get( elem, key.replace( rmultiDash, "-$&" ).toLowerCase() );
2015-06-26 11:53:49 -04:00
if ( data !== undefined ) {
return data;
}
2016-01-09 13:36:35 -05:00
camelKey = jQuery.camelCase( key );
2015-06-26 11:53:49 -04:00
// Attempt to get data from the cache
// with the key camelized
2016-01-09 13:36:35 -05:00
data = dataUser.get( elem, camelKey );
2015-06-26 11:53:49 -04:00
if ( data !== undefined ) {
return data;
}
// Attempt to "discover" the data in
// HTML5 custom data-* attrs
data = dataAttr( elem, camelKey, undefined );
if ( data !== undefined ) {
return data;
}
// We tried really hard, but the data doesn't exist.
return;
}
// Set the data...
2016-01-09 13:36:35 -05:00
camelKey = jQuery.camelCase( key );
this.each( function() {
2015-06-26 11:53:49 -04:00
// First, attempt to store a copy or reference of any
// data that might've been store with a camelCased key.
2016-01-09 13:36:35 -05:00
var data = dataUser.get( this, camelKey );
2015-06-26 11:53:49 -04:00
// For HTML5 data-* attribute interop, we have to
// store property names with dashes in a camelCase form.
// This might not apply to all properties...*
2016-01-09 13:36:35 -05:00
dataUser.set( this, camelKey, value );
2015-06-26 11:53:49 -04:00
// *... In the case of properties that might _actually_
// have dashes, we need to also store a copy of that
// unchanged property.
2016-01-09 13:36:35 -05:00
if ( key.indexOf( "-" ) > -1 && data !== undefined ) {
dataUser.set( this, key, value );
2015-06-26 11:53:49 -04:00
}
2016-01-09 13:36:35 -05:00
} );
2015-06-26 11:53:49 -04:00
}, null, value, arguments.length > 1, null, true );
},
removeData: function( key ) {
2016-01-09 13:36:35 -05:00
return this.each( function() {
dataUser.remove( this, key );
} );
2015-06-26 11:53:49 -04:00
}
2016-01-09 13:36:35 -05:00
} );
2015-06-26 11:53:49 -04:00
return jQuery;
2016-01-09 13:36:35 -05:00
} );