2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.data.HtmlTableStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.data.HtmlTableStore"] = true;
|
|
|
3 |
dojo.provide("dojox.data.HtmlTableStore");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.data.dom");
|
|
|
6 |
dojo.require("dojo.data.util.simpleFetch");
|
|
|
7 |
dojo.require("dojo.data.util.filter");
|
|
|
8 |
|
|
|
9 |
dojo.declare("dojox.data.HtmlTableStore", null, {
|
|
|
10 |
constructor: function(/*Object*/args){
|
|
|
11 |
// summary:
|
|
|
12 |
// Initializer for the HTML table store.
|
|
|
13 |
// description:
|
|
|
14 |
// The HtmlTableStore can be created in one of two ways: a) by parsing an existing
|
|
|
15 |
// table DOM node on the current page or b) by referencing an external url and giving
|
|
|
16 |
// the id of the table in that page. The remote url will be parsed as an html page.
|
|
|
17 |
//
|
|
|
18 |
// The HTML table should be of the following form:
|
|
|
19 |
// <table id="myTable">
|
|
|
20 |
// <thead>
|
|
|
21 |
// <tr>
|
|
|
22 |
// <th>Attribute1</th>
|
|
|
23 |
// <th>Attribute2</th>
|
|
|
24 |
// </tr>
|
|
|
25 |
// </thead>
|
|
|
26 |
// <tbody>
|
|
|
27 |
// <tr>
|
|
|
28 |
// <td>Value1.1</td>
|
|
|
29 |
// <td>Value1.2</td>
|
|
|
30 |
// </tr>
|
|
|
31 |
// <tr>
|
|
|
32 |
// <td>Value2.1</td>
|
|
|
33 |
// <td>Value2.2</td>
|
|
|
34 |
// </tr>
|
|
|
35 |
// </tbody>
|
|
|
36 |
// </table>
|
|
|
37 |
//
|
|
|
38 |
// args:
|
|
|
39 |
// An anonymous object to initialize properties. It expects the following values:
|
|
|
40 |
// tableId: The id of the HTML table to use.
|
|
|
41 |
// OR
|
|
|
42 |
// url: The url of the remote page to load
|
|
|
43 |
// tableId: The id of the table element in the remote page
|
|
|
44 |
|
|
|
45 |
if(args.url){
|
|
|
46 |
if(!args.tableId)
|
|
|
47 |
throw new Error("dojo.data.HtmlTableStore: Cannot instantiate using url without an id!");
|
|
|
48 |
this.url = args.url;
|
|
|
49 |
this.tableId = args.tableId;
|
|
|
50 |
}else{
|
|
|
51 |
if(args.tableId){
|
|
|
52 |
this._rootNode = dojo.byId(args.tableId);
|
|
|
53 |
this.tableId = this._rootNode.id;
|
|
|
54 |
}else{
|
|
|
55 |
this._rootNode = dojo.byId(this.tableId);
|
|
|
56 |
}
|
|
|
57 |
this._getHeadings();
|
|
|
58 |
for(var i=0; i<this._rootNode.rows.length; i++){
|
|
|
59 |
this._rootNode.rows[i].store = this;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
},
|
|
|
63 |
|
|
|
64 |
url: "", // So the parser can instantiate the store via markup.
|
|
|
65 |
tableId: "", // So the parser can instantiate the store via markup.
|
|
|
66 |
|
|
|
67 |
_getHeadings: function(){
|
|
|
68 |
// summary:
|
|
|
69 |
// Function to load the attribute names from the table header so that the
|
|
|
70 |
// attributes (cells in a row), can have a reasonable name.
|
|
|
71 |
this._headings = [];
|
|
|
72 |
dojo.forEach(this._rootNode.tHead.rows[0].cells, dojo.hitch(this, function(th){
|
|
|
73 |
this._headings.push(dojox.data.dom.textContent(th));
|
|
|
74 |
}));
|
|
|
75 |
},
|
|
|
76 |
|
|
|
77 |
_getAllItems: function(){
|
|
|
78 |
// summary:
|
|
|
79 |
// Function to return all rows in the table as an array of items.
|
|
|
80 |
var items = [];
|
|
|
81 |
for(var i=1; i<this._rootNode.rows.length; i++){
|
|
|
82 |
items.push(this._rootNode.rows[i]);
|
|
|
83 |
}
|
|
|
84 |
return items; //array
|
|
|
85 |
},
|
|
|
86 |
|
|
|
87 |
_assertIsItem: function(/* item */ item){
|
|
|
88 |
// summary:
|
|
|
89 |
// This function tests whether the item passed in is indeed an item in the store.
|
|
|
90 |
// item:
|
|
|
91 |
// The item to test for being contained by the store.
|
|
|
92 |
if(!this.isItem(item)){
|
|
|
93 |
throw new Error("dojo.data.HtmlTableStore: a function was passed an item argument that was not an item");
|
|
|
94 |
}
|
|
|
95 |
},
|
|
|
96 |
|
|
|
97 |
_assertIsAttribute: function(/* String */ attribute){
|
|
|
98 |
// summary:
|
|
|
99 |
// This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
|
|
|
100 |
// attribute:
|
|
|
101 |
// The attribute to test for being contained by the store.
|
|
|
102 |
//
|
|
|
103 |
// returns:
|
|
|
104 |
// Returns the index (column) that the attribute resides in the row.
|
|
|
105 |
if(typeof attribute !== "string"){
|
|
|
106 |
throw new Error("dojo.data.HtmlTableStore: a function was passed an attribute argument that was not an attribute name string");
|
|
|
107 |
return;
|
|
|
108 |
}
|
|
|
109 |
return dojo.indexOf(this._headings, attribute); //int
|
|
|
110 |
},
|
|
|
111 |
|
|
|
112 |
/***************************************
|
|
|
113 |
dojo.data.api.Read API
|
|
|
114 |
***************************************/
|
|
|
115 |
|
|
|
116 |
getValue: function( /* item */ item,
|
|
|
117 |
/* attribute-name-string */ attribute,
|
|
|
118 |
/* value? */ defaultValue){
|
|
|
119 |
// summary:
|
|
|
120 |
// See dojo.data.api.Read.getValue()
|
|
|
121 |
var values = this.getValues(item, attribute);
|
|
|
122 |
return (values.length > 0)?values[0]:defaultValue; //Object || int || Boolean
|
|
|
123 |
},
|
|
|
124 |
|
|
|
125 |
getValues: function(/* item */ item,
|
|
|
126 |
/* attribute-name-string */ attribute){
|
|
|
127 |
// summary:
|
|
|
128 |
// See dojo.data.api.Read.getValues()
|
|
|
129 |
|
|
|
130 |
this._assertIsItem(item);
|
|
|
131 |
var index = this._assertIsAttribute(attribute);
|
|
|
132 |
|
|
|
133 |
if(index>-1){
|
|
|
134 |
return [dojox.data.dom.textContent(item.cells[index])] ;
|
|
|
135 |
}
|
|
|
136 |
return []; //Array
|
|
|
137 |
},
|
|
|
138 |
|
|
|
139 |
getAttributes: function(/* item */ item){
|
|
|
140 |
// summary:
|
|
|
141 |
// See dojo.data.api.Read.getAttributes()
|
|
|
142 |
this._assertIsItem(item);
|
|
|
143 |
var attributes = [];
|
|
|
144 |
for(var i=0; i<this._headings.length; i++){
|
|
|
145 |
if(this.hasAttribute(item, this._headings[i]))
|
|
|
146 |
attributes.push(this._headings[i]);
|
|
|
147 |
}
|
|
|
148 |
return attributes; //Array
|
|
|
149 |
},
|
|
|
150 |
|
|
|
151 |
hasAttribute: function( /* item */ item,
|
|
|
152 |
/* attribute-name-string */ attribute){
|
|
|
153 |
// summary:
|
|
|
154 |
// See dojo.data.api.Read.hasAttribute()
|
|
|
155 |
return this.getValues(item, attribute).length > 0;
|
|
|
156 |
},
|
|
|
157 |
|
|
|
158 |
containsValue: function(/* item */ item,
|
|
|
159 |
/* attribute-name-string */ attribute,
|
|
|
160 |
/* anything */ value){
|
|
|
161 |
// summary:
|
|
|
162 |
// See dojo.data.api.Read.containsValue()
|
|
|
163 |
var regexp = undefined;
|
|
|
164 |
if(typeof value === "string"){
|
|
|
165 |
regexp = dojo.data.util.filter.patternToRegExp(value, false);
|
|
|
166 |
}
|
|
|
167 |
return this._containsValue(item, attribute, value, regexp); //boolean.
|
|
|
168 |
},
|
|
|
169 |
|
|
|
170 |
_containsValue: function( /* item */ item,
|
|
|
171 |
/* attribute-name-string */ attribute,
|
|
|
172 |
/* anything */ value,
|
|
|
173 |
/* RegExp?*/ regexp){
|
|
|
174 |
// summary:
|
|
|
175 |
// Internal function for looking at the values contained by the item.
|
|
|
176 |
// description:
|
|
|
177 |
// Internal function for looking at the values contained by the item. This
|
|
|
178 |
// function allows for denoting if the comparison should be case sensitive for
|
|
|
179 |
// strings or not (for handling filtering cases where string case should not matter)
|
|
|
180 |
//
|
|
|
181 |
// item:
|
|
|
182 |
// The data item to examine for attribute values.
|
|
|
183 |
// attribute:
|
|
|
184 |
// The attribute to inspect.
|
|
|
185 |
// value:
|
|
|
186 |
// The value to match.
|
|
|
187 |
// regexp:
|
|
|
188 |
// Optional regular expression generated off value if value was of string type to handle wildcarding.
|
|
|
189 |
// If present and attribute values are string, then it can be used for comparison instead of 'value'
|
|
|
190 |
var values = this.getValues(item, attribute);
|
|
|
191 |
for(var i = 0; i < values.length; ++i){
|
|
|
192 |
var possibleValue = values[i];
|
|
|
193 |
if(typeof possibleValue === "string" && regexp){
|
|
|
194 |
return (possibleValue.match(regexp) !== null);
|
|
|
195 |
}else{
|
|
|
196 |
//Non-string matching.
|
|
|
197 |
if(value === possibleValue){
|
|
|
198 |
return true; // Boolean
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
return false; // Boolean
|
|
|
203 |
},
|
|
|
204 |
|
|
|
205 |
isItem: function(/* anything */ something){
|
|
|
206 |
// summary:
|
|
|
207 |
// See dojo.data.api.Read.isItem()
|
|
|
208 |
if(something && something.store && something.store === this){
|
|
|
209 |
return true; //boolean
|
|
|
210 |
}
|
|
|
211 |
return false; //boolean
|
|
|
212 |
},
|
|
|
213 |
|
|
|
214 |
isItemLoaded: function(/* anything */ something){
|
|
|
215 |
// summary:
|
|
|
216 |
// See dojo.data.api.Read.isItemLoaded()
|
|
|
217 |
return this.isItem(something);
|
|
|
218 |
},
|
|
|
219 |
|
|
|
220 |
loadItem: function(/* Object */ keywordArgs){
|
|
|
221 |
// summary:
|
|
|
222 |
// See dojo.data.api.Read.loadItem()
|
|
|
223 |
this._assertIsItem(keywordArgs.item);
|
|
|
224 |
},
|
|
|
225 |
|
|
|
226 |
_fetchItems: function(request, fetchHandler, errorHandler) {
|
|
|
227 |
// summary:
|
|
|
228 |
// Fetch items (XML elements) that match to a query
|
|
|
229 |
// description:
|
|
|
230 |
// If '_fetchUrl' is specified, it is used to load an XML document
|
|
|
231 |
// with a query string.
|
|
|
232 |
// Otherwise and if 'url' is specified, the XML document is
|
|
|
233 |
// loaded and list XML elements that match to a query (set of element
|
|
|
234 |
// names and their text attribute values that the items to contain).
|
|
|
235 |
// A wildcard, "*" can be used to query values to match all
|
|
|
236 |
// occurrences.
|
|
|
237 |
// If '_rootItem' is specified, it is used to fetch items.
|
|
|
238 |
// request:
|
|
|
239 |
// A request object
|
|
|
240 |
// fetchHandler:
|
|
|
241 |
// A function to call for fetched items
|
|
|
242 |
// errorHandler:
|
|
|
243 |
// A function to call on error
|
|
|
244 |
|
|
|
245 |
if(this._rootNode){
|
|
|
246 |
this._finishFetchItems(request, fetchHandler, errorHandler);
|
|
|
247 |
}else{
|
|
|
248 |
if(!this.url){
|
|
|
249 |
this._rootNode = dojo.byId(this.tableId);
|
|
|
250 |
this._getHeadings();
|
|
|
251 |
for(var i=0; i<this._rootNode.rows.length; i++){
|
|
|
252 |
this._rootNode.rows[i].store = this;
|
|
|
253 |
}
|
|
|
254 |
}else{
|
|
|
255 |
var getArgs = {
|
|
|
256 |
url: this.url,
|
|
|
257 |
handleAs: "text"
|
|
|
258 |
};
|
|
|
259 |
var self = this;
|
|
|
260 |
var getHandler = dojo.xhrGet(getArgs);
|
|
|
261 |
getHandler.addCallback(function(data){
|
|
|
262 |
var findNode = function(node, id){
|
|
|
263 |
if(node.id == id){
|
|
|
264 |
return node; //object
|
|
|
265 |
}
|
|
|
266 |
if(node.childNodes){
|
|
|
267 |
for(var i=0; i<node.childNodes.length; i++){
|
|
|
268 |
var returnNode = findNode(node.childNodes[i], id);
|
|
|
269 |
if(returnNode){
|
|
|
270 |
return returnNode; //object
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
return null; //null
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
var d = document.createElement("div");
|
|
|
278 |
d.innerHTML = data;
|
|
|
279 |
self._rootNode = findNode(d, self.tableId);
|
|
|
280 |
self._getHeadings.call(self);
|
|
|
281 |
for(var i=0; i<self._rootNode.rows.length; i++) {
|
|
|
282 |
self._rootNode.rows[i].store = self;
|
|
|
283 |
}
|
|
|
284 |
self._finishFetchItems(request, fetchHandler, errorHandler);
|
|
|
285 |
});
|
|
|
286 |
getHandler.addErrback(function(error){
|
|
|
287 |
errorHandler(error, request);
|
|
|
288 |
});
|
|
|
289 |
}
|
|
|
290 |
}
|
|
|
291 |
},
|
|
|
292 |
|
|
|
293 |
_finishFetchItems: function(request, fetchHandler, errorHandler){
|
|
|
294 |
// summary:
|
|
|
295 |
// Internal function for processing the passed in request and locating the requested items.
|
|
|
296 |
var items = null;
|
|
|
297 |
var arrayOfAllItems = this._getAllItems();
|
|
|
298 |
if(request.query){
|
|
|
299 |
var ignoreCase = request.queryOptions ? request.queryOptions.ignoreCase : false;
|
|
|
300 |
items = [];
|
|
|
301 |
|
|
|
302 |
//See if there are any string values that can be regexp parsed first to avoid multiple regexp gens on the
|
|
|
303 |
//same value for each item examined. Much more efficient.
|
|
|
304 |
var regexpList = {};
|
|
|
305 |
for(var key in request.query){
|
|
|
306 |
var value = request.query[key]+'';
|
|
|
307 |
if(typeof value === "string"){
|
|
|
308 |
regexpList[key] = dojo.data.util.filter.patternToRegExp(value, ignoreCase);
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
for(var i = 0; i < arrayOfAllItems.length; ++i){
|
|
|
313 |
var match = true;
|
|
|
314 |
var candidateItem = arrayOfAllItems[i];
|
|
|
315 |
for(var key in request.query){
|
|
|
316 |
var value = request.query[key]+'';
|
|
|
317 |
if (!this._containsValue(candidateItem, key, value, regexpList[key])){
|
|
|
318 |
match = false;
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
if(match){
|
|
|
322 |
items.push(candidateItem);
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
fetchHandler(items, request);
|
|
|
326 |
}else{
|
|
|
327 |
// We want a copy to pass back in case the parent wishes to sort the array. We shouldn't allow resort
|
|
|
328 |
// of the internal list so that multiple callers can get listsand sort without affecting each other.
|
|
|
329 |
if(arrayOfAllItems.length> 0){
|
|
|
330 |
items = arrayOfAllItems.slice(0,arrayOfAllItems.length);
|
|
|
331 |
}
|
|
|
332 |
fetchHandler(items, request);
|
|
|
333 |
}
|
|
|
334 |
},
|
|
|
335 |
|
|
|
336 |
getFeatures: function(){
|
|
|
337 |
// summary:
|
|
|
338 |
// See dojo.data.api.Read.getFeatures()
|
|
|
339 |
return {
|
|
|
340 |
'dojo.data.api.Read': true,
|
|
|
341 |
'dojo.data.api.Identity': true
|
|
|
342 |
};
|
|
|
343 |
},
|
|
|
344 |
|
|
|
345 |
close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
|
|
|
346 |
// summary:
|
|
|
347 |
// See dojo.data.api.Read.close()
|
|
|
348 |
// nothing to do here!
|
|
|
349 |
},
|
|
|
350 |
|
|
|
351 |
getLabel: function(/* item */ item){
|
|
|
352 |
// summary:
|
|
|
353 |
// See dojo.data.api.Read.getLabel()
|
|
|
354 |
if(this.isItem(item))
|
|
|
355 |
return "Table Row #" + this.getIdentity(item);
|
|
|
356 |
return undefined;
|
|
|
357 |
},
|
|
|
358 |
|
|
|
359 |
getLabelAttributes: function(/* item */ item){
|
|
|
360 |
// summary:
|
|
|
361 |
// See dojo.data.api.Read.getLabelAttributes()
|
|
|
362 |
return null;
|
|
|
363 |
},
|
|
|
364 |
|
|
|
365 |
/***************************************
|
|
|
366 |
dojo.data.api.Identity API
|
|
|
367 |
***************************************/
|
|
|
368 |
|
|
|
369 |
getIdentity: function(/* item */ item){
|
|
|
370 |
// summary:
|
|
|
371 |
// See dojo.data.api.Identity.getIdentity()
|
|
|
372 |
this._assertIsItem(item);
|
|
|
373 |
//Opera doesn't support the sectionRowIndex,
|
|
|
374 |
//So, have to call the indexOf to locate it.
|
|
|
375 |
//Blah.
|
|
|
376 |
if(!dojo.isOpera){
|
|
|
377 |
return item.sectionRowIndex; // int
|
|
|
378 |
}else{
|
|
|
379 |
return (dojo.indexOf(this._rootNode.rows, item) - 1) // int
|
|
|
380 |
}
|
|
|
381 |
},
|
|
|
382 |
|
|
|
383 |
getIdentityAttributes: function(/* item */ item){
|
|
|
384 |
// summary:
|
|
|
385 |
// See dojo.data.api.Identity.getIdentityAttributes()
|
|
|
386 |
//Identity isn't taken from a public attribute.
|
|
|
387 |
return null;
|
|
|
388 |
},
|
|
|
389 |
|
|
|
390 |
fetchItemByIdentity: function(keywordArgs){
|
|
|
391 |
// summary:
|
|
|
392 |
// See dojo.data.api.Identity.fetchItemByIdentity()
|
|
|
393 |
var identity = keywordArgs.identity;
|
|
|
394 |
var self = this;
|
|
|
395 |
var item = null
|
|
|
396 |
|
|
|
397 |
if(!this._rootNode){
|
|
|
398 |
if(!this.url){
|
|
|
399 |
this._rootNode = dojo.byId(this.tableId);
|
|
|
400 |
this._getHeadings();
|
|
|
401 |
for(var i=0; i<this._rootNode.rows.length; i++){
|
|
|
402 |
this._rootNode.rows[i].store = this;
|
|
|
403 |
}
|
|
|
404 |
item = this._rootNode.rows[identity+1];
|
|
|
405 |
if (keywordArgs.onItem){
|
|
|
406 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
407 |
keywordArgs.onItem.call(scope, item);
|
|
|
408 |
}
|
|
|
409 |
|
|
|
410 |
}else{
|
|
|
411 |
var getArgs = {
|
|
|
412 |
url: this.url,
|
|
|
413 |
handleAs: "text"
|
|
|
414 |
};
|
|
|
415 |
var self = this;
|
|
|
416 |
var getHandler = dojo.xhrGet(getArgs);
|
|
|
417 |
getHandler.addCallback(function(data){
|
|
|
418 |
var findNode = function(node, id){
|
|
|
419 |
if(node.id == id){
|
|
|
420 |
return node; //object
|
|
|
421 |
}
|
|
|
422 |
if(node.childNodes) {
|
|
|
423 |
for(var i=0; i<node.childNodes.length; i++){
|
|
|
424 |
var returnNode = findNode(node.childNodes[i], id);
|
|
|
425 |
if(returnNode){
|
|
|
426 |
return returnNode; //object
|
|
|
427 |
}
|
|
|
428 |
}
|
|
|
429 |
}
|
|
|
430 |
return null; //null
|
|
|
431 |
}
|
|
|
432 |
var d = document.createElement("div");
|
|
|
433 |
d.innerHTML = data;
|
|
|
434 |
self._rootNode = findNode(d, self.tableId);
|
|
|
435 |
self._getHeadings.call(self);
|
|
|
436 |
for(var i=0; i<self._rootNode.rows.length; i++){
|
|
|
437 |
self._rootNode.rows[i].store = self;
|
|
|
438 |
}
|
|
|
439 |
item = self._rootNode.rows[identity+1];
|
|
|
440 |
if (keywordArgs.onItem){
|
|
|
441 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
442 |
keywordArgs.onItem.call(scope, item);
|
|
|
443 |
}
|
|
|
444 |
});
|
|
|
445 |
getHandler.addErrback(function(error){
|
|
|
446 |
if(keywordArgs.onError){
|
|
|
447 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
448 |
keywordArgs.onError.call(scope, error);
|
|
|
449 |
|
|
|
450 |
}
|
|
|
451 |
});
|
|
|
452 |
}
|
|
|
453 |
}else{
|
|
|
454 |
if(this._rootNode.rows[identity+1]){
|
|
|
455 |
item = this._rootNode.rows[identity+1];
|
|
|
456 |
if (keywordArgs.onItem){
|
|
|
457 |
var scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
|
|
|
458 |
keywordArgs.onItem.call(scope, item);
|
|
|
459 |
}
|
|
|
460 |
}
|
|
|
461 |
}
|
|
|
462 |
}
|
|
|
463 |
});
|
|
|
464 |
dojo.extend(dojox.data.HtmlTableStore,dojo.data.util.simpleFetch);
|
|
|
465 |
|
|
|
466 |
}
|