]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/util/Cookie.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / util / Cookie.js
1 /**
2  * Cookie.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 (function() {
12         var each = tinymce.each;
13
14         /**
15          * This class contains simple cookie manangement functions.
16          *
17          * @class tinymce.util.Cookie
18          * @static
19          * @example
20          * // Gets a cookie from the browser
21          * console.debug(tinymce.util.Cookie.get('mycookie'));
22          * 
23          * // Gets a hash table cookie from the browser and takes out the x parameter from it
24          * console.debug(tinymce.util.Cookie.getHash('mycookie').x);
25          * 
26          * // Sets a hash table cookie to the browser
27          * tinymce.util.Cookie.setHash({x : '1', y : '2'});
28          */
29         tinymce.create('static tinymce.util.Cookie', {
30                 /**
31                  * Parses the specified query string into an name/value object.
32                  *
33                  * @method getHash
34                  * @param {String} n String to parse into a n Hashtable object.
35                  * @return {Object} Name/Value object with items parsed from querystring.
36                  */
37                 getHash : function(n) {
38                         var v = this.get(n), h;
39
40                         if (v) {
41                                 each(v.split('&'), function(v) {
42                                         v = v.split('=');
43                                         h = h || {};
44                                         h[unescape(v[0])] = unescape(v[1]);
45                                 });
46                         }
47
48                         return h;
49                 },
50
51                 /**
52                  * Sets a hashtable name/value object to a cookie.
53                  *
54                  * @method setHash
55                  * @param {String} n Name of the cookie.
56                  * @param {Object} v Hashtable object to set as cookie.
57                  * @param {Date} e Optional date object for the expiration of the cookie.
58                  * @param {String} p Optional path to restrict the cookie to.
59                  * @param {String} d Optional domain to restrict the cookie to.
60                  * @param {String} s Is the cookie secure or not.
61                  */
62                 setHash : function(n, v, e, p, d, s) {
63                         var o = '';
64
65                         each(v, function(v, k) {
66                                 o += (!o ? '' : '&') + escape(k) + '=' + escape(v);
67                         });
68
69                         this.set(n, o, e, p, d, s);
70                 },
71
72                 /**
73                  * Gets the raw data of a cookie by name.
74                  *
75                  * @method get
76                  * @param {String} n Name of cookie to retrive.
77                  * @return {String} Cookie data string.
78                  */
79                 get : function(n) {
80                         var c = document.cookie, e, p = n + "=", b;
81
82                         // Strict mode
83                         if (!c)
84                                 return;
85
86                         b = c.indexOf("; " + p);
87
88                         if (b == -1) {
89                                 b = c.indexOf(p);
90
91                                 if (b != 0)
92                                         return null;
93                         } else
94                                 b += 2;
95
96                         e = c.indexOf(";", b);
97
98                         if (e == -1)
99                                 e = c.length;
100
101                         return unescape(c.substring(b + p.length, e));
102                 },
103
104                 /**
105                  * Sets a raw cookie string.
106                  *
107                  * @method set
108                  * @param {String} n Name of the cookie.
109                  * @param {String} v Raw cookie data.
110                  * @param {Date} e Optional date object for the expiration of the cookie.
111                  * @param {String} p Optional path to restrict the cookie to.
112                  * @param {String} d Optional domain to restrict the cookie to.
113                  * @param {String} s Is the cookie secure or not.
114                  */
115                 set : function(n, v, e, p, d, s) {
116                         document.cookie = n + "=" + escape(v) +
117                                 ((e) ? "; expires=" + e.toGMTString() : "") +
118                                 ((p) ? "; path=" + escape(p) : "") +
119                                 ((d) ? "; domain=" + d : "") +
120                                 ((s) ? "; secure" : "");
121                 },
122
123                 /**
124                  * Removes/deletes a cookie by name.
125                  *
126                  * @method remove
127                  * @param {String} n Cookie name to remove/delete.
128                  * @param {Strong} p Optional path to remove the cookie from.
129                  */
130                 remove : function(n, p) {
131                         var d = new Date();
132
133                         d.setTime(d.getTime() - 1000);
134
135                         this.set(n, '', d, p, d);
136                 }
137         });
138 })();