| 1 |
define(["dojo/_base/declare", "dojo/_base/lang", "dojo/_base/xhr", "dojo/_base/kernel", |
| 2 |
"dojo/data/util/simpleFetch", "dojo/data/util/filter"], |
| 3 |
function(declare, lang, xhr, kernel, simpleFetch, filterUtil) { |
| 4 |
|
| 5 |
var KeyValueStore = declare("dojox.data.KeyValueStore", null, { |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
constructor: function( keywordParameters){ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if(keywordParameters.url){ |
| 25 |
this.url = keywordParameters.url; |
| 26 |
} |
| 27 |
this._keyValueString = keywordParameters.data; |
| 28 |
this._keyValueVar = keywordParameters.dataVar; |
| 29 |
this._keyAttribute = "key"; |
| 30 |
this._valueAttribute = "value"; |
| 31 |
this._storeProp = "_keyValueStore"; |
| 32 |
this._features = { |
| 33 |
'dojo.data.api.Read': true, |
| 34 |
'dojo.data.api.Identity': true |
| 35 |
}; |
| 36 |
this._loadInProgress = false; |
| 37 |
this._queuedFetches = []; |
| 38 |
if(keywordParameters && "urlPreventCache" in keywordParameters){ |
| 39 |
this.urlPreventCache = keywordParameters.urlPreventCache?true:false; |
| 40 |
} |
| 41 |
}, |
| 42 |
|
| 43 |
url: "", |
| 44 |
data: "", |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
urlPreventCache: false, |
| 49 |
|
| 50 |
_assertIsItem: function( item){ |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if(!this.isItem(item)){ |
| 56 |
throw new Error("dojox.data.KeyValueStore: a function was passed an item argument that was not an item"); |
| 57 |
} |
| 58 |
}, |
| 59 |
|
| 60 |
_assertIsAttribute: function( item, attribute){ |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
if(!lang.isString(attribute)){ |
| 66 |
throw new Error("dojox.data.KeyValueStore: a function was passed an attribute argument that was not an attribute object nor an attribute name string"); |
| 67 |
} |
| 68 |
}, |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
getValue: function( item, |
| 74 |
attribute, |
| 75 |
defaultValue){ |
| 76 |
|
| 77 |
|
| 78 |
this._assertIsItem(item); |
| 79 |
this._assertIsAttribute(item, attribute); |
| 80 |
var value; |
| 81 |
if(attribute == this._keyAttribute){ |
| 82 |
value = item[this._keyAttribute]; |
| 83 |
}else{ |
| 84 |
value = item[this._valueAttribute]; |
| 85 |
} |
| 86 |
if(value === undefined){ |
| 87 |
value = defaultValue; |
| 88 |
} |
| 89 |
return value; |
| 90 |
}, |
| 91 |
|
| 92 |
getValues: function( item, |
| 93 |
attribute){ |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
var value = this.getValue(item, attribute); |
| 99 |
return (value ? [value] : []); |
| 100 |
}, |
| 101 |
|
| 102 |
getAttributes: function( item){ |
| 103 |
|
| 104 |
|
| 105 |
return [this._keyAttribute, this._valueAttribute, item[this._keyAttribute]]; |
| 106 |
}, |
| 107 |
|
| 108 |
hasAttribute: function( item, |
| 109 |
attribute){ |
| 110 |
|
| 111 |
|
| 112 |
this._assertIsItem(item); |
| 113 |
this._assertIsAttribute(item, attribute); |
| 114 |
return (attribute == this._keyAttribute || attribute == this._valueAttribute || attribute == item[this._keyAttribute]); |
| 115 |
}, |
| 116 |
|
| 117 |
containsValue: function( item, |
| 118 |
attribute, |
| 119 |
value){ |
| 120 |
|
| 121 |
|
| 122 |
var regexp = undefined; |
| 123 |
if(typeof value === "string"){ |
| 124 |
regexp = filterUtil.patternToRegExp(value, false); |
| 125 |
} |
| 126 |
return this._containsValue(item, attribute, value, regexp); |
| 127 |
}, |
| 128 |
|
| 129 |
_containsValue: function( item, |
| 130 |
attribute, |
| 131 |
value, |
| 132 |
regexp){ |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
var values = this.getValues(item, attribute); |
| 149 |
for(var i = 0; i < values.length; ++i){ |
| 150 |
var possibleValue = values[i]; |
| 151 |
if(typeof possibleValue === "string" && regexp){ |
| 152 |
return (possibleValue.match(regexp) !== null); |
| 153 |
}else{ |
| 154 |
|
| 155 |
if(value === possibleValue){ |
| 156 |
return true; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
return false; |
| 161 |
}, |
| 162 |
|
| 163 |
isItem: function( something){ |
| 164 |
|
| 165 |
|
| 166 |
if(something && something[this._storeProp] === this){ |
| 167 |
return true; |
| 168 |
} |
| 169 |
return false; |
| 170 |
}, |
| 171 |
|
| 172 |
isItemLoaded: function( something){ |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
return this.isItem(something); |
| 177 |
}, |
| 178 |
|
| 179 |
loadItem: function( keywordArgs){ |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
}, |
| 191 |
|
| 192 |
getFeatures: function(){ |
| 193 |
|
| 194 |
|
| 195 |
return this._features; |
| 196 |
}, |
| 197 |
|
| 198 |
close: function( request){ |
| 199 |
|
| 200 |
|
| 201 |
}, |
| 202 |
|
| 203 |
getLabel: function( item){ |
| 204 |
|
| 205 |
|
| 206 |
return item[this._keyAttribute]; |
| 207 |
}, |
| 208 |
|
| 209 |
getLabelAttributes: function( item){ |
| 210 |
|
| 211 |
|
| 212 |
return [this._keyAttribute]; |
| 213 |
}, |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
_fetchItems: function( keywordArgs, |
| 219 |
findCallback, |
| 220 |
errorCallback){ |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
var self = this; |
| 225 |
|
| 226 |
var filter = function(requestArgs, arrayOfAllItems){ |
| 227 |
var items = null; |
| 228 |
if(requestArgs.query){ |
| 229 |
items = []; |
| 230 |
var ignoreCase = requestArgs.queryOptions ? requestArgs.queryOptions.ignoreCase : false; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
var regexpList = {}; |
| 235 |
for(var key in requestArgs.query){ |
| 236 |
var value = requestArgs.query[key]; |
| 237 |
if(typeof value === "string"){ |
| 238 |
regexpList[key] = filterUtil.patternToRegExp(value, ignoreCase); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
for(var i = 0; i < arrayOfAllItems.length; ++i){ |
| 243 |
var match = true; |
| 244 |
var candidateItem = arrayOfAllItems[i]; |
| 245 |
for(var key in requestArgs.query){ |
| 246 |
var value = requestArgs.query[key]; |
| 247 |
if(!self._containsValue(candidateItem, key, value, regexpList[key])){ |
| 248 |
match = false; |
| 249 |
} |
| 250 |
} |
| 251 |
if(match){ |
| 252 |
items.push(candidateItem); |
| 253 |
} |
| 254 |
} |
| 255 |
}else if(requestArgs.identity){ |
| 256 |
items = []; |
| 257 |
var item; |
| 258 |
for(var key in arrayOfAllItems){ |
| 259 |
item = arrayOfAllItems[key]; |
| 260 |
if(item[self._keyAttribute] == requestArgs.identity){ |
| 261 |
items.push(item); |
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
}else{ |
| 266 |
|
| 267 |
|
| 268 |
if(arrayOfAllItems.length> 0){ |
| 269 |
items = arrayOfAllItems.slice(0,arrayOfAllItems.length); |
| 270 |
} |
| 271 |
} |
| 272 |
findCallback(items, requestArgs); |
| 273 |
}; |
| 274 |
|
| 275 |
if(this._loadFinished){ |
| 276 |
filter(keywordArgs, this._arrayOfAllItems); |
| 277 |
}else{ |
| 278 |
if(this.url !== ""){ |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if(this._loadInProgress){ |
| 283 |
this._queuedFetches.push({args: keywordArgs, filter: filter}); |
| 284 |
}else{ |
| 285 |
this._loadInProgress = true; |
| 286 |
var getArgs = { |
| 287 |
url: self.url, |
| 288 |
handleAs: "json-comment-filtered", |
| 289 |
preventCache: this.urlPreventCache |
| 290 |
}; |
| 291 |
var getHandler = xhr.get(getArgs); |
| 292 |
getHandler.addCallback(function(data){ |
| 293 |
self._processData(data); |
| 294 |
filter(keywordArgs, self._arrayOfAllItems); |
| 295 |
self._handleQueuedFetches(); |
| 296 |
}); |
| 297 |
getHandler.addErrback(function(error){ |
| 298 |
self._loadInProgress = false; |
| 299 |
throw error; |
| 300 |
}); |
| 301 |
} |
| 302 |
}else if(this._keyValueString){ |
| 303 |
this._processData(eval(this._keyValueString)); |
| 304 |
this._keyValueString = null; |
| 305 |
filter(keywordArgs, this._arrayOfAllItems); |
| 306 |
}else if(this._keyValueVar){ |
| 307 |
this._processData(this._keyValueVar); |
| 308 |
this._keyValueVar = null; |
| 309 |
filter(keywordArgs, this._arrayOfAllItems); |
| 310 |
}else{ |
| 311 |
throw new Error("dojox.data.KeyValueStore: No source data was provided as either URL, String, or Javascript variable data input."); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
}, |
| 316 |
|
| 317 |
_handleQueuedFetches: function(){ |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
if(this._queuedFetches.length > 0){ |
| 323 |
for(var i = 0; i < this._queuedFetches.length; i++){ |
| 324 |
var fData = this._queuedFetches[i]; |
| 325 |
var delayedFilter = fData.filter; |
| 326 |
var delayedQuery = fData.args; |
| 327 |
if(delayedFilter){ |
| 328 |
delayedFilter(delayedQuery, this._arrayOfAllItems); |
| 329 |
}else{ |
| 330 |
this.fetchItemByIdentity(fData.args); |
| 331 |
} |
| 332 |
} |
| 333 |
this._queuedFetches = []; |
| 334 |
} |
| 335 |
}, |
| 336 |
|
| 337 |
_processData: function( data){ |
| 338 |
this._arrayOfAllItems = []; |
| 339 |
for(var i=0; i<data.length; i++){ |
| 340 |
this._arrayOfAllItems.push(this._createItem(data[i])); |
| 341 |
} |
| 342 |
this._loadFinished = true; |
| 343 |
this._loadInProgress = false; |
| 344 |
}, |
| 345 |
|
| 346 |
_createItem: function( something){ |
| 347 |
var item = {}; |
| 348 |
item[this._storeProp] = this; |
| 349 |
for(var i in something){ |
| 350 |
item[this._keyAttribute] = i; |
| 351 |
item[this._valueAttribute] = something[i]; |
| 352 |
break; |
| 353 |
} |
| 354 |
return item; |
| 355 |
}, |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
getIdentity: function( item){ |
| 361 |
|
| 362 |
|
| 363 |
if(this.isItem(item)){ |
| 364 |
return item[this._keyAttribute]; |
| 365 |
} |
| 366 |
return null; |
| 367 |
}, |
| 368 |
|
| 369 |
getIdentityAttributes: function( item){ |
| 370 |
|
| 371 |
|
| 372 |
return [this._keyAttribute]; |
| 373 |
}, |
| 374 |
|
| 375 |
fetchItemByIdentity: function( keywordArgs){ |
| 376 |
|
| 377 |
|
| 378 |
keywordArgs.oldOnItem = keywordArgs.onItem; |
| 379 |
keywordArgs.onItem = null; |
| 380 |
keywordArgs.onComplete = this._finishFetchItemByIdentity ; |
| 381 |
this.fetch(keywordArgs); |
| 382 |
}, |
| 383 |
|
| 384 |
_finishFetchItemByIdentity: function( items, request){ |
| 385 |
var scope = request.scope || kernel.global; |
| 386 |
if(items.length){ |
| 387 |
request.oldOnItem.call(scope, items[0]); |
| 388 |
}else{ |
| 389 |
request.oldOnItem.call(scope, null); |
| 390 |
} |
| 391 |
} |
| 392 |
}); |
| 393 |
|
| 394 |
lang.extend(KeyValueStore,simpleFetch); |
| 395 |
return KeyValueStore; |
| 396 |
}); |