]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
8 | if(!dojo._hasResource["dojo.data.ItemFileReadStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dojo.data.ItemFileReadStore"] = true; | |
2f01fe57 AD |
10 | dojo.provide("dojo.data.ItemFileReadStore"); |
11 | dojo.require("dojo.data.util.filter"); | |
12 | dojo.require("dojo.data.util.simpleFetch"); | |
13 | dojo.require("dojo.date.stamp"); | |
a089699c | 14 | |
81bea17a | 15 | |
a089699c AD |
16 | dojo.declare("dojo.data.ItemFileReadStore", null,{ |
17 | // summary: | |
18 | // The ItemFileReadStore implements the dojo.data.api.Read API and reads | |
19 | // data from JSON files that have contents in this format -- | |
20 | // { items: [ | |
21 | // { name:'Kermit', color:'green', age:12, friends:['Gonzo', {_reference:{name:'Fozzie Bear'}}]}, | |
22 | // { name:'Fozzie Bear', wears:['hat', 'tie']}, | |
23 | // { name:'Miss Piggy', pets:'Foo-Foo'} | |
24 | // ]} | |
81bea17a | 25 | // Note that it can also contain an 'identifer' property that specified which attribute on the items |
a089699c AD |
26 | // in the array of items that acts as the unique identifier for that item. |
27 | // | |
28 | constructor: function(/* Object */ keywordParameters){ | |
29 | // summary: constructor | |
30 | // keywordParameters: {url: String} | |
31 | // keywordParameters: {data: jsonObject} | |
32 | // keywordParameters: {typeMap: object) | |
33 | // The structure of the typeMap object is as follows: | |
34 | // { | |
35 | // type0: function || object, | |
36 | // type1: function || object, | |
37 | // ... | |
38 | // typeN: function || object | |
39 | // } | |
81bea17a | 40 | // Where if it is a function, it is assumed to be an object constructor that takes the |
a089699c AD |
41 | // value of _value as the initialization parameters. If it is an object, then it is assumed |
42 | // to be an object of general form: | |
43 | // { | |
44 | // type: function, //constructor. | |
45 | // deserialize: function(value) //The function that parses the value and constructs the object defined by type appropriately. | |
46 | // } | |
47 | ||
48 | this._arrayOfAllItems = []; | |
49 | this._arrayOfTopLevelItems = []; | |
50 | this._loadFinished = false; | |
51 | this._jsonFileUrl = keywordParameters.url; | |
52 | this._ccUrl = keywordParameters.url; | |
53 | this.url = keywordParameters.url; | |
54 | this._jsonData = keywordParameters.data; | |
55 | this.data = null; | |
56 | this._datatypeMap = keywordParameters.typeMap || {}; | |
57 | if(!this._datatypeMap['Date']){ | |
58 | //If no default mapping for dates, then set this as default. | |
59 | //We use the dojo.date.stamp here because the ISO format is the 'dojo way' | |
60 | //of generically representing dates. | |
61 | this._datatypeMap['Date'] = { | |
62 | type: Date, | |
63 | deserialize: function(value){ | |
64 | return dojo.date.stamp.fromISOString(value); | |
65 | } | |
66 | }; | |
67 | } | |
68 | this._features = {'dojo.data.api.Read':true, 'dojo.data.api.Identity':true}; | |
69 | this._itemsByIdentity = null; | |
70 | this._storeRefPropName = "_S"; // Default name for the store reference to attach to every item. | |
71 | this._itemNumPropName = "_0"; // Default Item Id for isItem to attach to every item. | |
72 | this._rootItemPropName = "_RI"; // Default Item Id for isItem to attach to every item. | |
73 | this._reverseRefMap = "_RRM"; // Default attribute for constructing a reverse reference map for use with reference integrity | |
74 | this._loadInProgress = false; //Got to track the initial load to prevent duelling loads of the dataset. | |
75 | this._queuedFetches = []; | |
76 | if(keywordParameters.urlPreventCache !== undefined){ | |
77 | this.urlPreventCache = keywordParameters.urlPreventCache?true:false; | |
78 | } | |
79 | if(keywordParameters.hierarchical !== undefined){ | |
80 | this.hierarchical = keywordParameters.hierarchical?true:false; | |
81 | } | |
82 | if(keywordParameters.clearOnClose){ | |
83 | this.clearOnClose = true; | |
84 | } | |
85 | if("failOk" in keywordParameters){ | |
86 | this.failOk = keywordParameters.failOk?true:false; | |
87 | } | |
88 | }, | |
89 | ||
90 | url: "", // use "" rather than undefined for the benefit of the parser (#3539) | |
91 | ||
92 | //Internal var, crossCheckUrl. Used so that setting either url or _jsonFileUrl, can still trigger a reload | |
93 | //when clearOnClose and close is used. | |
94 | _ccUrl: "", | |
95 | ||
96 | data: null, // define this so that the parser can populate it | |
97 | ||
98 | typeMap: null, //Define so parser can populate. | |
99 | ||
100 | //Parameter to allow users to specify if a close call should force a reload or not. | |
101 | //By default, it retains the old behavior of not clearing if close is called. But | |
102 | //if set true, the store will be reset to default state. Note that by doing this, | |
103 | //all item handles will become invalid and a new fetch must be issued. | |
104 | clearOnClose: false, | |
105 | ||
81bea17a | 106 | //Parameter to allow specifying if preventCache should be passed to the xhrGet call or not when loading data from a url. |
a089699c AD |
107 | //Note this does not mean the store calls the server on each fetch, only that the data load has preventCache set as an option. |
108 | //Added for tracker: #6072 | |
109 | urlPreventCache: false, | |
110 | ||
111 | //Parameter for specifying that it is OK for the xhrGet call to fail silently. | |
112 | failOk: false, | |
113 | ||
81bea17a AD |
114 | //Parameter to indicate to process data from the url as hierarchical |
115 | //(data items can contain other data items in js form). Default is true | |
116 | //for backwards compatibility. False means only root items are processed | |
117 | //as items, all child objects outside of type-mapped objects and those in | |
a089699c AD |
118 | //specific reference format, are left straight JS data objects. |
119 | hierarchical: true, | |
120 | ||
121 | _assertIsItem: function(/* item */ item){ | |
122 | // summary: | |
123 | // This function tests whether the item passed in is indeed an item in the store. | |
81bea17a | 124 | // item: |
a089699c | 125 | // The item to test for being contained by the store. |
81bea17a | 126 | if(!this.isItem(item)){ |
a089699c AD |
127 | throw new Error("dojo.data.ItemFileReadStore: Invalid item argument."); |
128 | } | |
129 | }, | |
130 | ||
131 | _assertIsAttribute: function(/* attribute-name-string */ attribute){ | |
132 | // summary: | |
133 | // This function tests whether the item passed in is indeed a valid 'attribute' like type for the store. | |
81bea17a | 134 | // attribute: |
a089699c | 135 | // The attribute to test for being contained by the store. |
81bea17a | 136 | if(typeof attribute !== "string"){ |
a089699c AD |
137 | throw new Error("dojo.data.ItemFileReadStore: Invalid attribute argument."); |
138 | } | |
139 | }, | |
140 | ||
81bea17a AD |
141 | getValue: function( /* item */ item, |
142 | /* attribute-name-string */ attribute, | |
a089699c | 143 | /* value? */ defaultValue){ |
81bea17a | 144 | // summary: |
a089699c AD |
145 | // See dojo.data.api.Read.getValue() |
146 | var values = this.getValues(item, attribute); | |
147 | return (values.length > 0)?values[0]:defaultValue; // mixed | |
148 | }, | |
149 | ||
81bea17a | 150 | getValues: function(/* item */ item, |
a089699c | 151 | /* attribute-name-string */ attribute){ |
81bea17a | 152 | // summary: |
a089699c AD |
153 | // See dojo.data.api.Read.getValues() |
154 | ||
155 | this._assertIsItem(item); | |
156 | this._assertIsAttribute(attribute); | |
157 | // Clone it before returning. refs: #10474 | |
158 | return (item[attribute] || []).slice(0); // Array | |
159 | }, | |
160 | ||
161 | getAttributes: function(/* item */ item){ | |
81bea17a | 162 | // summary: |
a089699c AD |
163 | // See dojo.data.api.Read.getAttributes() |
164 | this._assertIsItem(item); | |
165 | var attributes = []; | |
166 | for(var key in item){ | |
167 | // Save off only the real item attributes, not the special id marks for O(1) isItem. | |
168 | if((key !== this._storeRefPropName) && (key !== this._itemNumPropName) && (key !== this._rootItemPropName) && (key !== this._reverseRefMap)){ | |
169 | attributes.push(key); | |
170 | } | |
171 | } | |
172 | return attributes; // Array | |
173 | }, | |
174 | ||
175 | hasAttribute: function( /* item */ item, | |
176 | /* attribute-name-string */ attribute){ | |
81bea17a | 177 | // summary: |
a089699c AD |
178 | // See dojo.data.api.Read.hasAttribute() |
179 | this._assertIsItem(item); | |
180 | this._assertIsAttribute(attribute); | |
181 | return (attribute in item); | |
182 | }, | |
183 | ||
81bea17a AD |
184 | containsValue: function(/* item */ item, |
185 | /* attribute-name-string */ attribute, | |
a089699c | 186 | /* anything */ value){ |
81bea17a | 187 | // summary: |
a089699c AD |
188 | // See dojo.data.api.Read.containsValue() |
189 | var regexp = undefined; | |
190 | if(typeof value === "string"){ | |
191 | regexp = dojo.data.util.filter.patternToRegExp(value, false); | |
192 | } | |
193 | return this._containsValue(item, attribute, value, regexp); //boolean. | |
194 | }, | |
195 | ||
81bea17a AD |
196 | _containsValue: function( /* item */ item, |
197 | /* attribute-name-string */ attribute, | |
a089699c AD |
198 | /* anything */ value, |
199 | /* RegExp?*/ regexp){ | |
81bea17a | 200 | // summary: |
a089699c | 201 | // Internal function for looking at the values contained by the item. |
81bea17a AD |
202 | // description: |
203 | // Internal function for looking at the values contained by the item. This | |
a089699c AD |
204 | // function allows for denoting if the comparison should be case sensitive for |
205 | // strings or not (for handling filtering cases where string case should not matter) | |
81bea17a | 206 | // |
a089699c AD |
207 | // item: |
208 | // The data item to examine for attribute values. | |
209 | // attribute: | |
210 | // The attribute to inspect. | |
81bea17a | 211 | // value: |
a089699c AD |
212 | // The value to match. |
213 | // regexp: | |
214 | // Optional regular expression generated off value if value was of string type to handle wildcarding. | |
215 | // If present and attribute values are string, then it can be used for comparison instead of 'value' | |
216 | return dojo.some(this.getValues(item, attribute), function(possibleValue){ | |
217 | if(possibleValue !== null && !dojo.isObject(possibleValue) && regexp){ | |
218 | if(possibleValue.toString().match(regexp)){ | |
219 | return true; // Boolean | |
220 | } | |
221 | }else if(value === possibleValue){ | |
222 | return true; // Boolean | |
223 | } | |
224 | }); | |
225 | }, | |
226 | ||
227 | isItem: function(/* anything */ something){ | |
81bea17a | 228 | // summary: |
a089699c AD |
229 | // See dojo.data.api.Read.isItem() |
230 | if(something && something[this._storeRefPropName] === this){ | |
231 | if(this._arrayOfAllItems[something[this._itemNumPropName]] === something){ | |
232 | return true; | |
233 | } | |
234 | } | |
235 | return false; // Boolean | |
236 | }, | |
237 | ||
238 | isItemLoaded: function(/* anything */ something){ | |
81bea17a | 239 | // summary: |
a089699c AD |
240 | // See dojo.data.api.Read.isItemLoaded() |
241 | return this.isItem(something); //boolean | |
242 | }, | |
243 | ||
244 | loadItem: function(/* object */ keywordArgs){ | |
81bea17a | 245 | // summary: |
a089699c AD |
246 | // See dojo.data.api.Read.loadItem() |
247 | this._assertIsItem(keywordArgs.item); | |
248 | }, | |
249 | ||
250 | getFeatures: function(){ | |
81bea17a | 251 | // summary: |
a089699c AD |
252 | // See dojo.data.api.Read.getFeatures() |
253 | return this._features; //Object | |
254 | }, | |
255 | ||
256 | getLabel: function(/* item */ item){ | |
81bea17a | 257 | // summary: |
a089699c AD |
258 | // See dojo.data.api.Read.getLabel() |
259 | if(this._labelAttr && this.isItem(item)){ | |
260 | return this.getValue(item,this._labelAttr); //String | |
261 | } | |
262 | return undefined; //undefined | |
263 | }, | |
264 | ||
265 | getLabelAttributes: function(/* item */ item){ | |
81bea17a | 266 | // summary: |
a089699c AD |
267 | // See dojo.data.api.Read.getLabelAttributes() |
268 | if(this._labelAttr){ | |
269 | return [this._labelAttr]; //array | |
270 | } | |
271 | return null; //null | |
272 | }, | |
273 | ||
81bea17a AD |
274 | _fetchItems: function( /* Object */ keywordArgs, |
275 | /* Function */ findCallback, | |
a089699c | 276 | /* Function */ errorCallback){ |
81bea17a | 277 | // summary: |
a089699c AD |
278 | // See dojo.data.util.simpleFetch.fetch() |
279 | var self = this, | |
280 | filter = function(requestArgs, arrayOfItems){ | |
281 | var items = [], | |
282 | i, key; | |
283 | if(requestArgs.query){ | |
284 | var value, | |
285 | ignoreCase = requestArgs.queryOptions ? requestArgs.queryOptions.ignoreCase : false; | |
286 | ||
287 | //See if there are any string values that can be regexp parsed first to avoid multiple regexp gens on the | |
288 | //same value for each item examined. Much more efficient. | |
289 | var regexpList = {}; | |
290 | for(key in requestArgs.query){ | |
291 | value = requestArgs.query[key]; | |
292 | if(typeof value === "string"){ | |
293 | regexpList[key] = dojo.data.util.filter.patternToRegExp(value, ignoreCase); | |
294 | }else if(value instanceof RegExp){ | |
295 | regexpList[key] = value; | |
296 | } | |
297 | } | |
298 | for(i = 0; i < arrayOfItems.length; ++i){ | |
299 | var match = true; | |
300 | var candidateItem = arrayOfItems[i]; | |
301 | if(candidateItem === null){ | |
302 | match = false; | |
303 | }else{ | |
304 | for(key in requestArgs.query){ | |
305 | value = requestArgs.query[key]; | |
306 | if(!self._containsValue(candidateItem, key, value, regexpList[key])){ | |
307 | match = false; | |
308 | } | |
309 | } | |
310 | } | |
311 | if(match){ | |
312 | items.push(candidateItem); | |
313 | } | |
314 | } | |
315 | findCallback(items, requestArgs); | |
316 | }else{ | |
81bea17a AD |
317 | // We want a copy to pass back in case the parent wishes to sort the array. |
318 | // We shouldn't allow resort of the internal list, so that multiple callers | |
a089699c AD |
319 | // can get lists and sort without affecting each other. We also need to |
320 | // filter out any null values that have been left as a result of deleteItem() | |
321 | // calls in ItemFileWriteStore. | |
322 | for(i = 0; i < arrayOfItems.length; ++i){ | |
323 | var item = arrayOfItems[i]; | |
324 | if(item !== null){ | |
325 | items.push(item); | |
326 | } | |
327 | } | |
328 | findCallback(items, requestArgs); | |
329 | } | |
330 | }; | |
331 | ||
332 | if(this._loadFinished){ | |
333 | filter(keywordArgs, this._getItemsArray(keywordArgs.queryOptions)); | |
334 | }else{ | |
335 | //Do a check on the JsonFileUrl and crosscheck it. | |
336 | //If it doesn't match the cross-check, it needs to be updated | |
337 | //This allows for either url or _jsonFileUrl to he changed to | |
81bea17a | 338 | //reset the store load location. Done this way for backwards |
a089699c AD |
339 | //compatibility. People use _jsonFileUrl (even though officially |
340 | //private. | |
341 | if(this._jsonFileUrl !== this._ccUrl){ | |
81bea17a | 342 | dojo.deprecated("dojo.data.ItemFileReadStore: ", |
a089699c AD |
343 | "To change the url, set the url property of the store," + |
344 | " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0"); | |
345 | this._ccUrl = this._jsonFileUrl; | |
346 | this.url = this._jsonFileUrl; | |
347 | }else if(this.url !== this._ccUrl){ | |
348 | this._jsonFileUrl = this.url; | |
349 | this._ccUrl = this.url; | |
350 | } | |
351 | ||
352 | //See if there was any forced reset of data. | |
81bea17a | 353 | if(this.data != null){ |
a089699c AD |
354 | this._jsonData = this.data; |
355 | this.data = null; | |
356 | } | |
357 | ||
358 | if(this._jsonFileUrl){ | |
359 | //If fetches come in before the loading has finished, but while | |
81bea17a | 360 | //a load is in progress, we have to defer the fetching to be |
a089699c AD |
361 | //invoked in the callback. |
362 | if(this._loadInProgress){ | |
363 | this._queuedFetches.push({args: keywordArgs, filter: filter}); | |
364 | }else{ | |
365 | this._loadInProgress = true; | |
366 | var getArgs = { | |
81bea17a | 367 | url: self._jsonFileUrl, |
a089699c AD |
368 | handleAs: "json-comment-optional", |
369 | preventCache: this.urlPreventCache, | |
370 | failOk: this.failOk | |
371 | }; | |
372 | var getHandler = dojo.xhrGet(getArgs); | |
373 | getHandler.addCallback(function(data){ | |
374 | try{ | |
375 | self._getItemsFromLoadedData(data); | |
376 | self._loadFinished = true; | |
377 | self._loadInProgress = false; | |
378 | ||
379 | filter(keywordArgs, self._getItemsArray(keywordArgs.queryOptions)); | |
380 | self._handleQueuedFetches(); | |
381 | }catch(e){ | |
382 | self._loadFinished = true; | |
383 | self._loadInProgress = false; | |
384 | errorCallback(e, keywordArgs); | |
385 | } | |
386 | }); | |
387 | getHandler.addErrback(function(error){ | |
388 | self._loadInProgress = false; | |
389 | errorCallback(error, keywordArgs); | |
390 | }); | |
391 | ||
392 | //Wire up the cancel to abort of the request | |
393 | //This call cancel on the deferred if it hasn't been called | |
394 | //yet and then will chain to the simple abort of the | |
395 | //simpleFetch keywordArgs | |
396 | var oldAbort = null; | |
397 | if(keywordArgs.abort){ | |
398 | oldAbort = keywordArgs.abort; | |
399 | } | |
400 | keywordArgs.abort = function(){ | |
401 | var df = getHandler; | |
402 | if(df && df.fired === -1){ | |
403 | df.cancel(); | |
404 | df = null; | |
405 | } | |
406 | if(oldAbort){ | |
407 | oldAbort.call(keywordArgs); | |
408 | } | |
409 | }; | |
410 | } | |
411 | }else if(this._jsonData){ | |
412 | try{ | |
413 | this._loadFinished = true; | |
414 | this._getItemsFromLoadedData(this._jsonData); | |
415 | this._jsonData = null; | |
416 | filter(keywordArgs, this._getItemsArray(keywordArgs.queryOptions)); | |
417 | }catch(e){ | |
418 | errorCallback(e, keywordArgs); | |
419 | } | |
420 | }else{ | |
421 | errorCallback(new Error("dojo.data.ItemFileReadStore: No JSON source data was provided as either URL or a nested Javascript object."), keywordArgs); | |
422 | } | |
423 | } | |
424 | }, | |
425 | ||
426 | _handleQueuedFetches: function(){ | |
81bea17a | 427 | // summary: |
a089699c AD |
428 | // Internal function to execute delayed request in the store. |
429 | //Execute any deferred fetches now. | |
430 | if(this._queuedFetches.length > 0){ | |
431 | for(var i = 0; i < this._queuedFetches.length; i++){ | |
432 | var fData = this._queuedFetches[i], | |
433 | delayedQuery = fData.args, | |
434 | delayedFilter = fData.filter; | |
435 | if(delayedFilter){ | |
81bea17a | 436 | delayedFilter(delayedQuery, this._getItemsArray(delayedQuery.queryOptions)); |
a089699c AD |
437 | }else{ |
438 | this.fetchItemByIdentity(delayedQuery); | |
439 | } | |
440 | } | |
441 | this._queuedFetches = []; | |
442 | } | |
443 | }, | |
444 | ||
445 | _getItemsArray: function(/*object?*/queryOptions){ | |
81bea17a | 446 | // summary: |
a089699c AD |
447 | // Internal function to determine which list of items to search over. |
448 | // queryOptions: The query options parameter, if any. | |
449 | if(queryOptions && queryOptions.deep){ | |
81bea17a | 450 | return this._arrayOfAllItems; |
a089699c AD |
451 | } |
452 | return this._arrayOfTopLevelItems; | |
453 | }, | |
454 | ||
455 | close: function(/*dojo.data.api.Request || keywordArgs || null */ request){ | |
81bea17a | 456 | // summary: |
a089699c | 457 | // See dojo.data.api.Read.close() |
81bea17a AD |
458 | if(this.clearOnClose && |
459 | this._loadFinished && | |
a089699c AD |
460 | !this._loadInProgress){ |
461 | //Reset all internalsback to default state. This will force a reload | |
81bea17a | 462 | //on next fetch. This also checks that the data or url param was set |
a089699c AD |
463 | //so that the store knows it can get data. Without one of those being set, |
464 | //the next fetch will trigger an error. | |
465 | ||
81bea17a | 466 | if(((this._jsonFileUrl == "" || this._jsonFileUrl == null) && |
a089699c AD |
467 | (this.url == "" || this.url == null) |
468 | ) && this.data == null){ | |
469 | console.debug("dojo.data.ItemFileReadStore: WARNING! Data reload " + | |
81bea17a | 470 | " information has not been provided." + |
a089699c AD |
471 | " Please set 'url' or 'data' to the appropriate value before" + |
472 | " the next fetch"); | |
473 | } | |
474 | this._arrayOfAllItems = []; | |
475 | this._arrayOfTopLevelItems = []; | |
476 | this._loadFinished = false; | |
477 | this._itemsByIdentity = null; | |
478 | this._loadInProgress = false; | |
479 | this._queuedFetches = []; | |
480 | } | |
481 | }, | |
482 | ||
483 | _getItemsFromLoadedData: function(/* Object */ dataObject){ | |
484 | // summary: | |
485 | // Function to parse the loaded data into item format and build the internal items array. | |
486 | // description: | |
487 | // Function to parse the loaded data into item format and build the internal items array. | |
488 | // | |
489 | // dataObject: | |
490 | // The JS data object containing the raw data to convery into item format. | |
491 | // | |
492 | // returns: array | |
493 | // Array of items in store item format. | |
494 | ||
495 | // First, we define a couple little utility functions... | |
496 | var addingArrays = false, | |
497 | self = this; | |
498 | ||
499 | function valueIsAnItem(/* anything */ aValue){ | |
500 | // summary: | |
501 | // Given any sort of value that could be in the raw json data, | |
502 | // return true if we should interpret the value as being an | |
503 | // item itself, rather than a literal value or a reference. | |
504 | // example: | |
505 | // | false == valueIsAnItem("Kermit"); | |
506 | // | false == valueIsAnItem(42); | |
507 | // | false == valueIsAnItem(new Date()); | |
81bea17a | 508 | // | false == valueIsAnItem({_type:'Date', _value:'1802-05-14'}); |
a089699c AD |
509 | // | false == valueIsAnItem({_reference:'Kermit'}); |
510 | // | true == valueIsAnItem({name:'Kermit', color:'green'}); | |
511 | // | true == valueIsAnItem({iggy:'pop'}); | |
512 | // | true == valueIsAnItem({foo:42}); | |
513 | var isItem = ( | |
514 | (aValue !== null) && | |
515 | (typeof aValue === "object") && | |
516 | (!dojo.isArray(aValue) || addingArrays) && | |
517 | (!dojo.isFunction(aValue)) && | |
518 | (aValue.constructor == Object || dojo.isArray(aValue)) && | |
81bea17a AD |
519 | (typeof aValue._reference === "undefined") && |
520 | (typeof aValue._type === "undefined") && | |
a089699c AD |
521 | (typeof aValue._value === "undefined") && |
522 | self.hierarchical | |
523 | ); | |
524 | return isItem; | |
525 | } | |
526 | ||
527 | function addItemAndSubItemsToArrayOfAllItems(/* Item */ anItem){ | |
528 | self._arrayOfAllItems.push(anItem); | |
529 | for(var attribute in anItem){ | |
530 | var valueForAttribute = anItem[attribute]; | |
531 | if(valueForAttribute){ | |
532 | if(dojo.isArray(valueForAttribute)){ | |
533 | var valueArray = valueForAttribute; | |
534 | for(var k = 0; k < valueArray.length; ++k){ | |
535 | var singleValue = valueArray[k]; | |
536 | if(valueIsAnItem(singleValue)){ | |
537 | addItemAndSubItemsToArrayOfAllItems(singleValue); | |
538 | } | |
539 | } | |
540 | }else{ | |
541 | if(valueIsAnItem(valueForAttribute)){ | |
542 | addItemAndSubItemsToArrayOfAllItems(valueForAttribute); | |
543 | } | |
544 | } | |
545 | } | |
546 | } | |
547 | } | |
548 | ||
549 | this._labelAttr = dataObject.label; | |
550 | ||
551 | // We need to do some transformations to convert the data structure | |
552 | // that we read from the file into a format that will be convenient | |
553 | // to work with in memory. | |
554 | ||
555 | // Step 1: Walk through the object hierarchy and build a list of all items | |
556 | var i, | |
557 | item; | |
558 | this._arrayOfAllItems = []; | |
559 | this._arrayOfTopLevelItems = dataObject.items; | |
560 | ||
561 | for(i = 0; i < this._arrayOfTopLevelItems.length; ++i){ | |
562 | item = this._arrayOfTopLevelItems[i]; | |
563 | if(dojo.isArray(item)){ | |
564 | addingArrays = true; | |
565 | } | |
566 | addItemAndSubItemsToArrayOfAllItems(item); | |
567 | item[this._rootItemPropName]=true; | |
568 | } | |
569 | ||
81bea17a | 570 | // Step 2: Walk through all the attribute values of all the items, |
a089699c AD |
571 | // and replace single values with arrays. For example, we change this: |
572 | // { name:'Miss Piggy', pets:'Foo-Foo'} | |
573 | // into this: | |
574 | // { name:['Miss Piggy'], pets:['Foo-Foo']} | |
81bea17a AD |
575 | // |
576 | // We also store the attribute names so we can validate our store | |
a089699c AD |
577 | // reference and item id special properties for the O(1) isItem |
578 | var allAttributeNames = {}, | |
579 | key; | |
580 | ||
581 | for(i = 0; i < this._arrayOfAllItems.length; ++i){ | |
582 | item = this._arrayOfAllItems[i]; | |
583 | for(key in item){ | |
584 | if(key !== this._rootItemPropName){ | |
585 | var value = item[key]; | |
586 | if(value !== null){ | |
587 | if(!dojo.isArray(value)){ | |
588 | item[key] = [value]; | |
589 | } | |
590 | }else{ | |
591 | item[key] = [null]; | |
592 | } | |
593 | } | |
594 | allAttributeNames[key]=key; | |
595 | } | |
596 | } | |
597 | ||
598 | // Step 3: Build unique property names to use for the _storeRefPropName and _itemNumPropName | |
599 | // This should go really fast, it will generally never even run the loop. | |
600 | while(allAttributeNames[this._storeRefPropName]){ | |
601 | this._storeRefPropName += "_"; | |
602 | } | |
603 | while(allAttributeNames[this._itemNumPropName]){ | |
604 | this._itemNumPropName += "_"; | |
605 | } | |
606 | while(allAttributeNames[this._reverseRefMap]){ | |
607 | this._reverseRefMap += "_"; | |
608 | } | |
609 | ||
81bea17a AD |
610 | // Step 4: Some data files specify an optional 'identifier', which is |
611 | // the name of an attribute that holds the identity of each item. | |
612 | // If this data file specified an identifier attribute, then build a | |
a089699c AD |
613 | // hash table of items keyed by the identity of the items. |
614 | var arrayOfValues; | |
615 | ||
616 | var identifier = dataObject.identifier; | |
617 | if(identifier){ | |
618 | this._itemsByIdentity = {}; | |
619 | this._features['dojo.data.api.Identity'] = identifier; | |
620 | for(i = 0; i < this._arrayOfAllItems.length; ++i){ | |
621 | item = this._arrayOfAllItems[i]; | |
622 | arrayOfValues = item[identifier]; | |
623 | var identity = arrayOfValues[0]; | |
81bea17a | 624 | if(!Object.hasOwnProperty.call(this._itemsByIdentity, identity)){ |
a089699c AD |
625 | this._itemsByIdentity[identity] = item; |
626 | }else{ | |
627 | if(this._jsonFileUrl){ | |
628 | throw new Error("dojo.data.ItemFileReadStore: The json data as specified by: [" + this._jsonFileUrl + "] is malformed. Items within the list have identifier: [" + identifier + "]. Value collided: [" + identity + "]"); | |
629 | }else if(this._jsonData){ | |
630 | throw new Error("dojo.data.ItemFileReadStore: The json data provided by the creation arguments is malformed. Items within the list have identifier: [" + identifier + "]. Value collided: [" + identity + "]"); | |
631 | } | |
632 | } | |
633 | } | |
634 | }else{ | |
635 | this._features['dojo.data.api.Identity'] = Number; | |
636 | } | |
637 | ||
81bea17a | 638 | // Step 5: Walk through all the items, and set each item's properties |
a089699c AD |
639 | // for _storeRefPropName and _itemNumPropName, so that store.isItem() will return true. |
640 | for(i = 0; i < this._arrayOfAllItems.length; ++i){ | |
641 | item = this._arrayOfAllItems[i]; | |
642 | item[this._storeRefPropName] = this; | |
643 | item[this._itemNumPropName] = i; | |
644 | } | |
645 | ||
646 | // Step 6: We walk through all the attribute values of all the items, | |
647 | // looking for type/value literals and item-references. | |
648 | // | |
649 | // We replace item-references with pointers to items. For example, we change: | |
650 | // { name:['Kermit'], friends:[{_reference:{name:'Miss Piggy'}}] } | |
651 | // into this: | |
81bea17a | 652 | // { name:['Kermit'], friends:[miss_piggy] } |
a089699c AD |
653 | // (where miss_piggy is the object representing the 'Miss Piggy' item). |
654 | // | |
655 | // We replace type/value pairs with typed-literals. For example, we change: | |
81bea17a | 656 | // { name:['Nelson Mandela'], born:[{_type:'Date', _value:'1918-07-18'}] } |
a089699c | 657 | // into this: |
81bea17a | 658 | // { name:['Kermit'], born:(new Date(1918, 6, 18)) } |
a089699c AD |
659 | // |
660 | // We also generate the associate map for all items for the O(1) isItem function. | |
661 | for(i = 0; i < this._arrayOfAllItems.length; ++i){ | |
662 | item = this._arrayOfAllItems[i]; // example: { name:['Kermit'], friends:[{_reference:{name:'Miss Piggy'}}] } | |
663 | for(key in item){ | |
664 | arrayOfValues = item[key]; // example: [{_reference:{name:'Miss Piggy'}}] | |
665 | for(var j = 0; j < arrayOfValues.length; ++j){ | |
666 | value = arrayOfValues[j]; // example: {_reference:{name:'Miss Piggy'}} | |
667 | if(value !== null && typeof value == "object"){ | |
668 | if(("_type" in value) && ("_value" in value)){ | |
669 | var type = value._type; // examples: 'Date', 'Color', or 'ComplexNumber' | |
670 | var mappingObj = this._datatypeMap[type]; // examples: Date, dojo.Color, foo.math.ComplexNumber, {type: dojo.Color, deserialize(value){ return new dojo.Color(value)}} | |
81bea17a | 671 | if(!mappingObj){ |
a089699c AD |
672 | throw new Error("dojo.data.ItemFileReadStore: in the typeMap constructor arg, no object class was specified for the datatype '" + type + "'"); |
673 | }else if(dojo.isFunction(mappingObj)){ | |
674 | arrayOfValues[j] = new mappingObj(value._value); | |
675 | }else if(dojo.isFunction(mappingObj.deserialize)){ | |
676 | arrayOfValues[j] = mappingObj.deserialize(value._value); | |
677 | }else{ | |
678 | throw new Error("dojo.data.ItemFileReadStore: Value provided in typeMap was neither a constructor, nor a an object with a deserialize function"); | |
679 | } | |
680 | } | |
681 | if(value._reference){ | |
682 | var referenceDescription = value._reference; // example: {name:'Miss Piggy'} | |
683 | if(!dojo.isObject(referenceDescription)){ | |
684 | // example: 'Miss Piggy' | |
685 | // from an item like: { name:['Kermit'], friends:[{_reference:'Miss Piggy'}]} | |
686 | arrayOfValues[j] = this._getItemByIdentity(referenceDescription); | |
687 | }else{ | |
688 | // example: {name:'Miss Piggy'} | |
689 | // from an item like: { name:['Kermit'], friends:[{_reference:{name:'Miss Piggy'}}] } | |
690 | for(var k = 0; k < this._arrayOfAllItems.length; ++k){ | |
691 | var candidateItem = this._arrayOfAllItems[k], | |
692 | found = true; | |
693 | for(var refKey in referenceDescription){ | |
81bea17a AD |
694 | if(candidateItem[refKey] != referenceDescription[refKey]){ |
695 | found = false; | |
a089699c AD |
696 | } |
697 | } | |
81bea17a AD |
698 | if(found){ |
699 | arrayOfValues[j] = candidateItem; | |
a089699c AD |
700 | } |
701 | } | |
702 | } | |
703 | if(this.referenceIntegrity){ | |
704 | var refItem = arrayOfValues[j]; | |
705 | if(this.isItem(refItem)){ | |
706 | this._addReferenceToMap(refItem, item, key); | |
707 | } | |
708 | } | |
709 | }else if(this.isItem(value)){ | |
81bea17a | 710 | //It's a child item (not one referenced through _reference). |
a089699c AD |
711 | //We need to treat this as a referenced item, so it can be cleaned up |
712 | //in a write store easily. | |
713 | if(this.referenceIntegrity){ | |
714 | this._addReferenceToMap(value, item, key); | |
715 | } | |
716 | } | |
717 | } | |
718 | } | |
719 | } | |
720 | } | |
721 | }, | |
722 | ||
723 | _addReferenceToMap: function(/*item*/ refItem, /*item*/ parentItem, /*string*/ attribute){ | |
724 | // summary: | |
725 | // Method to add an reference map entry for an item and attribute. | |
726 | // description: | |
727 | // Method to add an reference map entry for an item and attribute. // | |
728 | // refItem: | |
729 | // The item that is referenced. | |
730 | // parentItem: | |
731 | // The item that holds the new reference to refItem. | |
732 | // attribute: | |
733 | // The attribute on parentItem that contains the new reference. | |
734 | ||
735 | //Stub function, does nothing. Real processing is in ItemFileWriteStore. | |
736 | }, | |
737 | ||
738 | getIdentity: function(/* item */ item){ | |
81bea17a | 739 | // summary: |
a089699c AD |
740 | // See dojo.data.api.Identity.getIdentity() |
741 | var identifier = this._features['dojo.data.api.Identity']; | |
742 | if(identifier === Number){ | |
743 | return item[this._itemNumPropName]; // Number | |
744 | }else{ | |
745 | var arrayOfValues = item[identifier]; | |
746 | if(arrayOfValues){ | |
747 | return arrayOfValues[0]; // Object || String | |
748 | } | |
749 | } | |
750 | return null; // null | |
751 | }, | |
752 | ||
753 | fetchItemByIdentity: function(/* Object */ keywordArgs){ | |
81bea17a | 754 | // summary: |
a089699c AD |
755 | // See dojo.data.api.Identity.fetchItemByIdentity() |
756 | ||
757 | // Hasn't loaded yet, we have to trigger the load. | |
758 | var item, | |
759 | scope; | |
760 | if(!this._loadFinished){ | |
761 | var self = this; | |
762 | //Do a check on the JsonFileUrl and crosscheck it. | |
763 | //If it doesn't match the cross-check, it needs to be updated | |
764 | //This allows for either url or _jsonFileUrl to he changed to | |
81bea17a | 765 | //reset the store load location. Done this way for backwards |
a089699c AD |
766 | //compatibility. People use _jsonFileUrl (even though officially |
767 | //private. | |
768 | if(this._jsonFileUrl !== this._ccUrl){ | |
81bea17a | 769 | dojo.deprecated("dojo.data.ItemFileReadStore: ", |
a089699c AD |
770 | "To change the url, set the url property of the store," + |
771 | " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0"); | |
772 | this._ccUrl = this._jsonFileUrl; | |
773 | this.url = this._jsonFileUrl; | |
774 | }else if(this.url !== this._ccUrl){ | |
775 | this._jsonFileUrl = this.url; | |
776 | this._ccUrl = this.url; | |
777 | } | |
778 | ||
779 | //See if there was any forced reset of data. | |
780 | if(this.data != null && this._jsonData == null){ | |
781 | this._jsonData = this.data; | |
782 | this.data = null; | |
783 | } | |
784 | ||
785 | if(this._jsonFileUrl){ | |
786 | ||
787 | if(this._loadInProgress){ | |
788 | this._queuedFetches.push({args: keywordArgs}); | |
789 | }else{ | |
790 | this._loadInProgress = true; | |
791 | var getArgs = { | |
81bea17a | 792 | url: self._jsonFileUrl, |
a089699c AD |
793 | handleAs: "json-comment-optional", |
794 | preventCache: this.urlPreventCache, | |
795 | failOk: this.failOk | |
796 | }; | |
797 | var getHandler = dojo.xhrGet(getArgs); | |
798 | getHandler.addCallback(function(data){ | |
799 | var scope = keywordArgs.scope?keywordArgs.scope:dojo.global; | |
800 | try{ | |
801 | self._getItemsFromLoadedData(data); | |
802 | self._loadFinished = true; | |
803 | self._loadInProgress = false; | |
804 | item = self._getItemByIdentity(keywordArgs.identity); | |
805 | if(keywordArgs.onItem){ | |
806 | keywordArgs.onItem.call(scope, item); | |
807 | } | |
808 | self._handleQueuedFetches(); | |
809 | }catch(error){ | |
810 | self._loadInProgress = false; | |
811 | if(keywordArgs.onError){ | |
812 | keywordArgs.onError.call(scope, error); | |
813 | } | |
814 | } | |
815 | }); | |
816 | getHandler.addErrback(function(error){ | |
817 | self._loadInProgress = false; | |
818 | if(keywordArgs.onError){ | |
819 | var scope = keywordArgs.scope?keywordArgs.scope:dojo.global; | |
820 | keywordArgs.onError.call(scope, error); | |
821 | } | |
822 | }); | |
823 | } | |
824 | ||
825 | }else if(this._jsonData){ | |
826 | // Passed in data, no need to xhr. | |
827 | self._getItemsFromLoadedData(self._jsonData); | |
828 | self._jsonData = null; | |
829 | self._loadFinished = true; | |
830 | item = self._getItemByIdentity(keywordArgs.identity); | |
831 | if(keywordArgs.onItem){ | |
832 | scope = keywordArgs.scope?keywordArgs.scope:dojo.global; | |
833 | keywordArgs.onItem.call(scope, item); | |
834 | } | |
81bea17a | 835 | } |
a089699c AD |
836 | }else{ |
837 | // Already loaded. We can just look it up and call back. | |
838 | item = this._getItemByIdentity(keywordArgs.identity); | |
839 | if(keywordArgs.onItem){ | |
840 | scope = keywordArgs.scope?keywordArgs.scope:dojo.global; | |
841 | keywordArgs.onItem.call(scope, item); | |
842 | } | |
843 | } | |
844 | }, | |
845 | ||
846 | _getItemByIdentity: function(/* Object */ identity){ | |
847 | // summary: | |
848 | // Internal function to look an item up by its identity map. | |
849 | var item = null; | |
81bea17a AD |
850 | if(this._itemsByIdentity && |
851 | Object.hasOwnProperty.call(this._itemsByIdentity, identity)){ | |
a089699c | 852 | item = this._itemsByIdentity[identity]; |
81bea17a | 853 | }else if (Object.hasOwnProperty.call(this._arrayOfAllItems, identity)){ |
a089699c AD |
854 | item = this._arrayOfAllItems[identity]; |
855 | } | |
856 | if(item === undefined){ | |
857 | item = null; | |
858 | } | |
859 | return item; // Object | |
860 | }, | |
861 | ||
862 | getIdentityAttributes: function(/* item */ item){ | |
81bea17a AD |
863 | // summary: |
864 | // See dojo.data.api.Identity.getIdentityAttributes() | |
a089699c AD |
865 | |
866 | var identifier = this._features['dojo.data.api.Identity']; | |
867 | if(identifier === Number){ | |
868 | // If (identifier === Number) it means getIdentity() just returns | |
869 | // an integer item-number for each item. The dojo.data.api.Identity | |
81bea17a AD |
870 | // spec says we need to return null if the identity is not composed |
871 | // of attributes | |
a089699c AD |
872 | return null; // null |
873 | }else{ | |
874 | return [identifier]; // Array | |
875 | } | |
876 | }, | |
877 | ||
878 | _forceLoad: function(){ | |
81bea17a | 879 | // summary: |
a089699c | 880 | // Internal function to force a load of the store if it hasn't occurred yet. This is required |
81bea17a | 881 | // for specific functions to work properly. |
a089699c AD |
882 | var self = this; |
883 | //Do a check on the JsonFileUrl and crosscheck it. | |
884 | //If it doesn't match the cross-check, it needs to be updated | |
885 | //This allows for either url or _jsonFileUrl to he changed to | |
81bea17a | 886 | //reset the store load location. Done this way for backwards |
a089699c AD |
887 | //compatibility. People use _jsonFileUrl (even though officially |
888 | //private. | |
889 | if(this._jsonFileUrl !== this._ccUrl){ | |
81bea17a | 890 | dojo.deprecated("dojo.data.ItemFileReadStore: ", |
a089699c AD |
891 | "To change the url, set the url property of the store," + |
892 | " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0"); | |
893 | this._ccUrl = this._jsonFileUrl; | |
894 | this.url = this._jsonFileUrl; | |
895 | }else if(this.url !== this._ccUrl){ | |
896 | this._jsonFileUrl = this.url; | |
897 | this._ccUrl = this.url; | |
898 | } | |
899 | ||
900 | //See if there was any forced reset of data. | |
81bea17a | 901 | if(this.data != null){ |
a089699c AD |
902 | this._jsonData = this.data; |
903 | this.data = null; | |
904 | } | |
905 | ||
906 | if(this._jsonFileUrl){ | |
907 | var getArgs = { | |
81bea17a | 908 | url: this._jsonFileUrl, |
a089699c AD |
909 | handleAs: "json-comment-optional", |
910 | preventCache: this.urlPreventCache, | |
911 | failOk: this.failOk, | |
912 | sync: true | |
913 | }; | |
914 | var getHandler = dojo.xhrGet(getArgs); | |
915 | getHandler.addCallback(function(data){ | |
916 | try{ | |
81bea17a | 917 | //Check to be sure there wasn't another load going on concurrently |
a089699c AD |
918 | //So we don't clobber data that comes in on it. If there is a load going on |
919 | //then do not save this data. It will potentially clobber current data. | |
920 | //We mainly wanted to sync/wait here. | |
921 | //TODO: Revisit the loading scheme of this store to improve multi-initial | |
922 | //request handling. | |
923 | if(self._loadInProgress !== true && !self._loadFinished){ | |
924 | self._getItemsFromLoadedData(data); | |
925 | self._loadFinished = true; | |
926 | }else if(self._loadInProgress){ | |
927 | //Okay, we hit an error state we can't recover from. A forced load occurred | |
928 | //while an async load was occurring. Since we cannot block at this point, the best | |
929 | //that can be managed is to throw an error. | |
81bea17a | 930 | throw new Error("dojo.data.ItemFileReadStore: Unable to perform a synchronous load, an async load is in progress."); |
a089699c AD |
931 | } |
932 | }catch(e){ | |
933 | console.log(e); | |
934 | throw e; | |
935 | } | |
936 | }); | |
937 | getHandler.addErrback(function(error){ | |
938 | throw error; | |
939 | }); | |
940 | }else if(this._jsonData){ | |
941 | self._getItemsFromLoadedData(self._jsonData); | |
942 | self._jsonData = null; | |
943 | self._loadFinished = true; | |
81bea17a | 944 | } |
a089699c | 945 | } |
2f01fe57 | 946 | }); |
a089699c | 947 | //Mix in the simple fetch implementation to this class. |
2f01fe57 | 948 | dojo.extend(dojo.data.ItemFileReadStore,dojo.data.util.simpleFetch); |
a089699c | 949 | |
2f01fe57 | 950 | } |