1 if(!dojo._hasResource["dojox.data.HtmlStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.data.HtmlStore"] = true;
3 dojo.provide("dojox.data.HtmlStore");
5 dojo.require("dojox.data.dom");
6 dojo.require("dojo.data.util.simpleFetch");
7 dojo.require("dojo.data.util.filter");
9 dojo.declare("dojox.data.HtmlStore", null, {
10 constructor: function(/*Object*/args){
12 // Initializer for the HTML table store.
14 // The HtmlStore can be created in one of two ways: a) by parsing an existing
15 // table or list DOM node on the current page or b) by referencing an external url and giving
16 // the id of the table or listin that page. The remote url will be parsed as an html page.
18 // The HTML table or list should be of the following form:
20 // | <table id="myTable">
23 // | <th>Attribute1</th>
24 // | <th>Attribute2</th>
29 // | <td>Value1.1</td>
30 // | <td>Value1.2</td>
33 // | <td>Value2.1</td>
34 // | <td>Value2.2</td>
41 // | <ul id="myUnorderedList">
48 // | <ol id="myOrderedList">
54 // An anonymous object to initialize properties. It expects the following values:
55 // dataId: The id of the HTML table to use.
57 // url: The url of the remote page to load
58 // dataId: The id of the table element in the remote page
62 throw new Error("dojo.data.HtmlStore: Cannot instantiate using url without an id!");
64 this.dataId = args.dataId;
67 this._rootNode = dojo.byId(args.dataId);
68 this.dataId = this._rootNode.id;
70 this._rootNode = dojo.byId(this.dataId);
76 url: "", // So the parser can instantiate the store via markup.
77 dataId: "", // So the parser can instantiate the store via markup.
79 _indexItems: function(){
81 if (this._rootNode.rows){//tables
82 if(this._rootNode.tBodies && this._rootNode.tBodies.length > 0) {
83 this._rootNode = this._rootNode.tBodies[0];
86 for(i=0; i<this._rootNode.rows.length; i++){
87 this._rootNode.rows[i].store = this;
88 this._rootNode.rows[i]._ident = i+1;
92 for(i=0; i<this._rootNode.childNodes.length; i++){
93 if(this._rootNode.childNodes[i].nodeType===1){
94 this._rootNode.childNodes[i].store = this;
95 this._rootNode.childNodes[i]._ident = c;
102 _getHeadings: function(){
104 // Function to load the attribute names from the table header so that the
105 // attributes (cells in a row), can have a reasonable name.
106 // For list items, returns single implicit heading, ["name"]
108 if(this._rootNode.tHead){
109 dojo.forEach(this._rootNode.tHead.rows[0].cells, dojo.hitch(this, function(th){
110 this._headings.push(dojox.data.dom.textContent(th));
113 this._headings = ["name"];
117 _getAllItems: function(){
119 // Function to return all rows in the table as an array of items.
122 if(this._rootNode.rows){//table
123 for(i=0; i<this._rootNode.rows.length; i++){
124 items.push(this._rootNode.rows[i]);
127 for(i=0; i<this._rootNode.childNodes.length; i++){
128 if (this._rootNode.childNodes[i].nodeType===1){
129 items.push(this._rootNode.childNodes[i]);
133 return items; //array
136 _assertIsItem: function(/* item */ item){
138 // This function tests whether the item passed in is indeed an item in the store.
140 // The item to test for being contained by the store.
141 if(!this.isItem(item)){
142 throw new Error("dojo.data.HtmlStore: a function was passed an item argument that was not an item");
146 _assertIsAttribute: function(/* String */ attribute){
148 // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store.
150 // The attribute to test for being contained by the store.
153 // Returns the index (column) that the attribute resides in the row.
154 if(typeof attribute !== "string"){
155 throw new Error("dojo.data.HtmlStore: a function was passed an attribute argument that was not an attribute name string");
158 return dojo.indexOf(this._headings, attribute); //int
161 /***************************************
162 dojo.data.api.Read API
163 ***************************************/
165 getValue: function( /* item */ item,
166 /* attribute-name-string */ attribute,
167 /* value? */ defaultValue){
169 // See dojo.data.api.Read.getValue()
170 var values = this.getValues(item, attribute);
171 return (values.length > 0)?values[0]:defaultValue; //Object || int || Boolean
174 getValues: function(/* item */ item,
175 /* attribute-name-string */ attribute){
177 // See dojo.data.api.Read.getValues()
179 this._assertIsItem(item);
180 var index = this._assertIsAttribute(attribute);
184 return [dojox.data.dom.textContent(item.cells[index])];
185 }else{//return Value for lists
186 return [dojox.data.dom.textContent(item)];
192 getAttributes: function(/* item */ item){
194 // See dojo.data.api.Read.getAttributes()
195 this._assertIsItem(item);
197 for(var i=0; i<this._headings.length; i++){
198 if(this.hasAttribute(item, this._headings[i]))
199 attributes.push(this._headings[i]);
201 return attributes; //Array
204 hasAttribute: function( /* item */ item,
205 /* attribute-name-string */ attribute){
207 // See dojo.data.api.Read.hasAttribute()
208 return this.getValues(item, attribute).length > 0;
211 containsValue: function(/* item */ item,
212 /* attribute-name-string */ attribute,
213 /* anything */ value){
215 // See dojo.data.api.Read.containsValue()
216 var regexp = undefined;
217 if(typeof value === "string"){
218 regexp = dojo.data.util.filter.patternToRegExp(value, false);
220 return this._containsValue(item, attribute, value, regexp); //boolean.
223 _containsValue: function( /* item */ item,
224 /* attribute-name-string */ attribute,
225 /* anything */ value,
226 /* RegExp?*/ regexp){
228 // Internal function for looking at the values contained by the item.
230 // Internal function for looking at the values contained by the item. This
231 // function allows for denoting if the comparison should be case sensitive for
232 // strings or not (for handling filtering cases where string case should not matter)
235 // The data item to examine for attribute values.
237 // The attribute to inspect.
239 // The value to match.
241 // Optional regular expression generated off value if value was of string type to handle wildcarding.
242 // If present and attribute values are string, then it can be used for comparison instead of 'value'
243 var values = this.getValues(item, attribute);
244 for(var i = 0; i < values.length; ++i){
245 var possibleValue = values[i];
246 if(typeof possibleValue === "string" && regexp){
247 return (possibleValue.match(regexp) !== null);
249 //Non-string matching.
250 if(value === possibleValue){
251 return true; // Boolean
255 return false; // Boolean
258 isItem: function(/* anything */ something){
260 // See dojo.data.api.Read.isItem()
261 if(something && something.store && something.store === this){
262 return true; //boolean
264 return false; //boolean
267 isItemLoaded: function(/* anything */ something){
269 // See dojo.data.api.Read.isItemLoaded()
270 return this.isItem(something);
273 loadItem: function(/* Object */ keywordArgs){
275 // See dojo.data.api.Read.loadItem()
276 this._assertIsItem(keywordArgs.item);
279 _fetchItems: function(request, fetchHandler, errorHandler){
281 // Fetch items (XML elements) that match to a query
283 // If '_fetchUrl' is specified, it is used to load an XML document
284 // with a query string.
285 // Otherwise and if 'url' is specified, the XML document is
286 // loaded and list XML elements that match to a query (set of element
287 // names and their text attribute values that the items to contain).
288 // A wildcard, "*" can be used to query values to match all
290 // If '_rootItem' is specified, it is used to fetch items.
294 // A function to call for fetched items
296 // A function to call on error
299 this._finishFetchItems(request, fetchHandler, errorHandler);
302 this._rootNode = dojo.byId(this.dataId);
309 var getHandler = dojo.xhrGet(getArgs);
310 getHandler.addCallback(function(data){
311 var findNode = function(node, id){
313 return node; //object
316 for(var i=0; i<node.childNodes.length; i++){
317 var returnNode = findNode(node.childNodes[i], id);
319 return returnNode; //object
326 var d = document.createElement("div");
328 self._rootNode = findNode(d, self.dataId);
330 self._finishFetchItems(request, fetchHandler, errorHandler);
332 getHandler.addErrback(function(error){
333 errorHandler(error, request);
339 _finishFetchItems: function(request, fetchHandler, errorHandler){
341 // Internal function for processing the passed in request and locating the requested items.
343 var arrayOfAllItems = this._getAllItems();
345 var ignoreCase = request.queryOptions ? request.queryOptions.ignoreCase : false;
348 //See if there are any string values that can be regexp parsed first to avoid multiple regexp gens on the
349 //same value for each item examined. Much more efficient.
353 for(key in request.query){
354 value = request.query[key]+'';
355 if(typeof value === "string"){
356 regexpList[key] = dojo.data.util.filter.patternToRegExp(value, ignoreCase);
360 for(var i = 0; i < arrayOfAllItems.length; ++i){
362 var candidateItem = arrayOfAllItems[i];
363 for(key in request.query){
364 value = request.query[key]+'';
365 if (!this._containsValue(candidateItem, key, value, regexpList[key])){
370 items.push(candidateItem);
373 fetchHandler(items, request);
375 // We want a copy to pass back in case the parent wishes to sort the array. We shouldn't allow resort
376 // of the internal list so that multiple callers can get listsand sort without affecting each other.
377 if(arrayOfAllItems.length> 0){
378 items = arrayOfAllItems.slice(0,arrayOfAllItems.length);
380 fetchHandler(items, request);
384 getFeatures: function(){
386 // See dojo.data.api.Read.getFeatures()
388 'dojo.data.api.Read': true,
389 'dojo.data.api.Identity': true
393 close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
395 // See dojo.data.api.Read.close()
396 // nothing to do here!
399 getLabel: function(/* item */ item){
401 // See dojo.data.api.Read.getLabel()
402 if(this.isItem(item)){
404 return "Item #" + this.getIdentity(item);
406 return this.getValue(item,"name");
412 getLabelAttributes: function(/* item */ item){
414 // See dojo.data.api.Read.getLabelAttributes()
422 /***************************************
423 dojo.data.api.Identity API
424 ***************************************/
426 getIdentity: function(/* item */ item){
428 // See dojo.data.api.Identity.getIdentity()
429 this._assertIsItem(item);
430 if(this.hasAttribute(item, "name")){
431 return this.getValue(item,"name");
437 getIdentityAttributes: function(/* item */ item){
439 // See dojo.data.api.Identity.getIdentityAttributes()
440 //Identity isn't taken from a public attribute.
444 fetchItemByIdentity: function(keywordArgs){
446 // See dojo.data.api.Identity.fetchItemByIdentity()
447 var identity = keywordArgs.identity;
454 this._rootNode = dojo.byId(this.dataId);
456 if(self._rootNode.rows){ //Table
457 item = this._rootNode.rows[identity + 1];
459 for(var i = 0; i < self._rootNode.childNodes.length; i++){
460 if(self._rootNode.childNodes[i].nodeType === 1 && identity === dojox.data.dom.textContent(self._rootNode.childNodes[i])) {
461 item = self._rootNode.childNodes[i];
465 if(keywordArgs.onItem){
466 scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
467 keywordArgs.onItem.call(scope, item);
475 var getHandler = dojo.xhrGet(getArgs);
476 getHandler.addCallback(function(data){
477 var findNode = function(node, id){
479 return node; //object
482 for(var i=0; i<node.childNodes.length; i++){
483 var returnNode = findNode(node.childNodes[i], id);
485 return returnNode; //object
491 var d = document.createElement("div");
493 self._rootNode = findNode(d, self.dataId);
495 if(self._rootNode.rows && identity <= self._rootNode.rows.length){ //Table
496 item = self._rootNode.rows[identity-1];
498 for(var i = 0; i < self._rootNode.childNodes.length; i++){
499 if(self._rootNode.childNodes[i].nodeType === 1 && identity === dojox.data.dom.textContent(self._rootNode.childNodes[i])) {
500 item = self._rootNode.childNodes[i];
505 if(keywordArgs.onItem){
506 scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
507 keywordArgs.onItem.call(scope, item);
510 getHandler.addErrback(function(error){
511 if(keywordArgs.onError){
512 scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
513 keywordArgs.onError.call(scope, error);
519 if(this._rootNode.rows[identity+1]){
520 item = this._rootNode.rows[identity+1];
521 if(keywordArgs.onItem){
522 scope = keywordArgs.scope?keywordArgs.scope:dojo.global;
523 keywordArgs.onItem.call(scope, item);
529 dojo.extend(dojox.data.HtmlStore,dojo.data.util.simpleFetch);