author | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
commit | 541bb378ddece2eab135a8066a16994e94436dea (patch) (unidiff) | |
tree | ff160ea3e26f7fe07fcfd401387c5a0232ca715e /frontend/beta/js/Clipperz/PM/DataModel/Record.js | |
parent | 1bf431fd3d45cbdf4afa3e12afefe5d24f4d3bc7 (diff) | |
parent | ecad5e895831337216544e81f1a467e0c68c4a6a (diff) | |
download | clipperz-541bb378ddece2eab135a8066a16994e94436dea.zip clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.gz clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.bz2 |
Merge pull request #1 from gcsolaroli/master
First version of the restructured repository
Diffstat (limited to 'frontend/beta/js/Clipperz/PM/DataModel/Record.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/PM/DataModel/Record.js | 759 |
1 files changed, 759 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/PM/DataModel/Record.js b/frontend/beta/js/Clipperz/PM/DataModel/Record.js new file mode 100644 index 0000000..b4b5023 --- a/dev/null +++ b/frontend/beta/js/Clipperz/PM/DataModel/Record.js | |||
@@ -0,0 +1,759 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer 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 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
31 | if (typeof(Clipperz.PM.DataModel) == 'undefined') { Clipperz.PM.DataModel = {}; } | ||
32 | |||
33 | |||
34 | //############################################################################# | ||
35 | |||
36 | Clipperz.PM.DataModel.Record = function(args) { | ||
37 | args = args || {}; | ||
38 | |||
39 | this._user = args['user'] || null; | ||
40 | this._reference = args['reference'] || Clipperz.PM.Crypto.randomKey(); | ||
41 | this._version = args['version'] || Clipperz.PM.Crypto.encryptingFunctions.currentVersion; | ||
42 | this._key = args['key'] || Clipperz.PM.Crypto.randomKey(); | ||
43 | |||
44 | this.setLabel(args['label'] || Clipperz.PM.Strings['newRecordTitleLabel']); | ||
45 | |||
46 | this.setHeaderNotes(args['headerNotes'] || null); | ||
47 | this.setNotes(args['notes'] || args['headerNotes'] || ""); | ||
48 | //MochiKit.Logging.logDebug("--- new Record ('" + this._label + "')- _headerNotes: '" + this._headerNotes + "'"); | ||
49 | //MochiKit.Logging.logDebug("--- new Record ('" + this._label + "')- _notes: '" + this._notes + "'"); | ||
50 | //this._notes = args.notes || ""; | ||
51 | |||
52 | this._versions = {}; | ||
53 | this._directLogins = {}; | ||
54 | this._removedDirectLogins = []; | ||
55 | |||
56 | this.setIsBrandNew(args['reference'] == null); | ||
57 | |||
58 | this.setShouldLoadData(this.isBrandNew() ? false: true); | ||
59 | this.setShouldDecryptData(this.isBrandNew() ? false: true); | ||
60 | this.setShouldProcessData(this.isBrandNew() ? false: true); | ||
61 | |||
62 | this.setCurrentVersion(this.isBrandNew() ? new Clipperz.PM.DataModel.RecordVersion(this, null): null); | ||
63 | this.setCurrentVersionKey(null); | ||
64 | |||
65 | this._serverData = null; | ||
66 | this._decryptedData = null; | ||
67 | this._cachedData = null; | ||
68 | |||
69 | return this; | ||
70 | } | ||
71 | |||
72 | Clipperz.PM.DataModel.Record.prototype = MochiKit.Base.update(null, { | ||
73 | |||
74 | 'toString': function() { | ||
75 | return "Record (" + this.label() + ")"; | ||
76 | }, | ||
77 | |||
78 | //------------------------------------------------------------------------- | ||
79 | |||
80 | 'isBrandNew': function() { | ||
81 | return this._isBrandNew; | ||
82 | }, | ||
83 | |||
84 | 'setIsBrandNew': function(aValue) { | ||
85 | this._isBrandNew = aValue; | ||
86 | }, | ||
87 | |||
88 | //------------------------------------------------------------------------- | ||
89 | /* | ||
90 | 'shouldRunTheRecordCreationWizard': function() { | ||
91 | return (this.isBrandNew() && (MochiKit.Base.keys(this.currentVersion().fields()).length == 0)); | ||
92 | }, | ||
93 | */ | ||
94 | //------------------------------------------------------------------------- | ||
95 | |||
96 | 'user': function() { | ||
97 | return this._user; | ||
98 | }, | ||
99 | |||
100 | //------------------------------------------------------------------------- | ||
101 | |||
102 | 'reference': function() { | ||
103 | return this._reference; | ||
104 | }, | ||
105 | |||
106 | //------------------------------------------------------------------------- | ||
107 | |||
108 | 'key': function() { | ||
109 | return this._key; | ||
110 | }, | ||
111 | |||
112 | 'updateKey': function() { | ||
113 | this._key = Clipperz.PM.Crypto.randomKey(); | ||
114 | }, | ||
115 | |||
116 | //------------------------------------------------------------------------- | ||
117 | |||
118 | 'label': function() { | ||
119 | return this._label; | ||
120 | }, | ||
121 | |||
122 | 'setLabel': function(aValue) { | ||
123 | this._label = aValue; | ||
124 | }, | ||
125 | |||
126 | 'lowerCaseLabel': function() { | ||
127 | return this.label().toLowerCase(); | ||
128 | }, | ||
129 | |||
130 | //------------------------------------------------------------------------- | ||
131 | |||
132 | 'versions': function() { | ||
133 | return this._versions; | ||
134 | }, | ||
135 | |||
136 | //------------------------------------------------------------------------- | ||
137 | |||
138 | 'currentVersion': function() { | ||
139 | return this._currentVersion; | ||
140 | }, | ||
141 | |||
142 | 'setCurrentVersion': function(aValue) { | ||
143 | this._currentVersion = aValue; | ||
144 | }, | ||
145 | |||
146 | //------------------------------------------------------------------------- | ||
147 | |||
148 | 'currentVersionKey': function() { | ||
149 | return this._currentVersionKey; | ||
150 | }, | ||
151 | |||
152 | 'setCurrentVersionKey': function(aValue) { | ||
153 | this._currentVersionKey = aValue; | ||
154 | }, | ||
155 | |||
156 | //------------------------------------------------------------------------- | ||
157 | |||
158 | 'deferredData': function() { | ||
159 | vardeferredResult; | ||
160 | |||
161 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.deferredData - this: " + this); | ||
162 | deferredResult = new MochiKit.Async.Deferred(); | ||
163 | deferredResult.addCallback(MochiKit.Base.method(this, 'loadData')); | ||
164 | deferredResult.addCallback(MochiKit.Base.method(this, 'decryptData')); | ||
165 | deferredResult.addCallback(MochiKit.Base.method(this, 'processData')); | ||
166 | deferredResult.addCallback(function(aRecord) { | ||
167 | return aRecord.currentVersion().deferredData(); | ||
168 | }); | ||
169 | deferredResult.addCallback(MochiKit.Base.method(this, 'takeSnapshotOfCurrentData')); | ||
170 | deferredResult.addCallback(MochiKit.Async.succeed, this); | ||
171 | deferredResult.callback(); | ||
172 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.deferredData"); | ||
173 | |||
174 | return deferredResult; | ||
175 | }, | ||
176 | |||
177 | //------------------------------------------------------------------------- | ||
178 | |||
179 | 'exportedData': function() { | ||
180 | var result; | ||
181 | |||
182 | result = {}; | ||
183 | result['label'] = this.label(); | ||
184 | result['data'] = this.serializedData(); | ||
185 | result['currentVersion'] = this.currentVersion().serializedData(); | ||
186 | result['currentVersion']['reference'] = this.currentVersion().reference(); | ||
187 | // result['versions'] = MochiKit.Base.map(MochiKit.Base.methodcaller("serializedData"), MochiKit.Base.values(this.versions())); | ||
188 | |||
189 | return Clipperz.Base.serializeJSON(result); | ||
190 | }, | ||
191 | |||
192 | //------------------------------------------------------------------------- | ||
193 | |||
194 | 'shouldLoadData': function() { | ||
195 | return this._shouldLoadData; | ||
196 | }, | ||
197 | |||
198 | 'setShouldLoadData': function(aValue) { | ||
199 | this._shouldLoadData = aValue; | ||
200 | }, | ||
201 | |||
202 | //------------------------------------------------------------------------- | ||
203 | |||
204 | 'shouldDecryptData': function() { | ||
205 | return this._shouldDecryptData; | ||
206 | }, | ||
207 | |||
208 | 'setShouldDecryptData': function(aValue) { | ||
209 | this._shouldDecryptData = aValue; | ||
210 | }, | ||
211 | |||
212 | //------------------------------------------------------------------------- | ||
213 | |||
214 | 'shouldProcessData': function() { | ||
215 | return this._shouldProcessData; | ||
216 | }, | ||
217 | |||
218 | 'setShouldProcessData': function(aValue) { | ||
219 | this._shouldProcessData = aValue; | ||
220 | }, | ||
221 | |||
222 | //------------------------------------------------------------------------- | ||
223 | |||
224 | 'loadData': function() { | ||
225 | var result; | ||
226 | |||
227 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.loadData - this: " + this); | ||
228 | if (this.shouldLoadData()) { | ||
229 | var deferredResult; | ||
230 | |||
231 | deferredResult = new MochiKit.Async.Deferred(); | ||
232 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'notify', 'loadingRecordData'); | ||
233 | deferredResult.addCallback(MochiKit.Base.method(this.user().connection(), 'message'), 'getRecordDetail', {reference: this.reference()}); | ||
234 | deferredResult.addCallback(MochiKit.Base.method(this,'setServerData')); | ||
235 | deferredResult.callback(); | ||
236 | result = deferredResult; | ||
237 | } else { | ||
238 | result = MochiKit.Async.succeed(this.serverData()); | ||
239 | } | ||
240 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.loadData"); | ||
241 | |||
242 | return result; | ||
243 | }, | ||
244 | |||
245 | //------------------------------------------------------------------------- | ||
246 | |||
247 | 'decryptData': function(anEncryptedData) { | ||
248 | var result; | ||
249 | |||
250 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.decryptData - this: " + this + " (" + anEncryptedData + ")"); | ||
251 | if (this.shouldDecryptData()) { | ||
252 | var deferredResult; | ||
253 | |||
254 | deferredResult = new MochiKit.Async.Deferred(); | ||
255 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'notify', 'decryptingRecordData'); | ||
256 | deferredResult.addCallback(Clipperz.PM.Crypto.deferredDecrypt, this.key(), anEncryptedData['data'], anEncryptedData['version']); | ||
257 | deferredResult.addCallback(function(anEncryptedData, someDecryptedValues) { | ||
258 | varresult; | ||
259 | |||
260 | result = anEncryptedData; | ||
261 | result['data'] = someDecryptedValues; | ||
262 | |||
263 | return result; | ||
264 | }, anEncryptedData); | ||
265 | deferredResult.addCallback(MochiKit.Base.method(this, 'setDecryptedData')); | ||
266 | deferredResult.callback(); | ||
267 | |||
268 | result = deferredResult; | ||
269 | } else { | ||
270 | result = MochiKit.Async.succeed(this.decryptedData()); | ||
271 | } | ||
272 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.decryptData"); | ||
273 | |||
274 | return result; | ||
275 | }, | ||
276 | |||
277 | //------------------------------------------------------------------------- | ||
278 | |||
279 | 'processData': function(someValues) { | ||
280 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.processData"); | ||
281 | //MochiKit.Logging.logDebug("--- Record.processData: " + Clipperz.Base.serializeJSON(someValues)); | ||
282 | if (this.shouldProcessData()) { | ||
283 | var currentVersionParameters; | ||
284 | |||
285 | this.processDataToExtractLegacyValues(someValues['data']); | ||
286 | |||
287 | if (typeof(someValues['data']['notes']) != 'undefined') { | ||
288 | this.setNotes(someValues['data']['notes']); | ||
289 | } | ||
290 | if (someValues['data']['currentVersionKey'] != null) { | ||
291 | this.setCurrentVersionKey(someValues['data']['currentVersionKey']); | ||
292 | } else { | ||
293 | this.setCurrentVersionKey(this.key()); | ||
294 | } | ||
295 | |||
296 | currentVersionParameters = someValues['currentVersion']; | ||
297 | currentVersionParameters['key'] = this.currentVersionKey(); | ||
298 | this.setCurrentVersion(new Clipperz.PM.DataModel.RecordVersion(this, currentVersionParameters)); | ||
299 | |||
300 | if (someValues['data']['directLogins'] != null) { | ||
301 | vardirectLoginReference; | ||
302 | |||
303 | for (directLoginReference in someValues['data']['directLogins']) { | ||
304 | var directLogin; | ||
305 | var directLoginParameters; | ||
306 | |||
307 | directLoginParameters = someValues['data']['directLogins'][directLoginReference]; | ||
308 | directLoginParameters.record = this; | ||
309 | directLoginParameters.reference = directLoginReference; | ||
310 | |||
311 | directLogin = new Clipperz.PM.DataModel.DirectLogin(directLoginParameters); | ||
312 | this.addDirectLogin(directLogin, true); | ||
313 | } | ||
314 | } | ||
315 | this.setShouldProcessData(false); | ||
316 | } | ||
317 | |||
318 | Clipperz.NotificationCenter.notify(this, 'recordDataReady'); | ||
319 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.processData"); | ||
320 | //MochiKit.Logging.logDebug("<<< Record.processData"); | ||
321 | |||
322 | return this; | ||
323 | }, | ||
324 | |||
325 | //------------------------------------------------------------------------- | ||
326 | |||
327 | 'processDataToExtractLegacyValues': function(someValues) { | ||
328 | //MochiKit.Logging.logDebug(">>> Record.processDataToExtractLegacyValues"); | ||
329 | if (someValues['data'] != null) { | ||
330 | this.setNotes(someValues['data']); | ||
331 | } | ||
332 | |||
333 | if ( | ||
334 | (typeof(someValues['loginFormData']) != "undefined") | ||
335 | &&(typeof(someValues['loginBindings'] != "undefined")) | ||
336 | &&(someValues['loginFormData'] != "") | ||
337 | &&(someValues['loginBindings'] != "") | ||
338 | ) { | ||
339 | vardirectLogin; | ||
340 | |||
341 | directLogin = new Clipperz.PM.DataModel.DirectLogin({ | ||
342 | record:this, | ||
343 | label:this.label() + Clipperz.PM.Strings['newDirectLoginLabelSuffix'], | ||
344 | reference:Clipperz.Crypto.SHA.sha256(new Clipperz.ByteArray(this.label() + | ||
345 | someValues['loginFormData'] + | ||
346 | someValues['loginBindings'])).toHexString().substring(2), | ||
347 | formData:Clipperz.Base.evalJSON(someValues['loginFormData']), | ||
348 | legacyBindingData:Clipperz.Base.evalJSON(someValues['loginBindings']), | ||
349 | bookmarkletVersion:'0.1' | ||
350 | }); | ||
351 | this.addDirectLogin(directLogin, true); | ||
352 | } | ||
353 | //MochiKit.Logging.logDebug("<<< Record.processDataToExtractLegacyValues"); | ||
354 | }, | ||
355 | |||
356 | //------------------------------------------------------------------------- | ||
357 | |||
358 | 'getReadyBeforeUpdatingVersionValues': function() { | ||
359 | }, | ||
360 | |||
361 | //------------------------------------------------------------------------- | ||
362 | |||
363 | 'addNewField': function() { | ||
364 | varnewField; | ||
365 | |||
366 | //MochiKit.Logging.logDebug(">>> Record.addNewField - " + this); | ||
367 | this.getReadyBeforeUpdatingVersionValues(); | ||
368 | newField = this.currentVersion().addNewField(); | ||
369 | Clipperz.NotificationCenter.notify(this, 'recordUpdated'); | ||
370 | //MochiKit.Logging.logDebug("<<< Record.addNewField"); | ||
371 | |||
372 | return newField; | ||
373 | }, | ||
374 | |||
375 | //------------------------------------------------------------------------- | ||
376 | |||
377 | 'removeField': function(aField) { | ||
378 | this.getReadyBeforeUpdatingVersionValues(); | ||
379 | this.currentVersion().removeField(aField); | ||
380 | Clipperz.NotificationCenter.notify(this, 'recordUpdated'); | ||
381 | }, | ||
382 | |||
383 | 'removeEmptyFields': function() { | ||
384 | MochiKit.Iter.forEach(MochiKit.Base.values(this.currentVersion().fields()), MochiKit.Base.bind(function(aField) { | ||
385 | if (aField.isEmpty()) { | ||
386 | this.removeField(aField); | ||
387 | // this.currentVersion().removeField(aField); | ||
388 | } | ||
389 | }, this)); | ||
390 | }, | ||
391 | |||
392 | //------------------------------------------------------------------------- | ||
393 | |||
394 | 'notes': function() { | ||
395 | return this._notes; | ||
396 | }, | ||
397 | |||
398 | 'setNotes': function(aValue) { | ||
399 | this._notes = aValue; | ||
400 | this.setHeaderNotes(null); | ||
401 | }, | ||
402 | |||
403 | //------------------------------------------------------------------------- | ||
404 | |||
405 | 'headerNotes': function() { | ||
406 | return this._headerNotes; | ||
407 | }, | ||
408 | |||
409 | 'setHeaderNotes': function(aValue) { | ||
410 | this._headerNotes = aValue; | ||
411 | }, | ||
412 | |||
413 | //------------------------------------------------------------------------- | ||
414 | |||
415 | 'remove': function() { | ||
416 | //MochiKit.Logging.logDebug(">>> Record.remove - " + this); | ||
417 | MochiKit.Iter.forEach(MochiKit.Base.values(this.directLogins()), MochiKit.Base.method(this, 'removeDirectLogin')); | ||
418 | |||
419 | this.syncDirectLoginReferenceValues(); | ||
420 | this.user().removeRecord(this); | ||
421 | //MochiKit.Logging.logDebug("<<< Record.remove"); | ||
422 | }, | ||
423 | |||
424 | //------------------------------------------------------------------------- | ||
425 | |||
426 | 'directLogins': function() { | ||
427 | return this._directLogins; | ||
428 | }, | ||
429 | |||
430 | 'addDirectLogin': function(aDirectLogin, shouldUpdateUser) { | ||
431 | this.directLogins()[aDirectLogin.reference()] = aDirectLogin; | ||
432 | if (shouldUpdateUser == true) { | ||
433 | this.user().addDirectLogin(aDirectLogin); | ||
434 | } | ||
435 | }, | ||
436 | |||
437 | 'removeDirectLogin': function(aDirectLogin) { | ||
438 | this.removedDirectLogins().push(aDirectLogin); | ||
439 | delete this.directLogins()[aDirectLogin.reference()]; | ||
440 | // this.user().removeDirectLogin(aDirectLogin); | ||
441 | }, | ||
442 | |||
443 | 'resetDirectLogins': function() { | ||
444 | this._directLogins = {}; | ||
445 | }, | ||
446 | |||
447 | 'removedDirectLogins': function() { | ||
448 | return this._removedDirectLogins; | ||
449 | }, | ||
450 | |||
451 | 'resetRemovedDirectLogins': function() { | ||
452 | this._removedDirectLogins = []; | ||
453 | }, | ||
454 | |||
455 | //------------------------------------------------------------------------- | ||
456 | |||
457 | 'serverData': function() { | ||
458 | return this._serverData; | ||
459 | }, | ||
460 | |||
461 | 'setServerData': function(aValue) { | ||
462 | this._serverData = aValue; | ||
463 | this.setShouldLoadData(false); | ||
464 | return aValue; | ||
465 | }, | ||
466 | |||
467 | //------------------------------------------------------------------------- | ||
468 | |||
469 | 'decryptedData': function() { | ||
470 | return this._decryptedData; | ||
471 | }, | ||
472 | |||
473 | 'setDecryptedData': function(aValue) { | ||
474 | this._decryptedData = aValue; | ||
475 | this.setShouldDecryptData(false); | ||
476 | return aValue; | ||
477 | }, | ||
478 | |||
479 | //------------------------------------------------------------------------- | ||
480 | |||
481 | 'cachedData': function() { | ||
482 | return this._cachedData; | ||
483 | }, | ||
484 | |||
485 | 'setCachedData': function(aValue) { | ||
486 | //MochiKit.Logging.logDebug(">>> Record.setCachedData"); | ||
487 | //MochiKit.Logging.logDebug("--- Record.setCachedData - aValue: " + Clipperz.Base.serializeJSON(aValue)); | ||
488 | this._cachedData = aValue; | ||
489 | this.setShouldProcessData(false); | ||
490 | //MochiKit.Logging.logDebug("<<< Record.setCachedData"); | ||
491 | |||
492 | return aValue; | ||
493 | }, | ||
494 | |||
495 | //------------------------------------------------------------------------- | ||
496 | |||
497 | 'hasPendingChanges': function() { | ||
498 | var result; | ||
499 | |||
500 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.hasPendingChanges"); | ||
501 | //MochiKit.Logging.logDebug(">>> Record.hasPendingChanges - cachedData: " + this.cachedData()); | ||
502 | //MochiKit.Logging.logDebug(">>> Record.hasPendingChanges - cachedData: " + Clipperz.Base.serializeJSON(this.cachedData())); | ||
503 | //MochiKit.Logging.logDebug(">>> Record.hasPendingChanges - currentSnapshot: " + this.currentDataSnapshot()); | ||
504 | //MochiKit.Logging.logDebug(">>> Record.hasPendingChanges - currentSnapshot: " + Clipperz.Base.serializeJSON(this.currentDataSnapshot())); | ||
505 | //console.log(">>> Record.hasPendingChanges - cachedData: %o", this.cachedData()); | ||
506 | //console.log(">>> Record.hasPendingChanges - currentSnapshot: %o", this.currentDataSnapshot()); | ||
507 | result = (MochiKit.Base.compare(this.cachedData(), this.currentDataSnapshot()) != 0); | ||
508 | //MochiKit.Logging.logDebug("<<< Record.hasPendingChanges - " + result); | ||
509 | |||
510 | if ((result == false) && this.isBrandNew() && (this.label() != Clipperz.PM.Strings['newRecordTitleLabel'])) { | ||
511 | result = true; | ||
512 | } | ||
513 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.hasPendingChanges"); | ||
514 | |||
515 | return result; | ||
516 | }, | ||
517 | |||
518 | //------------------------------------------------------------------------- | ||
519 | |||
520 | 'currentDataSnapshot': function() { | ||
521 | varresult; | ||
522 | |||
523 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.currentDataSnapshot"); | ||
524 | result = { | ||
525 | 'label': this.label(), | ||
526 | 'data': this.serializedData(), | ||
527 | 'currentVersion': this.currentVersion().currentDataSnapshot() | ||
528 | }; | ||
529 | |||
530 | // result['data']['data'] = this.notes(); | ||
531 | result = Clipperz.Base.serializeJSON(result); | ||
532 | |||
533 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.currentDataSnapshot"); | ||
534 | //MochiKit.Logging.logDebug("<<< Record.currentDataSnapshot"); | ||
535 | |||
536 | return result; | ||
537 | }, | ||
538 | |||
539 | //......................................................................... | ||
540 | |||
541 | 'takeSnapshotOfCurrentData': function() { | ||
542 | this.setCachedData(this.currentDataSnapshot()); | ||
543 | }, | ||
544 | |||
545 | //------------------------------------------------------------------------- | ||
546 | |||
547 | 'headerData': function() { | ||
548 | var result; | ||
549 | |||
550 | result = { | ||
551 | 'label': this.label(), | ||
552 | 'key': this.key() | ||
553 | }; | ||
554 | |||
555 | if (this.headerNotes() != null) { | ||
556 | result['headerNotes'] = this.headerNotes(); | ||
557 | } | ||
558 | |||
559 | return result; | ||
560 | }, | ||
561 | |||
562 | //------------------------------------------------------------------------- | ||
563 | |||
564 | 'serializedData': function() { | ||
565 | var result; | ||
566 | var directLoginReference; | ||
567 | |||
568 | result = {}; | ||
569 | result['currentVersionKey'] = this.currentVersion().key(); | ||
570 | |||
571 | result['directLogins'] = {}; | ||
572 | for (directLoginReference in this.directLogins()) { | ||
573 | result['directLogins'][directLoginReference] = this.directLogins()[directLoginReference].serializedData(); | ||
574 | } | ||
575 | result['notes'] = this.notes(); | ||
576 | |||
577 | return result; | ||
578 | }, | ||
579 | |||
580 | //------------------------------------------------------------------------- | ||
581 | |||
582 | 'encryptedData': function() { | ||
583 | var deferredResult; | ||
584 | varresult; | ||
585 | |||
586 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.encryptedData"); | ||
587 | result = {} | ||
588 | //MochiKit.Logging.logDebug("--- Record.encryptedData - 1"); | ||
589 | deferredResult = new MochiKit.Async.Deferred(); | ||
590 | //MochiKit.Logging.logDebug("--- Record.encryptedData - 2"); | ||
591 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Record.encryptedData - 1: " + res); return res;}); | ||
592 | deferredResult.addCallback(function(aResult, aRecord) { | ||
593 | aResult['reference'] = aRecord.reference(); | ||
594 | return aResult; | ||
595 | }, result, this); | ||
596 | //MochiKit.Logging.logDebug("--- Record.encryptedData - 3"); | ||
597 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Record.encryptedData - 2: " + res); return res;}); | ||
598 | deferredResult.addCallback(Clipperz.PM.Crypto.deferredEncryptWithCurrentVersion, this.key(), this.serializedData()); | ||
599 | //MochiKit.Logging.logDebug("--- Record.encryptedData - 4"); | ||
600 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Record.encryptedData - 3: " + res); return res;}); | ||
601 | deferredResult.addCallback(function(aResult, res) { | ||
602 | aResult['data'] = res; | ||
603 | return aResult; | ||
604 | }, result); | ||
605 | //MochiKit.Logging.logDebug("--- Record.encryptedData - 5"); | ||
606 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Record.encryptedData - 4: " + res); return res;}); | ||
607 | deferredResult.addCallback(function(aResult) { | ||
608 | aResult['version'] = Clipperz.PM.Crypto.encryptingFunctions.currentVersion; | ||
609 | return aResult; | ||
610 | }, result); | ||
611 | //MochiKit.Logging.logDebug("--- Record.encryptedData - 6"); | ||
612 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("Record.encryptedData - 5: " + res); return res;}); | ||
613 | deferredResult.callback(); | ||
614 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.encryptedData"); | ||
615 | |||
616 | return deferredResult; | ||
617 | }, | ||
618 | |||
619 | //------------------------------------------------------------------------- | ||
620 | |||
621 | 'syncDirectLoginReferenceValues': function() { | ||
622 | //MochiKit.Logging.logDebug(">>> Record.syncDirectLoginReferenceValues"); | ||
623 | MochiKit.Iter.forEach(MochiKit.Base.values(this.directLogins()), function(aDirectLogin) { | ||
624 | aDirectLogin.record().user().synchronizeDirectLogin(aDirectLogin); | ||
625 | }); | ||
626 | |||
627 | MochiKit.Iter.forEach(this.removedDirectLogins(), function(aDirectLogin) { | ||
628 | aDirectLogin.record().user().removeDirectLogin(aDirectLogin); | ||
629 | }); | ||
630 | |||
631 | this.resetRemovedDirectLogins(); | ||
632 | //MochiKit.Logging.logDebug("<<< Record.syncDirectLoginReferenceValues"); | ||
633 | }, | ||
634 | |||
635 | //------------------------------------------------------------------------- | ||
636 | |||
637 | 'saveChanges': function() { | ||
638 | var result; | ||
639 | |||
640 | if (this.isBrandNew() == false) { | ||
641 | result = this.user().saveRecords([this], 'updateData'); | ||
642 | } else { | ||
643 | result = this.user().saveRecords([this], 'addNewRecords'); | ||
644 | } | ||
645 | |||
646 | return result; | ||
647 | }, | ||
648 | |||
649 | /* | ||
650 | 'saveChanges': function() { | ||
651 | var deferredResult; | ||
652 | varresult; | ||
653 | |||
654 | Clipperz.NotificationCenter.notify(this.user(), 'updatedSection', 'records', true); | ||
655 | //MochiKit.Logging.logDebug(">>> Record.saveChanges"); | ||
656 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.saveChanges"); | ||
657 | if (this.headerNotes() != null) { | ||
658 | this.setNotes(this.headerNotes()); | ||
659 | } | ||
660 | this.syncDirectLoginReferenceValues(); | ||
661 | this.currentVersion().createNewVersion(); | ||
662 | |||
663 | result = {'records': [{}]}; | ||
664 | |||
665 | deferredResult = new MochiKit.Async.Deferred(); | ||
666 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveCard_collectRecordInfo'); | ||
667 | deferredResult.addCallback(MochiKit.Base.method(this, 'updateKey')); | ||
668 | |||
669 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveCard_encryptUserData'); | ||
670 | deferredResult.addCallback(MochiKit.Base.method(this.user(), 'encryptedData')); | ||
671 | deferredResult.addCallback(function(aResult, res) { | ||
672 | aResult['user'] = res; | ||
673 | return aResult; | ||
674 | }, result); | ||
675 | |||
676 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveCard_encryptRecordData'); | ||
677 | deferredResult.addCallback(MochiKit.Base.method(this, 'encryptedData')); | ||
678 | deferredResult.addCallback(function(aResult, res) { | ||
679 | //# aResult['record'] = res; | ||
680 | aResult['records'][0]['record'] = res; | ||
681 | return aResult; | ||
682 | }, result); | ||
683 | |||
684 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveCard_encryptRecordVersions'); | ||
685 | deferredResult.addCallback(MochiKit.Base.method(this.currentVersion(), 'encryptedData')); | ||
686 | deferredResult.addCallback(function(aResult, res) { | ||
687 | // aResult['currentRecordVersion'] = res; | ||
688 | aResult['records'][0]['currentRecordVersion'] = res; | ||
689 | return aResult; | ||
690 | }, result); | ||
691 | |||
692 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveCard_sendingData'); | ||
693 | if (this.isBrandNew() == false) { | ||
694 | deferredResult.addCallback(MochiKit.Base.method(this.user().connection(), 'message'), 'updateData'); | ||
695 | } else { | ||
696 | //# deferredResult.addCallback(MochiKit.Base.method(this.user().connection(), 'message'), 'addNewRecord'); | ||
697 | deferredResult.addCallback(MochiKit.Base.method(this.user().connection(), 'message'), 'addNewRecords'); | ||
698 | } | ||
699 | |||
700 | deferredResult.addCallback(MochiKit.Base.method(this, 'takeSnapshotOfCurrentData')); | ||
701 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'updatedProgressState', 'saveCard_updatingInterface'); | ||
702 | deferredResult.addCallback(MochiKit.Base.method(this, 'setIsBrandNew'), false); | ||
703 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'notify', 'recordUpdated'); | ||
704 | deferredResult.addCallback(Clipperz.NotificationCenter.deferredNotification, this, 'notify', 'directLoginUpdated'); | ||
705 | deferredResult.callback(); | ||
706 | |||
707 | return deferredResult; | ||
708 | }, | ||
709 | */ | ||
710 | //------------------------------------------------------------------------- | ||
711 | |||
712 | 'cancelChanges': function() { | ||
713 | //MochiKit.Logging.logDebug(">>> Record.cancelChanges"); | ||
714 | //MochiKit.Logging.logDebug("--- Record.cancelChanges - cachedData: " + Clipperz.Base.serializeJSON(this.cachedData())); | ||
715 | if (this.isBrandNew()) { | ||
716 | this.user().removeRecord(this); | ||
717 | } else { | ||
718 | this.restoreValuesFromSnapshot(this.cachedData()); | ||
719 | } | ||
720 | //MochiKit.Logging.logDebug("<<< Record.cancelChanges"); | ||
721 | }, | ||
722 | |||
723 | //------------------------------------------------------------------------- | ||
724 | |||
725 | 'restoreValuesFromSnapshot': function(someSnapshotData) { | ||
726 | varsnapshotData; | ||
727 | |||
728 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Record.restoreValuesFromSnapshot"); | ||
729 | snapshotData = Clipperz.Base.evalJSON(someSnapshotData); | ||
730 | //MochiKit.Logging.logDebug("--- Record.restoreValuesFromSnapshot - someSnapshotData (1): " + Clipperz.Base.serializeJSON(someSnapshotData)); | ||
731 | this.setLabel(snapshotData['label']); | ||
732 | this.resetDirectLogins(); | ||
733 | this.setShouldProcessData(true); | ||
734 | this.processData(snapshotData); | ||
735 | //MochiKit.Logging.logDebug("--- Record.restoreValuesFromSnapshot - snapshotData: (2)" + Clipperz.Base.serializeJSON(snapshotData)); | ||
736 | |||
737 | this.resetRemovedDirectLogins(); | ||
738 | |||
739 | { | ||
740 | var currentSnapshot; | ||
741 | varcomparisonResult; | ||
742 | |||
743 | currentSnapshot = this.currentDataSnapshot(); | ||
744 | //MochiKit.Logging.logDebug("--- Record.restoreValuesFromSnapshot - 1"); | ||
745 | //console.log("snapshot data: %o", someSnapshotData.currentVersion); | ||
746 | //console.log("current data: %o", currentSnapshot.currentVersion); | ||
747 | //MochiKit.Logging.logDebug("--- Record.restoreValuesFromSnapshot - someSnapshotData: " + Clipperz.Base.serializeJSON(someSnapshotData.currentVersion)); | ||
748 | //MochiKit.Logging.logDebug("--- Record.restoreValuesFromSnapshot - currentSnapshot: " + Clipperz.Base.serializeJSON(currentSnapshot.currentVersion)); | ||
749 | comparisonResult = MochiKit.Base.compare(someSnapshotData.currentVersion, currentSnapshot.currentVersion); | ||
750 | //MochiKit.Logging.logDebug("--- Record.restoreValuesFromSnapshot - " + comparisonResult); | ||
751 | } | ||
752 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Record.restoreValuesFromSnapshot"); | ||
753 | }, | ||
754 | |||
755 | //------------------------------------------------------------------------- | ||
756 | __syntaxFix__: "syntax fix" | ||
757 | }); | ||
758 | |||
759 | |||