| 1 |
define(["dojo/_base/declare", "dojo/_base/lang", "dojo/_base/kernel", "dojo/_base/json", "dojo/_base/xhr"], |
| 2 |
function(declare, lang, kernel, jsonUtil, xhr) { |
| 3 |
|
| 4 |
return declare("dojox.data.FileStore", null, { |
| 5 |
constructor: function(args){ |
| 6 |
|
| 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 |
if(args && args.label){ |
| 33 |
this.label = args.label; |
| 34 |
} |
| 35 |
if(args && args.url){ |
| 36 |
this.url = args.url; |
| 37 |
} |
| 38 |
if(args && args.options){ |
| 39 |
if(lang.isArray(args.options)){ |
| 40 |
this.options = args.options; |
| 41 |
}else{ |
| 42 |
if(lang.isString(args.options)){ |
| 43 |
this.options = args.options.split(","); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
if(args && args.pathAsQueryParam){ |
| 48 |
this.pathAsQueryParam = true; |
| 49 |
} |
| 50 |
if(args && "urlPreventCache" in args){ |
| 51 |
this.urlPreventCache = args.urlPreventCache?true:false; |
| 52 |
} |
| 53 |
}, |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
url: "", |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
_storeRef: "_S", |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
label: "name", |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
_identifier: "path", |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
_attributes: ["children", "directory", "name", "path", "modified", "size", "parentDir"], |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
pathSeparator: "/", |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
options: [], |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
failOk: false, |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
urlPreventCache: true, |
| 94 |
|
| 95 |
_assertIsItem: function( item){ |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if(!this.isItem(item)){ |
| 101 |
throw new Error("dojox.data.FileStore: a function was passed an item argument that was not an item"); |
| 102 |
} |
| 103 |
}, |
| 104 |
|
| 105 |
_assertIsAttribute: function( attribute){ |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if(typeof attribute !== "string"){ |
| 111 |
throw new Error("dojox.data.FileStore: a function was passed an attribute argument that was not an attribute name string"); |
| 112 |
} |
| 113 |
}, |
| 114 |
|
| 115 |
pathAsQueryParam: false, |
| 116 |
|
| 117 |
getFeatures: function(){ |
| 118 |
|
| 119 |
|
| 120 |
return { |
| 121 |
'dojo.data.api.Read': true, 'dojo.data.api.Identity':true |
| 122 |
}; |
| 123 |
}, |
| 124 |
|
| 125 |
getValue: function(item, attribute, defaultValue){ |
| 126 |
|
| 127 |
|
| 128 |
var values = this.getValues(item, attribute); |
| 129 |
if(values && values.length > 0){ |
| 130 |
return values[0]; |
| 131 |
} |
| 132 |
return defaultValue; |
| 133 |
}, |
| 134 |
|
| 135 |
getAttributes: function(item){ |
| 136 |
|
| 137 |
|
| 138 |
return this._attributes; |
| 139 |
}, |
| 140 |
|
| 141 |
hasAttribute: function(item, attribute){ |
| 142 |
|
| 143 |
|
| 144 |
this._assertIsItem(item); |
| 145 |
this._assertIsAttribute(attribute); |
| 146 |
return (attribute in item); |
| 147 |
}, |
| 148 |
|
| 149 |
getIdentity: function( item){ |
| 150 |
|
| 151 |
|
| 152 |
return this.getValue(item, this._identifier); |
| 153 |
}, |
| 154 |
|
| 155 |
getIdentityAttributes: function(item){ |
| 156 |
|
| 157 |
|
| 158 |
return [this._identifier]; |
| 159 |
}, |
| 160 |
|
| 161 |
|
| 162 |
isItemLoaded: function(item){ |
| 163 |
|
| 164 |
|
| 165 |
var loaded = this.isItem(item); |
| 166 |
if(loaded && typeof item._loaded == "boolean" && !item._loaded){ |
| 167 |
loaded = false; |
| 168 |
} |
| 169 |
return loaded; |
| 170 |
}, |
| 171 |
|
| 172 |
loadItem: function(keywordArgs){ |
| 173 |
|
| 174 |
|
| 175 |
var item = keywordArgs.item; |
| 176 |
var self = this; |
| 177 |
var scope = keywordArgs.scope || kernel.global; |
| 178 |
|
| 179 |
var content = {}; |
| 180 |
|
| 181 |
if(this.options.length > 0){ |
| 182 |
content.options = jsonUtil.toJson(this.options); |
| 183 |
} |
| 184 |
|
| 185 |
if(this.pathAsQueryParam){ |
| 186 |
content.path = item.parentPath + this.pathSeparator + item.name; |
| 187 |
} |
| 188 |
var xhrData = { |
| 189 |
url: this.pathAsQueryParam? this.url : this.url + "/" + item.parentPath + "/" + item.name, |
| 190 |
handleAs: "json-comment-optional", |
| 191 |
content: content, |
| 192 |
preventCache: this.urlPreventCache, |
| 193 |
failOk: this.failOk |
| 194 |
}; |
| 195 |
|
| 196 |
var deferred = xhr.get(xhrData); |
| 197 |
deferred.addErrback(function(error){ |
| 198 |
if(keywordArgs.onError){ |
| 199 |
keywordArgs.onError.call(scope, error); |
| 200 |
} |
| 201 |
}); |
| 202 |
|
| 203 |
deferred.addCallback(function(data){ |
| 204 |
delete item.parentPath; |
| 205 |
delete item._loaded; |
| 206 |
lang.mixin(item, data); |
| 207 |
self._processItem(item); |
| 208 |
if(keywordArgs.onItem){ |
| 209 |
keywordArgs.onItem.call(scope, item); |
| 210 |
} |
| 211 |
}); |
| 212 |
}, |
| 213 |
|
| 214 |
getLabel: function(item){ |
| 215 |
|
| 216 |
|
| 217 |
return this.getValue(item,this.label); |
| 218 |
}, |
| 219 |
|
| 220 |
getLabelAttributes: function(item){ |
| 221 |
|
| 222 |
|
| 223 |
return [this.label]; |
| 224 |
}, |
| 225 |
|
| 226 |
containsValue: function(item, attribute, value){ |
| 227 |
|
| 228 |
|
| 229 |
var values = this.getValues(item,attribute); |
| 230 |
for(var i = 0; i < values.length; i++){ |
| 231 |
if(values[i] == value){ |
| 232 |
return true; |
| 233 |
} |
| 234 |
} |
| 235 |
return false; |
| 236 |
}, |
| 237 |
|
| 238 |
getValues: function(item, attribute){ |
| 239 |
|
| 240 |
|
| 241 |
this._assertIsItem(item); |
| 242 |
this._assertIsAttribute(attribute); |
| 243 |
|
| 244 |
var value = item[attribute]; |
| 245 |
if(typeof value !== "undefined" && !lang.isArray(value)){ |
| 246 |
value = [value]; |
| 247 |
}else if(typeof value === "undefined"){ |
| 248 |
value = []; |
| 249 |
} |
| 250 |
return value; |
| 251 |
}, |
| 252 |
|
| 253 |
isItem: function(item){ |
| 254 |
|
| 255 |
|
| 256 |
if(item && item[this._storeRef] === this){ |
| 257 |
return true; |
| 258 |
} |
| 259 |
return false; |
| 260 |
}, |
| 261 |
|
| 262 |
close: function(request){ |
| 263 |
|
| 264 |
|
| 265 |
}, |
| 266 |
|
| 267 |
fetch: function(request){ |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
request = request || {}; |
| 274 |
if(!request.store){ |
| 275 |
request.store = this; |
| 276 |
} |
| 277 |
var self = this; |
| 278 |
var scope = request.scope || kernel.global; |
| 279 |
|
| 280 |
|
| 281 |
var reqParams = {}; |
| 282 |
if(request.query){ |
| 283 |
reqParams.query = jsonUtil.toJson(request.query); |
| 284 |
} |
| 285 |
|
| 286 |
if(request.sort){ |
| 287 |
reqParams.sort = jsonUtil.toJson(request.sort); |
| 288 |
} |
| 289 |
|
| 290 |
if(request.queryOptions){ |
| 291 |
reqParams.queryOptions = jsonUtil.toJson(request.queryOptions); |
| 292 |
} |
| 293 |
|
| 294 |
if(typeof request.start == "number"){ |
| 295 |
reqParams.start = "" + request.start; |
| 296 |
} |
| 297 |
if(typeof request.count == "number"){ |
| 298 |
reqParams.count = "" + request.count; |
| 299 |
} |
| 300 |
|
| 301 |
if(this.options.length > 0){ |
| 302 |
reqParams.options = jsonUtil.toJson(this.options); |
| 303 |
} |
| 304 |
|
| 305 |
var getArgs = { |
| 306 |
url: this.url, |
| 307 |
preventCache: this.urlPreventCache, |
| 308 |
failOk: this.failOk, |
| 309 |
handleAs: "json-comment-optional", |
| 310 |
content: reqParams |
| 311 |
}; |
| 312 |
|
| 313 |
|
| 314 |
var deferred = xhr.get(getArgs); |
| 315 |
|
| 316 |
deferred.addCallback(function(data){self._processResult(data, request);}); |
| 317 |
deferred.addErrback(function(error){ |
| 318 |
if(request.onError){ |
| 319 |
request.onError.call(scope, error, request); |
| 320 |
} |
| 321 |
}); |
| 322 |
}, |
| 323 |
|
| 324 |
fetchItemByIdentity: function(keywordArgs){ |
| 325 |
|
| 326 |
|
| 327 |
var path = keywordArgs.identity; |
| 328 |
var self = this; |
| 329 |
var scope = keywordArgs.scope || kernel.global; |
| 330 |
|
| 331 |
var content = {}; |
| 332 |
|
| 333 |
if(this.options.length > 0){ |
| 334 |
content.options = jsonUtil.toJson(this.options); |
| 335 |
} |
| 336 |
|
| 337 |
if(this.pathAsQueryParam){ |
| 338 |
content.path = path; |
| 339 |
} |
| 340 |
var xhrData = { |
| 341 |
url: this.pathAsQueryParam? this.url : this.url + "/" + path, |
| 342 |
handleAs: "json-comment-optional", |
| 343 |
content: content, |
| 344 |
preventCache: this.urlPreventCache, |
| 345 |
failOk: this.failOk |
| 346 |
}; |
| 347 |
|
| 348 |
var deferred = xhr.get(xhrData); |
| 349 |
deferred.addErrback(function(error){ |
| 350 |
if(keywordArgs.onError){ |
| 351 |
keywordArgs.onError.call(scope, error); |
| 352 |
} |
| 353 |
}); |
| 354 |
|
| 355 |
deferred.addCallback(function(data){ |
| 356 |
var item = self._processItem(data); |
| 357 |
if(keywordArgs.onItem){ |
| 358 |
keywordArgs.onItem.call(scope, item); |
| 359 |
} |
| 360 |
}); |
| 361 |
}, |
| 362 |
|
| 363 |
_processResult: function(data, request){ |
| 364 |
var scope = request.scope || kernel.global; |
| 365 |
try{ |
| 366 |
|
| 367 |
if(data.pathSeparator){ |
| 368 |
this.pathSeparator = data.pathSeparator; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
if(request.onBegin){ |
| 373 |
request.onBegin.call(scope, data.total, request); |
| 374 |
} |
| 375 |
|
| 376 |
var items = this._processItemArray(data.items); |
| 377 |
if(request.onItem){ |
| 378 |
var i; |
| 379 |
for(i = 0; i < items.length; i++){ |
| 380 |
request.onItem.call(scope, items[i], request); |
| 381 |
} |
| 382 |
items = null; |
| 383 |
} |
| 384 |
if(request.onComplete){ |
| 385 |
request.onComplete.call(scope, items, request); |
| 386 |
} |
| 387 |
}catch (e){ |
| 388 |
if(request.onError){ |
| 389 |
request.onError.call(scope, e, request); |
| 390 |
}else{ |
| 391 |
console.log(e); |
| 392 |
} |
| 393 |
} |
| 394 |
}, |
| 395 |
|
| 396 |
_processItemArray: function(itemArray){ |
| 397 |
|
| 398 |
|
| 399 |
var i; |
| 400 |
for(i = 0; i < itemArray.length; i++){ |
| 401 |
this._processItem(itemArray[i]); |
| 402 |
} |
| 403 |
return itemArray; |
| 404 |
}, |
| 405 |
|
| 406 |
_processItem: function(item){ |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
if(!item){return null;} |
| 412 |
item[this._storeRef] = this; |
| 413 |
if(item.children && item.directory){ |
| 414 |
if(lang.isArray(item.children)){ |
| 415 |
var children = item.children; |
| 416 |
var i; |
| 417 |
for(i = 0; i < children.length; i++ ){ |
| 418 |
var name = children[i]; |
| 419 |
if(lang.isObject(name)){ |
| 420 |
children[i] = this._processItem(name); |
| 421 |
}else{ |
| 422 |
children[i] = {name: name, _loaded: false, parentPath: item.path}; |
| 423 |
children[i][this._storeRef] = this; |
| 424 |
} |
| 425 |
} |
| 426 |
}else{ |
| 427 |
delete item.children; |
| 428 |
} |
| 429 |
} |
| 430 |
return item; |
| 431 |
} |
| 432 |
}); |
| 433 |
}); |