balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

utils.js (4665B)


      1 /**
      2  * Cookie functions.
      3  *
      4  * @output wp-includes/js/utils.js
      5  */
      6 
      7 /* global userSettings, getAllUserSettings, wpCookies, setUserSetting */
      8 /* exported getUserSetting, setUserSetting, deleteUserSetting */
      9 
     10 window.wpCookies = {
     11 // The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
     12 
     13 	each: function( obj, cb, scope ) {
     14 		var n, l;
     15 
     16 		if ( ! obj ) {
     17 			return 0;
     18 		}
     19 
     20 		scope = scope || obj;
     21 
     22 		if ( typeof( obj.length ) !== 'undefined' ) {
     23 			for ( n = 0, l = obj.length; n < l; n++ ) {
     24 				if ( cb.call( scope, obj[n], n, obj ) === false ) {
     25 					return 0;
     26 				}
     27 			}
     28 		} else {
     29 			for ( n in obj ) {
     30 				if ( obj.hasOwnProperty(n) ) {
     31 					if ( cb.call( scope, obj[n], n, obj ) === false ) {
     32 						return 0;
     33 					}
     34 				}
     35 			}
     36 		}
     37 		return 1;
     38 	},
     39 
     40 	/**
     41 	 * Get a multi-values cookie.
     42 	 * Returns a JS object with the name: 'value' pairs.
     43 	 */
     44 	getHash: function( name ) {
     45 		var cookie = this.get( name ), values;
     46 
     47 		if ( cookie ) {
     48 			this.each( cookie.split('&'), function( pair ) {
     49 				pair = pair.split('=');
     50 				values = values || {};
     51 				values[pair[0]] = pair[1];
     52 			});
     53 		}
     54 
     55 		return values;
     56 	},
     57 
     58 	/**
     59 	 * Set a multi-values cookie.
     60 	 *
     61 	 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
     62 	 */
     63 	setHash: function( name, values_obj, expires, path, domain, secure ) {
     64 		var str = '';
     65 
     66 		this.each( values_obj, function( val, key ) {
     67 			str += ( ! str ? '' : '&' ) + key + '=' + val;
     68 		});
     69 
     70 		this.set( name, str, expires, path, domain, secure );
     71 	},
     72 
     73 	/**
     74 	 * Get a cookie.
     75 	 */
     76 	get: function( name ) {
     77 		var e, b,
     78 			cookie = document.cookie,
     79 			p = name + '=';
     80 
     81 		if ( ! cookie ) {
     82 			return;
     83 		}
     84 
     85 		b = cookie.indexOf( '; ' + p );
     86 
     87 		if ( b === -1 ) {
     88 			b = cookie.indexOf(p);
     89 
     90 			if ( b !== 0 ) {
     91 				return null;
     92 			}
     93 		} else {
     94 			b += 2;
     95 		}
     96 
     97 		e = cookie.indexOf( ';', b );
     98 
     99 		if ( e === -1 ) {
    100 			e = cookie.length;
    101 		}
    102 
    103 		return decodeURIComponent( cookie.substring( b + p.length, e ) );
    104 	},
    105 
    106 	/**
    107 	 * Set a cookie.
    108 	 *
    109 	 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
    110 	 * or the number of seconds until expiration
    111 	 */
    112 	set: function( name, value, expires, path, domain, secure ) {
    113 		var d = new Date();
    114 
    115 		if ( typeof( expires ) === 'object' && expires.toGMTString ) {
    116 			expires = expires.toGMTString();
    117 		} else if ( parseInt( expires, 10 ) ) {
    118 			d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // Time must be in milliseconds.
    119 			expires = d.toGMTString();
    120 		} else {
    121 			expires = '';
    122 		}
    123 
    124 		document.cookie = name + '=' + encodeURIComponent( value ) +
    125 			( expires ? '; expires=' + expires : '' ) +
    126 			( path    ? '; path=' + path       : '' ) +
    127 			( domain  ? '; domain=' + domain   : '' ) +
    128 			( secure  ? '; secure'             : '' );
    129 	},
    130 
    131 	/**
    132 	 * Remove a cookie.
    133 	 *
    134 	 * This is done by setting it to an empty value and setting the expiration time in the past.
    135 	 */
    136 	remove: function( name, path, domain, secure ) {
    137 		this.set( name, '', -1000, path, domain, secure );
    138 	}
    139 };
    140 
    141 // Returns the value as string. Second arg or empty string is returned when value is not set.
    142 window.getUserSetting = function( name, def ) {
    143 	var settings = getAllUserSettings();
    144 
    145 	if ( settings.hasOwnProperty( name ) ) {
    146 		return settings[name];
    147 	}
    148 
    149 	if ( typeof def !== 'undefined' ) {
    150 		return def;
    151 	}
    152 
    153 	return '';
    154 };
    155 
    156 /*
    157  * Both name and value must be only ASCII letters, numbers or underscore
    158  * and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
    159  * The value is converted and stored as string.
    160  */
    161 window.setUserSetting = function( name, value, _del ) {
    162 	if ( 'object' !== typeof userSettings ) {
    163 		return false;
    164 	}
    165 
    166 	var uid = userSettings.uid,
    167 		settings = wpCookies.getHash( 'wp-settings-' + uid ),
    168 		path = userSettings.url,
    169 		secure = !! userSettings.secure;
    170 
    171 	name = name.toString().replace( /[^A-Za-z0-9_-]/g, '' );
    172 
    173 	if ( typeof value === 'number' ) {
    174 		value = parseInt( value, 10 );
    175 	} else {
    176 		value = value.toString().replace( /[^A-Za-z0-9_-]/g, '' );
    177 	}
    178 
    179 	settings = settings || {};
    180 
    181 	if ( _del ) {
    182 		delete settings[name];
    183 	} else {
    184 		settings[name] = value;
    185 	}
    186 
    187 	wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
    188 	wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
    189 
    190 	return name;
    191 };
    192 
    193 window.deleteUserSetting = function( name ) {
    194 	return setUserSetting( name, '', 1 );
    195 };
    196 
    197 // Returns all settings as JS object.
    198 window.getAllUserSettings = function() {
    199 	if ( 'object' !== typeof userSettings ) {
    200 		return {};
    201 	}
    202 
    203 	return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
    204 };