Changeset 19954
- Timestamp:
- 02/19/2012 11:08:10 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/js/utils.dev.js
r16282 r19954 4 4 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL. 5 5 6 each : function(o , cb, s) {6 each : function(obj, cb, scope) { 7 7 var n, l; 8 8 9 if ( !o)9 if ( !obj ) 10 10 return 0; 11 11 12 s = s || o;12 scope = scope || obj; 13 13 14 if ( typeof(o.length) != 'undefined') {15 for ( n=0, l = o.length; n<l; n++) {16 if ( cb.call(s, o[n], n, o) === false)14 if ( typeof(obj.length) != 'undefined' ) { 15 for ( n = 0, l = obj.length; n < l; n++ ) { 16 if ( cb.call(scope, obj[n], n, obj) === false ) 17 17 return 0; 18 18 } 19 19 } else { 20 for ( n in o) {21 if ( o.hasOwnProperty(n)) {22 if ( cb.call(s, o[n], n, o) === false) {20 for ( n in obj ) { 21 if ( obj.hasOwnProperty(n) ) { 22 if ( cb.call(scope, obj[n], n, obj) === false ) { 23 23 return 0; 24 24 } … … 29 29 }, 30 30 31 getHash : function(n) { 32 var v = this.get(n), h; 31 /** 32 * Get a multi-values cookie. 33 * Returns a JS object with the name: 'value' pairs. 34 */ 35 getHash : function(name) { 36 var all = this.get(name), ret; 33 37 34 if ( v) {35 this.each( v.split('&'), function(v) {36 v = v.split('=');37 h = h|| {};38 h[v[0]] = v[1];38 if ( all ) { 39 this.each( all.split('&'), function(pair) { 40 pair = pair.split('='); 41 ret = ret || {}; 42 ret[pair[0]] = pair[1]; 39 43 }); 40 44 } 41 return h;45 return ret; 42 46 }, 43 47 44 setHash : function(n, v, e, p, d, s) { 45 var o = ''; 48 /** 49 * Set a multi-values cookie. 50 * 51 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set(). 52 */ 53 setHash : function(name, values_obj, expires, path, domain, secure) { 54 var str = ''; 46 55 47 this.each(v , function(v, k) {48 o += (!o ? '' : '&') + k + '=' + v;56 this.each(values_obj, function(val, key) { 57 str += (!str ? '' : '&') + key + '=' + val; 49 58 }); 50 59 51 this.set(n , o, e, p, d, s);60 this.set(name, str, expires, path, domain, secure); 52 61 }, 53 62 54 get : function(n) { 55 var c = document.cookie, e, p = n + "=", b; 63 /** 64 * Get a cookie. 65 */ 66 get : function(name) { 67 var cookie = document.cookie, e, p = name + "=", b; 56 68 57 if ( !c)69 if ( !cookie ) 58 70 return; 59 71 60 b = c .indexOf("; " + p);72 b = cookie.indexOf("; " + p); 61 73 62 if ( b == -1) {63 b = c .indexOf(p);74 if ( b == -1 ) { 75 b = cookie.indexOf(p); 64 76 65 if ( b != 0)77 if ( b != 0 ) 66 78 return null; 67 79 … … 70 82 } 71 83 72 e = c .indexOf(";", b);84 e = cookie.indexOf(";", b); 73 85 74 if ( e == -1)75 e = c .length;86 if ( e == -1 ) 87 e = cookie.length; 76 88 77 return decodeURIComponent( c.substring(b + p.length, e));89 return decodeURIComponent( cookie.substring(b + p.length, e) ); 78 90 }, 79 91 80 set : function(n, v, e, p, d, s) { 81 document.cookie = n + "=" + encodeURIComponent(v) + 82 ((e) ? "; expires=" + e.toGMTString() : "") + 83 ((p) ? "; path=" + p : "") + 84 ((d) ? "; domain=" + d : "") + 85 ((s) ? "; secure" : ""); 92 /** 93 * Set a cookie. 94 * 95 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat) 96 * or the number of seconds until expiration 97 */ 98 set : function(name, value, expires, path, domain, secure) { 99 var d = new Date(); 100 101 if ( typeof(expires) == 'object' && expires.toGMTString ) { 102 expires = expires.toGMTString(); 103 } else if ( parseInt(expires, 10) ) { 104 d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds 105 expires = d.toGMTString(); 106 } else { 107 expires = ''; 108 } 109 110 document.cookie = name + "=" + encodeURIComponent(value) + 111 ((expires) ? "; expires=" + expires : "") + 112 ((path) ? "; path=" + path : "") + 113 ((domain) ? "; domain=" + domain : "") + 114 ((secure) ? "; secure" : ""); 86 115 }, 87 116 88 remove : function(n, p) { 89 var d = new Date(); 90 91 d.setTime(d.getTime() - 1000); 92 93 this.set(n, '', d, p, d); 117 /** 118 * Remove a cookie. 119 * 120 * This is done by setting it to an empty value and setting the expiration time in the past. 121 */ 122 remove : function(name, path) { 123 this.set(name, '', -1000, path); 94 124 } 95 125 }; … … 97 127 // Returns the value as string. Second arg or empty string is returned when value is not set. 98 128 function getUserSetting( name, def ) { 99 var o = getAllUserSettings();129 var obj = getAllUserSettings(); 100 130 101 if ( o .hasOwnProperty(name) )102 return o [name];131 if ( obj.hasOwnProperty(name) ) 132 return obj[name]; 103 133 104 134 if ( typeof def != 'undefined' ) … … 110 140 // Both name and value must be only ASCII letters, numbers or underscore 111 141 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text. 112 function setUserSetting( name, value, del ) {142 function setUserSetting( name, value, _del ) { 113 143 if ( 'object' !== typeof userSettings ) 114 144 return false; 115 145 116 var c = 'wp-settings-' + userSettings.uid, o = wpCookies.getHash(c) || {}, d = new Date(), p,146 var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url, 117 147 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, ''); 118 148 119 if ( del ) {120 delete o[n];149 if ( _del ) { 150 delete all[n]; 121 151 } else { 122 o[n] = v;152 all[n] = v; 123 153 } 124 154 125 d.setTime( d.getTime() + 31536000000 ); 126 p = userSettings.url; 127 128 wpCookies.setHash(c, o, d, p); 129 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, d, p); 155 wpCookies.setHash(cookie, all, 31536000, path); 156 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path); 130 157 131 158 return name;
Note: See TracChangeset
for help on using the changeset viewer.