summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/Clipperz/KeyValueObjectStore.js
Unidiff
Diffstat (limited to 'frontend/gamma/js/Clipperz/KeyValueObjectStore.js') (more/less context) (show whitespace changes)
-rw-r--r--frontend/gamma/js/Clipperz/KeyValueObjectStore.js176
1 files changed, 176 insertions, 0 deletions
diff --git a/frontend/gamma/js/Clipperz/KeyValueObjectStore.js b/frontend/gamma/js/Clipperz/KeyValueObjectStore.js
new file mode 100644
index 0000000..04beb85
--- a/dev/null
+++ b/frontend/gamma/js/Clipperz/KeyValueObjectStore.js
@@ -0,0 +1,176 @@
1/*
2
3Copyright 2008-2011 Clipperz Srl
4
5This file is part of Clipperz's Javascript Crypto Library.
6Javascript Crypto Library provides web developers with an extensive
7and efficient set of cryptographic functions. The library aims to
8obtain maximum execution speed while preserving modularity and
9reusability.
10For further information about its features and functionalities please
11refer to http://www.clipperz.com
12
13* Javascript Crypto Library is free software: you can redistribute
14 it and/or modify it under the terms of the GNU Affero General Public
15 License as published by the Free Software Foundation, either version
16 3 of the License, or (at your option) any later version.
17
18* Javascript Crypto Library is distributed in the hope that it will
19 be useful, but WITHOUT ANY WARRANTY; without even the implied
20 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU Affero General Public License for more details.
22
23* You should have received a copy of the GNU Affero General Public
24 License along with Javascript Crypto Library. If not, see
25 <http://www.gnu.org/licenses/>.
26
27*/
28
29if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
30
31//#############################################################################
32
33Clipperz.KeyValueObjectStore = function(args) {
34 args = args || {};
35
36 //this._name = args['name'] || "unnamed KeyValueObjectStore";
37 this._values = args['values'] || {};
38 //this._referenceObjectStore = null;
39//console.log("new KeyValueObjectStore", args, this._values);
40
41 return this;
42}
43
44Clipperz.KeyValueObjectStore.prototype = MochiKit.Base.update(null, {
45
46 'values': function() {
47 return this._values;
48 },
49
50 'initWithValues': function (someValues) {
51 this._values = Clipperz.Base.deepClone(someValues) || {};
52 return this;
53 },
54
55 'setValues': function (someValues) {
56//console.log("KeyValueObjectStore.setValues", someValues);
57 this._values = someValues;
58 return this;
59 },
60
61 //'initWithObjectStore': function (anObjectStore) {
62 // this._referenceObjectStore = anObjectStore;
63 //},
64
65 'removeAllData': function () {
66 this._values = {};
67 },
68
69 //-------------------------------------------------------------------------
70
71 'getValue': function(aKeyPath) {
72 var result;
73 var keys;
74 var i,c;
75
76 result = this.values();
77
78 keys = (aKeyPath + '').split('.');
79 c = keys.length;
80 i = 0;
81
82 while ((i<c) && (result != null)) {
83 if (typeof result[keys[i]] != 'undefined') {
84 result = result[keys[i]];
85 } else {
86 result = null;
87 }
88
89 i++;
90 }
91
92 return result;
93 },
94
95 //-------------------------------------------------------------------------
96
97 'setValue': function(aKeyPath, aValue) {
98 var targetObject;
99 var keys;
100 var i,c;
101
102//console.log(">>> KeyValueObjectStore.setValue", this, this.values(), aKeyPath, aValue);
103 targetObject = this.values();
104 keys = (aKeyPath + '').split('.');
105 c = keys.length - 1;
106 for (i=0; i<c; i++) {
107//console.log("--- KeyValueObjectStore.setValue", i, targetObject, keys[i]);
108 if (typeof targetObject[keys[i]] == 'undefined') {
109 targetObject[keys[i]] = {}
110 }
111
112 targetObject = targetObject[keys[i]];
113 }
114
115 targetObject[keys[c]] = aValue;
116//console.log("<<< KeyValueObjectStore.setValue");
117
118 return aValue;
119 },
120
121 //-------------------------------------------------------------------------
122
123 'removeValue': function (aKeyPath) {
124 // this.setValue(aKeyPath, null);
125
126 var targetObject;
127 var keys;
128 var i,c;
129
130 targetObject = this.values();
131 keys = ('' + aKeyPath).split('.');
132 c = keys.length - 1;
133 for (i=0; i<c; i++) {
134 if (typeof targetObject[keys[i]] == 'undefined') {
135 targetObject[keys[i]] = {}
136 }
137
138 targetObject = targetObject[keys[i]];
139 }
140
141 delete targetObject[keys[c]];
142 },
143
144 //-------------------------------------------------------------------------
145
146 'deferredGetOrSet': function(aKeyPath, aGetterFunction) {
147 var deferredResult;
148
149 if (this.getValue(aKeyPath) != null) {
150 deferredResult = MochiKit.Async.succeed(this.getValue(aKeyPath));
151 } else {
152 deferredResult = new Clipperz.Async.Deferred("KeyValueObjectStore.deferredGetOrSet [" + aKeyPath + "]", {trace:false});
153
154 deferredResult.addCallback(aGetterFunction);
155 deferredResult.addMethod(this, 'setValue', aKeyPath);
156 deferredResult.callback();
157 }
158
159 return deferredResult;
160 },
161
162 //-------------------------------------------------------------------------
163
164 'isEmpty': function () {
165 return (MochiKit.Base.keys(this.values()).length == 0)
166 },
167
168 //-------------------------------------------------------------------------
169/*
170 'dumpData': function () {
171 return Clipperz.Base.serializeJSON(this.values());
172 },
173*/
174 //-------------------------------------------------------------------------
175 __syntaxFix__: "syntax fix"
176});