| 1 | define( [
|
| 2 | "./core",
|
| 3 | "./core/access",
|
| 4 | "./core/camelCase",
|
| 5 | "./data/var/dataPriv",
|
| 6 | "./data/var/dataUser"
|
| 7 | ], function( jQuery, access, camelCase, dataPriv, dataUser ) {
|
| 8 |
|
| 9 | "use strict";
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 | var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/,
|
| 22 | rmultiDash = /[A-Z]/g;
|
| 23 |
|
| 24 | function getData( data ) {
|
| 25 | if ( data === "true" ) {
|
| 26 | return true;
|
| 27 | }
|
| 28 |
|
| 29 | if ( data === "false" ) {
|
| 30 | return false;
|
| 31 | }
|
| 32 |
|
| 33 | if ( data === "null" ) {
|
| 34 | return null;
|
| 35 | }
|
| 36 |
|
| 37 |
|
| 38 | if ( data === +data + "" ) {
|
| 39 | return +data;
|
| 40 | }
|
| 41 |
|
| 42 | if ( rbrace.test( data ) ) {
|
| 43 | return JSON.parse( data );
|
| 44 | }
|
| 45 |
|
| 46 | return data;
|
| 47 | }
|
| 48 |
|
| 49 | function dataAttr( elem, key, data ) {
|
| 50 | var name;
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 | if ( data === undefined && elem.nodeType === 1 ) {
|
| 55 | name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase();
|
| 56 | data = elem.getAttribute( name );
|
| 57 |
|
| 58 | if ( typeof data === "string" ) {
|
| 59 | try {
|
| 60 | data = getData( data );
|
| 61 | } catch ( e ) {}
|
| 62 |
|
| 63 |
|
| 64 | dataUser.set( elem, key, data );
|
| 65 | } else {
|
| 66 | data = undefined;
|
| 67 | }
|
| 68 | }
|
| 69 | return data;
|
| 70 | }
|
| 71 |
|
| 72 | jQuery.extend( {
|
| 73 | hasData: function( elem ) {
|
| 74 | return dataUser.hasData( elem ) || dataPriv.hasData( elem );
|
| 75 | },
|
| 76 |
|
| 77 | data: function( elem, name, data ) {
|
| 78 | return dataUser.access( elem, name, data );
|
| 79 | },
|
| 80 |
|
| 81 | removeData: function( elem, name ) {
|
| 82 | dataUser.remove( elem, name );
|
| 83 | },
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 | _data: function( elem, name, data ) {
|
| 88 | return dataPriv.access( elem, name, data );
|
| 89 | },
|
| 90 |
|
| 91 | _removeData: function( elem, name ) {
|
| 92 | dataPriv.remove( elem, name );
|
| 93 | }
|
| 94 | } );
|
| 95 |
|
| 96 | jQuery.fn.extend( {
|
| 97 | data: function( key, value ) {
|
| 98 | var i, name, data,
|
| 99 | elem = this[ 0 ],
|
| 100 | attrs = elem && elem.attributes;
|
| 101 |
|
| 102 |
|
| 103 | if ( key === undefined ) {
|
| 104 | if ( this.length ) {
|
| 105 | data = dataUser.get( elem );
|
| 106 |
|
| 107 | if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) {
|
| 108 | i = attrs.length;
|
| 109 | while ( i-- ) {
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 | if ( attrs[ i ] ) {
|
| 114 | name = attrs[ i ].name;
|
| 115 | if ( name.indexOf( "data-" ) === 0 ) {
|
| 116 | name = camelCase( name.slice( 5 ) );
|
| 117 | dataAttr( elem, name, data[ name ] );
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 | dataPriv.set( elem, "hasDataAttrs", true );
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | return data;
|
| 126 | }
|
| 127 |
|
| 128 |
|
| 129 | if ( typeof key === "object" ) {
|
| 130 | return this.each( function() {
|
| 131 | dataUser.set( this, key );
|
| 132 | } );
|
| 133 | }
|
| 134 |
|
| 135 | return access( this, function( value ) {
|
| 136 | var data;
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 | if ( elem && value === undefined ) {
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 | data = dataUser.get( elem, key );
|
| 148 | if ( data !== undefined ) {
|
| 149 | return data;
|
| 150 | }
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 | data = dataAttr( elem, key );
|
| 155 | if ( data !== undefined ) {
|
| 156 | return data;
|
| 157 | }
|
| 158 |
|
| 159 |
|
| 160 | return;
|
| 161 | }
|
| 162 |
|
| 163 |
|
| 164 | this.each( function() {
|
| 165 |
|
| 166 |
|
| 167 | dataUser.set( this, key, value );
|
| 168 | } );
|
| 169 | }, null, value, arguments.length > 1, null, true );
|
| 170 | },
|
| 171 |
|
| 172 | removeData: function( key ) {
|
| 173 | return this.each( function() {
|
| 174 | dataUser.remove( this, key );
|
| 175 | } );
|
| 176 | }
|
| 177 | } );
|
| 178 |
|
| 179 | return jQuery;
|
| 180 | } );
|