| 1 |
define(["dojo/_base/declare", "dojo/_base/array", "dojo/_base/lang", "dojo/dom", "dojo/_base/xhr", "dojo/_base/kernel", |
| 2 |
"dojo/data/util/simpleFetch", "dojo/data/util/filter", "dojox/xml/parser"], |
| 3 |
function(declare, array, lang, dom, xhr, kernel, simpleFetch, filter, xmlParser) { |
| 4 |
|
| 5 |
var HtmlStore = declare("dojox.data.HtmlStore", null, { |
| 6 |
constructor: function( args){ |
| 7 |
|
| 8 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
if(args && "urlPreventCache" in args){ |
| 66 |
this.urlPreventCache = args.urlPreventCache?true:false; |
| 67 |
} |
| 68 |
if(args && "trimWhitespace" in args){ |
| 69 |
this.trimWhitespace = args.trimWhitespace?true:false; |
| 70 |
} |
| 71 |
if(args.url){ |
| 72 |
if(!args.dataId){ |
| 73 |
throw new Error("dojo.data.HtmlStore: Cannot instantiate using url without an id!"); |
| 74 |
} |
| 75 |
this.url = args.url; |
| 76 |
this.dataId = args.dataId; |
| 77 |
}else{ |
| 78 |
if(args.dataId){ |
| 79 |
this.dataId = args.dataId; |
| 80 |
} |
| 81 |
} |
| 82 |
if(args && "fetchOnCreate" in args){ |
| 83 |
this.fetchOnCreate = args.fetchOnCreate?true:false; |
| 84 |
} |
| 85 |
if(this.fetchOnCreate && this.dataId){ |
| 86 |
this.fetch(); |
| 87 |
} |
| 88 |
}, |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
url: "", |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
dataId: "", |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
trimWhitespace: false, |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
urlPreventCache: false, |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
fetchOnCreate: false, |
| 114 |
|
| 115 |
_indexItems: function(){ |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
this._getHeadings(); |
| 121 |
if(this._rootNode.rows){ |
| 122 |
if(this._rootNode.tBodies && this._rootNode.tBodies.length > 0){ |
| 123 |
this._rootNode = this._rootNode.tBodies[0]; |
| 124 |
} |
| 125 |
var i; |
| 126 |
for(i=0; i<this._rootNode.rows.length; i++){ |
| 127 |
this._rootNode.rows[i]._ident = i+1; |
| 128 |
} |
| 129 |
}else{ |
| 130 |
var c=1; |
| 131 |
for(i=0; i<this._rootNode.childNodes.length; i++){ |
| 132 |
if(this._rootNode.childNodes[i].nodeType === 1){ |
| 133 |
this._rootNode.childNodes[i]._ident = c; |
| 134 |
c++; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
}, |
| 139 |
|
| 140 |
_getHeadings: function(){ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
this._headings = []; |
| 146 |
if(this._rootNode.tHead){ |
| 147 |
array.forEach(this._rootNode.tHead.rows[0].cells, lang.hitch(this, function(th){ |
| 148 |
var text = xmlParser.textContent(th); |
| 149 |
this._headings.push(this.trimWhitespace?lang.trim(text):text); |
| 150 |
})); |
| 151 |
}else{ |
| 152 |
this._headings = ["name"]; |
| 153 |
} |
| 154 |
}, |
| 155 |
|
| 156 |
_getAllItems: function(){ |
| 157 |
|
| 158 |
|
| 159 |
var items = []; |
| 160 |
var i; |
| 161 |
if(this._rootNode.rows){ |
| 162 |
for(i=0; i<this._rootNode.rows.length; i++){ |
| 163 |
items.push(this._rootNode.rows[i]); |
| 164 |
} |
| 165 |
}else{ |
| 166 |
for(i=0; i<this._rootNode.childNodes.length; i++){ |
| 167 |
if(this._rootNode.childNodes[i].nodeType === 1){ |
| 168 |
items.push(this._rootNode.childNodes[i]); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
return items; |
| 173 |
}, |
| 174 |
|
| 175 |
_assertIsItem: function( item){ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if(!this.isItem(item)){ |
| 181 |
throw new Error("dojo.data.HtmlStore: a function was passed an item argument that was not an item"); |
| 182 |
} |
| 183 |
}, |
| 184 |
|
| 185 |
_assertIsAttribute: function( attribute){ |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if(typeof attribute !== "string"){ |
| 193 |
throw new Error("dojo.data.HtmlStore: a function was passed an attribute argument that was not an attribute name string"); |
| 194 |
} |
| 195 |
return array.indexOf(this._headings, attribute); |
| 196 |
}, |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
getValue: function( item, |
| 203 |
attribute, |
| 204 |
defaultValue){ |
| 205 |
|
| 206 |
|
| 207 |
var values = this.getValues(item, attribute); |
| 208 |
return (values.length > 0)?values[0]:defaultValue; |
| 209 |
}, |
| 210 |
|
| 211 |
getValues: function( item, |
| 212 |
attribute){ |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
this._assertIsItem(item); |
| 217 |
var index = this._assertIsAttribute(attribute); |
| 218 |
if(index>-1){ |
| 219 |
var text; |
| 220 |
if(item.cells){ |
| 221 |
text = xmlParser.textContent(item.cells[index]); |
| 222 |
}else{ |
| 223 |
text = xmlParser.textContent(item); |
| 224 |
} |
| 225 |
return [this.trimWhitespace?lang.trim(text):text]; |
| 226 |
} |
| 227 |
return []; |
| 228 |
}, |
| 229 |
|
| 230 |
getAttributes: function( item){ |
| 231 |
|
| 232 |
|
| 233 |
this._assertIsItem(item); |
| 234 |
var attributes = []; |
| 235 |
for(var i=0; i<this._headings.length; i++){ |
| 236 |
if(this.hasAttribute(item, this._headings[i])) |
| 237 |
attributes.push(this._headings[i]); |
| 238 |
} |
| 239 |
return attributes; |
| 240 |
}, |
| 241 |
|
| 242 |
hasAttribute: function( item, |
| 243 |
attribute){ |
| 244 |
|
| 245 |
|
| 246 |
return this.getValues(item, attribute).length > 0; |
| 247 |
}, |
| 248 |
|
| 249 |
containsValue: function( item, |
| 250 |
attribute, |
| 251 |
value){ |
| 252 |
|
| 253 |
|
| 254 |
var regexp = undefined; |
| 255 |
if(typeof value === "string"){ |
| 256 |
regexp = filter.patternToRegExp(value, false); |
| 257 |
} |
| 258 |
return this._containsValue(item, attribute, value, regexp); |
| 259 |
}, |
| 260 |
|
| 261 |
_containsValue: function( item, |
| 262 |
attribute, |
| 263 |
value, |
| 264 |
regexp){ |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
var values = this.getValues(item, attribute); |
| 281 |
for(var i = 0; i < values.length; ++i){ |
| 282 |
var possibleValue = values[i]; |
| 283 |
if(typeof possibleValue === "string" && regexp){ |
| 284 |
return (possibleValue.match(regexp) !== null); |
| 285 |
}else{ |
| 286 |
|
| 287 |
if(value === possibleValue){ |
| 288 |
return true; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
return false; |
| 293 |
}, |
| 294 |
|
| 295 |
isItem: function( something){ |
| 296 |
|
| 297 |
|
| 298 |
return something && dom.isDescendant(something, this._rootNode); |
| 299 |
}, |
| 300 |
|
| 301 |
isItemLoaded: function( something){ |
| 302 |
|
| 303 |
|
| 304 |
return this.isItem(something); |
| 305 |
}, |
| 306 |
|
| 307 |
loadItem: function( keywordArgs){ |
| 308 |
|
| 309 |
|
| 310 |
this._assertIsItem(keywordArgs.item); |
| 311 |
}, |
| 312 |
|
| 313 |
_fetchItems: function(request, fetchHandler, errorHandler){ |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
if(this._rootNode){ |
| 333 |
this._finishFetchItems(request, fetchHandler, errorHandler); |
| 334 |
}else{ |
| 335 |
if(!this.url){ |
| 336 |
this._rootNode = dom.byId(this.dataId); |
| 337 |
this._indexItems(); |
| 338 |
this._finishFetchItems(request, fetchHandler, errorHandler); |
| 339 |
}else{ |
| 340 |
var getArgs = { |
| 341 |
url: this.url, |
| 342 |
handleAs: "text", |
| 343 |
preventCache: this.urlPreventCache |
| 344 |
}; |
| 345 |
var self = this; |
| 346 |
var getHandler = xhr.get(getArgs); |
| 347 |
getHandler.addCallback(function(data){ |
| 348 |
var findNode = function(node, id){ |
| 349 |
if(node.id == id){ |
| 350 |
return node; |
| 351 |
} |
| 352 |
if(node.childNodes){ |
| 353 |
for(var i=0; i<node.childNodes.length; i++){ |
| 354 |
var returnNode = findNode(node.childNodes[i], id); |
| 355 |
if(returnNode){ |
| 356 |
return returnNode; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
return null; |
| 361 |
} |
| 362 |
|
| 363 |
var d = document.createElement("div"); |
| 364 |
d.innerHTML = data; |
| 365 |
self._rootNode = findNode(d, self.dataId); |
| 366 |
self._indexItems(); |
| 367 |
self._finishFetchItems(request, fetchHandler, errorHandler); |
| 368 |
}); |
| 369 |
getHandler.addErrback(function(error){ |
| 370 |
errorHandler(error, request); |
| 371 |
}); |
| 372 |
} |
| 373 |
} |
| 374 |
}, |
| 375 |
|
| 376 |
_finishFetchItems: function(request, fetchHandler, errorHandler){ |
| 377 |
|
| 378 |
|
| 379 |
var items = []; |
| 380 |
var arrayOfAllItems = this._getAllItems(); |
| 381 |
if(request.query){ |
| 382 |
var ignoreCase = request.queryOptions ? request.queryOptions.ignoreCase : false; |
| 383 |
items = []; |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
var regexpList = {}; |
| 388 |
var key; |
| 389 |
var value; |
| 390 |
for(key in request.query){ |
| 391 |
value = request.query[key]+''; |
| 392 |
if(typeof value === "string"){ |
| 393 |
regexpList[key] = filter.patternToRegExp(value, ignoreCase); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
for(var i = 0; i < arrayOfAllItems.length; ++i){ |
| 398 |
var match = true; |
| 399 |
var candidateItem = arrayOfAllItems[i]; |
| 400 |
for(key in request.query){ |
| 401 |
value = request.query[key]+''; |
| 402 |
if(!this._containsValue(candidateItem, key, value, regexpList[key])){ |
| 403 |
match = false; |
| 404 |
} |
| 405 |
} |
| 406 |
if(match){ |
| 407 |
items.push(candidateItem); |
| 408 |
} |
| 409 |
} |
| 410 |
fetchHandler(items, request); |
| 411 |
}else{ |
| 412 |
|
| 413 |
|
| 414 |
if(arrayOfAllItems.length> 0){ |
| 415 |
items = arrayOfAllItems.slice(0,arrayOfAllItems.length); |
| 416 |
} |
| 417 |
fetchHandler(items, request); |
| 418 |
} |
| 419 |
}, |
| 420 |
|
| 421 |
getFeatures: function(){ |
| 422 |
|
| 423 |
|
| 424 |
return { |
| 425 |
'dojo.data.api.Read': true, |
| 426 |
'dojo.data.api.Identity': true |
| 427 |
}; |
| 428 |
}, |
| 429 |
|
| 430 |
close: function( request){ |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
}, |
| 436 |
|
| 437 |
getLabel: function( item){ |
| 438 |
|
| 439 |
|
| 440 |
if(this.isItem(item)){ |
| 441 |
if(item.cells){ |
| 442 |
return "Item #" + this.getIdentity(item); |
| 443 |
}else{ |
| 444 |
return this.getValue(item,"name"); |
| 445 |
} |
| 446 |
} |
| 447 |
return undefined; |
| 448 |
}, |
| 449 |
|
| 450 |
getLabelAttributes: function( item){ |
| 451 |
|
| 452 |
|
| 453 |
if(item.cells){ |
| 454 |
return null; |
| 455 |
}else{ |
| 456 |
return ["name"]; |
| 457 |
} |
| 458 |
}, |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
getIdentity: function( item){ |
| 465 |
|
| 466 |
|
| 467 |
this._assertIsItem(item); |
| 468 |
if(this.hasAttribute(item, "name")){ |
| 469 |
return this.getValue(item,"name"); |
| 470 |
}else{ |
| 471 |
return item._ident; |
| 472 |
} |
| 473 |
}, |
| 474 |
|
| 475 |
getIdentityAttributes: function( item){ |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
return null; |
| 481 |
}, |
| 482 |
|
| 483 |
fetchItemByIdentity: function(keywordArgs){ |
| 484 |
|
| 485 |
|
| 486 |
var identity = keywordArgs.identity; |
| 487 |
var self = this; |
| 488 |
var item = null; |
| 489 |
var scope = null; |
| 490 |
if(!this._rootNode){ |
| 491 |
if(!this.url){ |
| 492 |
this._rootNode = dom.byId(this.dataId); |
| 493 |
this._indexItems(); |
| 494 |
if(self._rootNode.rows){ |
| 495 |
item = this._rootNode.rows[identity + 1]; |
| 496 |
}else{ |
| 497 |
for(var i = 0; i < self._rootNode.childNodes.length; i++){ |
| 498 |
if(self._rootNode.childNodes[i].nodeType === 1 && identity === xmlParser.textContent(self._rootNode.childNodes[i])){ |
| 499 |
item = self._rootNode.childNodes[i]; |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
if(keywordArgs.onItem){ |
| 504 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 505 |
keywordArgs.onItem.call(scope, item); |
| 506 |
} |
| 507 |
|
| 508 |
}else{ |
| 509 |
var getArgs = { |
| 510 |
url: this.url, |
| 511 |
handleAs: "text" |
| 512 |
}; |
| 513 |
var getHandler = xhr.get(getArgs); |
| 514 |
getHandler.addCallback(function(data){ |
| 515 |
var findNode = function(node, id){ |
| 516 |
if(node.id == id){ |
| 517 |
return node; |
| 518 |
} |
| 519 |
if(node.childNodes){ |
| 520 |
for(var i=0; i<node.childNodes.length; i++){ |
| 521 |
var returnNode = findNode(node.childNodes[i], id); |
| 522 |
if(returnNode){ |
| 523 |
return returnNode; |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
return null; |
| 528 |
} |
| 529 |
var d = document.createElement("div"); |
| 530 |
d.innerHTML = data; |
| 531 |
self._rootNode = findNode(d, self.dataId); |
| 532 |
self._indexItems(); |
| 533 |
if(self._rootNode.rows && identity <= self._rootNode.rows.length){ |
| 534 |
item = self._rootNode.rows[identity-1]; |
| 535 |
}else{ |
| 536 |
for(var i = 0; i < self._rootNode.childNodes.length; i++){ |
| 537 |
if(self._rootNode.childNodes[i].nodeType === 1 && identity === xmlParser.textContent(self._rootNode.childNodes[i])){ |
| 538 |
item = self._rootNode.childNodes[i]; |
| 539 |
break; |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
if(keywordArgs.onItem){ |
| 544 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 545 |
keywordArgs.onItem.call(scope, item); |
| 546 |
} |
| 547 |
}); |
| 548 |
getHandler.addErrback(function(error){ |
| 549 |
if(keywordArgs.onError){ |
| 550 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 551 |
keywordArgs.onError.call(scope, error); |
| 552 |
|
| 553 |
} |
| 554 |
}); |
| 555 |
} |
| 556 |
}else{ |
| 557 |
if(this._rootNode.rows[identity+1]){ |
| 558 |
item = this._rootNode.rows[identity+1]; |
| 559 |
if(keywordArgs.onItem){ |
| 560 |
scope = keywordArgs.scope?keywordArgs.scope:kernel.global; |
| 561 |
keywordArgs.onItem.call(scope, item); |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
}); |
| 567 |
lang.extend(HtmlStore, simpleFetch); |
| 568 |
return HtmlStore; |
| 569 |
}); |