1 jls.loader.provide('jls.util.Cookie');
  2 
  3 /**
  4  * @class Cookie utility class.
  5  */
  6 jls.util.Cookie = jls.lang.Class.create();
  7 
  8 Object.extend(jls.util.Cookie, /** @lends jls.util.Cookie */
  9 {
 10     /**
 11      * Returns a cookie depending on its name or null if there is no cookie for this name.
 12      * 
 13      * @param {String} name The name of the cookie to get.
 14      * @returns {String} The value or null if there is no cookie for this name.
 15      */
 16     get : function(name) {
 17         var cookies = document.cookie;
 18         var prefix = name + '=';
 19         var indexOfPrefix = cookies.indexOf('; ' + prefix);
 20         if (indexOfPrefix > 0) {
 21             indexOfPrefix += 2;
 22         } else {
 23             indexOfPrefix = cookies.indexOf(prefix);
 24             if (indexOfPrefix != 0) {
 25                 return null;
 26             }
 27         }
 28         var end = document.cookie.indexOf(';', indexOfPrefix);
 29         if (end == -1) {
 30             end = cookies.length;
 31         }
 32         return unescape(cookies.substring(indexOfPrefix + prefix.length, end));
 33     },
 34     /**
 35      * Sets a cookie.
 36      * The expiration date tells the browser when to delete the cookie.
 37      * The domain and path tell the browser that the cookie has to be sent back to the server when requesting URLs of a given domain and path.
 38      * If not specified, they default to the domain and path of the object that was requested.
 39      * As a result, the domain and path strings may tell the browser to send the cookie when it normally would not.
 40      * For security reasons, the cookie is accepted only if the server is a member of the domain specified by the domain string.
 41      * A secure cookie is only used when a browser is visiting a server via HTTPS.
 42      * 
 43      * @param {String} name The name of the cookie to set.
 44      * @param {String} value The value.
 45      * @param {Object} [opts] The options.
 46      * @param {Date} [opts.expires] The expiration date.
 47      * @param {String} [opts.domain] The domain.
 48      * @param {String} [opts.path] The path.
 49      * @param {String} [opts.secure] The secure.
 50      */
 51     set : function(name, value, opts) {
 52         var cookie = name + '=' + escape(value);
 53         if (opts) {
 54             if (Object.isDate(opts.expires)) {
 55                 cookie += '; expires=' + opts.expires.toGMTString();
 56             }
 57             if (('domain' in opts) && (opts.domain != null)) {
 58                 cookie += '; domain=' + opts.domain.toString();
 59             }
 60             if (('path' in opts) && (opts.path != null)) {
 61                 cookie += '; path=' + opts.path.toString();
 62             }
 63             if (('secure' in opts) && opts.secure) {
 64                 cookie += '; secure';
 65             }
 66         }
 67         document.cookie = cookie;
 68     },
 69     /**
 70      * Clears a cookie depending on its name.
 71      * 
 72      * @param {String} name The name of the cookie to get.
 73      */
 74     clear : function(name) {
 75         document.cookie = name + '=; expires=' + new Date(0).toGMTString(); 
 76     },
 77     /**
 78      * Returns all cookies.
 79      * 
 80      * @returns {Object} The cookies.
 81      */
 82     all : function() {
 83         var cookies = document.cookie;
 84         cookies = cookies.split('; ');
 85         var all = {};
 86         for (var i = 0; i < cookies.length; i++) {
 87             var kv = cookies[i].split('=');
 88             all[kv[0]] = kv[1];
 89         }
 90         return all;
 91     }
 92 });
 93