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(/*Object*/args){ |
---|
6 | // summary: |
---|
7 | // A simple store that provides a datastore interface to a filesystem. |
---|
8 | // description: |
---|
9 | // A simple store that provides a datastore interface to a filesystem. It takes a few parameters |
---|
10 | // for initialization: |
---|
11 | // |
---|
12 | // - url: The URL of the service which provides the file store serverside implementation. |
---|
13 | // - label: The attribute of the file to use as the human-readable text. Default is 'name'. |
---|
14 | // |
---|
15 | // The purpose of this store is to represent a file as a datastore item. The |
---|
16 | // datastore item by default has the following attributes that can be examined on it. |
---|
17 | // |
---|
18 | // - directory: Boolean indicating if the file item represents a directory. |
---|
19 | // - name: The filename with no path informatiom. |
---|
20 | // - path: The file complete file path including name, relative to the location the |
---|
21 | // file service scans from |
---|
22 | // - size: The size of the file, in bytes. |
---|
23 | // - parentDir: The parent directory path. |
---|
24 | // - children: Any child files contained by a directory file item. |
---|
25 | // |
---|
26 | // Note that the store's server call pattern is RESTlike. |
---|
27 | // |
---|
28 | // The store also supports the passing of configurable options to the back end service, such as |
---|
29 | // expanding all child files (no lazy load), displaying hidden files, displaying only directories, and so on. |
---|
30 | // These are defined through a comma-separated list in declarative, or through setting the options array in programmatic. |
---|
31 | // example: options="expand,dirsOnly,showHiddenFiles" |
---|
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 | // url: [public] string |
---|
56 | // The URL to the file path service. |
---|
57 | url: "", |
---|
58 | |
---|
59 | // _storeRef: [private] string |
---|
60 | // Internal variable used to denote an item came from this store instance. |
---|
61 | _storeRef: "_S", |
---|
62 | |
---|
63 | // label: [public] string |
---|
64 | // Default attribute to use to represent the item as a user-readable |
---|
65 | // string. Public, so users can change it. |
---|
66 | label: "name", |
---|
67 | |
---|
68 | // _identifier: [private] string |
---|
69 | // Default attribute to use to represent the item's identifier. |
---|
70 | // Path should always be unique in the store instance. |
---|
71 | _identifier: "path", |
---|
72 | |
---|
73 | // _attributes: [private] string |
---|
74 | // Internal variable of attributes all file items should have. |
---|
75 | _attributes: ["children", "directory", "name", "path", "modified", "size", "parentDir"], |
---|
76 | |
---|
77 | // pathSeparator: [public] string |
---|
78 | // The path separator to use when chaining requests for children |
---|
79 | // Can be overriden by the server on initial load |
---|
80 | pathSeparator: "/", |
---|
81 | |
---|
82 | // options: [public] array |
---|
83 | // Array of options to always send when doing requests. |
---|
84 | // Back end service controls this, like 'dirsOnly', 'showHiddenFiles', 'expandChildren', etc. |
---|
85 | options: [], |
---|
86 | |
---|
87 | // failOk: [public] boolean |
---|
88 | // Flag to pass on to xhr functions to check if we are OK to fail the call silently |
---|
89 | failOk: false, |
---|
90 | |
---|
91 | // urlPreventCache: [public] string |
---|
92 | // Flag to dennote if preventCache should be passed to xhrGet. |
---|
93 | urlPreventCache: true, |
---|
94 | |
---|
95 | _assertIsItem: function(/* item */ item){ |
---|
96 | // summary: |
---|
97 | // This function tests whether the item passed in is indeed an item in the store. |
---|
98 | // item: |
---|
99 | // The item to test for being contained by the store. |
---|
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-name-string */ attribute){ |
---|
106 | // summary: |
---|
107 | // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store. |
---|
108 | // attribute: |
---|
109 | // The attribute to test for being contained by the store. |
---|
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, //Function to switch between REST style URL lookups and passing the path to specific items as a query param: 'path'. |
---|
116 | |
---|
117 | getFeatures: function(){ |
---|
118 | // summary: |
---|
119 | // See dojo/data/api/Read.getFeatures() |
---|
120 | return { |
---|
121 | 'dojo.data.api.Read': true, 'dojo.data.api.Identity':true |
---|
122 | }; |
---|
123 | }, |
---|
124 | |
---|
125 | getValue: function(item, attribute, defaultValue){ |
---|
126 | // summary: |
---|
127 | // See dojo/data/api/Read.getValue() |
---|
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 | // summary: |
---|
137 | // See dojo/data/api/Read.getAttributes() |
---|
138 | return this._attributes; |
---|
139 | }, |
---|
140 | |
---|
141 | hasAttribute: function(item, attribute){ |
---|
142 | // summary: |
---|
143 | // See dojo/data/api/Read.hasAttribute() |
---|
144 | this._assertIsItem(item); |
---|
145 | this._assertIsAttribute(attribute); |
---|
146 | return (attribute in item); |
---|
147 | }, |
---|
148 | |
---|
149 | getIdentity: function(/* item */ item){ |
---|
150 | // summary: |
---|
151 | // See dojo/data/api/Identity.getIdentity() |
---|
152 | return this.getValue(item, this._identifier); |
---|
153 | }, |
---|
154 | |
---|
155 | getIdentityAttributes: function(item){ |
---|
156 | // summary: |
---|
157 | // See dojo/data/api/Read.getLabelAttributes() |
---|
158 | return [this._identifier]; |
---|
159 | }, |
---|
160 | |
---|
161 | |
---|
162 | isItemLoaded: function(item){ |
---|
163 | // summary: |
---|
164 | // See dojo/data/api/Read.isItemLoaded() |
---|
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 | // summary: |
---|
174 | // See dojo/data/api/Read.loadItem() |
---|
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 | // summary: |
---|
216 | // See dojo/data/api/Read.getLabel() |
---|
217 | return this.getValue(item,this.label); |
---|
218 | }, |
---|
219 | |
---|
220 | getLabelAttributes: function(item){ |
---|
221 | // summary: |
---|
222 | // See dojo/data/api/Read.getLabelAttributes() |
---|
223 | return [this.label]; |
---|
224 | }, |
---|
225 | |
---|
226 | containsValue: function(item, attribute, value){ |
---|
227 | // summary: |
---|
228 | // See dojo/data/api/Read.containsValue() |
---|
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 | // summary: |
---|
240 | // See dojo/data/api/Read.getValue() |
---|
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 | // summary: |
---|
255 | // See dojo/data/api/Read.isItem() |
---|
256 | if(item && item[this._storeRef] === this){ |
---|
257 | return true; |
---|
258 | } |
---|
259 | return false; |
---|
260 | }, |
---|
261 | |
---|
262 | close: function(request){ |
---|
263 | // summary: |
---|
264 | // See dojo/data/api/Read.close() |
---|
265 | }, |
---|
266 | |
---|
267 | fetch: function(request){ |
---|
268 | // summary: |
---|
269 | // Fetch items that match to a query |
---|
270 | // request: |
---|
271 | // A request object |
---|
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 | //Generate what will be sent over. |
---|
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 | // summary: |
---|
326 | // See dojo/data/api/Read.loadItem() |
---|
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 | //If the data contains a path separator, set ours |
---|
367 | if(data.pathSeparator){ |
---|
368 | this.pathSeparator = data.pathSeparator; |
---|
369 | } |
---|
370 | //Invoke the onBegin handler, if any, to return the |
---|
371 | //size of the dataset as indicated by the service. |
---|
372 | if(request.onBegin){ |
---|
373 | request.onBegin.call(scope, data.total, request); |
---|
374 | } |
---|
375 | //Now process all the returned items thro |
---|
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 | // summary: |
---|
398 | // Internal function for processing an array of items for return. |
---|
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 | // summary: |
---|
408 | // Internal function for processing an item returned from the store. |
---|
409 | // It sets up the store ref as well as sets up the attributes necessary |
---|
410 | // to invoke a lazy load on a child, if there are any. |
---|
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 | }); |
---|