Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / util / Cookies.js
index 381923f..b765a92 100644 (file)
@@ -1,15 +1,17 @@
-/*!
- * Ext JS Library 3.3.1
- * Copyright(c) 2006-2010 Sencha Inc.
- * licensing@sencha.com
- * http://www.sencha.com/license
- */
 /**
  * @class Ext.util.Cookies
- * Utility class for managing and interacting with cookies.
+
+Utility class for setting/reading values from browser cookies.
+Values can be written using the {@link #set} method.
+Values can be read using the {@link #get} method.
+A cookie can be invalidated on the client machine using the {@link #clear} method.
+
+ * @markdown
  * @singleton
  */
-Ext.util.Cookies = {
+Ext.define('Ext.util.Cookies', {
+    singleton: true,
+    
     /**
      * Create a cookie with the specified name and value. Additional settings
      * for the cookie may be optionally specified (for example: expiration,
@@ -23,9 +25,9 @@ Ext.util.Cookies = {
      * access to pages that match that path. Defaults to all pages (<tt>'/'</tt>). 
      * @param {String} domain (Optional) Setting a domain restricts access to
      * pages on a given domain (typically used to allow cookie access across
-     * subdomains). For example, "extjs.com" will create a cookie that can be
-     * accessed from any subdomain of extjs.com, including www.extjs.com,
-     * support.extjs.com, etc.
+     * subdomains). For example, "sencha.com" will create a cookie that can be
+     * accessed from any subdomain of sencha.com, including www.sencha.com,
+     * support.sencha.com, etc.
      * @param {Boolean} secure (Optional) Specify true to indicate that the cookie
      * should only be accessible via SSL on a page using the HTTPS protocol.
      * Defaults to <tt>false</tt>. Note that this will only work if the page
@@ -33,12 +35,13 @@ Ext.util.Cookies = {
      * created with default options.
      */
     set : function(name, value){
-        var argv = arguments;
-        var argc = arguments.length;
-        var expires = (argc > 2) ? argv[2] : null;
-        var path = (argc > 3) ? argv[3] : '/';
-        var domain = (argc > 4) ? argv[4] : null;
-        var secure = (argc > 5) ? argv[5] : false;
+        var argv = arguments,
+            argc = arguments.length,
+            expires = (argc > 2) ? argv[2] : null,
+            path = (argc > 3) ? argv[3] : '/',
+            domain = (argc > 4) ? argv[4] : null,
+            secure = (argc > 5) ? argv[5] : false;
+            
         document.cookie = name + "=" + escape(value) + ((expires === null) ? "" : ("; expires=" + expires.toGMTString())) + ((path === null) ? "" : ("; path=" + path)) + ((domain === null) ? "" : ("; domain=" + domain)) + ((secure === true) ? "; secure" : "");
     },
 
@@ -55,15 +58,16 @@ Ext.util.Cookies = {
      * null if the cookie name does not exist.
      */
     get : function(name){
-        var arg = name + "=";
-        var alen = arg.length;
-        var clen = document.cookie.length;
-        var i = 0;
-        var j = 0;
+        var arg = name + "=",
+            alen = arg.length,
+            clen = document.cookie.length,
+            i = 0,
+            j = 0;
+            
         while(i < clen){
             j = i + alen;
             if(document.cookie.substring(i, j) == arg){
-                return Ext.util.Cookies.getCookieVal(j);
+                return this.getCookieVal(j);
             }
             i = document.cookie.indexOf(" ", i) + 1;
             if(i === 0){
@@ -77,12 +81,15 @@ Ext.util.Cookies = {
      * Removes a cookie with the provided name from the browser
      * if found by setting its expiration date to sometime in the past. 
      * @param {String} name The name of the cookie to remove
+     * @param {String} path (optional) The path for the cookie. This must be included if you included a path while setting the cookie.
      */
-    clear : function(name){
-        if(Ext.util.Cookies.get(name)){
-            document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
+    clear : function(name, path){
+        if(this.get(name)){
+            path = path || '/';
+            document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT; path=' + path;
         }
     },
+    
     /**
      * @private
      */
@@ -93,4 +100,4 @@ Ext.util.Cookies = {
         }
         return unescape(document.cookie.substring(offset, endstr));
     }
-};
\ No newline at end of file
+});