Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / data / AndOrReadStore.js

History | View | Annotate | Download (15 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.AndOrReadStore"]){
9
dojo._hasResource["dojox.data.AndOrReadStore"]=true;
10
dojo.provide("dojox.data.AndOrReadStore");
11
dojo.require("dojo.data.util.filter");
12
dojo.require("dojo.data.util.simpleFetch");
13
dojo.require("dojo.date.stamp");
14
dojo.declare("dojox.data.AndOrReadStore",null,{constructor:function(_1){
15
this._arrayOfAllItems=[];
16
this._arrayOfTopLevelItems=[];
17
this._loadFinished=false;
18
this._jsonFileUrl=_1.url;
19
this._ccUrl=_1.url;
20
this.url=_1.url;
21
this._jsonData=_1.data;
22
this.data=null;
23
this._datatypeMap=_1.typeMap||{};
24
if(!this._datatypeMap["Date"]){
25
this._datatypeMap["Date"]={type:Date,deserialize:function(_2){
26
return dojo.date.stamp.fromISOString(_2);
27
}};
28
}
29
this._features={"dojo.data.api.Read":true,"dojo.data.api.Identity":true};
30
this._itemsByIdentity=null;
31
this._storeRefPropName="_S";
32
this._itemNumPropName="_0";
33
this._rootItemPropName="_RI";
34
this._reverseRefMap="_RRM";
35
this._loadInProgress=false;
36
this._queuedFetches=[];
37
if(_1.urlPreventCache!==undefined){
38
this.urlPreventCache=_1.urlPreventCache?true:false;
39
}
40
if(_1.hierarchical!==undefined){
41
this.hierarchical=_1.hierarchical?true:false;
42
}
43
if(_1.clearOnClose){
44
this.clearOnClose=true;
45
}
46
},url:"",_ccUrl:"",data:null,typeMap:null,clearOnClose:false,urlPreventCache:false,hierarchical:true,_assertIsItem:function(_3){
47
if(!this.isItem(_3)){
48
throw new Error("dojox.data.AndOrReadStore: Invalid item argument.");
49
}
50
},_assertIsAttribute:function(_4){
51
if(typeof _4!=="string"){
52
throw new Error("dojox.data.AndOrReadStore: Invalid attribute argument.");
53
}
54
},getValue:function(_5,_6,_7){
55
var _8=this.getValues(_5,_6);
56
return (_8.length>0)?_8[0]:_7;
57
},getValues:function(_9,_a){
58
this._assertIsItem(_9);
59
this._assertIsAttribute(_a);
60
var _b=_9[_a]||[];
61
return _b.slice(0,_b.length);
62
},getAttributes:function(_c){
63
this._assertIsItem(_c);
64
var _d=[];
65
for(var _e in _c){
66
if((_e!==this._storeRefPropName)&&(_e!==this._itemNumPropName)&&(_e!==this._rootItemPropName)&&(_e!==this._reverseRefMap)){
67
_d.push(_e);
68
}
69
}
70
return _d;
71
},hasAttribute:function(_f,_10){
72
this._assertIsItem(_f);
73
this._assertIsAttribute(_10);
74
return (_10 in _f);
75
},containsValue:function(_11,_12,_13){
76
var _14=undefined;
77
if(typeof _13==="string"){
78
_14=dojo.data.util.filter.patternToRegExp(_13,false);
79
}
80
return this._containsValue(_11,_12,_13,_14);
81
},_containsValue:function(_15,_16,_17,_18){
82
return dojo.some(this.getValues(_15,_16),function(_19){
83
if(_19!==null&&!dojo.isObject(_19)&&_18){
84
if(_19.toString().match(_18)){
85
return true;
86
}
87
}else{
88
if(_17===_19){
89
return true;
90
}
91
}
92
});
93
},isItem:function(_1a){
94
if(_1a&&_1a[this._storeRefPropName]===this){
95
if(this._arrayOfAllItems[_1a[this._itemNumPropName]]===_1a){
96
return true;
97
}
98
}
99
return false;
100
},isItemLoaded:function(_1b){
101
return this.isItem(_1b);
102
},loadItem:function(_1c){
103
this._assertIsItem(_1c.item);
104
},getFeatures:function(){
105
return this._features;
106
},getLabel:function(_1d){
107
if(this._labelAttr&&this.isItem(_1d)){
108
return this.getValue(_1d,this._labelAttr);
109
}
110
return undefined;
111
},getLabelAttributes:function(_1e){
112
if(this._labelAttr){
113
return [this._labelAttr];
114
}
115
return null;
116
},_fetchItems:function(_1f,_20,_21){
117
var _22=this;
118
var _23=function(_24,_25){
119
var _26=[];
120
if(_24.query){
121
var _27=dojo.fromJson(dojo.toJson(_24.query));
122
if(typeof _27=="object"){
123
var _28=0;
124
var p;
125
for(p in _27){
126
_28++;
127
}
128
if(_28>1&&_27.complexQuery){
129
var cq=_27.complexQuery;
130
var _29=false;
131
for(p in _27){
132
if(p!=="complexQuery"){
133
if(!_29){
134
cq="( "+cq+" )";
135
_29=true;
136
}
137
var v=_24.query[p];
138
if(dojo.isString(v)){
139
v="'"+v+"'";
140
}
141
cq+=" AND "+p+":"+v;
142
delete _27[p];
143
}
144
}
145
_27.complexQuery=cq;
146
}
147
}
148
var _2a=_24.queryOptions?_24.queryOptions.ignoreCase:false;
149
if(typeof _27!="string"){
150
_27=dojo.toJson(_27);
151
_27=_27.replace(/\\\\/g,"\\");
152
}
153
_27=_27.replace(/\\"/g,"\"");
154
var _2b=dojo.trim(_27.replace(/{|}/g,""));
155
var _2c,i;
156
if(_2b.match(/"? *complexQuery *"?:/)){
157
_2b=dojo.trim(_2b.replace(/"?\s*complexQuery\s*"?:/,""));
158
var _2d=["'","\""];
159
var _2e,_2f;
160
var _30=false;
161
for(i=0;i<_2d.length;i++){
162
_2e=_2b.indexOf(_2d[i]);
163
_2c=_2b.indexOf(_2d[i],1);
164
_2f=_2b.indexOf(":",1);
165
if(_2e===0&&_2c!=-1&&_2f<_2c){
166
_30=true;
167
break;
168
}
169
}
170
if(_30){
171
_2b=_2b.replace(/^\"|^\'|\"$|\'$/g,"");
172
}
173
}
174
var _31=_2b;
175
var _32=/^,|^NOT |^AND |^OR |^\(|^\)|^!|^&&|^\|\|/i;
176
var _33="";
177
var op="";
178
var val="";
179
var pos=-1;
180
var err=false;
181
var key="";
182
var _34="";
183
var tok="";
184
_2c=-1;
185
for(i=0;i<_25.length;++i){
186
var _35=true;
187
var _36=_25[i];
188
if(_36===null){
189
_35=false;
190
}else{
191
_2b=_31;
192
_33="";
193
while(_2b.length>0&&!err){
194
op=_2b.match(_32);
195
while(op&&!err){
196
_2b=dojo.trim(_2b.replace(op[0],""));
197
op=dojo.trim(op[0]).toUpperCase();
198
op=op=="NOT"?"!":op=="AND"||op==","?"&&":op=="OR"?"||":op;
199
op=" "+op+" ";
200
_33+=op;
201
op=_2b.match(_32);
202
}
203
if(_2b.length>0){
204
pos=_2b.indexOf(":");
205
if(pos==-1){
206
err=true;
207
break;
208
}else{
209
key=dojo.trim(_2b.substring(0,pos).replace(/\"|\'/g,""));
210
_2b=dojo.trim(_2b.substring(pos+1));
211
tok=_2b.match(/^\'|^\"/);
212
if(tok){
213
tok=tok[0];
214
pos=_2b.indexOf(tok);
215
_2c=_2b.indexOf(tok,pos+1);
216
if(_2c==-1){
217
err=true;
218
break;
219
}
220
_34=_2b.substring(pos+1,_2c);
221
if(_2c==_2b.length-1){
222
_2b="";
223
}else{
224
_2b=dojo.trim(_2b.substring(_2c+1));
225
}
226
_33+=_22._containsValue(_36,key,_34,dojo.data.util.filter.patternToRegExp(_34,_2a));
227
}else{
228
tok=_2b.match(/\s|\)|,/);
229
if(tok){
230
var _37=new Array(tok.length);
231
for(var j=0;j<tok.length;j++){
232
_37[j]=_2b.indexOf(tok[j]);
233
}
234
pos=_37[0];
235
if(_37.length>1){
236
for(var j=1;j<_37.length;j++){
237
pos=Math.min(pos,_37[j]);
238
}
239
}
240
_34=dojo.trim(_2b.substring(0,pos));
241
_2b=dojo.trim(_2b.substring(pos));
242
}else{
243
_34=dojo.trim(_2b);
244
_2b="";
245
}
246
_33+=_22._containsValue(_36,key,_34,dojo.data.util.filter.patternToRegExp(_34,_2a));
247
}
248
}
249
}
250
}
251
_35=eval(_33);
252
}
253
if(_35){
254
_26.push(_36);
255
}
256
}
257
if(err){
258
_26=[];
259
}
260
_20(_26,_24);
261
}else{
262
for(var i=0;i<_25.length;++i){
263
var _38=_25[i];
264
if(_38!==null){
265
_26.push(_38);
266
}
267
}
268
_20(_26,_24);
269
}
270
};
271
if(this._loadFinished){
272
_23(_1f,this._getItemsArray(_1f.queryOptions));
273
}else{
274
if(this._jsonFileUrl!==this._ccUrl){
275
dojo.deprecated("dojox.data.AndOrReadStore: ","To change the url, set the url property of the store,"+" not _jsonFileUrl.  _jsonFileUrl support will be removed in 2.0");
276
this._ccUrl=this._jsonFileUrl;
277
this.url=this._jsonFileUrl;
278
}else{
279
if(this.url!==this._ccUrl){
280
this._jsonFileUrl=this.url;
281
this._ccUrl=this.url;
282
}
283
}
284
if(this.data!=null&&this._jsonData==null){
285
this._jsonData=this.data;
286
this.data=null;
287
}
288
if(this._jsonFileUrl){
289
if(this._loadInProgress){
290
this._queuedFetches.push({args:_1f,filter:_23});
291
}else{
292
this._loadInProgress=true;
293
var _39={url:_22._jsonFileUrl,handleAs:"json-comment-optional",preventCache:this.urlPreventCache};
294
var _3a=dojo.xhrGet(_39);
295
_3a.addCallback(function(_3b){
296
try{
297
_22._getItemsFromLoadedData(_3b);
298
_22._loadFinished=true;
299
_22._loadInProgress=false;
300
_23(_1f,_22._getItemsArray(_1f.queryOptions));
301
_22._handleQueuedFetches();
302
}
303
catch(e){
304
_22._loadFinished=true;
305
_22._loadInProgress=false;
306
_21(e,_1f);
307
}
308
});
309
_3a.addErrback(function(_3c){
310
_22._loadInProgress=false;
311
_21(_3c,_1f);
312
});
313
var _3d=null;
314
if(_1f.abort){
315
_3d=_1f.abort;
316
}
317
_1f.abort=function(){
318
var df=_3a;
319
if(df&&df.fired===-1){
320
df.cancel();
321
df=null;
322
}
323
if(_3d){
324
_3d.call(_1f);
325
}
326
};
327
}
328
}else{
329
if(this._jsonData){
330
try{
331
this._loadFinished=true;
332
this._getItemsFromLoadedData(this._jsonData);
333
this._jsonData=null;
334
_23(_1f,this._getItemsArray(_1f.queryOptions));
335
}
336
catch(e){
337
_21(e,_1f);
338
}
339
}else{
340
_21(new Error("dojox.data.AndOrReadStore: No JSON source data was provided as either URL or a nested Javascript object."),_1f);
341
}
342
}
343
}
344
},_handleQueuedFetches:function(){
345
if(this._queuedFetches.length>0){
346
for(var i=0;i<this._queuedFetches.length;i++){
347
var _3e=this._queuedFetches[i];
348
var _3f=_3e.args;
349
var _40=_3e.filter;
350
if(_40){
351
_40(_3f,this._getItemsArray(_3f.queryOptions));
352
}else{
353
this.fetchItemByIdentity(_3f);
354
}
355
}
356
this._queuedFetches=[];
357
}
358
},_getItemsArray:function(_41){
359
if(_41&&_41.deep){
360
return this._arrayOfAllItems;
361
}
362
return this._arrayOfTopLevelItems;
363
},close:function(_42){
364
if(this.clearOnClose&&this._loadFinished&&!this._loadInProgress){
365
if(((this._jsonFileUrl==""||this._jsonFileUrl==null)&&(this.url==""||this.url==null))&&this.data==null){
366
}
367
this._arrayOfAllItems=[];
368
this._arrayOfTopLevelItems=[];
369
this._loadFinished=false;
370
this._itemsByIdentity=null;
371
this._loadInProgress=false;
372
this._queuedFetches=[];
373
}
374
},_getItemsFromLoadedData:function(_43){
375
var _44=this;
376
function _45(_46){
377
var _47=((_46!==null)&&(typeof _46==="object")&&(!dojo.isArray(_46))&&(!dojo.isFunction(_46))&&(_46.constructor==Object)&&(typeof _46._reference==="undefined")&&(typeof _46._type==="undefined")&&(typeof _46._value==="undefined")&&_44.hierarchical);
378
return _47;
379
};
380
function _48(_49){
381
_44._arrayOfAllItems.push(_49);
382
for(var _4a in _49){
383
var _4b=_49[_4a];
384
if(_4b){
385
if(dojo.isArray(_4b)){
386
var _4c=_4b;
387
for(var k=0;k<_4c.length;++k){
388
var _4d=_4c[k];
389
if(_45(_4d)){
390
_48(_4d);
391
}
392
}
393
}else{
394
if(_45(_4b)){
395
_48(_4b);
396
}
397
}
398
}
399
}
400
};
401
this._labelAttr=_43.label;
402
var i;
403
var _4e;
404
this._arrayOfAllItems=[];
405
this._arrayOfTopLevelItems=_43.items;
406
for(i=0;i<this._arrayOfTopLevelItems.length;++i){
407
_4e=this._arrayOfTopLevelItems[i];
408
_48(_4e);
409
_4e[this._rootItemPropName]=true;
410
}
411
var _4f={};
412
var key;
413
for(i=0;i<this._arrayOfAllItems.length;++i){
414
_4e=this._arrayOfAllItems[i];
415
for(key in _4e){
416
if(key!==this._rootItemPropName){
417
var _50=_4e[key];
418
if(_50!==null){
419
if(!dojo.isArray(_50)){
420
_4e[key]=[_50];
421
}
422
}else{
423
_4e[key]=[null];
424
}
425
}
426
_4f[key]=key;
427
}
428
}
429
while(_4f[this._storeRefPropName]){
430
this._storeRefPropName+="_";
431
}
432
while(_4f[this._itemNumPropName]){
433
this._itemNumPropName+="_";
434
}
435
while(_4f[this._reverseRefMap]){
436
this._reverseRefMap+="_";
437
}
438
var _51;
439
var _52=_43.identifier;
440
if(_52){
441
this._itemsByIdentity={};
442
this._features["dojo.data.api.Identity"]=_52;
443
for(i=0;i<this._arrayOfAllItems.length;++i){
444
_4e=this._arrayOfAllItems[i];
445
_51=_4e[_52];
446
var _53=_51[0];
447
if(!this._itemsByIdentity[_53]){
448
this._itemsByIdentity[_53]=_4e;
449
}else{
450
if(this._jsonFileUrl){
451
throw new Error("dojox.data.AndOrReadStore:  The json data as specified by: ["+this._jsonFileUrl+"] is malformed.  Items within the list have identifier: ["+_52+"].  Value collided: ["+_53+"]");
452
}else{
453
if(this._jsonData){
454
throw new Error("dojox.data.AndOrReadStore:  The json data provided by the creation arguments is malformed.  Items within the list have identifier: ["+_52+"].  Value collided: ["+_53+"]");
455
}
456
}
457
}
458
}
459
}else{
460
this._features["dojo.data.api.Identity"]=Number;
461
}
462
for(i=0;i<this._arrayOfAllItems.length;++i){
463
_4e=this._arrayOfAllItems[i];
464
_4e[this._storeRefPropName]=this;
465
_4e[this._itemNumPropName]=i;
466
}
467
for(i=0;i<this._arrayOfAllItems.length;++i){
468
_4e=this._arrayOfAllItems[i];
469
for(key in _4e){
470
_51=_4e[key];
471
for(var j=0;j<_51.length;++j){
472
_50=_51[j];
473
if(_50!==null&&typeof _50=="object"){
474
if(("_type" in _50)&&("_value" in _50)){
475
var _54=_50._type;
476
var _55=this._datatypeMap[_54];
477
if(!_55){
478
throw new Error("dojox.data.AndOrReadStore: in the typeMap constructor arg, no object class was specified for the datatype '"+_54+"'");
479
}else{
480
if(dojo.isFunction(_55)){
481
_51[j]=new _55(_50._value);
482
}else{
483
if(dojo.isFunction(_55.deserialize)){
484
_51[j]=_55.deserialize(_50._value);
485
}else{
486
throw new Error("dojox.data.AndOrReadStore: Value provided in typeMap was neither a constructor, nor a an object with a deserialize function");
487
}
488
}
489
}
490
}
491
if(_50._reference){
492
var _56=_50._reference;
493
if(!dojo.isObject(_56)){
494
_51[j]=this._getItemByIdentity(_56);
495
}else{
496
for(var k=0;k<this._arrayOfAllItems.length;++k){
497
var _57=this._arrayOfAllItems[k];
498
var _58=true;
499
for(var _59 in _56){
500
if(_57[_59]!=_56[_59]){
501
_58=false;
502
}
503
}
504
if(_58){
505
_51[j]=_57;
506
}
507
}
508
}
509
if(this.referenceIntegrity){
510
var _5a=_51[j];
511
if(this.isItem(_5a)){
512
this._addReferenceToMap(_5a,_4e,key);
513
}
514
}
515
}else{
516
if(this.isItem(_50)){
517
if(this.referenceIntegrity){
518
this._addReferenceToMap(_50,_4e,key);
519
}
520
}
521
}
522
}
523
}
524
}
525
}
526
},_addReferenceToMap:function(_5b,_5c,_5d){
527
},getIdentity:function(_5e){
528
var _5f=this._features["dojo.data.api.Identity"];
529
if(_5f===Number){
530
return _5e[this._itemNumPropName];
531
}else{
532
var _60=_5e[_5f];
533
if(_60){
534
return _60[0];
535
}
536
}
537
return null;
538
},fetchItemByIdentity:function(_61){
539
if(!this._loadFinished){
540
var _62=this;
541
if(this._jsonFileUrl!==this._ccUrl){
542
dojo.deprecated("dojox.data.AndOrReadStore: ","To change the url, set the url property of the store,"+" not _jsonFileUrl.  _jsonFileUrl support will be removed in 2.0");
543
this._ccUrl=this._jsonFileUrl;
544
this.url=this._jsonFileUrl;
545
}else{
546
if(this.url!==this._ccUrl){
547
this._jsonFileUrl=this.url;
548
this._ccUrl=this.url;
549
}
550
}
551
if(this.data!=null&&this._jsonData==null){
552
this._jsonData=this.data;
553
this.data=null;
554
}
555
if(this._jsonFileUrl){
556
if(this._loadInProgress){
557
this._queuedFetches.push({args:_61});
558
}else{
559
this._loadInProgress=true;
560
var _63={url:_62._jsonFileUrl,handleAs:"json-comment-optional",preventCache:this.urlPreventCache};
561
var _64=dojo.xhrGet(_63);
562
_64.addCallback(function(_65){
563
var _66=_61.scope?_61.scope:dojo.global;
564
try{
565
_62._getItemsFromLoadedData(_65);
566
_62._loadFinished=true;
567
_62._loadInProgress=false;
568
var _67=_62._getItemByIdentity(_61.identity);
569
if(_61.onItem){
570
_61.onItem.call(_66,_67);
571
}
572
_62._handleQueuedFetches();
573
}
574
catch(error){
575
_62._loadInProgress=false;
576
if(_61.onError){
577
_61.onError.call(_66,error);
578
}
579
}
580
});
581
_64.addErrback(function(_68){
582
_62._loadInProgress=false;
583
if(_61.onError){
584
var _69=_61.scope?_61.scope:dojo.global;
585
_61.onError.call(_69,_68);
586
}
587
});
588
}
589
}else{
590
if(this._jsonData){
591
_62._getItemsFromLoadedData(_62._jsonData);
592
_62._jsonData=null;
593
_62._loadFinished=true;
594
var _6a=_62._getItemByIdentity(_61.identity);
595
if(_61.onItem){
596
var _6b=_61.scope?_61.scope:dojo.global;
597
_61.onItem.call(_6b,_6a);
598
}
599
}
600
}
601
}else{
602
var _6a=this._getItemByIdentity(_61.identity);
603
if(_61.onItem){
604
var _6b=_61.scope?_61.scope:dojo.global;
605
_61.onItem.call(_6b,_6a);
606
}
607
}
608
},_getItemByIdentity:function(_6c){
609
var _6d=null;
610
if(this._itemsByIdentity){
611
_6d=this._itemsByIdentity[_6c];
612
}else{
613
_6d=this._arrayOfAllItems[_6c];
614
}
615
if(_6d===undefined){
616
_6d=null;
617
}
618
return _6d;
619
},getIdentityAttributes:function(_6e){
620
var _6f=this._features["dojo.data.api.Identity"];
621
if(_6f===Number){
622
return null;
623
}else{
624
return [_6f];
625
}
626
},_forceLoad:function(){
627
var _70=this;
628
if(this._jsonFileUrl!==this._ccUrl){
629
dojo.deprecated("dojox.data.AndOrReadStore: ","To change the url, set the url property of the store,"+" not _jsonFileUrl.  _jsonFileUrl support will be removed in 2.0");
630
this._ccUrl=this._jsonFileUrl;
631
this.url=this._jsonFileUrl;
632
}else{
633
if(this.url!==this._ccUrl){
634
this._jsonFileUrl=this.url;
635
this._ccUrl=this.url;
636
}
637
}
638
if(this.data!=null&&this._jsonData==null){
639
this._jsonData=this.data;
640
this.data=null;
641
}
642
if(this._jsonFileUrl){
643
var _71={url:_70._jsonFileUrl,handleAs:"json-comment-optional",preventCache:this.urlPreventCache,sync:true};
644
var _72=dojo.xhrGet(_71);
645
_72.addCallback(function(_73){
646
try{
647
if(_70._loadInProgress!==true&&!_70._loadFinished){
648
_70._getItemsFromLoadedData(_73);
649
_70._loadFinished=true;
650
}else{
651
if(_70._loadInProgress){
652
throw new Error("dojox.data.AndOrReadStore:  Unable to perform a synchronous load, an async load is in progress.");
653
}
654
}
655
}
656
catch(e){
657
throw e;
658
}
659
});
660
_72.addErrback(function(_74){
661
throw _74;
662
});
663
}else{
664
if(this._jsonData){
665
_70._getItemsFromLoadedData(_70._jsonData);
666
_70._jsonData=null;
667
_70._loadFinished=true;
668
}
669
}
670
}});
671
dojo.extend(dojox.data.AndOrReadStore,dojo.data.util.simpleFetch);
672
}