| 1 |
define(["dojo", "dojox", "dojo/data/util/filter", "dojo/data/util/simpleFetch", "dojo/date/stamp"], function(dojo, dojox) { |
| 2 |
dojo.experimental("dojox.data.AtomReadStore"); |
| 3 |
|
| 4 |
var AtomReadStore = dojo.declare("dojox.data.AtomReadStore", null, { |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
constructor: function( args){ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
if(args){ |
| 22 |
this.url = args.url; |
| 23 |
this.rewriteUrl = args.rewriteUrl; |
| 24 |
this.label = args.label || this.label; |
| 25 |
this.sendQuery = (args.sendQuery || args.sendquery || this.sendQuery); |
| 26 |
this.unescapeHTML = args.unescapeHTML; |
| 27 |
if("urlPreventCache" in args){ |
| 28 |
this.urlPreventCache = args.urlPreventCache?true:false; |
| 29 |
} |
| 30 |
} |
| 31 |
if(!this.url){ |
| 32 |
throw new Error("AtomReadStore: a URL must be specified when creating the data store"); |
| 33 |
} |
| 34 |
}, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
url: "", |
| 40 |
|
| 41 |
label: "title", |
| 42 |
|
| 43 |
sendQuery: false, |
| 44 |
|
| 45 |
unescapeHTML: false, |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
urlPreventCache: false, |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
getValue: function( item, attribute, defaultValue){ |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
this._assertIsItem(item); |
| 69 |
this._assertIsAttribute(attribute); |
| 70 |
this._initItem(item); |
| 71 |
attribute = attribute.toLowerCase(); |
| 72 |
|
| 73 |
if(!item._attribs[attribute] && !item._parsed){ |
| 74 |
this._parseItem(item); |
| 75 |
item._parsed = true; |
| 76 |
} |
| 77 |
var retVal = item._attribs[attribute]; |
| 78 |
|
| 79 |
if(!retVal && attribute == "summary"){ |
| 80 |
var content = this.getValue(item, "content"); |
| 81 |
var regexp = new RegExp("/(<([^>]+)>)/g", "i"); |
| 82 |
var text = content.text.replace(regexp,""); |
| 83 |
retVal = { |
| 84 |
text: text.substring(0, Math.min(400, text.length)), |
| 85 |
type: "text" |
| 86 |
}; |
| 87 |
item._attribs[attribute] = retVal; |
| 88 |
} |
| 89 |
|
| 90 |
if(retVal && this.unescapeHTML){ |
| 91 |
if((attribute == "content" || attribute == "summary" || attribute == "subtitle") && !item["_"+attribute+"Escaped"]){ |
| 92 |
retVal.text = this._unescapeHTML(retVal.text); |
| 93 |
item["_"+attribute+"Escaped"] = true; |
| 94 |
} |
| 95 |
} |
| 96 |
return retVal ? dojo.isArray(retVal) ? retVal[0]: retVal : defaultValue; |
| 97 |
}, |
| 98 |
|
| 99 |
getValues: function( item, attribute){ |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
this._assertIsItem(item); |
| 113 |
this._assertIsAttribute(attribute); |
| 114 |
this._initItem(item); |
| 115 |
attribute = attribute.toLowerCase(); |
| 116 |
|
| 117 |
if(!item._attribs[attribute]){ |
| 118 |
this._parseItem(item); |
| 119 |
} |
| 120 |
var retVal = item._attribs[attribute]; |
| 121 |
return retVal ? ((retVal.length !== undefined && typeof(retVal) !== "string") ? retVal : [retVal]) : undefined; |
| 122 |
}, |
| 123 |
|
| 124 |
getAttributes: function( item){ |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
this._assertIsItem(item); |
| 140 |
if(!item._attribs){ |
| 141 |
this._initItem(item); |
| 142 |
this._parseItem(item); |
| 143 |
} |
| 144 |
var attrNames = []; |
| 145 |
for(var x in item._attribs){ |
| 146 |
attrNames.push(x); |
| 147 |
} |
| 148 |
return attrNames; |
| 149 |
}, |
| 150 |
|
| 151 |
hasAttribute: function( item, attribute){ |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
return (this.getValue(item, attribute) !== undefined); |
| 161 |
}, |
| 162 |
|
| 163 |
containsValue: function( item, attribute, value){ |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
var values = this.getValues(item, attribute); |
| 174 |
for(var i = 0; i < values.length; i++){ |
| 175 |
if((typeof value === "string")){ |
| 176 |
if(values[i].toString && values[i].toString() === value){ |
| 177 |
return true; |
| 178 |
} |
| 179 |
}else if(values[i] === value){ |
| 180 |
return true; |
| 181 |
} |
| 182 |
} |
| 183 |
return false; |
| 184 |
}, |
| 185 |
|
| 186 |
isItem: function( something){ |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if(something && something.element && something.store && something.store === this){ |
| 194 |
return true; |
| 195 |
} |
| 196 |
return false; |
| 197 |
}, |
| 198 |
|
| 199 |
isItemLoaded: function( something){ |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
return this.isItem(something); |
| 207 |
}, |
| 208 |
|
| 209 |
loadItem: function( keywordArgs){ |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
}, |
| 215 |
|
| 216 |
getFeatures: function(){ |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
var features = { |
| 222 |
"dojo.data.api.Read": true |
| 223 |
}; |
| 224 |
return features; |
| 225 |
}, |
| 226 |
|
| 227 |
getLabel: function( item){ |
| 228 |
|
| 229 |
|
| 230 |
if((this.label !== "") && this.isItem(item)){ |
| 231 |
var label = this.getValue(item,this.label); |
| 232 |
if(label && label.text){ |
| 233 |
return label.text; |
| 234 |
}else if(label){ |
| 235 |
return label.toString(); |
| 236 |
}else{ |
| 237 |
return undefined; |
| 238 |
} |
| 239 |
} |
| 240 |
return undefined; |
| 241 |
}, |
| 242 |
|
| 243 |
getLabelAttributes: function( item){ |
| 244 |
|
| 245 |
|
| 246 |
if(this.label !== ""){ |
| 247 |
return [this.label]; |
| 248 |
} |
| 249 |
return null; |
| 250 |
}, |
| 251 |
|
| 252 |
getFeedValue: function(attribute, defaultValue){ |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
var values = this.getFeedValues(attribute, defaultValue); |
| 257 |
if(dojo.isArray(values)){ |
| 258 |
return values[0]; |
| 259 |
} |
| 260 |
return values; |
| 261 |
}, |
| 262 |
|
| 263 |
getFeedValues: function(attribute, defaultValue){ |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if(!this.doc){ |
| 268 |
return defaultValue; |
| 269 |
} |
| 270 |
if(!this._feedMetaData){ |
| 271 |
this._feedMetaData = { |
| 272 |
element: this.doc.getElementsByTagName("feed")[0], |
| 273 |
store: this, |
| 274 |
_attribs: {} |
| 275 |
}; |
| 276 |
this._parseItem(this._feedMetaData); |
| 277 |
} |
| 278 |
return this._feedMetaData._attribs[attribute] || defaultValue; |
| 279 |
}, |
| 280 |
|
| 281 |
_initItem: function(item){ |
| 282 |
|
| 283 |
|
| 284 |
if(!item._attribs){ |
| 285 |
item._attribs = {}; |
| 286 |
} |
| 287 |
}, |
| 288 |
|
| 289 |
_fetchItems: function(request, fetchHandler, errorHandler){ |
| 290 |
|
| 291 |
|
| 292 |
var url = this._getFetchUrl(request); |
| 293 |
if(!url){ |
| 294 |
errorHandler(new Error("No URL specified.")); |
| 295 |
return; |
| 296 |
} |
| 297 |
var localRequest = (!this.sendQuery ? request : null); |
| 298 |
|
| 299 |
var _this = this; |
| 300 |
var docHandler = function(data){ |
| 301 |
_this.doc = data; |
| 302 |
var items = _this._getItems(data, localRequest); |
| 303 |
var query = request.query; |
| 304 |
if(query){ |
| 305 |
if(query.id){ |
| 306 |
items = dojo.filter(items, function(item){ |
| 307 |
return (_this.getValue(item, "id") == query.id); |
| 308 |
}); |
| 309 |
}else if(query.category){ |
| 310 |
items = dojo.filter(items, function(entry){ |
| 311 |
var cats = _this.getValues(entry, "category"); |
| 312 |
if(!cats){ |
| 313 |
return false; |
| 314 |
} |
| 315 |
return dojo.some(cats, "return item.term=='"+query.category+"'"); |
| 316 |
}); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
if(items && items.length > 0){ |
| 321 |
fetchHandler(items, request); |
| 322 |
}else{ |
| 323 |
fetchHandler([], request); |
| 324 |
} |
| 325 |
}; |
| 326 |
|
| 327 |
if(this.doc){ |
| 328 |
docHandler(this.doc); |
| 329 |
}else{ |
| 330 |
var getArgs = { |
| 331 |
url: url, |
| 332 |
handleAs: "xml", |
| 333 |
preventCache: this.urlPreventCache |
| 334 |
}; |
| 335 |
var getHandler = dojo.xhrGet(getArgs); |
| 336 |
getHandler.addCallback(docHandler); |
| 337 |
|
| 338 |
getHandler.addErrback(function(data){ |
| 339 |
errorHandler(data, request); |
| 340 |
}); |
| 341 |
} |
| 342 |
}, |
| 343 |
|
| 344 |
_getFetchUrl: function(request){ |
| 345 |
if(!this.sendQuery){ |
| 346 |
return this.url; |
| 347 |
} |
| 348 |
var query = request.query; |
| 349 |
if(!query){ |
| 350 |
return this.url; |
| 351 |
} |
| 352 |
if(dojo.isString(query)){ |
| 353 |
return this.url + query; |
| 354 |
} |
| 355 |
var queryString = ""; |
| 356 |
for(var name in query){ |
| 357 |
var value = query[name]; |
| 358 |
if(value){ |
| 359 |
if(queryString){ |
| 360 |
queryString += "&"; |
| 361 |
} |
| 362 |
queryString += (name + "=" + value); |
| 363 |
} |
| 364 |
} |
| 365 |
if(!queryString){ |
| 366 |
return this.url; |
| 367 |
} |
| 368 |
|
| 369 |
var fullUrl = this.url; |
| 370 |
if(fullUrl.indexOf("?") < 0){ |
| 371 |
fullUrl += "?"; |
| 372 |
}else{ |
| 373 |
fullUrl += "&"; |
| 374 |
} |
| 375 |
return fullUrl + queryString; |
| 376 |
}, |
| 377 |
|
| 378 |
_getItems: function(document, request){ |
| 379 |
|
| 380 |
|
| 381 |
if(this._items){ |
| 382 |
return this._items; |
| 383 |
} |
| 384 |
var items = []; |
| 385 |
var nodes = []; |
| 386 |
|
| 387 |
if(document.childNodes.length < 1){ |
| 388 |
this._items = items; |
| 389 |
console.log("dojox.data.AtomReadStore: Received an invalid Atom document. Check the content type header"); |
| 390 |
return items; |
| 391 |
} |
| 392 |
|
| 393 |
var feedNodes = dojo.filter(document.childNodes, "return item.tagName && item.tagName.toLowerCase() == 'feed'"); |
| 394 |
|
| 395 |
var query = request.query; |
| 396 |
|
| 397 |
if(!feedNodes || feedNodes.length != 1){ |
| 398 |
console.log("dojox.data.AtomReadStore: Received an invalid Atom document, number of feed tags = " + (feedNodes? feedNodes.length : 0)); |
| 399 |
return items; |
| 400 |
} |
| 401 |
|
| 402 |
nodes = dojo.filter(feedNodes[0].childNodes, "return item.tagName && item.tagName.toLowerCase() == 'entry'"); |
| 403 |
|
| 404 |
if(request.onBegin){ |
| 405 |
request.onBegin(nodes.length, this.sendQuery ? request : {}); |
| 406 |
} |
| 407 |
|
| 408 |
for(var i = 0; i < nodes.length; i++){ |
| 409 |
var node = nodes[i]; |
| 410 |
if(node.nodeType != 1 ){ |
| 411 |
continue; |
| 412 |
} |
| 413 |
items.push(this._getItem(node)); |
| 414 |
} |
| 415 |
this._items = items; |
| 416 |
return items; |
| 417 |
}, |
| 418 |
|
| 419 |
close: function( request){ |
| 420 |
|
| 421 |
|
| 422 |
}, |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
_getItem: function(element){ |
| 427 |
return { |
| 428 |
element: element, |
| 429 |
store: this |
| 430 |
}; |
| 431 |
}, |
| 432 |
|
| 433 |
_parseItem: function(item){ |
| 434 |
var attribs = item._attribs; |
| 435 |
var _this = this; |
| 436 |
var text, type; |
| 437 |
|
| 438 |
function getNodeText(node){ |
| 439 |
var txt = node.textContent || node.innerHTML || node.innerXML; |
| 440 |
if(!txt && node.childNodes[0]){ |
| 441 |
var child = node.childNodes[0]; |
| 442 |
if(child && (child.nodeType == 3 || child.nodeType == 4)){ |
| 443 |
txt = node.childNodes[0].nodeValue; |
| 444 |
} |
| 445 |
} |
| 446 |
return txt; |
| 447 |
} |
| 448 |
function parseTextAndType(node){ |
| 449 |
return {text: getNodeText(node),type: node.getAttribute("type")}; |
| 450 |
} |
| 451 |
dojo.forEach(item.element.childNodes, function(node){ |
| 452 |
var tagName = node.tagName ? node.tagName.toLowerCase() : ""; |
| 453 |
switch(tagName){ |
| 454 |
case "title": |
| 455 |
attribs[tagName] = { |
| 456 |
text: getNodeText(node), |
| 457 |
type: node.getAttribute("type") |
| 458 |
}; break; |
| 459 |
case "subtitle": |
| 460 |
case "summary": |
| 461 |
case "content": |
| 462 |
attribs[tagName] = parseTextAndType(node); |
| 463 |
break; |
| 464 |
case "author": |
| 465 |
var nameNode ,uriNode; |
| 466 |
dojo.forEach(node.childNodes, function(child){ |
| 467 |
if(!child.tagName){ |
| 468 |
return; |
| 469 |
} |
| 470 |
switch(child.tagName.toLowerCase()){ |
| 471 |
case "name": |
| 472 |
nameNode = child; |
| 473 |
break; |
| 474 |
case "uri": |
| 475 |
uriNode = child; |
| 476 |
break; |
| 477 |
} |
| 478 |
}); |
| 479 |
var author = {}; |
| 480 |
if(nameNode && nameNode.length == 1){ |
| 481 |
author.name = getNodeText(nameNode[0]); |
| 482 |
} |
| 483 |
if(uriNode && uriNode.length == 1){ |
| 484 |
author.uri = getNodeText(uriNode[0]); |
| 485 |
} |
| 486 |
attribs[tagName] = author; |
| 487 |
break; |
| 488 |
case "id": |
| 489 |
attribs[tagName] = getNodeText(node); |
| 490 |
break; |
| 491 |
case "updated": |
| 492 |
attribs[tagName] = dojo.date.stamp.fromISOString(getNodeText(node) ); |
| 493 |
break; |
| 494 |
case "published": |
| 495 |
attribs[tagName] = dojo.date.stamp.fromISOString(getNodeText(node)); |
| 496 |
break; |
| 497 |
case "category": |
| 498 |
if(!attribs[tagName]){ |
| 499 |
attribs[tagName] = []; |
| 500 |
} |
| 501 |
attribs[tagName].push({scheme:node.getAttribute("scheme"), term: node.getAttribute("term")}); |
| 502 |
break; |
| 503 |
case "link": |
| 504 |
if(!attribs[tagName]){ |
| 505 |
attribs[tagName] = []; |
| 506 |
} |
| 507 |
var link = { |
| 508 |
rel: node.getAttribute("rel"), |
| 509 |
href: node.getAttribute("href"), |
| 510 |
type: node.getAttribute("type")}; |
| 511 |
attribs[tagName].push(link); |
| 512 |
|
| 513 |
if(link.rel == "alternate"){ |
| 514 |
attribs["alternate"] = link; |
| 515 |
} |
| 516 |
break; |
| 517 |
default: |
| 518 |
break; |
| 519 |
} |
| 520 |
}); |
| 521 |
}, |
| 522 |
|
| 523 |
_unescapeHTML : function(text){ |
| 524 |
|
| 525 |
text = text.replace(/’/m , "'").replace(/″/m , "\"").replace(/</m,">").replace(/>/m,"<").replace(/&/m,"&"); |
| 526 |
return text; |
| 527 |
}, |
| 528 |
|
| 529 |
_assertIsItem: function( item){ |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
if(!this.isItem(item)){ |
| 535 |
throw new Error("dojox.data.AtomReadStore: Invalid item argument."); |
| 536 |
} |
| 537 |
}, |
| 538 |
|
| 539 |
_assertIsAttribute: function( attribute){ |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
if(typeof attribute !== "string"){ |
| 545 |
throw new Error("dojox.data.AtomReadStore: Invalid attribute argument."); |
| 546 |
} |
| 547 |
} |
| 548 |
}); |
| 549 |
|
| 550 |
dojo.extend(AtomReadStore, dojo.data.util.simpleFetch); |
| 551 |
|
| 552 |
return AtomReadStore; |
| 553 |
}); |