/**
* Utilities for cookie management
* @module cookie
*/
//shortcuts
var L = Y.Lang,
O = Y.Object,
NULL = null,
//shortcuts to functions
isUndefined = L.isUndefined,
isFunction = L.isFunction,
//shortcut to document
/*
* Throws an error message.
*/
}
/*
* Checks the validity of a cookie name.
*/
error("Cookie name must be a non-empty string.");
}
}
/*
* Checks the validity of a subcookie name.
*/
error("Subcookie name must be a non-empty string.");
}
}
/**
* Cookie utility.
* @class Cookie
* @static
*/
Y.Cookie = {
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/**
* Creates a cookie string that can be assigned into document.cookie.
* @param {String} name The name of the cookie.
* @param {String} value The value of the cookie.
* @param {Boolean} encodeValue True to encode the value, false to leave as-is.
* @param {Object} options (Optional) Options for the cookie.
* @return {String} The formatted cookie string.
* @method _createCookieString
* @private
* @static
*/
_createCookieString : function (name /*:String*/, value /*:Variant*/, encodeValue /*:Boolean*/, options /*:Object*/) /*:String*/ {
//expiration date
if (expires instanceof Date){
}
//path
}
//domain
}
//secure
text += "; secure";
}
}
return text;
},
/**
* Formats a cookie value for an object containing multiple values.
* @param {Object} hash An object of key-value pairs to create a string for.
* @return {String} A string suitable for use as a cookie value.
* @method _createCookieHashString
* @private
* @static
*/
error("Cookie._createCookieHashString(): Argument must be an object.");
}
var text /*:Array*/ = [];
}
});
},
/**
* Parses a cookie hash string into an object.
* @param {String} text The cookie hash string to parse (format: n1=v1&n2=v2).
* @return {Object} An object containing entries for each cookie value.
* @method _parseCookieHash
* @private
* @static
*/
_parseCookieHash : function (text) {
hash = {};
}
}
return hash;
},
/**
* Parses a cookie string into an object representing all accessible cookies.
* @param {String} text The cookie string to parse.
* @param {Boolean} shouldDecode (Optional) Indicates if the cookie values should be decoded or not. Default is true.
* @return {Object} An object containing entries for each accessible cookie.
* @method _parseCookieString
* @private
* @static
*/
var cookies /*:Object*/ = {};
cookieName = NULL,
cookieValue = NULL,
//check for normally-formatted cookie (name-value)
if (cookieNameValue instanceof Array){
try {
} catch (ex){
//intentionally ignore the cookie - the encoding is wrong
}
} else {
//means the cookie does not have an "=", so treat it as a boolean flag
cookieValue = "";
}
}
}
return cookies;
},
/**
* Sets the document object that the cookie utility uses for setting
* cookies. This method is necessary to ensure that the cookie utility
* unit tests can pass even when run on a domain instead of locally.
* This method should not be used otherwise; you should use
* <code>Y.config.doc</code> to change the document that the cookie
* utility uses for everyday purposes.
* @param {Object} newDoc The object to use as the document.
* @return {void}
* @method _setDoc
* @private
*/
},
//-------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------
/**
* Determines if the cookie with the given name exists. This is useful for
* Boolean cookies (those that do not follow the name=value convention).
* @param {String} name The name of the cookie to check.
* @return {Boolean} True if the cookie exists, false if not.
* @method exists
* @static
*/
},
/**
* Returns the cookie value for the given name.
* @param {String} name The name of the cookie to retrieve.
* @param {Function|Object} options (Optional) An object containing one or more
* The converter function is run on the value before returning it. The
* function is not used if the cookie doesn't exist. The function can be
* passed instead of the options object for backwards compatibility. When
* raw is set to true, the cookie value is not URI decoded.
* @return {Variant} If no converter is specified, returns a string or null if
* the cookie doesn't exist. If the converter is specified, returns the value
* returned from the converter or null if the cookie doesn't exist.
* @method get
* @static
*/
var cookies,
//if options is a function, then it's the converter
if (isFunction(options)) {
options = {};
} else {
options = {};
}
//should return null, not undefined if the cookie doesn't exist
if (isUndefined(cookie)) {
return NULL;
}
if (!isFunction(converter)){
return cookie;
} else {
}
},
/**
* Returns the value of a subcookie.
* @param {String} name The name of the cookie to retrieve.
* @param {String} subName The name of the subcookie to retrieve.
* @param {Function} converter (Optional) A function to run on the value before returning
* it. The function is not used if the cookie doesn't exist.
* @return {Variant} If the cookie doesn't exist, null is returned. If the subcookie
* doesn't exist, null if also returned. If no converter is specified and the
* subcookie exists, a string is returned. If a converter is specified and the
* subcookie exists, the value returned from the converter is returned.
* @method getSub
* @static
*/
return NULL;
}
if (!isFunction(converter)){
} else {
}
} else {
return NULL;
}
},
/**
* Returns an object containing name-value pairs stored in the cookie with the given name.
* @param {String} name The name of the cookie to retrieve.
* @return {Object} An object of name-value pairs if the cookie with the given name
* exists, null if it does not.
* @method getSubs
* @static
*/
}
return NULL;
},
/**
* Removes a cookie from the machine by setting its expiration date to
* sometime in the past.
* @param {String} name The name of the cookie to remove.
* @param {Object} options (Optional) An object containing one or more
* cookie options: path (a string), domain (a string),
* by the method.
* @return {String} The created cookie string.
* @method remove
* @static
*/
//set options
expires: new Date(0)
});
//set cookie
},
/**
* Removes a sub cookie with a given name.
* @param {String} name The name of the cookie in which the subcookie exists.
* @param {String} subName The name of the subcookie to remove.
* @param {Object} options (Optional) An object containing one or more
* cookie options: path (a string), domain (a string), expires (a Date object),
* settings as the original subcookie.
* @return {String} The created cookie string.
* @method removeSub
* @static
*/
//get all subcookies for this cookie
//delete the indicated subcookie
if (!options.removeIfEmpty) {
//reset the cookie
} else {
//reset the cookie if there are subcookies left, else remove
}
}
}
} else {
return "";
}
},
/**
* Sets a cookie with a given name and value.
* @param {String} name The name of the cookie to set.
* @param {Variant} value The value to set for the cookie.
* @param {Object} options (Optional) An object containing one or more
* cookie options: path (a string), domain (a string), expires (a Date object),
* that the cookie should not be URI encoded before being set.
* @return {String} The created cookie string.
* @method set
* @static
*/
if (isUndefined(value)){
error("Cookie.set(): Value cannot be undefined.");
}
return text;
},
/**
* Sets a sub cookie with a given name to a particular value.
* @param {String} name The name of the cookie to set.
* @param {String} subName The name of the subcookie to set.
* @param {Variant} value The value to set.
* @param {Object} options (Optional) An object containing one or more
* cookie options: path (a string), domain (a string), expires (a Date object),
* @return {String} The created cookie string.
* @method setSub
* @static
*/
if (isUndefined(value)){
error("Cookie.setSub(): Subcookie value cannot be undefined.");
}
hash = {};
}
},
/**
* Sets a cookie with a given name to contain a hash of name-value pairs.
* @param {String} name The name of the cookie to set.
* @param {Object} value An object containing name-value pairs.
* @param {Object} options (Optional) An object containing one or more
* cookie options: path (a string), domain (a string), expires (a Date object),
* @return {String} The created cookie string.
* @method setSubs
* @static
*/
error("Cookie.setSubs(): Cookie value must be an object.");
}
var text /*:String*/ = this._createCookieString(name, this._createCookieHashString(value), false, options);
return text;
}
};