| 1 |
define(["dojo/_base/kernel", "dojo/_base/declare", "dojo/_base/lang", "dojo/dom", "dojo/_base/array", |
| 2 |
"dojo/_base/xhr", "dojo/_base/sniff", "dojo/data/util/simpleFetch", |
| 3 |
"dojo/data/util/filter", "dojox/xml/parser"], |
| 4 |
function(kernel, declare, lang, dom, array, xhr, has, simpleFetch, filter, xmlParser) { |
| 5 |
|
| 6 |
var HtmlTableStore = declare("dojox.data.HtmlTableStore", null, { |
| 7 |
constructor: function(args){ |
| 8 |
kernel.deprecated("dojox.data.HtmlTableStore", "Please use dojox.data.HtmlStore"); |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 = dom.byId(args.tableId); |
| 53 |
this.tableId = this._rootNode.id; |
| 54 |
}else{ |
| 55 |
this._rootNode = dom.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 |
|
| 65 |
|
| 66 |
url: "", |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
tableId: "", |
| 71 |
|
| 72 |
_getHeadings: function(){ |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
this._headings = []; |
| 77 |
array.forEach(this._rootNode.tHead.rows[0].cells, lang.hitch(this, function(th){ |
| 78 |
this._headings.push(xmlParser.textContent(th)); |
| 79 |
})); |
| 80 |
}, |
| 81 |
|
| 82 |
_getAllItems: function(){ |
| 83 |
|
| 84 |
|
| 85 |
var items = []; |
| 86 |
for(var i=1; i<this._rootNode.rows.length; i++){ |
| 87 |
items.push(this._rootNode.rows[i]); |
| 88 |
} |
| 89 |
return items; |
| 90 |
}, |
| 91 |
|
| 92 |
_assertIsItem: function( item){ |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
if(!this.isItem(item)){ |
| 98 |
throw new Error("dojo.data.HtmlTableStore: a function was passed an item argument that was not an item"); |
| 99 |
} |
| 100 |
}, |
| 101 |
|
| 102 |
_assertIsAttribute: function( attribute){ |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if(typeof attribute !== "string"){ |
| 110 |
throw new Error("dojo.data.HtmlTableStore: a function was passed an attribute argument that was not an attribute name string"); |
| 111 |
} |
| 112 |
return array.indexOf(this._headings, attribute); |
| 113 |
}, |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
getValue: function( item, |
| 120 |
attribute, |
| 121 |
defaultValue){ |
| 122 |
|
| 123 |
|
| 124 |
var values = this.getValues(item, attribute); |
| 125 |
return (values.length > 0)?values[0]:defaultValue; |
| 126 |
}, |
| 127 |
|
| 128 |
getValues: function( item, |
| 129 |
attribute){ |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
this._assertIsItem(item); |
| 134 |
var index = this._assertIsAttribute(attribute); |
| 135 |
|
| 136 |
if(index>-1){ |
| 137 |
return [xmlParser.textContent(item.cells[index])] ; |
| 138 |
} |
| 139 |
return []; |
| 140 |
}, |
| 141 |
|
| 142 |
getAttributes: function( item){ |
| 143 |
|
| 144 |
|
| 145 |
this._assertIsItem(item); |
| 146 |
var attributes = []; |
| 147 |
for(var i=0; i<this._headings.length; i++){ |
| 148 |
if(this.hasAttribute(item, this._headings[i])) |
| 149 |
attributes.push(this._headings[i]); |
| 150 |
} |
| 151 |
return attributes; |
| 152 |
}, |
| 153 |
|
| 154 |
hasAttribute: function( item, |
| 155 |
attribute){ |
| 156 |
|
| 157 |
|
| 158 |
return this.getValues(item, attribute).length > 0; |
| 159 |
}, |
| 160 |
|
| 161 |
containsValue: function( item, |
| 162 |
attribute, |
| 163 |
value){ |
| 164 |
|
| 165 |
|
| 166 |
var regexp = undefined; |
| 167 |
if(typeof value === "string"){ |
| 168 |
regexp = filter.patternToRegExp(value, false); |
| 169 |
} |
| 170 |
return this._containsValue(item, attribute, value, regexp); |
| 171 |
}, |
| 172 |
|
| 173 |
_containsValue: function( item, |
| 174 |
attribute, |
| 175 |
value, |
| 176 |
regexp){ |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
var values = this.getValues(item, attribute); |
| 193 |
for(var i = 0; i < values.length; ++i){ |
| 194 |
var possibleValue = values[i]; |
| 195 |
if(typeof possibleValue === "string" && regexp){ |
| 196 |
return (possibleValue.match(regexp) !== null); |
| 197 |
}else{ |
| 198 |
|
| 199 |
if(value === possibleValue){ |
| 200 |
return true; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
return false; |
| 205 |
}, |
| 206 |
|
| 207 |
isItem: function( something){ |
| 208 |
|
| 209 |
|
| 210 |
if(something && something.store && something.store === this){ |
| 211 |
return true; |
| 212 |
} |
| 213 |
return false; |
| 214 |
}, |
| 215 |
|
| 216 |
isItemLoaded: function( something){ |
| 217 |
|
| 218 |
|
| 219 |
return this.isItem(something); |
| 220 |
}, |
| 221 |
|
| 222 |
loadItem: function( keywordArgs){ |
| 223 |
|
| 224 |
|
| 225 |
this._assertIsItem(keywordArgs.item); |
| 226 |
}, |
| 227 |
|
| 228 |
_fetchItems: function(request, fetchHandler, errorHandler){ |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if(this._rootNode){ |
| 248 |
this._finishFetchItems(request, fetchHandler, errorHandler); |
| 249 |
}else{ |
| 250 |
if(!this.url){ |
| 251 |
this._rootNode = dom.byId(this.tableId); |
| 252 |
this._getHeadings(); |
| 253 |
for(var i=0; i<this._rootNode.rows.length; i++){ |
| 254 |
this._rootNode.rows[i].store = this; |
| 255 |
} |
| 256 |
}else{ |
| 257 |
var getArgs = { |
| 258 |
url: this.url, |
| 259 |
handleAs: "text" |
| 260 |
}; |
| 261 |
var self = this; |
| 262 |
var getHandler = xhr.get(getArgs); |
| 263 |
getHandler.addCallback(function(data){ |
| 264 |
var findNode = function(node, id){ |
| 265 |
if(node.id == id){ |
| 266 |
return node; |
| 267 |
} |
| 268 |
if(node.childNodes){ |
| 269 |
for(var i=0; i<node.childNodes.length; i++){ |
| 270 |
var returnNode = findNode(node.childNodes[i], id); |
| 271 |
if(returnNode){ |
| 272 |
return returnNode; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
return null; |
| 277 |
} |
| 278 |
|
| 279 |
var d = document.createElement("div"); |
| 280 |
d.innerHTML = data; |
| 281 |
self._rootNode = findNode(d, self.tableId); |
| 282 |
self._getHeadings.call(self); |
| 283 |
for(var i=0; i<self._rootNode.rows.length; i++){ |
| 284 |
self._rootNode.rows[i].store = self; |
| 285 |
} |
| 286 |
self._finishFetchItems(request, fetchHandler, errorHandler); |
| 287 |
}); |
| 288 |
getHandler.addErrback(function(error){ |
| 289 |
errorHandler(error, request); |
| 290 |
}); |
| 291 |
} |
| 292 |
} |
| 293 |
}, |
| 294 |
|
| 295 |
_finishFetchItems: function(request, fetchHandler, errorHandler){ |
| 296 |
|
| 297 |
|
| 298 |
var items = null; |
| 299 |
var arrayOfAllItems = this._getAllItems(); |
| 300 |
if(request.query){ |
| 301 |
var ignoreCase = request.queryOptions ? request.queryOptions.ignoreCase : false; |
| 302 |
items = []; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
var regexpList = {}; |
| 307 |
var value; |
| 308 |
var key; |
| 309 |
for(key in request.query){ |
| 310 |
value = request.query[key]+''; |
| 311 |
if(typeof value === "string"){ |
| 312 |
regexpList[key] = filter.patternToRegExp(value, ignoreCase); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
for(var i = 0; i < arrayOfAllItems.length; ++i){ |
| 317 |
var match = true; |
| 318 |
var candidateItem = arrayOfAllItems[i]; |
| 319 |
for(key in request.query){ |
| 320 |
value = request.query[key]+''; |
| 321 |
if(!this._containsValue(candidateItem, key, value, regexpList[key])){ |
| 322 |
match = false; |
| 323 |
} |
| 324 |
} |
| 325 |
if(match){ |
| 326 |
items.push(candidateItem); |
| 327 |
} |
| 328 |
} |
| 329 |
fetchHandler(items, request); |
| 330 |
}else{ |
| 331 |
|
| 332 |
|
| 333 |
if(arrayOfAllItems.length> 0){ |
| 334 |
items = arrayOfAllItems.slice(0,arrayOfAllItems.length); |
| 335 |
} |
| 336 |
fetchHandler(items, request); |
| 337 |
} |
| 338 |
}, |
| 339 |
|
| 340 |
getFeatures: function(){ |
| 341 |
|
| 342 |
|
| 343 |
return { |
| 344 |
'dojo.data.api.Read': true, |
| 345 |
'dojo.data.api.Identity': true |
| 346 |
}; |
| 347 |
}, |
| 348 |
|
| 349 |
close: function( request){ |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
}, |
| 355 |
|
| 356 |
getLabel: function( item){ |
| 357 |
|
| 358 |
|
| 359 |
if(this.isItem(item)) |
| 360 |
return "Table Row #" + this.getIdentity(item); |
| 361 |
return undefined; |
| 362 |
}, |
| 363 |
|
| 364 |
getLabelAttributes: function( item){ |
| 365 |
|
| 366 |
|
| 367 |
return null; |
| 368 |
}, |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
getIdentity: function( item){ |
| 375 |
|
| 376 |
|
| 377 |
this._assertIsItem(item); |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if(!has("opera")){ |
| 382 |
return item.sectionRowIndex; |
| 383 |
}else{ |
| 384 |
return (array.indexOf(this._rootNode.rows, item) - 1) |
| 385 |
} |
| 386 |
}, |
| 387 |
|
| 388 |
getIdentityAttributes: function( item){ |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
return null; |
| 394 |
}, |
| 395 |
|
| 396 |
fetchItemByIdentity: function(keywordArgs){ |
| 397 |
|
| 398 |
|
| 399 |
var identity = keywordArgs.identity; |
| 400 |
var self = this; |
| 401 |
var item = null; |
| 402 |
var scope = null; |
| 403 |
|
| 404 |
if(!this._rootNode){ |
| 405 |
if(!this.url){ |
| 406 |
this._rootNode = dom.byId(this.tableId); |
| 407 |
this._getHeadings(); |
| 408 |
for(var i=0; i<this._rootNode.rows.length; i++){ |
| 409 |
this._rootNode.rows[i].store = this; |
| 410 |
} |
| 411 |
item = this._rootNode.rows[identity+1]; |
| 412 |
if(keywordArgs.onItem){ |
| 413 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 414 |
keywordArgs.onItem.call(scope, item); |
| 415 |
} |
| 416 |
|
| 417 |
}else{ |
| 418 |
var getArgs = { |
| 419 |
url: this.url, |
| 420 |
handleAs: "text" |
| 421 |
}; |
| 422 |
var getHandler = xhr.get(getArgs); |
| 423 |
getHandler.addCallback(function(data){ |
| 424 |
var findNode = function(node, id){ |
| 425 |
if(node.id == id){ |
| 426 |
return node; |
| 427 |
} |
| 428 |
if(node.childNodes){ |
| 429 |
for(var i=0; i<node.childNodes.length; i++){ |
| 430 |
var returnNode = findNode(node.childNodes[i], id); |
| 431 |
if(returnNode){ |
| 432 |
return returnNode; |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
return null; |
| 437 |
} |
| 438 |
var d = document.createElement("div"); |
| 439 |
d.innerHTML = data; |
| 440 |
self._rootNode = findNode(d, self.tableId); |
| 441 |
self._getHeadings.call(self); |
| 442 |
for(var i=0; i<self._rootNode.rows.length; i++){ |
| 443 |
self._rootNode.rows[i].store = self; |
| 444 |
} |
| 445 |
item = self._rootNode.rows[identity+1]; |
| 446 |
if(keywordArgs.onItem){ |
| 447 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 448 |
keywordArgs.onItem.call(scope, item); |
| 449 |
} |
| 450 |
}); |
| 451 |
getHandler.addErrback(function(error){ |
| 452 |
if(keywordArgs.onError){ |
| 453 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 454 |
keywordArgs.onError.call(scope, error); |
| 455 |
|
| 456 |
} |
| 457 |
}); |
| 458 |
} |
| 459 |
}else{ |
| 460 |
if(this._rootNode.rows[identity+1]){ |
| 461 |
item = this._rootNode.rows[identity+1]; |
| 462 |
if(keywordArgs.onItem){ |
| 463 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 464 |
keywordArgs.onItem.call(scope, item); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
}); |
| 470 |
lang.extend(HtmlTableStore,simpleFetch); |
| 471 |
|
| 472 |
return HtmlTableStore; |
| 473 |
}); |