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