1 | // cookie data store
|
---|
2 | function DataStore(name)
|
---|
3 | {
|
---|
4 | this.name = name;
|
---|
5 | this.load();
|
---|
6 | }
|
---|
7 |
|
---|
8 | DataStore.prototype.load = function ()
|
---|
9 | {
|
---|
10 | // create a key/value store
|
---|
11 | this.language = new Object();
|
---|
12 |
|
---|
13 | // get cookie text
|
---|
14 | var text = getCookie(this.name);
|
---|
15 |
|
---|
16 | if (text == null) return;
|
---|
17 |
|
---|
18 | // populate the store using the cookie text
|
---|
19 | var data = text.split(';');
|
---|
20 |
|
---|
21 | for (var i=0; i<data.length; i++)
|
---|
22 | {
|
---|
23 | var datum = data[i];
|
---|
24 | var index = datum.indexOf('=');
|
---|
25 |
|
---|
26 | if (index > 0)
|
---|
27 | {
|
---|
28 | var key = datum.substring(0,index);
|
---|
29 | var value = datum.substring(index+1);
|
---|
30 | this.language[key] = value;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | }
|
---|
35 |
|
---|
36 | function setCookie(name, value, expires, path, domain, secure)
|
---|
37 | {
|
---|
38 | var text = name + "=" + escape(value);
|
---|
39 |
|
---|
40 | if (expires)
|
---|
41 | {
|
---|
42 |
|
---|
43 | var currentDate = new Date();
|
---|
44 | var expireDate = new Date( currentDate.getTime() + expires*24*60*60*1000 );
|
---|
45 | text = text + ";expires=" + expireDate.toGMTString();
|
---|
46 | }
|
---|
47 | if (path) text = text + ";path=" + path;
|
---|
48 | if (domain) text = text + ";domain=" + domain;
|
---|
49 | if (secure) text = text + ";secure";
|
---|
50 |
|
---|
51 | document.cookie = text;
|
---|
52 | }
|
---|
53 |
|
---|
54 | function removeCookie(name)
|
---|
55 | {
|
---|
56 | setCookie(name, "", -1);
|
---|
57 | }
|
---|
58 |
|
---|
59 | function getCookie(name)
|
---|
60 | {
|
---|
61 | var text = document.cookie;
|
---|
62 |
|
---|
63 | var index = text.indexOf(name + "=");
|
---|
64 |
|
---|
65 | if (index < 0) return(null);
|
---|
66 |
|
---|
67 | var start = index + name.length + 1;
|
---|
68 | var end = text.indexOf(";", start);
|
---|
69 |
|
---|
70 | if (end < 0) end = text.length;
|
---|
71 |
|
---|
72 | var value = unescape( text.substring(start, end) );
|
---|
73 | return(value);
|
---|
74 | }
|
---|
75 |
|
---|
76 | DataStore.prototype.set = function(key, value)
|
---|
77 | {
|
---|
78 | this.language[key] = value;
|
---|
79 | }
|
---|
80 |
|
---|
81 | DataStore.prototype.get = function(key)
|
---|
82 | {
|
---|
83 | return(this.language[key]);
|
---|
84 | }
|
---|
85 |
|
---|
86 | DataStore.prototype.clear = function ()
|
---|
87 | {
|
---|
88 | this.language = new Object();
|
---|
89 | }
|
---|
90 |
|
---|
91 | DataStore.prototype.save = function ()
|
---|
92 | {
|
---|
93 | // prepare a cookie string
|
---|
94 | var text = "";
|
---|
95 |
|
---|
96 | // construct the string
|
---|
97 | for (var key in this.language)
|
---|
98 | {
|
---|
99 | var datum = key + "=" + this.language[key];
|
---|
100 | text = text + datum + ";";
|
---|
101 | }
|
---|
102 |
|
---|
103 | // set it
|
---|
104 | setCookie(this.name, text);
|
---|
105 | }
|
---|
106 |
|
---|
107 | DataStore.prototype.count = function()
|
---|
108 | {
|
---|
109 | var i = 0;
|
---|
110 | for (var key in this.data)
|
---|
111 | {
|
---|
112 | i++;
|
---|
113 | }
|
---|
114 | return(i);
|
---|
115 | }
|
---|
116 |
|
---|