root / trunk / web / dojo / dojox / data / AppStore.js @ 13
History | View | Annotate | Download (11.5 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.AppStore"]){ |
9 |
dojo._hasResource["dojox.data.AppStore"]=true; |
10 |
dojo.provide("dojox.data.AppStore");
|
11 |
dojo.require("dojox.atom.io.Connection");
|
12 |
dojo.require("dojo.data.util.simpleFetch");
|
13 |
dojo.require("dojo.data.util.filter");
|
14 |
dojo.experimental("dojox.data.AppStore");
|
15 |
dojo.declare("dojox.data.AppStore",null,{url:"",urlPreventCache:false,xmethod:false,_atomIO:null,_feed:null,_requests:null,_processing:null,_updates:null,_adds:null,_deletes:null,constructor:function(_1){ |
16 |
if(_1&&_1.url){
|
17 |
this.url=_1.url;
|
18 |
} |
19 |
if(_1&&_1.urlPreventCache){
|
20 |
this.urlPreventCache=_1.urlPreventCache;
|
21 |
} |
22 |
if(!this.url){ |
23 |
throw new Error("A URL is required to instantiate an APP Store object"); |
24 |
} |
25 |
},_setFeed:function(_2,_3){ |
26 |
this._feed=_2;
|
27 |
var i;
|
28 |
for(i=0;i<this._feed.entries.length;i++){ |
29 |
this._feed.entries[i].store=this; |
30 |
} |
31 |
if(this._requests){ |
32 |
for(i=0;i<this._requests.length;i++){ |
33 |
var _4=this._requests[i]; |
34 |
if(_4.request&&_4.fh&&_4.eh){
|
35 |
this._finishFetchItems(_4.request,_4.fh,_4.eh);
|
36 |
}else{
|
37 |
if(_4.clear){
|
38 |
this._feed=null; |
39 |
}else{
|
40 |
if(_4.add){
|
41 |
this._feed.addEntry(_4.add);
|
42 |
}else{
|
43 |
if(_4.remove){
|
44 |
this._feed.removeEntry(_4.remove);
|
45 |
} |
46 |
} |
47 |
} |
48 |
} |
49 |
} |
50 |
} |
51 |
this._requests=null; |
52 |
},_getAllItems:function(){ |
53 |
var _5=[];
|
54 |
for(var i=0;i<this._feed.entries.length;i++){ |
55 |
_5.push(this._feed.entries[i]);
|
56 |
} |
57 |
return _5;
|
58 |
},_assertIsItem:function(_6){ |
59 |
if(!this.isItem(_6)){ |
60 |
throw new Error("This error message is provided when a function is called in the following form: "+"getAttribute(argument, attributeName). The argument variable represents the member "+"or owner of the object. The error is created when an item that does not belong "+"to this store is specified as an argument."); |
61 |
} |
62 |
},_assertIsAttribute:function(_7){ |
63 |
if(typeof _7!=="string"){ |
64 |
throw new Error("The attribute argument must be a string. The error is created "+"when a different type of variable is specified such as an array or object."); |
65 |
} |
66 |
for(var _8 in dojox.atom.io.model._actions){ |
67 |
if(_8==_7){
|
68 |
return true; |
69 |
} |
70 |
} |
71 |
return false; |
72 |
},_addUpdate:function(_9){ |
73 |
if(!this._updates){ |
74 |
this._updates=[_9];
|
75 |
}else{
|
76 |
this._updates.push(_9);
|
77 |
} |
78 |
},getValue:function(_a,_b,_c){ |
79 |
var _d=this.getValues(_a,_b); |
80 |
return (_d.length>0)?_d[0]:_c; |
81 |
},getValues:function(_e,_f){ |
82 |
this._assertIsItem(_e);
|
83 |
var _10=this._assertIsAttribute(_f); |
84 |
if(_10){
|
85 |
if((_f==="author"||_f==="contributor"||_f==="link")&&_e[_f+"s"]){ |
86 |
return _e[_f+"s"]; |
87 |
} |
88 |
if(_f==="category"&&_e.categories){ |
89 |
return _e.categories;
|
90 |
} |
91 |
if(_e[_f]){
|
92 |
_e=_e[_f]; |
93 |
if(_e.declaredClass=="dojox.atom.io.model.Content"){ |
94 |
return [_e.value];
|
95 |
} |
96 |
return [_e];
|
97 |
} |
98 |
} |
99 |
return [];
|
100 |
},getAttributes:function(_11){ |
101 |
this._assertIsItem(_11);
|
102 |
var _12=[];
|
103 |
for(var key in dojox.atom.io.model._actions){ |
104 |
if(this.hasAttribute(_11,key)){ |
105 |
_12.push(key); |
106 |
} |
107 |
} |
108 |
return _12;
|
109 |
},hasAttribute:function(_13,_14){ |
110 |
return this.getValues(_13,_14).length>0; |
111 |
},containsValue:function(_15,_16,_17){ |
112 |
var _18=undefined; |
113 |
if(typeof _17==="string"){ |
114 |
_18=dojo.data.util.filter.patternToRegExp(_17,false);
|
115 |
} |
116 |
return this._containsValue(_15,_16,_17,_18); |
117 |
},_containsValue:function(_19,_1a,_1b,_1c,_1d){ |
118 |
var _1e=this.getValues(_19,_1a); |
119 |
for(var i=0;i<_1e.length;++i){ |
120 |
var _1f=_1e[i];
|
121 |
if(typeof _1f==="string"&&_1c){ |
122 |
if(_1d){
|
123 |
_1f=_1f.replace(new RegExp(/^\s+/),""); |
124 |
_1f=_1f.replace(new RegExp(/\s+$/),""); |
125 |
} |
126 |
_1f=_1f.replace(/\r|\n|\r\n/g,""); |
127 |
return (_1f.match(_1c)!==null); |
128 |
}else{
|
129 |
if(_1b===_1f){
|
130 |
return true; |
131 |
} |
132 |
} |
133 |
} |
134 |
return false; |
135 |
},isItem:function(_20){ |
136 |
return _20&&_20.store&&_20.store===this; |
137 |
},isItemLoaded:function(_21){ |
138 |
return this.isItem(_21); |
139 |
},loadItem:function(_22){ |
140 |
this._assertIsItem(_22.item);
|
141 |
},_fetchItems:function(_23,_24,_25){ |
142 |
if(this._feed){ |
143 |
this._finishFetchItems(_23,_24,_25);
|
144 |
}else{
|
145 |
var _26=false; |
146 |
if(!this._requests){ |
147 |
this._requests=[];
|
148 |
_26=true;
|
149 |
} |
150 |
this._requests.push({request:_23,fh:_24,eh:_25}); |
151 |
if(_26){
|
152 |
this._atomIO=new dojox.atom.io.Connection(false,this.urlPreventCache); |
153 |
this._atomIO.getFeed(this.url,this._setFeed,null,this); |
154 |
} |
155 |
} |
156 |
},_finishFetchItems:function(_27,_28,_29){ |
157 |
var _2a=null; |
158 |
var _2b=this._getAllItems(); |
159 |
if(_27.query){
|
160 |
var _2c=_27.queryOptions?_27.queryOptions.ignoreCase:false; |
161 |
_2a=[]; |
162 |
var _2d={};
|
163 |
var key;
|
164 |
var _2e;
|
165 |
for(key in _27.query){ |
166 |
_2e=_27.query[key]+"";
|
167 |
if(typeof _2e==="string"){ |
168 |
_2d[key]=dojo.data.util.filter.patternToRegExp(_2e,_2c); |
169 |
} |
170 |
} |
171 |
for(var i=0;i<_2b.length;++i){ |
172 |
var _2f=true; |
173 |
var _30=_2b[i];
|
174 |
for(key in _27.query){ |
175 |
_2e=_27.query[key]+"";
|
176 |
if(!this._containsValue(_30,key,_2e,_2d[key],_27.trim)){ |
177 |
_2f=false;
|
178 |
} |
179 |
} |
180 |
if(_2f){
|
181 |
_2a.push(_30); |
182 |
} |
183 |
} |
184 |
}else{
|
185 |
if(_2b.length>0){ |
186 |
_2a=_2b.slice(0,_2b.length);
|
187 |
} |
188 |
} |
189 |
try{
|
190 |
_28(_2a,_27); |
191 |
} |
192 |
catch(e){
|
193 |
_29(e,_27); |
194 |
} |
195 |
},getFeatures:function(){ |
196 |
return {"dojo.data.api.Read":true,"dojo.data.api.Write":true,"dojo.data.api.Identity":true}; |
197 |
},close:function(_31){ |
198 |
this._feed=null; |
199 |
},getLabel:function(_32){ |
200 |
if(this.isItem(_32)){ |
201 |
return this.getValue(_32,"title","No Title"); |
202 |
} |
203 |
return undefined; |
204 |
},getLabelAttributes:function(_33){ |
205 |
return ["title"]; |
206 |
},getIdentity:function(_34){ |
207 |
this._assertIsItem(_34);
|
208 |
return this.getValue(_34,"id"); |
209 |
},getIdentityAttributes:function(_35){ |
210 |
return ["id"]; |
211 |
},fetchItemByIdentity:function(_36){ |
212 |
this._fetchItems({query:{id:_36.identity},onItem:_36.onItem,scope:_36.scope},function(_37,_38){ |
213 |
var _39=_38.scope;
|
214 |
if(!_39){
|
215 |
_39=dojo.global; |
216 |
} |
217 |
if(_37.length<1){ |
218 |
_38.onItem.call(_39,null);
|
219 |
}else{
|
220 |
_38.onItem.call(_39,_37[0]);
|
221 |
} |
222 |
},_36.onError); |
223 |
},newItem:function(_3a){ |
224 |
var _3b=new dojox.atom.io.model.Entry(); |
225 |
var _3c=null; |
226 |
var _3d=null; |
227 |
var i;
|
228 |
for(var key in _3a){ |
229 |
if(this._assertIsAttribute(key)){ |
230 |
_3c=_3a[key]; |
231 |
switch(key){
|
232 |
case "link": |
233 |
for(i in _3c){ |
234 |
_3d=_3c[i]; |
235 |
_3b.addLink(_3d.href,_3d.rel,_3d.hrefLang,_3d.title,_3d.type); |
236 |
} |
237 |
break;
|
238 |
case "author": |
239 |
for(i in _3c){ |
240 |
_3d=_3c[i]; |
241 |
_3b.addAuthor(_3d.name,_3d.email,_3d.uri); |
242 |
} |
243 |
break;
|
244 |
case "contributor": |
245 |
for(i in _3c){ |
246 |
_3d=_3c[i]; |
247 |
_3b.addContributor(_3d.name,_3d.email,_3d.uri); |
248 |
} |
249 |
break;
|
250 |
case "category": |
251 |
for(i in _3c){ |
252 |
_3d=_3c[i]; |
253 |
_3b.addCategory(_3d.scheme,_3d.term,_3d.label); |
254 |
} |
255 |
break;
|
256 |
case "icon": |
257 |
case "id": |
258 |
case "logo": |
259 |
case "xmlBase": |
260 |
case "rights": |
261 |
_3b[key]=_3c; |
262 |
break;
|
263 |
case "updated": |
264 |
case "published": |
265 |
case "issued": |
266 |
case "modified": |
267 |
_3b[key]=dojox.atom.io.model.util.createDate(_3c); |
268 |
break;
|
269 |
case "content": |
270 |
case "summary": |
271 |
case "title": |
272 |
case "subtitle": |
273 |
_3b[key]=new dojox.atom.io.model.Content(key);
|
274 |
_3b[key].value=_3c; |
275 |
break;
|
276 |
default:
|
277 |
_3b[key]=_3c; |
278 |
break;
|
279 |
} |
280 |
} |
281 |
} |
282 |
_3b.store=this;
|
283 |
_3b.isDirty=true;
|
284 |
if(!this._adds){ |
285 |
this._adds=[_3b];
|
286 |
}else{
|
287 |
this._adds.push(_3b);
|
288 |
} |
289 |
if(this._feed){ |
290 |
this._feed.addEntry(_3b);
|
291 |
}else{
|
292 |
if(this._requests){ |
293 |
this._requests.push({add:_3b}); |
294 |
}else{
|
295 |
this._requests=[{add:_3b}]; |
296 |
this._atomIO=new dojox.atom.io.Connection(false,this.urlPreventCache); |
297 |
this._atomIO.getFeed(this.url,dojo.hitch(this,this._setFeed)); |
298 |
} |
299 |
} |
300 |
return true; |
301 |
},deleteItem:function(_3e){ |
302 |
this._assertIsItem(_3e);
|
303 |
if(!this._deletes){ |
304 |
this._deletes=[_3e];
|
305 |
}else{
|
306 |
this._deletes.push(_3e);
|
307 |
} |
308 |
if(this._feed){ |
309 |
this._feed.removeEntry(_3e);
|
310 |
}else{
|
311 |
if(this._requests){ |
312 |
this._requests.push({remove:_3e}); |
313 |
}else{
|
314 |
this._requests=[{remove:_3e}]; |
315 |
this._atomIO=new dojox.atom.io.Connection(false,this.urlPreventCache); |
316 |
this._atomIO.getFeed(this.url,dojo.hitch(this,this._setFeed)); |
317 |
} |
318 |
} |
319 |
_3e=null;
|
320 |
return true; |
321 |
},setValue:function(_3f,_40,_41){ |
322 |
this._assertIsItem(_3f);
|
323 |
var _42={item:_3f}; |
324 |
if(this._assertIsAttribute(_40)){ |
325 |
switch(_40){
|
326 |
case "link": |
327 |
_42.links=_3f.links; |
328 |
this._addUpdate(_42);
|
329 |
_3f.links=null;
|
330 |
_3f.addLink(_41.href,_41.rel,_41.hrefLang,_41.title,_41.type); |
331 |
_3f.isDirty=true;
|
332 |
return true; |
333 |
case "author": |
334 |
_42.authors=_3f.authors; |
335 |
this._addUpdate(_42);
|
336 |
_3f.authors=null;
|
337 |
_3f.addAuthor(_41.name,_41.email,_41.uri); |
338 |
_3f.isDirty=true;
|
339 |
return true; |
340 |
case "contributor": |
341 |
_42.contributors=_3f.contributors; |
342 |
this._addUpdate(_42);
|
343 |
_3f.contributors=null;
|
344 |
_3f.addContributor(_41.name,_41.email,_41.uri); |
345 |
_3f.isDirty=true;
|
346 |
return true; |
347 |
case "category": |
348 |
_42.categories=_3f.categories; |
349 |
this._addUpdate(_42);
|
350 |
_3f.categories=null;
|
351 |
_3f.addCategory(_41.scheme,_41.term,_41.label); |
352 |
_3f.isDirty=true;
|
353 |
return true; |
354 |
case "icon": |
355 |
case "id": |
356 |
case "logo": |
357 |
case "xmlBase": |
358 |
case "rights": |
359 |
_42[_40]=_3f[_40]; |
360 |
this._addUpdate(_42);
|
361 |
_3f[_40]=_41; |
362 |
_3f.isDirty=true;
|
363 |
return true; |
364 |
case "updated": |
365 |
case "published": |
366 |
case "issued": |
367 |
case "modified": |
368 |
_42[_40]=_3f[_40]; |
369 |
this._addUpdate(_42);
|
370 |
_3f[_40]=dojox.atom.io.model.util.createDate(_41); |
371 |
_3f.isDirty=true;
|
372 |
return true; |
373 |
case "content": |
374 |
case "summary": |
375 |
case "title": |
376 |
case "subtitle": |
377 |
_42[_40]=_3f[_40]; |
378 |
this._addUpdate(_42);
|
379 |
_3f[_40]=new dojox.atom.io.model.Content(_40);
|
380 |
_3f[_40].value=_41; |
381 |
_3f.isDirty=true;
|
382 |
return true; |
383 |
default:
|
384 |
_42[_40]=_3f[_40]; |
385 |
this._addUpdate(_42);
|
386 |
_3f[_40]=_41; |
387 |
_3f.isDirty=true;
|
388 |
return true; |
389 |
} |
390 |
} |
391 |
return false; |
392 |
},setValues:function(_43,_44,_45){ |
393 |
if(_45.length===0){ |
394 |
return this.unsetAttribute(_43,_44); |
395 |
} |
396 |
this._assertIsItem(_43);
|
397 |
var _46={item:_43}; |
398 |
var _47;
|
399 |
var i;
|
400 |
if(this._assertIsAttribute(_44)){ |
401 |
switch(_44){
|
402 |
case "link": |
403 |
_46.links=_43.links; |
404 |
_43.links=null;
|
405 |
for(i in _45){ |
406 |
_47=_45[i]; |
407 |
_43.addLink(_47.href,_47.rel,_47.hrefLang,_47.title,_47.type); |
408 |
} |
409 |
_43.isDirty=true;
|
410 |
return true; |
411 |
case "author": |
412 |
_46.authors=_43.authors; |
413 |
_43.authors=null;
|
414 |
for(i in _45){ |
415 |
_47=_45[i]; |
416 |
_43.addAuthor(_47.name,_47.email,_47.uri); |
417 |
} |
418 |
_43.isDirty=true;
|
419 |
return true; |
420 |
case "contributor": |
421 |
_46.contributors=_43.contributors; |
422 |
_43.contributors=null;
|
423 |
for(i in _45){ |
424 |
_47=_45[i]; |
425 |
_43.addContributor(_47.name,_47.email,_47.uri); |
426 |
} |
427 |
_43.isDirty=true;
|
428 |
return true; |
429 |
case "categories": |
430 |
_46.categories=_43.categories; |
431 |
_43.categories=null;
|
432 |
for(i in _45){ |
433 |
_47=_45[i]; |
434 |
_43.addCategory(_47.scheme,_47.term,_47.label); |
435 |
} |
436 |
_43.isDirty=true;
|
437 |
return true; |
438 |
case "icon": |
439 |
case "id": |
440 |
case "logo": |
441 |
case "xmlBase": |
442 |
case "rights": |
443 |
_46[_44]=_43[_44]; |
444 |
_43[_44]=_45[0];
|
445 |
_43.isDirty=true;
|
446 |
return true; |
447 |
case "updated": |
448 |
case "published": |
449 |
case "issued": |
450 |
case "modified": |
451 |
_46[_44]=_43[_44]; |
452 |
_43[_44]=dojox.atom.io.model.util.createDate(_45[0]);
|
453 |
_43.isDirty=true;
|
454 |
return true; |
455 |
case "content": |
456 |
case "summary": |
457 |
case "title": |
458 |
case "subtitle": |
459 |
_46[_44]=_43[_44]; |
460 |
_43[_44]=new dojox.atom.io.model.Content(_44);
|
461 |
_43[_44].values[0]=_45[0]; |
462 |
_43.isDirty=true;
|
463 |
return true; |
464 |
default:
|
465 |
_46[_44]=_43[_44]; |
466 |
_43[_44]=_45[0];
|
467 |
_43.isDirty=true;
|
468 |
return true; |
469 |
} |
470 |
} |
471 |
this._addUpdate(_46);
|
472 |
return false; |
473 |
},unsetAttribute:function(_48,_49){ |
474 |
this._assertIsItem(_48);
|
475 |
if(this._assertIsAttribute(_49)){ |
476 |
if(_48[_49]!==null){ |
477 |
var _4a={item:_48}; |
478 |
switch(_49){
|
479 |
case "author": |
480 |
case "contributor": |
481 |
case "link": |
482 |
_4a[_49+"s"]=_48[_49+"s"]; |
483 |
break;
|
484 |
case "category": |
485 |
_4a.categories=_48.categories; |
486 |
break;
|
487 |
default:
|
488 |
_4a[_49]=_48[_49]; |
489 |
break;
|
490 |
} |
491 |
_48.isDirty=true;
|
492 |
_48[_49]=null;
|
493 |
this._addUpdate(_4a);
|
494 |
return true; |
495 |
} |
496 |
} |
497 |
return false; |
498 |
},save:function(_4b){ |
499 |
var i;
|
500 |
for(i in this._adds){ |
501 |
this._atomIO.addEntry(this._adds[i],null,function(){ |
502 |
},_4b.onError,false,_4b.scope);
|
503 |
} |
504 |
this._adds=null; |
505 |
for(i in this._updates){ |
506 |
this._atomIO.updateEntry(this._updates[i].item,function(){ |
507 |
},_4b.onError,false,this.xmethod,_4b.scope); |
508 |
} |
509 |
this._updates=null; |
510 |
for(i in this._deletes){ |
511 |
this._atomIO.removeEntry(this._deletes[i],function(){ |
512 |
},_4b.onError,this.xmethod,_4b.scope);
|
513 |
} |
514 |
this._deletes=null; |
515 |
this._atomIO.getFeed(this.url,dojo.hitch(this,this._setFeed)); |
516 |
if(_4b.onComplete){
|
517 |
var _4c=_4b.scope||dojo.global;
|
518 |
_4b.onComplete.call(_4c); |
519 |
} |
520 |
},revert:function(){ |
521 |
var i;
|
522 |
for(i in this._adds){ |
523 |
this._feed.removeEntry(this._adds[i]); |
524 |
} |
525 |
this._adds=null; |
526 |
var _4d,_4e,key;
|
527 |
for(i in this._updates){ |
528 |
_4d=this._updates[i];
|
529 |
_4e=_4d.item; |
530 |
for(key in _4d){ |
531 |
if(key!=="item"){ |
532 |
_4e[key]=_4d[key]; |
533 |
} |
534 |
} |
535 |
} |
536 |
this._updates=null; |
537 |
for(i in this._deletes){ |
538 |
this._feed.addEntry(this._deletes[i]); |
539 |
} |
540 |
this._deletes=null; |
541 |
return true; |
542 |
},isDirty:function(_4f){ |
543 |
if(_4f){
|
544 |
this._assertIsItem(_4f);
|
545 |
return _4f.isDirty?true:false; |
546 |
} |
547 |
return (this._adds!==null||this._updates!==null); |
548 |
}}); |
549 |
dojo.extend(dojox.data.AppStore,dojo.data.util.simpleFetch); |
550 |
} |