wp/wp-includes/js/utils.js
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     1 /* global userSettings */
     1 /**
       
     2  * Cookie functions.
       
     3  *
       
     4  * @output wp-includes/js/utils.js
       
     5  */
       
     6 
       
     7 /* global userSettings, getAllUserSettings, wpCookies, setUserSetting */
     2 /* exported getUserSetting, setUserSetting, deleteUserSetting */
     8 /* exported getUserSetting, setUserSetting, deleteUserSetting */
     3 // utility functions
     9 
     4 
    10 window.wpCookies = {
     5 var wpCookies = {
       
     6 // The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
    11 // The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
     7 
    12 
     8 	each: function( obj, cb, scope ) {
    13 	each: function( obj, cb, scope ) {
     9 		var n, l;
    14 		var n, l;
    10 
    15 
   132 		this.set( name, '', -1000, path, domain, secure );
   137 		this.set( name, '', -1000, path, domain, secure );
   133 	}
   138 	}
   134 };
   139 };
   135 
   140 
   136 // Returns the value as string. Second arg or empty string is returned when value is not set.
   141 // Returns the value as string. Second arg or empty string is returned when value is not set.
   137 function getUserSetting( name, def ) {
   142 window.getUserSetting = function( name, def ) {
   138 	var settings = getAllUserSettings();
   143 	var settings = getAllUserSettings();
   139 
   144 
   140 	if ( settings.hasOwnProperty( name ) ) {
   145 	if ( settings.hasOwnProperty( name ) ) {
   141 		return settings[name];
   146 		return settings[name];
   142 	}
   147 	}
   144 	if ( typeof def !== 'undefined' ) {
   149 	if ( typeof def !== 'undefined' ) {
   145 		return def;
   150 		return def;
   146 	}
   151 	}
   147 
   152 
   148 	return '';
   153 	return '';
   149 }
   154 };
   150 
   155 
   151 // Both name and value must be only ASCII letters, numbers or underscore
   156 // Both name and value must be only ASCII letters, numbers or underscore
   152 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
   157 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
   153 // The value is converted and stored as string.
   158 // The value is converted and stored as string.
   154 function setUserSetting( name, value, _del ) {
   159 window.setUserSetting = function( name, value, _del ) {
   155 	if ( 'object' !== typeof userSettings ) {
   160 	if ( 'object' !== typeof userSettings ) {
   156 		return false;
   161 		return false;
   157 	}
   162 	}
   158 
   163 
   159 	var uid = userSettings.uid,
   164 	var uid = userSettings.uid,
   179 
   184 
   180 	wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
   185 	wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
   181 	wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
   186 	wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
   182 
   187 
   183 	return name;
   188 	return name;
   184 }
   189 };
   185 
   190 
   186 function deleteUserSetting( name ) {
   191 window.deleteUserSetting = function( name ) {
   187 	return setUserSetting( name, '', 1 );
   192 	return setUserSetting( name, '', 1 );
   188 }
   193 };
   189 
   194 
   190 // Returns all settings as js object.
   195 // Returns all settings as js object.
   191 function getAllUserSettings() {
   196 window.getAllUserSettings = function() {
   192 	if ( 'object' !== typeof userSettings ) {
   197 	if ( 'object' !== typeof userSettings ) {
   193 		return {};
   198 		return {};
   194 	}
   199 	}
   195 
   200 
   196 	return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
   201 	return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
   197 }
   202 };
   198