-
Notifications
You must be signed in to change notification settings - Fork 0
/
CookieDataProvider.js
109 lines (100 loc) · 3.38 KB
/
CookieDataProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(function (ns) {
"use strict";
/**
* @name wader.CookieDataProvider
* @class Wader Cookie Data Provider
* @augments wader.ADataProvider
* @author sc0rp10 <[email protected]>
* @version 0.3
*/
ADataProvider.extend("wader.CookieDataProvider",
/** @lends wader.CookieDataProvider# */
{
_ls: {
/** @inner */
setItem: function (k, v, props) {
props = props || {};
var exp = props.expires;
if (typeof exp == "number" && exp) {
var d = new Date();
d.setTime(d.getTime() + exp*1000);
exp = props.expires = d;
}
if(exp && exp.toUTCString) {
props.expires = exp.toUTCString();
}
var updatedCookie = k + "=" + v;
for(var propName in props) {
updatedCookie += "; " + propName;
var propValue = props[propName];
if(propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
},
/** @inner */
getItem: function (k) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + k.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
))
return matches ? matches[1] : null;
},
/** @inner */
removeItem: function (k) {
setCookie(k, null, {
expires: -1
})
}
},
/**
* @param {String} method
* @param {String} key
* @param {String} value
* @return {undefined}
*/
_makeRequest: function (method, key, value) {
var newKey = this.resource + "_" + key;
switch (method) {
case "get":
var result = this._ls.getItem(newKey);
try {
return JSON.parse(result);
} catch (e) {
return result;
}
case "post":
case "put":
return this._ls.setItem(newKey, JSON.stringify(value));
case "delete":
return this._ls.removeItem(newKey);
default:
break;
}
},
/**
* @param {String} pattern
* @return {Hash[]}
*/
getMulti: function (pattern) {
if (!pattern instanceof RegExp) {
throw new Error("Invalid params in LocalStorageDataProvider.getMulti: pattern must be instance of RegExp");
};
var items = [];
for (var key in this._ls) {
if (this._ls.hasOwnProperty(key)) {
if (pattern.test(key)) {
var obj = {};
key = key.replace(this.resource + "_", "")
obj[key] = this.get(key);
items.push(obj);
};
};
}
return items;
}
});
if (ns !== wader) {
ns.CookieDataProvider = wader.CookieDataProvider;
};
})(window.WADER_NS || window);