Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / data / CsvStore.js @ 11

History | View | Annotate | Download (8.72 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.CsvStore"]){
9
dojo._hasResource["dojox.data.CsvStore"]=true;
10
dojo.provide("dojox.data.CsvStore");
11
dojo.require("dojo.data.util.filter");
12
dojo.require("dojo.data.util.simpleFetch");
13
dojo.declare("dojox.data.CsvStore",null,{constructor:function(_1){
14
this._attributes=[];
15
this._attributeIndexes={};
16
this._dataArray=[];
17
this._arrayOfAllItems=[];
18
this._loadFinished=false;
19
if(_1.url){
20
this.url=_1.url;
21
}
22
this._csvData=_1.data;
23
if(_1.label){
24
this.label=_1.label;
25
}else{
26
if(this.label===""){
27
this.label=undefined;
28
}
29
}
30
this._storeProp="_csvStore";
31
this._idProp="_csvId";
32
this._features={"dojo.data.api.Read":true,"dojo.data.api.Identity":true};
33
this._loadInProgress=false;
34
this._queuedFetches=[];
35
this.identifier=_1.identifier;
36
if(this.identifier===""){
37
delete this.identifier;
38
}else{
39
this._idMap={};
40
}
41
if("separator" in _1){
42
this.separator=_1.separator;
43
}
44
if("urlPreventCache" in _1){
45
this.urlPreventCache=_1.urlPreventCache?true:false;
46
}
47
},url:"",label:"",identifier:"",separator:",",urlPreventCache:false,_assertIsItem:function(_2){
48
if(!this.isItem(_2)){
49
throw new Error(this.declaredClass+": a function was passed an item argument that was not an item");
50
}
51
},_getIndex:function(_3){
52
var _4=this.getIdentity(_3);
53
if(this.identifier){
54
_4=this._idMap[_4];
55
}
56
return _4;
57
},getValue:function(_5,_6,_7){
58
this._assertIsItem(_5);
59
var _8=_7;
60
if(typeof _6==="string"){
61
var ai=this._attributeIndexes[_6];
62
if(ai!=null){
63
var _9=this._dataArray[this._getIndex(_5)];
64
_8=_9[ai]||_7;
65
}
66
}else{
67
throw new Error(this.declaredClass+": a function was passed an attribute argument that was not a string");
68
}
69
return _8;
70
},getValues:function(_a,_b){
71
var _c=this.getValue(_a,_b);
72
return (_c?[_c]:[]);
73
},getAttributes:function(_d){
74
this._assertIsItem(_d);
75
var _e=[];
76
var _f=this._dataArray[this._getIndex(_d)];
77
for(var i=0;i<_f.length;i++){
78
if(_f[i]!==""){
79
_e.push(this._attributes[i]);
80
}
81
}
82
return _e;
83
},hasAttribute:function(_10,_11){
84
this._assertIsItem(_10);
85
if(typeof _11==="string"){
86
var _12=this._attributeIndexes[_11];
87
var _13=this._dataArray[this._getIndex(_10)];
88
return (typeof _12!=="undefined"&&_12<_13.length&&_13[_12]!=="");
89
}else{
90
throw new Error(this.declaredClass+": a function was passed an attribute argument that was not a string");
91
}
92
},containsValue:function(_14,_15,_16){
93
var _17=undefined;
94
if(typeof _16==="string"){
95
_17=dojo.data.util.filter.patternToRegExp(_16,false);
96
}
97
return this._containsValue(_14,_15,_16,_17);
98
},_containsValue:function(_18,_19,_1a,_1b){
99
var _1c=this.getValues(_18,_19);
100
for(var i=0;i<_1c.length;++i){
101
var _1d=_1c[i];
102
if(typeof _1d==="string"&&_1b){
103
return (_1d.match(_1b)!==null);
104
}else{
105
if(_1a===_1d){
106
return true;
107
}
108
}
109
}
110
return false;
111
},isItem:function(_1e){
112
if(_1e&&_1e[this._storeProp]===this){
113
var _1f=_1e[this._idProp];
114
if(this.identifier){
115
var _20=this._dataArray[this._idMap[_1f]];
116
if(_20){
117
return true;
118
}
119
}else{
120
if(_1f>=0&&_1f<this._dataArray.length){
121
return true;
122
}
123
}
124
}
125
return false;
126
},isItemLoaded:function(_21){
127
return this.isItem(_21);
128
},loadItem:function(_22){
129
},getFeatures:function(){
130
return this._features;
131
},getLabel:function(_23){
132
if(this.label&&this.isItem(_23)){
133
return this.getValue(_23,this.label);
134
}
135
return undefined;
136
},getLabelAttributes:function(_24){
137
if(this.label){
138
return [this.label];
139
}
140
return null;
141
},_fetchItems:function(_25,_26,_27){
142
var _28=this;
143
var _29=function(_2a,_2b){
144
var _2c=null;
145
if(_2a.query){
146
var key,_2d;
147
_2c=[];
148
var _2e=_2a.queryOptions?_2a.queryOptions.ignoreCase:false;
149
var _2f={};
150
for(key in _2a.query){
151
_2d=_2a.query[key];
152
if(typeof _2d==="string"){
153
_2f[key]=dojo.data.util.filter.patternToRegExp(_2d,_2e);
154
}
155
}
156
for(var i=0;i<_2b.length;++i){
157
var _30=true;
158
var _31=_2b[i];
159
for(key in _2a.query){
160
_2d=_2a.query[key];
161
if(!_28._containsValue(_31,key,_2d,_2f[key])){
162
_30=false;
163
}
164
}
165
if(_30){
166
_2c.push(_31);
167
}
168
}
169
}else{
170
_2c=_2b.slice(0,_2b.length);
171
}
172
_26(_2c,_2a);
173
};
174
if(this._loadFinished){
175
_29(_25,this._arrayOfAllItems);
176
}else{
177
if(this.url!==""){
178
if(this._loadInProgress){
179
this._queuedFetches.push({args:_25,filter:_29});
180
}else{
181
this._loadInProgress=true;
182
var _32={url:_28.url,handleAs:"text",preventCache:_28.urlPreventCache};
183
var _33=dojo.xhrGet(_32);
184
_33.addCallback(function(_34){
185
try{
186
_28._processData(_34);
187
_29(_25,_28._arrayOfAllItems);
188
_28._handleQueuedFetches();
189
}
190
catch(e){
191
_27(e,_25);
192
}
193
});
194
_33.addErrback(function(_35){
195
_28._loadInProgress=false;
196
if(_27){
197
_27(_35,_25);
198
}else{
199
throw _35;
200
}
201
});
202
var _36=null;
203
if(_25.abort){
204
_36=_25.abort;
205
}
206
_25.abort=function(){
207
var df=_33;
208
if(df&&df.fired===-1){
209
df.cancel();
210
df=null;
211
}
212
if(_36){
213
_36.call(_25);
214
}
215
};
216
}
217
}else{
218
if(this._csvData){
219
try{
220
this._processData(this._csvData);
221
this._csvData=null;
222
_29(_25,this._arrayOfAllItems);
223
}
224
catch(e){
225
_27(e,_25);
226
}
227
}else{
228
var _37=new Error(this.declaredClass+": No CSV source data was provided as either URL or String data input.");
229
if(_27){
230
_27(_37,_25);
231
}else{
232
throw _37;
233
}
234
}
235
}
236
}
237
},close:function(_38){
238
},_getArrayOfArraysFromCsvFileContents:function(_39){
239
if(dojo.isString(_39)){
240
var _3a=new RegExp("^\\s+","g");
241
var _3b=new RegExp("\\s+$","g");
242
var _3c=new RegExp("\"\"","g");
243
var _3d=[];
244
var i;
245
var _3e=this._splitLines(_39);
246
for(i=0;i<_3e.length;++i){
247
var _3f=_3e[i];
248
if(_3f.length>0){
249
var _40=_3f.split(this.separator);
250
var j=0;
251
while(j<_40.length){
252
var _41=_40[j];
253
var _42=_41.replace(_3a,"");
254
var _43=_42.replace(_3b,"");
255
var _44=_43.charAt(0);
256
var _45=_43.charAt(_43.length-1);
257
var _46=_43.charAt(_43.length-2);
258
var _47=_43.charAt(_43.length-3);
259
if(_43.length===2&&_43=="\"\""){
260
_40[j]="";
261
}else{
262
if((_44=="\"")&&((_45!="\"")||((_45=="\"")&&(_46=="\"")&&(_47!="\"")))){
263
if(j+1===_40.length){
264
return;
265
}
266
var _48=_40[j+1];
267
_40[j]=_42+this.separator+_48;
268
_40.splice(j+1,1);
269
}else{
270
if((_44=="\"")&&(_45=="\"")){
271
_43=_43.slice(1,(_43.length-1));
272
_43=_43.replace(_3c,"\"");
273
}
274
_40[j]=_43;
275
j+=1;
276
}
277
}
278
}
279
_3d.push(_40);
280
}
281
}
282
this._attributes=_3d.shift();
283
for(i=0;i<this._attributes.length;i++){
284
this._attributeIndexes[this._attributes[i]]=i;
285
}
286
this._dataArray=_3d;
287
}
288
},_splitLines:function(_49){
289
var _4a=[];
290
var i;
291
var _4b="";
292
var _4c=false;
293
for(i=0;i<_49.length;i++){
294
var c=_49.charAt(i);
295
switch(c){
296
case "\"":
297
_4c=!_4c;
298
_4b+=c;
299
break;
300
case "\r":
301
if(_4c){
302
_4b+=c;
303
}else{
304
_4a.push(_4b);
305
_4b="";
306
if(i<(_49.length-1)&&_49.charAt(i+1)=="\n"){
307
i++;
308
}
309
}
310
break;
311
case "\n":
312
if(_4c){
313
_4b+=c;
314
}else{
315
_4a.push(_4b);
316
_4b="";
317
}
318
break;
319
default:
320
_4b+=c;
321
}
322
}
323
if(_4b!==""){
324
_4a.push(_4b);
325
}
326
return _4a;
327
},_processData:function(_4d){
328
this._getArrayOfArraysFromCsvFileContents(_4d);
329
this._arrayOfAllItems=[];
330
if(this.identifier){
331
if(this._attributeIndexes[this.identifier]===undefined){
332
throw new Error(this.declaredClass+": Identity specified is not a column header in the data set.");
333
}
334
}
335
for(var i=0;i<this._dataArray.length;i++){
336
var id=i;
337
if(this.identifier){
338
var _4e=this._dataArray[i];
339
id=_4e[this._attributeIndexes[this.identifier]];
340
this._idMap[id]=i;
341
}
342
this._arrayOfAllItems.push(this._createItemFromIdentity(id));
343
}
344
this._loadFinished=true;
345
this._loadInProgress=false;
346
},_createItemFromIdentity:function(_4f){
347
var _50={};
348
_50[this._storeProp]=this;
349
_50[this._idProp]=_4f;
350
return _50;
351
},getIdentity:function(_51){
352
if(this.isItem(_51)){
353
return _51[this._idProp];
354
}
355
return null;
356
},fetchItemByIdentity:function(_52){
357
var _53;
358
var _54=_52.scope?_52.scope:dojo.global;
359
if(!this._loadFinished){
360
var _55=this;
361
if(this.url!==""){
362
if(this._loadInProgress){
363
this._queuedFetches.push({args:_52});
364
}else{
365
this._loadInProgress=true;
366
var _56={url:_55.url,handleAs:"text"};
367
var _57=dojo.xhrGet(_56);
368
_57.addCallback(function(_58){
369
try{
370
_55._processData(_58);
371
var _59=_55._createItemFromIdentity(_52.identity);
372
if(!_55.isItem(_59)){
373
_59=null;
374
}
375
if(_52.onItem){
376
_52.onItem.call(_54,_59);
377
}
378
_55._handleQueuedFetches();
379
}
380
catch(error){
381
if(_52.onError){
382
_52.onError.call(_54,error);
383
}
384
}
385
});
386
_57.addErrback(function(_5a){
387
this._loadInProgress=false;
388
if(_52.onError){
389
_52.onError.call(_54,_5a);
390
}
391
});
392
}
393
}else{
394
if(this._csvData){
395
try{
396
_55._processData(_55._csvData);
397
_55._csvData=null;
398
_53=_55._createItemFromIdentity(_52.identity);
399
if(!_55.isItem(_53)){
400
_53=null;
401
}
402
if(_52.onItem){
403
_52.onItem.call(_54,_53);
404
}
405
}
406
catch(e){
407
if(_52.onError){
408
_52.onError.call(_54,e);
409
}
410
}
411
}
412
}
413
}else{
414
_53=this._createItemFromIdentity(_52.identity);
415
if(!this.isItem(_53)){
416
_53=null;
417
}
418
if(_52.onItem){
419
_52.onItem.call(_54,_53);
420
}
421
}
422
},getIdentityAttributes:function(_5b){
423
if(this.identifier){
424
return [this.identifier];
425
}else{
426
return null;
427
}
428
},_handleQueuedFetches:function(){
429
if(this._queuedFetches.length>0){
430
for(var i=0;i<this._queuedFetches.length;i++){
431
var _5c=this._queuedFetches[i];
432
var _5d=_5c.filter;
433
var _5e=_5c.args;
434
if(_5d){
435
_5d(_5e,this._arrayOfAllItems);
436
}else{
437
this.fetchItemByIdentity(_5c.args);
438
}
439
}
440
this._queuedFetches=[];
441
}
442
}});
443
dojo.extend(dojox.data.CsvStore,dojo.data.util.simpleFetch);
444
}