]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - wradmin/wradmin/public/yui/cookie/cookie-debug.js
88939ad97da821c2ca3f25c8fa132d61d339856d
[philipp/winterrodeln/wradmin.git] / wradmin / wradmin / public / yui / cookie / cookie-debug.js
1 /*
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
5 version: 2.7.0
6 */
7 /**
8  * Utilities for cookie management
9  * @namespace YAHOO.util
10  * @module cookie
11  */
12 YAHOO.namespace("util");
13
14 /**
15  * Cookie utility.
16  * @class Cookie
17  * @static
18  */
19 YAHOO.util.Cookie = {
20     
21     //-------------------------------------------------------------------------
22     // Private Methods
23     //-------------------------------------------------------------------------
24     
25     /**
26      * Creates a cookie string that can be assigned into document.cookie.
27      * @param {String} name The name of the cookie.
28      * @param {String} value The value of the cookie.
29      * @param {encodeValue} encodeValue True to encode the value, false to leave as-is.
30      * @param {Object} options (Optional) Options for the cookie.
31      * @return {String} The formatted cookie string.
32      * @method _createCookieString
33      * @private
34      * @static
35      */
36     _createCookieString : function (name /*:String*/, value /*:Variant*/, encodeValue /*:Boolean*/, options /*:Object*/) /*:String*/ {
37     
38         //shortcut
39         var lang = YAHOO.lang;
40     
41         var text /*:String*/ = encodeURIComponent(name) + "=" + (encodeValue ? encodeURIComponent(value) : value);
42         
43     
44         if (lang.isObject(options)){
45             //expiration date
46             if (options.expires instanceof Date){
47                 text += "; expires=" + options.expires.toGMTString();
48             }
49         
50             //path
51             if (lang.isString(options.path) && options.path != ""){
52                 text += "; path=" + options.path;
53             }
54     
55             //domain
56             if (lang.isString(options.domain) && options.domain != ""){
57                 text += "; domain=" + options.domain;
58             }
59             
60             //secure
61             if (options.secure === true){
62                 text += "; secure";
63             }
64         }
65         
66         return text;
67     },
68     
69     /**
70      * Formats a cookie value for an object containing multiple values.
71      * @param {Object} hash An object of key-value pairs to create a string for.
72      * @return {String} A string suitable for use as a cookie value.
73      * @method _createCookieHash
74      * @private
75      * @static
76      */
77     _createCookieHashString : function (hash /*:Object*/) /*:String*/ {
78         
79         //shortcuts
80         var lang = YAHOO.lang;
81         
82         if (!lang.isObject(hash)){
83             throw new TypeError("Cookie._createCookieHashString(): Argument must be an object.");
84         }
85         
86         var text /*:Array*/ = new Array();
87         
88         for (var key in hash){
89             if (lang.hasOwnProperty(hash, key) && !lang.isFunction(hash[key]) && !lang.isUndefined(hash[key])){
90                 text.push(encodeURIComponent(key) + "=" + encodeURIComponent(String(hash[key])));
91             }
92         }
93         
94         return text.join("&");
95     },
96     
97     /**
98      * Parses a cookie hash string into an object.
99      * @param {String} text The cookie hash string to parse. The string should already be URL-decoded.
100      * @return {Object} An object containing entries for each cookie value.
101      * @method _parseCookieHash
102      * @private
103      * @static
104      */
105     _parseCookieHash : function (text /*:String*/) /*:Object*/ {
106     
107         var hashParts /*:Array*/ = text.split("&"),
108             hashPart /*:Array*/ = null,
109             hash /*:Object*/ = new Object();
110         
111         if (text.length > 0){
112             for (var i=0, len=hashParts.length; i < len; i++){
113                 hashPart = hashParts[i].split("=");
114                 hash[decodeURIComponent(hashPart[0])] = decodeURIComponent(hashPart[1]);
115             }
116         }
117         
118         return hash;
119     },    
120     
121     /**
122      * Parses a cookie string into an object representing all accessible cookies.
123      * @param {String} text The cookie string to parse.
124      * @param {Boolean} decode (Optional) Indicates if the cookie values should be decoded or not. Default is true.
125      * @return {Object} An object containing entries for each accessible cookie.
126      * @method _parseCookieString
127      * @private
128      * @static
129      */
130     _parseCookieString : function (text /*:String*/, decode /*:Boolean*/) /*:Object*/ {
131     
132         var cookies /*:Object*/ = new Object();        
133         
134         if (YAHOO.lang.isString(text) && text.length > 0) {
135         
136             var decodeValue = (decode === false ? function(s){return s;} : decodeURIComponent);
137         
138             if (/[^=]+=[^=;]?(?:; [^=]+=[^=]?)?/.test(text)){            
139                 var cookieParts /*:Array*/ = text.split(/;\s/g),
140                     cookieName /*:String*/ = null,
141                     cookieValue /*:String*/ = null,
142                     cookieNameValue /*:Array*/ = null;
143                 
144                 for (var i=0, len=cookieParts.length; i < len; i++){
145                 
146                     //check for normally-formatted cookie (name-value)
147                     cookieNameValue = cookieParts[i].match(/([^=]+)=/i);
148                     if (cookieNameValue instanceof Array){
149                         try {
150                             cookieName = decodeURIComponent(cookieNameValue[1]);
151                             cookieValue = decodeValue(cookieParts[i].substring(cookieNameValue[1].length+1));
152                         } catch (ex){
153                             //ignore the entire cookie - encoding is likely invalid
154                         }
155                     } else {
156                         //means the cookie does not have an "=", so treat it as a boolean flag
157                         cookieName = decodeURIComponent(cookieParts[i]);
158                         cookieValue = cookieName;
159                     }
160                     cookies[cookieName] = cookieValue;
161                 }
162             }
163         }
164         
165         return cookies;
166     },    
167     
168     //-------------------------------------------------------------------------
169     // Public Methods
170     //-------------------------------------------------------------------------
171
172     /**
173      * Returns the cookie value for the given name.
174      * @param {String} name The name of the cookie to retrieve.
175      * @param {Function} converter (Optional) A function to run on the value before returning
176      *      it. The function is not used if the cookie doesn't exist.
177      * @return {Variant} If no converter is specified, returns a string or null if
178      *      the cookie doesn't exist. If the converter is specified, returns the value
179      *      returned from the converter or null if the cookie doesn't exist.
180      * @method get
181      * @static
182      */
183     get : function (name /*:String*/, converter /*:Function*/) /*:Variant*/{
184         
185         var lang = YAHOO.lang;
186         var cookies /*:Object*/ = this._parseCookieString(document.cookie);        
187         
188         if (!lang.isString(name) || name === ""){
189             throw new TypeError("Cookie.get(): Cookie name must be a non-empty string.");
190         }
191         
192         if (lang.isUndefined(cookies[name])) {
193             return null;
194         }
195         
196         if (!lang.isFunction(converter)){
197             return cookies[name];
198         } else {
199             return converter(cookies[name]);
200         }
201     },
202     
203     /**
204      * Returns the value of a subcookie.
205      * @param {String} name The name of the cookie to retrieve.
206      * @param {String} subName The name of the subcookie to retrieve.
207      * @param {Function} converter (Optional) A function to run on the value before returning
208      *      it. The function is not used if the cookie doesn't exist.
209      * @return {Variant} If the cookie doesn't exist, null is returned. If the subcookie
210      *      doesn't exist, null if also returned. If no converter is specified and the
211      *      subcookie exists, a string is returned. If a converter is specified and the
212      *      subcookie exists, the value returned from the converter is returned.
213      * @method getSub
214      * @static
215      */
216     getSub : function (name /*:String*/, subName /*:String*/, converter /*:Function*/) /*:Variant*/ {
217     
218         var lang = YAHOO.lang;    
219         var hash /*:Variant*/ = this.getSubs(name);  
220
221         if (hash !== null) {
222             
223             if (!lang.isString(subName) || subName === ""){
224                 throw new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string.");
225             }
226             
227             if (lang.isUndefined(hash[subName])){
228                 return null;
229             }            
230         
231             if (!lang.isFunction(converter)){
232                 return hash[subName];
233             } else {
234                 return converter(hash[subName]);
235             }
236         } else {
237             return null;
238         }
239     
240     },
241     
242     /**
243      * Returns an object containing name-value pairs stored in the cookie with the given name.
244      * @param {String} name The name of the cookie to retrieve.
245      * @return {Object} An object of name-value pairs if the cookie with the given name
246      *      exists, null if it does not.
247      * @method getHash
248      * @static
249      */
250     getSubs : function (name /*:String*/) /*:Object*/ {
251         
252         //check cookie name
253         if (!YAHOO.lang.isString(name) || name === ""){
254             throw new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string.");
255         }
256         
257         var cookies = this._parseCookieString(document.cookie, false);
258         if (YAHOO.lang.isString(cookies[name])){
259             return this._parseCookieHash(cookies[name]);
260         }
261         return null;
262     },
263     
264     /**
265      * Removes a cookie from the machine by setting its expiration date to
266      * sometime in the past.
267      * @param {String} name The name of the cookie to remove.
268      * @param {Object} options (Optional) An object containing one or more
269      *      cookie options: path (a string), domain (a string), 
270      *      and secure (true/false). The expires option will be overwritten
271      *      by the method.
272      * @return {String} The created cookie string.
273      * @method remove
274      * @static
275      */
276     remove : function (name /*:String*/, options /*:Object*/) /*:String*/ {
277         
278         //check cookie name
279         if (!YAHOO.lang.isString(name) || name === ""){
280             throw new TypeError("Cookie.remove(): Cookie name must be a non-empty string.");
281         }
282         
283         //set options
284         options = options || {};
285         options.expires = new Date(0);
286         
287         //set cookie
288         return this.set(name, "", options);
289     },
290
291     /**
292      * Removes a sub cookie with a given name.
293      * @param {String} name The name of the cookie in which the subcookie exists.
294      * @param {String} subName The name of the subcookie to remove.
295      * @param {Object} options (Optional) An object containing one or more
296      *      cookie options: path (a string), domain (a string), expires (a Date object),
297      *      and secure (true/false). This must be the same settings as the original
298      *      subcookie.
299      * @return {String} The created cookie string.
300      * @method removeSub
301      * @static
302      */
303     removeSub : function(name /*:String*/, subName /*:String*/, options /*:Object*/) /*:String*/ {
304     
305         //check cookie name
306         if (!YAHOO.lang.isString(name) || name === ""){
307             throw new TypeError("Cookie.removeSub(): Cookie name must be a non-empty string.");
308         }
309         
310         //check subcookie name
311         if (!YAHOO.lang.isString(subName) || subName === ""){
312             throw new TypeError("Cookie.removeSub(): Subcookie name must be a non-empty string.");
313         }
314         
315         //get all subcookies for this cookie
316         var subs = this.getSubs(name);
317         
318         //delete the indicated subcookie
319         if (YAHOO.lang.isObject(subs) && YAHOO.lang.hasOwnProperty(subs, subName)){
320             delete subs[subName];
321             
322             //reset the cookie
323             return this.setSubs(name, subs, options);
324         } else {
325             return "";
326         }
327         
328     },
329
330     /**
331      * Sets a cookie with a given name and value.
332      * @param {String} name The name of the cookie to set.
333      * @param {Variant} value The value to set for the cookie.
334      * @param {Object} options (Optional) An object containing one or more
335      *      cookie options: path (a string), domain (a string), expires (a Date object),
336      *      and secure (true/false).
337      * @return {String} The created cookie string.
338      * @method set
339      * @static
340      */
341     set : function (name /*:String*/, value /*:Variant*/, options /*:Object*/) /*:String*/ {
342     
343         var lang = YAHOO.lang;
344     
345         if (!lang.isString(name)){
346             throw new TypeError("Cookie.set(): Cookie name must be a string.");
347         }
348         
349         if (lang.isUndefined(value)){
350             throw new TypeError("Cookie.set(): Value cannot be undefined.");
351         }
352         
353     
354         var text /*:String*/ = this._createCookieString(name, value, true, options);
355         document.cookie = text;
356         return text;
357     },
358         
359     /**
360      * Sets a sub cookie with a given name to a particular value.
361      * @param {String} name The name of the cookie to set.
362      * @param {String} subName The name of the subcookie to set.
363      * @param {Variant} value The value to set.
364      * @param {Object} options (Optional) An object containing one or more
365      *      cookie options: path (a string), domain (a string), expires (a Date object),
366      *      and secure (true/false).
367      * @return {String} The created cookie string.
368      * @method setSub
369      * @static
370      */
371     setSub : function (name /*:String*/, subName /*:String*/, value /*:Variant*/, options /*:Object*/) /*:String*/ {
372         
373         var lang = YAHOO.lang;
374
375         if (!lang.isString(name) || name === ""){
376             throw new TypeError("Cookie.setSub(): Cookie name must be a non-empty string.");
377         }
378
379         if (!lang.isString(subName) || subName === ""){
380             throw new TypeError("Cookie.setSub(): Subcookie name must be a non-empty string.");
381         }
382         
383         if (lang.isUndefined(value)){
384             throw new TypeError("Cookie.setSub(): Subcookie value cannot be undefined.");
385         }
386
387         var hash /*:Object*/ = this.getSubs(name);
388         
389         if (!lang.isObject(hash)){
390             hash = new Object();
391         }
392         
393         hash[subName] = value;        
394         
395         return this.setSubs(name, hash, options);
396         
397     },
398     
399     /**
400      * Sets a cookie with a given name to contain a hash of name-value pairs.
401      * @param {String} name The name of the cookie to set.
402      * @param {Object} value An object containing name-value pairs.
403      * @param {Object} options (Optional) An object containing one or more
404      *      cookie options: path (a string), domain (a string), expires (a Date object),
405      *      and secure (true/false).
406      * @return {String} The created cookie string.
407      * @method setSubs
408      * @static
409      */
410     setSubs : function (name /*:String*/, value /*:Object*/, options /*:Object*/) /*:String*/ {
411         
412         var lang = YAHOO.lang;
413         
414         if (!lang.isString(name)){
415             throw new TypeError("Cookie.setSubs(): Cookie name must be a string.");
416         }
417         
418         if (!lang.isObject(value)){
419             throw new TypeError("Cookie.setSubs(): Cookie value must be an object.");
420         }
421     
422         var text /*:String*/ = this._createCookieString(name, this._createCookieHashString(value), false, options);
423         document.cookie = text;
424         return text;        
425     }    
426
427 };
428
429 YAHOO.register("cookie", YAHOO.util.Cookie, {version: "2.7.0", build: "1799"});