root / trunk / web / dojo / dojox / data / HtmlStore.js @ 13
History | View | Annotate | Download (7.22 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
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.HtmlStore"]){ |
||
9 | dojo._hasResource["dojox.data.HtmlStore"]=true; |
||
10 | dojo.provide("dojox.data.HtmlStore");
|
||
11 | dojo.require("dojox.xml.parser");
|
||
12 | dojo.require("dojo.data.util.simpleFetch");
|
||
13 | dojo.require("dojo.data.util.filter");
|
||
14 | dojo.declare("dojox.data.HtmlStore",null,{constructor:function(_1){ |
||
15 | if(_1&&"urlPreventCache" in _1){ |
||
16 | this.urlPreventCache=_1.urlPreventCache?true:false; |
||
17 | } |
||
18 | if(_1&&"trimWhitespace" in _1){ |
||
19 | this.trimWhitespace=_1.trimWhitespace?true:false; |
||
20 | } |
||
21 | if(_1.url){
|
||
22 | if(!_1.dataId){
|
||
23 | throw new Error("dojo.data.HtmlStore: Cannot instantiate using url without an id!"); |
||
24 | } |
||
25 | this.url=_1.url;
|
||
26 | this.dataId=_1.dataId;
|
||
27 | }else{
|
||
28 | if(_1.dataId){
|
||
29 | this._rootNode=dojo.byId(_1.dataId);
|
||
30 | this.dataId=this._rootNode.id; |
||
31 | }else{
|
||
32 | this._rootNode=dojo.byId(this.dataId); |
||
33 | } |
||
34 | this._indexItems();
|
||
35 | } |
||
36 | },url:"",dataId:"",trimWhitespace:false,urlPreventCache:false,_indexItems:function(){ |
||
37 | this._getHeadings();
|
||
38 | if(this._rootNode.rows){ |
||
39 | if(this._rootNode.tBodies&&this._rootNode.tBodies.length>0){ |
||
40 | this._rootNode=this._rootNode.tBodies[0]; |
||
41 | } |
||
42 | var i;
|
||
43 | for(i=0;i<this._rootNode.rows.length;i++){ |
||
44 | this._rootNode.rows[i].store=this; |
||
45 | this._rootNode.rows[i]._ident=i+1; |
||
46 | } |
||
47 | }else{
|
||
48 | var c=1; |
||
49 | for(i=0;i<this._rootNode.childNodes.length;i++){ |
||
50 | if(this._rootNode.childNodes[i].nodeType===1){ |
||
51 | this._rootNode.childNodes[i].store=this; |
||
52 | this._rootNode.childNodes[i]._ident=c;
|
||
53 | c++; |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | },_getHeadings:function(){ |
||
58 | this._headings=[];
|
||
59 | if(this._rootNode.tHead){ |
||
60 | dojo.forEach(this._rootNode.tHead.rows[0].cells,dojo.hitch(this,function(th){ |
||
61 | var _2=dojox.xml.parser.textContent(th);
|
||
62 | this._headings.push(this.trimWhitespace?dojo.trim(_2):_2); |
||
63 | })); |
||
64 | }else{
|
||
65 | this._headings=["name"]; |
||
66 | } |
||
67 | },_getAllItems:function(){ |
||
68 | var _3=[];
|
||
69 | var i;
|
||
70 | if(this._rootNode.rows){ |
||
71 | for(i=0;i<this._rootNode.rows.length;i++){ |
||
72 | _3.push(this._rootNode.rows[i]);
|
||
73 | } |
||
74 | }else{
|
||
75 | for(i=0;i<this._rootNode.childNodes.length;i++){ |
||
76 | if(this._rootNode.childNodes[i].nodeType===1){ |
||
77 | _3.push(this._rootNode.childNodes[i]);
|
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | return _3;
|
||
82 | },_assertIsItem:function(_4){ |
||
83 | if(!this.isItem(_4)){ |
||
84 | throw new Error("dojo.data.HtmlStore: a function was passed an item argument that was not an item"); |
||
85 | } |
||
86 | },_assertIsAttribute:function(_5){ |
||
87 | if(typeof _5!=="string"){ |
||
88 | throw new Error("dojo.data.HtmlStore: a function was passed an attribute argument that was not an attribute name string"); |
||
89 | return -1; |
||
90 | } |
||
91 | return dojo.indexOf(this._headings,_5); |
||
92 | },getValue:function(_6,_7,_8){ |
||
93 | var _9=this.getValues(_6,_7); |
||
94 | return (_9.length>0)?_9[0]:_8; |
||
95 | },getValues:function(_a,_b){ |
||
96 | this._assertIsItem(_a);
|
||
97 | var _c=this._assertIsAttribute(_b); |
||
98 | if(_c>-1){ |
||
99 | var _d;
|
||
100 | if(_a.cells){
|
||
101 | _d=dojox.xml.parser.textContent(_a.cells[_c]); |
||
102 | }else{
|
||
103 | _d=dojox.xml.parser.textContent(_a); |
||
104 | } |
||
105 | return [this.trimWhitespace?dojo.trim(_d):_d]; |
||
106 | } |
||
107 | return [];
|
||
108 | },getAttributes:function(_e){ |
||
109 | this._assertIsItem(_e);
|
||
110 | var _f=[];
|
||
111 | for(var i=0;i<this._headings.length;i++){ |
||
112 | if(this.hasAttribute(_e,this._headings[i])){ |
||
113 | _f.push(this._headings[i]);
|
||
114 | } |
||
115 | } |
||
116 | return _f;
|
||
117 | },hasAttribute:function(_10,_11){ |
||
118 | return this.getValues(_10,_11).length>0; |
||
119 | },containsValue:function(_12,_13,_14){ |
||
120 | var _15=undefined; |
||
121 | if(typeof _14==="string"){ |
||
122 | _15=dojo.data.util.filter.patternToRegExp(_14,false);
|
||
123 | } |
||
124 | return this._containsValue(_12,_13,_14,_15); |
||
125 | },_containsValue:function(_16,_17,_18,_19){ |
||
126 | var _1a=this.getValues(_16,_17); |
||
127 | for(var i=0;i<_1a.length;++i){ |
||
128 | var _1b=_1a[i];
|
||
129 | if(typeof _1b==="string"&&_19){ |
||
130 | return (_1b.match(_19)!==null); |
||
131 | }else{
|
||
132 | if(_18===_1b){
|
||
133 | return true; |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | return false; |
||
138 | },isItem:function(_1c){ |
||
139 | if(_1c&&_1c.store&&_1c.store===this){ |
||
140 | return true; |
||
141 | } |
||
142 | return false; |
||
143 | },isItemLoaded:function(_1d){ |
||
144 | return this.isItem(_1d); |
||
145 | },loadItem:function(_1e){ |
||
146 | this._assertIsItem(_1e.item);
|
||
147 | },_fetchItems:function(_1f,_20,_21){ |
||
148 | if(this._rootNode){ |
||
149 | this._finishFetchItems(_1f,_20,_21);
|
||
150 | }else{
|
||
151 | if(!this.url){ |
||
152 | this._rootNode=dojo.byId(this.dataId); |
||
153 | }else{
|
||
154 | var _22={url:this.url,handleAs:"text",preventCache:this.urlPreventCache}; |
||
155 | var _23=this; |
||
156 | var _24=dojo.xhrGet(_22);
|
||
157 | _24.addCallback(function(_25){
|
||
158 | var _26=function(_27,id){ |
||
159 | if(_27.id==id){
|
||
160 | return _27;
|
||
161 | } |
||
162 | if(_27.childNodes){
|
||
163 | for(var i=0;i<_27.childNodes.length;i++){ |
||
164 | var _28=_26(_27.childNodes[i],id);
|
||
165 | if(_28){
|
||
166 | return _28;
|
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | return null; |
||
171 | }; |
||
172 | var d=document.createElement("div"); |
||
173 | d.innerHTML=_25; |
||
174 | _23._rootNode=_26(d,_23.dataId); |
||
175 | _23._indexItems(); |
||
176 | _23._finishFetchItems(_1f,_20,_21); |
||
177 | }); |
||
178 | _24.addErrback(function(_29){
|
||
179 | _21(_29,_1f); |
||
180 | }); |
||
181 | } |
||
182 | } |
||
183 | },_finishFetchItems:function(_2a,_2b,_2c){ |
||
184 | var _2d=[];
|
||
185 | var _2e=this._getAllItems(); |
||
186 | if(_2a.query){
|
||
187 | var _2f=_2a.queryOptions?_2a.queryOptions.ignoreCase:false; |
||
188 | _2d=[]; |
||
189 | var _30={};
|
||
190 | var key;
|
||
191 | var _31;
|
||
192 | for(key in _2a.query){ |
||
193 | _31=_2a.query[key]+"";
|
||
194 | if(typeof _31==="string"){ |
||
195 | _30[key]=dojo.data.util.filter.patternToRegExp(_31,_2f); |
||
196 | } |
||
197 | } |
||
198 | for(var i=0;i<_2e.length;++i){ |
||
199 | var _32=true; |
||
200 | var _33=_2e[i];
|
||
201 | for(key in _2a.query){ |
||
202 | _31=_2a.query[key]+"";
|
||
203 | if(!this._containsValue(_33,key,_31,_30[key])){ |
||
204 | _32=false;
|
||
205 | } |
||
206 | } |
||
207 | if(_32){
|
||
208 | _2d.push(_33); |
||
209 | } |
||
210 | } |
||
211 | _2b(_2d,_2a); |
||
212 | }else{
|
||
213 | if(_2e.length>0){ |
||
214 | _2d=_2e.slice(0,_2e.length);
|
||
215 | } |
||
216 | _2b(_2d,_2a); |
||
217 | } |
||
218 | },getFeatures:function(){ |
||
219 | return {"dojo.data.api.Read":true,"dojo.data.api.Identity":true}; |
||
220 | },close:function(_34){ |
||
221 | },getLabel:function(_35){ |
||
222 | if(this.isItem(_35)){ |
||
223 | if(_35.cells){
|
||
224 | return "Item #"+this.getIdentity(_35); |
||
225 | }else{
|
||
226 | return this.getValue(_35,"name"); |
||
227 | } |
||
228 | } |
||
229 | return undefined; |
||
230 | },getLabelAttributes:function(_36){ |
||
231 | if(_36.cells){
|
||
232 | return null; |
||
233 | }else{
|
||
234 | return ["name"]; |
||
235 | } |
||
236 | },getIdentity:function(_37){ |
||
237 | this._assertIsItem(_37);
|
||
238 | if(this.hasAttribute(_37,"name")){ |
||
239 | return this.getValue(_37,"name"); |
||
240 | }else{
|
||
241 | return _37._ident;
|
||
242 | } |
||
243 | },getIdentityAttributes:function(_38){ |
||
244 | return null; |
||
245 | },fetchItemByIdentity:function(_39){ |
||
246 | var _3a=_39.identity;
|
||
247 | var _3b=this; |
||
248 | var _3c=null; |
||
249 | var _3d=null; |
||
250 | if(!this._rootNode){ |
||
251 | if(!this.url){ |
||
252 | this._rootNode=dojo.byId(this.dataId); |
||
253 | this._indexItems();
|
||
254 | if(_3b._rootNode.rows){
|
||
255 | _3c=this._rootNode.rows[_3a+1]; |
||
256 | }else{
|
||
257 | for(var i=0;i<_3b._rootNode.childNodes.length;i++){ |
||
258 | if(_3b._rootNode.childNodes[i].nodeType===1&&_3a===dojox.xml.parser.textContent(_3b._rootNode.childNodes[i])){ |
||
259 | _3c=_3b._rootNode.childNodes[i]; |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | if(_39.onItem){
|
||
264 | _3d=_39.scope?_39.scope:dojo.global; |
||
265 | _39.onItem.call(_3d,_3c); |
||
266 | } |
||
267 | }else{
|
||
268 | var _3e={url:this.url,handleAs:"text"}; |
||
269 | var _3f=dojo.xhrGet(_3e);
|
||
270 | _3f.addCallback(function(_40){
|
||
271 | var _41=function(_42,id){ |
||
272 | if(_42.id==id){
|
||
273 | return _42;
|
||
274 | } |
||
275 | if(_42.childNodes){
|
||
276 | for(var i=0;i<_42.childNodes.length;i++){ |
||
277 | var _43=_41(_42.childNodes[i],id);
|
||
278 | if(_43){
|
||
279 | return _43;
|
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | return null; |
||
284 | }; |
||
285 | var d=document.createElement("div"); |
||
286 | d.innerHTML=_40; |
||
287 | _3b._rootNode=_41(d,_3b.dataId); |
||
288 | _3b._indexItems(); |
||
289 | if(_3b._rootNode.rows&&_3a<=_3b._rootNode.rows.length){
|
||
290 | _3c=_3b._rootNode.rows[_3a-1];
|
||
291 | }else{
|
||
292 | for(var i=0;i<_3b._rootNode.childNodes.length;i++){ |
||
293 | if(_3b._rootNode.childNodes[i].nodeType===1&&_3a===dojox.xml.parser.textContent(_3b._rootNode.childNodes[i])){ |
||
294 | _3c=_3b._rootNode.childNodes[i]; |
||
295 | break;
|
||
296 | } |
||
297 | } |
||
298 | } |
||
299 | if(_39.onItem){
|
||
300 | _3d=_39.scope?_39.scope:dojo.global; |
||
301 | _39.onItem.call(_3d,_3c); |
||
302 | } |
||
303 | }); |
||
304 | _3f.addErrback(function(_44){
|
||
305 | if(_39.onError){
|
||
306 | _3d=_39.scope?_39.scope:dojo.global; |
||
307 | _39.onError.call(_3d,_44); |
||
308 | } |
||
309 | }); |
||
310 | } |
||
311 | }else{
|
||
312 | if(this._rootNode.rows[_3a+1]){ |
||
313 | _3c=this._rootNode.rows[_3a+1]; |
||
314 | if(_39.onItem){
|
||
315 | _3d=_39.scope?_39.scope:dojo.global; |
||
316 | _39.onItem.call(_3d,_3c); |
||
317 | } |
||
318 | } |
||
319 | } |
||
320 | }}); |
||
321 | dojo.extend(dojox.data.HtmlStore,dojo.data.util.simpleFetch); |
||
322 | } |