root / trunk / web / dojo / dojox / data / AndOrWriteStore.js @ 10
History | View | Annotate | Download (11.9 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.data.AndOrWriteStore"]){ |
9 |
dojo._hasResource["dojox.data.AndOrWriteStore"]=true; |
10 |
dojo.provide("dojox.data.AndOrWriteStore");
|
11 |
dojo.require("dojox.data.AndOrReadStore");
|
12 |
dojo.declare("dojox.data.AndOrWriteStore",dojox.data.AndOrReadStore,{constructor:function(_1){ |
13 |
this._features["dojo.data.api.Write"]=true; |
14 |
this._features["dojo.data.api.Notification"]=true; |
15 |
this._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}}; |
16 |
if(!this._datatypeMap["Date"].serialize){ |
17 |
this._datatypeMap["Date"].serialize=function(_2){ |
18 |
return dojo.date.stamp.toISOString(_2,{zulu:true}); |
19 |
}; |
20 |
} |
21 |
if(_1&&(_1.referenceIntegrity===false)){ |
22 |
this.referenceIntegrity=false; |
23 |
} |
24 |
this._saveInProgress=false; |
25 |
},referenceIntegrity:true,_assert:function(_3){ |
26 |
if(!_3){
|
27 |
throw new Error("assertion failed in ItemFileWriteStore"); |
28 |
} |
29 |
},_getIdentifierAttribute:function(){ |
30 |
var _4=this.getFeatures()["dojo.data.api.Identity"]; |
31 |
return _4;
|
32 |
},newItem:function(_5,_6){ |
33 |
this._assert(!this._saveInProgress); |
34 |
if(!this._loadFinished){ |
35 |
this._forceLoad();
|
36 |
} |
37 |
if(typeof _5!="object"&&typeof _5!="undefined"){ |
38 |
throw new Error("newItem() was passed something other than an object"); |
39 |
} |
40 |
var _7=null; |
41 |
var _8=this._getIdentifierAttribute(); |
42 |
if(_8===Number){
|
43 |
_7=this._arrayOfAllItems.length;
|
44 |
}else{
|
45 |
_7=_5[_8]; |
46 |
if(typeof _7==="undefined"){ |
47 |
throw new Error("newItem() was not passed an identity for the new item"); |
48 |
} |
49 |
if(dojo.isArray(_7)){
|
50 |
throw new Error("newItem() was not passed an single-valued identity"); |
51 |
} |
52 |
} |
53 |
if(this._itemsByIdentity){ |
54 |
this._assert(typeof this._itemsByIdentity[_7]==="undefined"); |
55 |
} |
56 |
this._assert(typeof this._pending._newItems[_7]==="undefined"); |
57 |
this._assert(typeof this._pending._deletedItems[_7]==="undefined"); |
58 |
var _9={};
|
59 |
_9[this._storeRefPropName]=this; |
60 |
_9[this._itemNumPropName]=this._arrayOfAllItems.length; |
61 |
if(this._itemsByIdentity){ |
62 |
this._itemsByIdentity[_7]=_9;
|
63 |
_9[_8]=[_7]; |
64 |
} |
65 |
this._arrayOfAllItems.push(_9);
|
66 |
var _a=null; |
67 |
if(_6&&_6.parent&&_6.attribute){
|
68 |
_a={item:_6.parent,attribute:_6.attribute,oldValue:undefined}; |
69 |
var _b=this.getValues(_6.parent,_6.attribute); |
70 |
if(_b&&_b.length>0){ |
71 |
var _c=_b.slice(0,_b.length); |
72 |
if(_b.length===1){ |
73 |
_a.oldValue=_b[0];
|
74 |
}else{
|
75 |
_a.oldValue=_b.slice(0,_b.length);
|
76 |
} |
77 |
_c.push(_9); |
78 |
this._setValueOrValues(_6.parent,_6.attribute,_c,false); |
79 |
_a.newValue=this.getValues(_6.parent,_6.attribute);
|
80 |
}else{
|
81 |
this._setValueOrValues(_6.parent,_6.attribute,_9,false); |
82 |
_a.newValue=_9; |
83 |
} |
84 |
}else{
|
85 |
_9[this._rootItemPropName]=true; |
86 |
this._arrayOfTopLevelItems.push(_9);
|
87 |
} |
88 |
this._pending._newItems[_7]=_9;
|
89 |
for(var _d in _5){ |
90 |
if(_d===this._storeRefPropName||_d===this._itemNumPropName){ |
91 |
throw new Error("encountered bug in ItemFileWriteStore.newItem"); |
92 |
} |
93 |
var _e=_5[_d];
|
94 |
if(!dojo.isArray(_e)){
|
95 |
_e=[_e]; |
96 |
} |
97 |
_9[_d]=_e; |
98 |
if(this.referenceIntegrity){ |
99 |
for(var i=0;i<_e.length;i++){ |
100 |
var _f=_e[i];
|
101 |
if(this.isItem(_f)){ |
102 |
this._addReferenceToMap(_f,_9,_d);
|
103 |
} |
104 |
} |
105 |
} |
106 |
} |
107 |
this.onNew(_9,_a);
|
108 |
return _9;
|
109 |
},_removeArrayElement:function(_10,_11){ |
110 |
var _12=dojo.indexOf(_10,_11);
|
111 |
if(_12!=-1){ |
112 |
_10.splice(_12,1);
|
113 |
return true; |
114 |
} |
115 |
return false; |
116 |
},deleteItem:function(_13){ |
117 |
this._assert(!this._saveInProgress); |
118 |
this._assertIsItem(_13);
|
119 |
var _14=_13[this._itemNumPropName]; |
120 |
var _15=this.getIdentity(_13); |
121 |
if(this.referenceIntegrity){ |
122 |
var _16=this.getAttributes(_13); |
123 |
if(_13[this._reverseRefMap]){ |
124 |
_13["backup_"+this._reverseRefMap]=dojo.clone(_13[this._reverseRefMap]); |
125 |
} |
126 |
dojo.forEach(_16,function(_17){
|
127 |
dojo.forEach(this.getValues(_13,_17),function(_18){ |
128 |
if(this.isItem(_18)){ |
129 |
if(!_13["backupRefs_"+this._reverseRefMap]){ |
130 |
_13["backupRefs_"+this._reverseRefMap]=[]; |
131 |
} |
132 |
_13["backupRefs_"+this._reverseRefMap].push({id:this.getIdentity(_18),attr:_17}); |
133 |
this._removeReferenceFromMap(_18,_13,_17);
|
134 |
} |
135 |
},this);
|
136 |
},this);
|
137 |
var _19=_13[this._reverseRefMap]; |
138 |
if(_19){
|
139 |
for(var _1a in _19){ |
140 |
var _1b=null; |
141 |
if(this._itemsByIdentity){ |
142 |
_1b=this._itemsByIdentity[_1a];
|
143 |
}else{
|
144 |
_1b=this._arrayOfAllItems[_1a];
|
145 |
} |
146 |
if(_1b){
|
147 |
for(var _1c in _19[_1a]){ |
148 |
var _1d=this.getValues(_1b,_1c)||[]; |
149 |
var _1e=dojo.filter(_1d,function(_1f){ |
150 |
return !(this.isItem(_1f)&&this.getIdentity(_1f)==_15); |
151 |
},this);
|
152 |
this._removeReferenceFromMap(_13,_1b,_1c);
|
153 |
if(_1e.length<_1d.length){
|
154 |
this._setValueOrValues(_1b,_1c,_1e);
|
155 |
} |
156 |
} |
157 |
} |
158 |
} |
159 |
} |
160 |
} |
161 |
this._arrayOfAllItems[_14]=null; |
162 |
_13[this._storeRefPropName]=null; |
163 |
if(this._itemsByIdentity){ |
164 |
delete this._itemsByIdentity[_15]; |
165 |
} |
166 |
this._pending._deletedItems[_15]=_13;
|
167 |
if(_13[this._rootItemPropName]){ |
168 |
this._removeArrayElement(this._arrayOfTopLevelItems,_13); |
169 |
} |
170 |
this.onDelete(_13);
|
171 |
return true; |
172 |
},setValue:function(_20,_21,_22){ |
173 |
return this._setValueOrValues(_20,_21,_22,true); |
174 |
},setValues:function(_23,_24,_25){ |
175 |
return this._setValueOrValues(_23,_24,_25,true); |
176 |
},unsetAttribute:function(_26,_27){ |
177 |
return this._setValueOrValues(_26,_27,[],true); |
178 |
},_setValueOrValues:function(_28,_29,_2a,_2b){ |
179 |
this._assert(!this._saveInProgress); |
180 |
this._assertIsItem(_28);
|
181 |
this._assert(dojo.isString(_29));
|
182 |
this._assert(typeof _2a!=="undefined"); |
183 |
var _2c=this._getIdentifierAttribute(); |
184 |
if(_29==_2c){
|
185 |
throw new Error("ItemFileWriteStore does not have support for changing the value of an item's identifier."); |
186 |
} |
187 |
var _2d=this._getValueOrValues(_28,_29); |
188 |
var _2e=this.getIdentity(_28); |
189 |
if(!this._pending._modifiedItems[_2e]){ |
190 |
var _2f={};
|
191 |
for(var key in _28){ |
192 |
if((key===this._storeRefPropName)||(key===this._itemNumPropName)||(key===this._rootItemPropName)){ |
193 |
_2f[key]=_28[key]; |
194 |
}else{
|
195 |
if(key===this._reverseRefMap){ |
196 |
_2f[key]=dojo.clone(_28[key]); |
197 |
}else{
|
198 |
_2f[key]=_28[key].slice(0,_28[key].length);
|
199 |
} |
200 |
} |
201 |
} |
202 |
this._pending._modifiedItems[_2e]=_2f;
|
203 |
} |
204 |
var _30=false; |
205 |
if(dojo.isArray(_2a)&&_2a.length===0){ |
206 |
_30=delete _28[_29];
|
207 |
_2a=undefined;
|
208 |
if(this.referenceIntegrity&&_2d){ |
209 |
var _31=_2d;
|
210 |
if(!dojo.isArray(_31)){
|
211 |
_31=[_31]; |
212 |
} |
213 |
for(var i=0;i<_31.length;i++){ |
214 |
var _32=_31[i];
|
215 |
if(this.isItem(_32)){ |
216 |
this._removeReferenceFromMap(_32,_28,_29);
|
217 |
} |
218 |
} |
219 |
} |
220 |
}else{
|
221 |
var _33;
|
222 |
if(dojo.isArray(_2a)){
|
223 |
var _34=_2a;
|
224 |
_33=_2a.slice(0,_2a.length);
|
225 |
}else{
|
226 |
_33=[_2a]; |
227 |
} |
228 |
if(this.referenceIntegrity){ |
229 |
if(_2d){
|
230 |
var _31=_2d;
|
231 |
if(!dojo.isArray(_31)){
|
232 |
_31=[_31]; |
233 |
} |
234 |
var map={};
|
235 |
dojo.forEach(_31,function(_35){
|
236 |
if(this.isItem(_35)){ |
237 |
var id=this.getIdentity(_35); |
238 |
map[id.toString()]=true;
|
239 |
} |
240 |
},this);
|
241 |
dojo.forEach(_33,function(_36){
|
242 |
if(this.isItem(_36)){ |
243 |
var id=this.getIdentity(_36); |
244 |
if(map[id.toString()]){
|
245 |
delete map[id.toString()];
|
246 |
}else{
|
247 |
this._addReferenceToMap(_36,_28,_29);
|
248 |
} |
249 |
} |
250 |
},this);
|
251 |
for(var rId in map){ |
252 |
var _37;
|
253 |
if(this._itemsByIdentity){ |
254 |
_37=this._itemsByIdentity[rId];
|
255 |
}else{
|
256 |
_37=this._arrayOfAllItems[rId];
|
257 |
} |
258 |
this._removeReferenceFromMap(_37,_28,_29);
|
259 |
} |
260 |
}else{
|
261 |
for(var i=0;i<_33.length;i++){ |
262 |
var _32=_33[i];
|
263 |
if(this.isItem(_32)){ |
264 |
this._addReferenceToMap(_32,_28,_29);
|
265 |
} |
266 |
} |
267 |
} |
268 |
} |
269 |
_28[_29]=_33; |
270 |
_30=true;
|
271 |
} |
272 |
if(_2b){
|
273 |
this.onSet(_28,_29,_2d,_2a);
|
274 |
} |
275 |
return _30;
|
276 |
},_addReferenceToMap:function(_38,_39,_3a){ |
277 |
var _3b=this.getIdentity(_39); |
278 |
var _3c=_38[this._reverseRefMap]; |
279 |
if(!_3c){
|
280 |
_3c=_38[this._reverseRefMap]={};
|
281 |
} |
282 |
var _3d=_3c[_3b];
|
283 |
if(!_3d){
|
284 |
_3d=_3c[_3b]={}; |
285 |
} |
286 |
_3d[_3a]=true;
|
287 |
},_removeReferenceFromMap:function(_3e,_3f,_40){ |
288 |
var _41=this.getIdentity(_3f); |
289 |
var _42=_3e[this._reverseRefMap]; |
290 |
var _43;
|
291 |
if(_42){
|
292 |
for(_43 in _42){ |
293 |
if(_43==_41){
|
294 |
delete _42[_43][_40];
|
295 |
if(this._isEmpty(_42[_43])){ |
296 |
delete _42[_43];
|
297 |
} |
298 |
} |
299 |
} |
300 |
if(this._isEmpty(_42)){ |
301 |
delete _3e[this._reverseRefMap]; |
302 |
} |
303 |
} |
304 |
},_dumpReferenceMap:function(){ |
305 |
var i;
|
306 |
for(i=0;i<this._arrayOfAllItems.length;i++){ |
307 |
var _44=this._arrayOfAllItems[i]; |
308 |
if(_44&&_44[this._reverseRefMap]){ |
309 |
} |
310 |
} |
311 |
},_getValueOrValues:function(_45,_46){ |
312 |
var _47=undefined; |
313 |
if(this.hasAttribute(_45,_46)){ |
314 |
var _48=this.getValues(_45,_46); |
315 |
if(_48.length==1){ |
316 |
_47=_48[0];
|
317 |
}else{
|
318 |
_47=_48; |
319 |
} |
320 |
} |
321 |
return _47;
|
322 |
},_flatten:function(_49){ |
323 |
if(this.isItem(_49)){ |
324 |
var _4a=_49;
|
325 |
var _4b=this.getIdentity(_4a); |
326 |
var _4c={_reference:_4b}; |
327 |
return _4c;
|
328 |
}else{
|
329 |
if(typeof _49==="object"){ |
330 |
for(var _4d in this._datatypeMap){ |
331 |
var _4e=this._datatypeMap[_4d]; |
332 |
if(dojo.isObject(_4e)&&!dojo.isFunction(_4e)){
|
333 |
if(_49 instanceof _4e.type){ |
334 |
if(!_4e.serialize){
|
335 |
throw new Error("ItemFileWriteStore: No serializer defined for type mapping: ["+_4d+"]"); |
336 |
} |
337 |
return {_type:_4d,_value:_4e.serialize(_49)}; |
338 |
} |
339 |
}else{
|
340 |
if(_49 instanceof _4e){ |
341 |
return {_type:_4d,_value:_49.toString()}; |
342 |
} |
343 |
} |
344 |
} |
345 |
} |
346 |
return _49;
|
347 |
} |
348 |
},_getNewFileContentString:function(){ |
349 |
var _4f={};
|
350 |
var _50=this._getIdentifierAttribute(); |
351 |
if(_50!==Number){
|
352 |
_4f.identifier=_50; |
353 |
} |
354 |
if(this._labelAttr){ |
355 |
_4f.label=this._labelAttr;
|
356 |
} |
357 |
_4f.items=[]; |
358 |
for(var i=0;i<this._arrayOfAllItems.length;++i){ |
359 |
var _51=this._arrayOfAllItems[i]; |
360 |
if(_51!==null){ |
361 |
var _52={};
|
362 |
for(var key in _51){ |
363 |
if(key!==this._storeRefPropName&&key!==this._itemNumPropName&&key!==this._reverseRefMap&&key!==this._rootItemPropName){ |
364 |
var _53=key;
|
365 |
var _54=this.getValues(_51,_53); |
366 |
if(_54.length==1){ |
367 |
_52[_53]=this._flatten(_54[0]); |
368 |
}else{
|
369 |
var _55=[];
|
370 |
for(var j=0;j<_54.length;++j){ |
371 |
_55.push(this._flatten(_54[j]));
|
372 |
_52[_53]=_55; |
373 |
} |
374 |
} |
375 |
} |
376 |
} |
377 |
_4f.items.push(_52); |
378 |
} |
379 |
} |
380 |
var _56=true; |
381 |
return dojo.toJson(_4f,_56);
|
382 |
},_isEmpty:function(_57){ |
383 |
var _58=true; |
384 |
if(dojo.isObject(_57)){
|
385 |
var i;
|
386 |
for(i in _57){ |
387 |
_58=false;
|
388 |
break;
|
389 |
} |
390 |
}else{
|
391 |
if(dojo.isArray(_57)){
|
392 |
if(_57.length>0){ |
393 |
_58=false;
|
394 |
} |
395 |
} |
396 |
} |
397 |
return _58;
|
398 |
},save:function(_59){ |
399 |
this._assert(!this._saveInProgress); |
400 |
this._saveInProgress=true; |
401 |
var _5a=this; |
402 |
var _5b=function(){ |
403 |
_5a._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}}; |
404 |
_5a._saveInProgress=false;
|
405 |
if(_59&&_59.onComplete){
|
406 |
var _5c=_59.scope||dojo.global;
|
407 |
_59.onComplete.call(_5c); |
408 |
} |
409 |
}; |
410 |
var _5d=function(){ |
411 |
_5a._saveInProgress=false;
|
412 |
if(_59&&_59.onError){
|
413 |
var _5e=_59.scope||dojo.global;
|
414 |
_59.onError.call(_5e); |
415 |
} |
416 |
}; |
417 |
if(this._saveEverything){ |
418 |
var _5f=this._getNewFileContentString(); |
419 |
this._saveEverything(_5b,_5d,_5f);
|
420 |
} |
421 |
if(this._saveCustom){ |
422 |
this._saveCustom(_5b,_5d);
|
423 |
} |
424 |
if(!this._saveEverything&&!this._saveCustom){ |
425 |
_5b(); |
426 |
} |
427 |
},revert:function(){ |
428 |
this._assert(!this._saveInProgress); |
429 |
var _60;
|
430 |
for(_60 in this._pending._modifiedItems){ |
431 |
var _61=this._pending._modifiedItems[_60]; |
432 |
var _62=null; |
433 |
if(this._itemsByIdentity){ |
434 |
_62=this._itemsByIdentity[_60];
|
435 |
}else{
|
436 |
_62=this._arrayOfAllItems[_60];
|
437 |
} |
438 |
_61[this._storeRefPropName]=this; |
439 |
for(key in _62){ |
440 |
delete _62[key];
|
441 |
} |
442 |
dojo.mixin(_62,_61); |
443 |
} |
444 |
var _63;
|
445 |
for(_60 in this._pending._deletedItems){ |
446 |
_63=this._pending._deletedItems[_60];
|
447 |
_63[this._storeRefPropName]=this; |
448 |
var _64=_63[this._itemNumPropName]; |
449 |
if(_63["backup_"+this._reverseRefMap]){ |
450 |
_63[this._reverseRefMap]=_63["backup_"+this._reverseRefMap]; |
451 |
delete _63["backup_"+this._reverseRefMap]; |
452 |
} |
453 |
this._arrayOfAllItems[_64]=_63;
|
454 |
if(this._itemsByIdentity){ |
455 |
this._itemsByIdentity[_60]=_63;
|
456 |
} |
457 |
if(_63[this._rootItemPropName]){ |
458 |
this._arrayOfTopLevelItems.push(_63);
|
459 |
} |
460 |
} |
461 |
for(_60 in this._pending._deletedItems){ |
462 |
_63=this._pending._deletedItems[_60];
|
463 |
if(_63["backupRefs_"+this._reverseRefMap]){ |
464 |
dojo.forEach(_63["backupRefs_"+this._reverseRefMap],function(_65){ |
465 |
var _66;
|
466 |
if(this._itemsByIdentity){ |
467 |
_66=this._itemsByIdentity[_65.id];
|
468 |
}else{
|
469 |
_66=this._arrayOfAllItems[_65.id];
|
470 |
} |
471 |
this._addReferenceToMap(_66,_63,_65.attr);
|
472 |
},this);
|
473 |
delete _63["backupRefs_"+this._reverseRefMap]; |
474 |
} |
475 |
} |
476 |
for(_60 in this._pending._newItems){ |
477 |
var _67=this._pending._newItems[_60]; |
478 |
_67[this._storeRefPropName]=null; |
479 |
this._arrayOfAllItems[_67[this._itemNumPropName]]=null; |
480 |
if(_67[this._rootItemPropName]){ |
481 |
this._removeArrayElement(this._arrayOfTopLevelItems,_67); |
482 |
} |
483 |
if(this._itemsByIdentity){ |
484 |
delete this._itemsByIdentity[_60]; |
485 |
} |
486 |
} |
487 |
this._pending={_newItems:{},_modifiedItems:{},_deletedItems:{}}; |
488 |
return true; |
489 |
},isDirty:function(_68){ |
490 |
if(_68){
|
491 |
var _69=this.getIdentity(_68); |
492 |
return new Boolean(this._pending._newItems[_69]||this._pending._modifiedItems[_69]||this._pending._deletedItems[_69]).valueOf(); |
493 |
}else{
|
494 |
if(!this._isEmpty(this._pending._newItems)||!this._isEmpty(this._pending._modifiedItems)||!this._isEmpty(this._pending._deletedItems)){ |
495 |
return true; |
496 |
} |
497 |
return false; |
498 |
} |
499 |
},onSet:function(_6a,_6b,_6c,_6d){ |
500 |
},onNew:function(_6e,_6f){ |
501 |
},onDelete:function(_70){ |
502 |
},close:function(_71){ |
503 |
if(this.clearOnClose){ |
504 |
if(!this.isDirty()){ |
505 |
this.inherited(arguments); |
506 |
}else{
|
507 |
throw new Error("dojox.data.AndOrWriteStore: There are unsaved changes present in the store. Please save or revert the changes before invoking close."); |
508 |
} |
509 |
} |
510 |
}}); |
511 |
} |