-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbi.storage.js
125 lines (100 loc) · 2.98 KB
/
bbi.storage.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*! BBI Storage (c) Blackbaud, Inc. */
(function(_win, bbi) {
"use strict";
var alias = "storage";
bbi.on("init", function() {
bbi.extension({
alias: alias,
defaults: {},
directive: function(ext, bbi, $) {
var _x = {};
var methods = {
clear: function (key) {
if (typeof key === "string") {
delete _x[key];
} else {
_x = {};
}
methods.save();
},
expose: function () {
return _x;
},
get: function (key) {
if (typeof _x[key] === "undefined" || _x[key] === "undefined") {
return null;
} else {
return _x[key];
}
},
set: function (key, value) {
_x[key] = value;
methods.save();
},
objectToString: function (obj) {
return JSON.stringify(obj);
},
stringToObject: function (str) {
var temp;
if (str === "undefined") {
str = "null";
}
if (typeof JSON.parse === "function") {
if (bbi.isDebugMode() === true) {
bbi.log("[BBI.storage] Parsing storage via JSON.parse.", false);
}
try {
temp = JSON.parse(str);
} catch (e) {
temp = {};
bbi.log("[BBI.Storage] JSON.parse ERROR: ", e);
}
} else if (typeof $.parseJSON === "function") {
if (bbi.isDebugMode() === true) {
bbi.log("[BBI.storage] Parsing storage via $.parseJSON.", false);
}
temp = $.parseJSON(str);
} else {
if (bbi.isDebugMode() === true) {
bbi.log("[BBI.storage] Parsing storage via eval().", false);
}
temp = eval('(' + str + ')');
}
return temp;
},
load: function () {
var ls = _win.sessionStorage;
for (var k in ls) {
if (typeof ls[k] === "string") {
_x[k] = methods.stringToObject(ls[k]);
}
}
},
save: function () {
var val;
if (typeof _win.sessvars === "object") {
return;
}
_win.sessionStorage.clear();
for (var k in _x) {
val = methods.objectToString(_x[k]);
_win.sessionStorage.setItem(k, val);
}
}
};
var __construct = (function () {
methods.load();
_win.onunload = methods.save;
}());
return {
clear: methods.clear,
expose: methods.expose,
get: methods.get,
set: methods.set
};
}
});
var instance = bbi.instantiate(alias);
bbi.map(alias, instance);
});
}.call({}, window, bbiGetInstance()));