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