286
Sf
simple-flask-coffee-sass-boilerplate / files
simple-flask-coffee-sass-boilerplate / bower_components / jquery / src / data.js
4.8 KB
1define([ 2 "./core", 3 "./var/rnotwhite", 4 "./core/access", 5 "./data/var/data_priv", 6 "./data/var/data_user" 7], function( jQuery, rnotwhite, access, data_priv, data_user ) { 8 9/* 10 Implementation Summary 11 12 1. Enforce API surface and semantic compatibility with 1.9.x branch 13 2. Improve the module's maintainability by reducing the storage 14 paths to a single mechanism. 15 3. Use the same single mechanism to support "private" and "user" data. 16 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData) 17 5. Avoid exposing implementation details on user objects (eg. expando properties) 18 6. Provide a clear path for implementation upgrade to WeakMap in 2014 19*/ 20var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/, 21 rmultiDash = /([A-Z])/g; 22 23function dataAttr( elem, key, data ) { 24 var name; 25 26 // If nothing was found internally, try to fetch any 27 // data from the HTML5 data-* attribute 28 if ( data === undefined && elem.nodeType === 1 ) { 29 name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase(); 30 data = elem.getAttribute( name ); 31 32 if ( typeof data === "string" ) { 33 try { 34 data = data === "true" ? true : 35 data === "false" ? false : 36 data === "null" ? null : 37 // Only convert to a number if it doesn't change the string 38 +data + "" === data ? +data : 39 rbrace.test( data ) ? jQuery.parseJSON( data ) : 40 data; 41 } catch( e ) {} 42 43 // Make sure we set the data so it isn't changed later 44 data_user.set( elem, key, data ); 45 } else { 46 data = undefined; 47 } 48 } 49 return data; 50} 51 52jQuery.extend({ 53 hasData: function( elem ) { 54 return data_user.hasData( elem ) || data_priv.hasData( elem ); 55 }, 56 57 data: function( elem, name, data ) { 58 return data_user.access( elem, name, data ); 59 }, 60 61 removeData: function( elem, name ) { 62 data_user.remove( elem, name ); 63 }, 64 65 // TODO: Now that all calls to _data and _removeData have been replaced 66 // with direct calls to data_priv methods, these can be deprecated. 67 _data: function( elem, name, data ) { 68 return data_priv.access( elem, name, data ); 69 }, 70 71 _removeData: function( elem, name ) { 72 data_priv.remove( elem, name ); 73 } 74}); 75 76jQuery.fn.extend({ 77 data: function( key, value ) { 78 var i, name, data, 79 elem = this[ 0 ], 80 attrs = elem && elem.attributes; 81 82 // Gets all values 83 if ( key === undefined ) { 84 if ( this.length ) { 85 data = data_user.get( elem ); 86 87 if ( elem.nodeType === 1 && !data_priv.get( elem, "hasDataAttrs" ) ) { 88 i = attrs.length; 89 while ( i-- ) { 90 91 // Support: IE11+ 92 // The attrs elements can be null (#14894) 93 if ( attrs[ i ] ) { 94 name = attrs[ i ].name; 95 if ( name.indexOf( "data-" ) === 0 ) { 96 name = jQuery.camelCase( name.slice(5) ); 97 dataAttr( elem, name, data[ name ] ); 98 } 99 } 100 } 101 data_priv.set( elem, "hasDataAttrs", true ); 102 } 103 } 104 105 return data; 106 } 107 108 // Sets multiple values 109 if ( typeof key === "object" ) { 110 return this.each(function() { 111 data_user.set( this, key ); 112 }); 113 } 114 115 return access( this, function( value ) { 116 var data, 117 camelKey = jQuery.camelCase( key ); 118 119 // The calling jQuery object (element matches) is not empty 120 // (and therefore has an element appears at this[ 0 ]) and the 121 // `value` parameter was not undefined. An empty jQuery object 122 // will result in `undefined` for elem = this[ 0 ] which will 123 // throw an exception if an attempt to read a data cache is made. 124 if ( elem && value === undefined ) { 125 // Attempt to get data from the cache 126 // with the key as-is 127 data = data_user.get( elem, key ); 128 if ( data !== undefined ) { 129 return data; 130 } 131 132 // Attempt to get data from the cache 133 // with the key camelized 134 data = data_user.get( elem, camelKey ); 135 if ( data !== undefined ) { 136 return data; 137 } 138 139 // Attempt to "discover" the data in 140 // HTML5 custom data-* attrs 141 data = dataAttr( elem, camelKey, undefined ); 142 if ( data !== undefined ) { 143 return data; 144 } 145 146 // We tried really hard, but the data doesn't exist. 147 return; 148 } 149 150 // Set the data... 151 this.each(function() { 152 // First, attempt to store a copy or reference of any 153 // data that might've been store with a camelCased key. 154 var data = data_user.get( this, camelKey ); 155 156 // For HTML5 data-* attribute interop, we have to 157 // store property names with dashes in a camelCase form. 158 // This might not apply to all properties...* 159 data_user.set( this, camelKey, value ); 160 161 // *... In the case of properties that might _actually_ 162 // have dashes, we need to also store a copy of that 163 // unchanged property. 164 if ( key.indexOf("-") !== -1 && data !== undefined ) { 165 data_user.set( this, key, value ); 166 } 167 }); 168 }, null, value, arguments.length > 1, null, true ); 169 }, 170 171 removeData: function( key ) { 172 return this.each(function() { 173 data_user.remove( this, key ); 174 }); 175 } 176}); 177 178return jQuery; 179});