]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/cookie.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / cookie.js
1 /*
2         Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3         Available via Academic Free License >= 2.1 OR the modified BSD license.
4         see: http://dojotoolkit.org/license for details
5 */
6
7
8 if(!dojo._hasResource["dojo.cookie"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo._hasResource["dojo.cookie"] = true;
10 dojo.provide("dojo.cookie");
11
12 dojo.require("dojo.regexp");
13
14 /*=====
15 dojo.__cookieProps = function(){
16         //      expires: Date|String|Number?
17         //              If a number, the number of days from today at which the cookie
18         //              will expire. If a date, the date past which the cookie will expire.
19         //              If expires is in the past, the cookie will be deleted.
20         //              If expires is omitted or is 0, the cookie will expire when the browser closes. << FIXME: 0 seems to disappear right away? FF3.
21         //      path: String?
22         //              The path to use for the cookie.
23         //      domain: String?
24         //              The domain to use for the cookie.
25         //      secure: Boolean?
26         //              Whether to only send the cookie on secure connections
27         this.expires = expires;
28         this.path = path;
29         this.domain = domain;
30         this.secure = secure;
31 }
32 =====*/
33
34
35 dojo.cookie = function(/*String*/name, /*String?*/value, /*dojo.__cookieProps?*/props){
36         //      summary: 
37         //              Get or set a cookie.
38         //      description:
39         //              If one argument is passed, returns the value of the cookie
40         //              For two or more arguments, acts as a setter.
41         //      name:
42         //              Name of the cookie
43         //      value:
44         //              Value for the cookie
45         //      props: 
46         //              Properties for the cookie
47         //      example:
48         //              set a cookie with the JSON-serialized contents of an object which
49         //              will expire 5 days from now:
50         //      |       dojo.cookie("configObj", dojo.toJson(config), { expires: 5 });
51         //      
52         //      example:
53         //              de-serialize a cookie back into a JavaScript object:
54         //      |       var config = dojo.fromJson(dojo.cookie("configObj"));
55         //      
56         //      example:
57         //              delete a cookie:
58         //      |       dojo.cookie("configObj", null, {expires: -1});
59         var c = document.cookie;
60         if(arguments.length == 1){
61                 var matches = c.match(new RegExp("(?:^|; )" + dojo.regexp.escapeString(name) + "=([^;]*)"));
62                 return matches ? decodeURIComponent(matches[1]) : undefined; // String or undefined
63         }else{
64                 props = props || {};
65 // FIXME: expires=0 seems to disappear right away, not on close? (FF3)  Change docs?
66                 var exp = props.expires;
67                 if(typeof exp == "number"){ 
68                         var d = new Date();
69                         d.setTime(d.getTime() + exp*24*60*60*1000);
70                         exp = props.expires = d;
71                 }
72                 if(exp && exp.toUTCString){ props.expires = exp.toUTCString(); }
73
74                 value = encodeURIComponent(value);
75                 var updatedCookie = name + "=" + value, propName;
76                 for(propName in props){
77                         updatedCookie += "; " + propName;
78                         var propValue = props[propName];
79                         if(propValue !== true){ updatedCookie += "=" + propValue; }
80                 }
81                 document.cookie = updatedCookie;
82         }
83 };
84
85 dojo.cookie.isSupported = function(){
86         //      summary:
87         //              Use to determine if the current browser supports cookies or not.
88         //              
89         //              Returns true if user allows cookies.
90         //              Returns false if user doesn't allow cookies.
91
92         if(!("cookieEnabled" in navigator)){
93                 this("__djCookieTest__", "CookiesAllowed");
94                 navigator.cookieEnabled = this("__djCookieTest__") == "CookiesAllowed";
95                 if(navigator.cookieEnabled){
96                         this("__djCookieTest__", "", {expires: -1});
97                 }
98         }
99         return navigator.cookieEnabled;
100 };
101
102 }