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