initial import
[roojs1] / Roo / state / CookieProvider.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 /**
12  * @class Roo.state.CookieProvider
13  * @extends Roo.state.Provider
14  * The default Provider implementation which saves state via cookies.
15  * <br />Usage:
16  <pre><code>
17    var cp = new Roo.state.CookieProvider({
18        path: "/cgi-bin/",
19        expires: new Date(new Date().getTime()+(1000*60*60*24*30)); //30 days
20        domain: "roojs.com"
21    })
22    Roo.state.Manager.setProvider(cp);
23  </code></pre>
24  * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)
25  * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
26  * @cfg {String} domain The domain to save the cookie for.  Note that you cannot specify a different domain than
27  * your page is on, but you can specify a sub-domain, or simply the domain itself like 'roojs.com' to include
28  * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
29  * domain the page is running on including the 'www' like 'www.roojs.com')
30  * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
31  * @constructor
32  * Create a new CookieProvider
33  * @param {Object} config The configuration object
34  */
35 Roo.state.CookieProvider = function(config){
36     Roo.state.CookieProvider.superclass.constructor.call(this);
37     this.path = "/";
38     this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
39     this.domain = null;
40     this.secure = false;
41     Roo.apply(this, config);
42     this.state = this.readCookies();
43 };
44
45 Roo.extend(Roo.state.CookieProvider, Roo.state.Provider, {
46     // private
47     set : function(name, value){
48         if(typeof value == "undefined" || value === null){
49             this.clear(name);
50             return;
51         }
52         this.setCookie(name, value);
53         Roo.state.CookieProvider.superclass.set.call(this, name, value);
54     },
55
56     // private
57     clear : function(name){
58         this.clearCookie(name);
59         Roo.state.CookieProvider.superclass.clear.call(this, name);
60     },
61
62     // private
63     readCookies : function(){
64         var cookies = {};
65         var c = document.cookie + ";";
66         var re = /\s?(.*?)=(.*?);/g;
67         var matches;
68         while((matches = re.exec(c)) != null){
69             var name = matches[1];
70             var value = matches[2];
71             if(name && name.substring(0,3) == "ys-"){
72                 cookies[name.substr(3)] = this.decodeValue(value);
73             }
74         }
75         return cookies;
76     },
77
78     // private
79     setCookie : function(name, value){
80         document.cookie = "ys-"+ name + "=" + this.encodeValue(value) +
81            ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +
82            ((this.path == null) ? "" : ("; path=" + this.path)) +
83            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
84            ((this.secure == true) ? "; secure" : "");
85     },
86
87     // private
88     clearCookie : function(name){
89         document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
90            ((this.path == null) ? "" : ("; path=" + this.path)) +
91            ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
92            ((this.secure == true) ? "; secure" : "");
93     }
94 });