123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945 |
- define("dojo/data/ItemFileReadStore", ["../_base/kernel", "../_base/lang", "../_base/declare", "../_base/array", "../_base/xhr",
- "../Evented", "../_base/window", "./util/filter", "./util/simpleFetch", "../date/stamp"
- ], function(kernel, lang, declare, array, xhr, Evented, window, filterUtil, simpleFetch, dateStamp) {
-
-
-
-
- var ItemFileReadStore = declare("dojo.data.ItemFileReadStore", [Evented],{
-
-
-
-
-
-
-
-
-
-
-
- constructor: function(/* Object */ keywordParameters){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- this._arrayOfAllItems = [];
- this._arrayOfTopLevelItems = [];
- this._loadFinished = false;
- this._jsonFileUrl = keywordParameters.url;
- this._ccUrl = keywordParameters.url;
- this.url = keywordParameters.url;
- this._jsonData = keywordParameters.data;
- this.data = null;
- this._datatypeMap = keywordParameters.typeMap || {};
- if(!this._datatypeMap['Date']){
-
-
-
- this._datatypeMap['Date'] = {
- type: Date,
- deserialize: function(value){
- return dateStamp.fromISOString(value);
- }
- };
- }
- this._features = {'dojo.data.api.Read':true, 'dojo.data.api.Identity':true};
- this._itemsByIdentity = null;
- this._storeRefPropName = "_S";
- this._itemNumPropName = "_0";
- this._rootItemPropName = "_RI";
- this._reverseRefMap = "_RRM";
- this._loadInProgress = false;
- this._queuedFetches = [];
- if(keywordParameters.urlPreventCache !== undefined){
- this.urlPreventCache = keywordParameters.urlPreventCache?true:false;
- }
- if(keywordParameters.hierarchical !== undefined){
- this.hierarchical = keywordParameters.hierarchical?true:false;
- }
- if(keywordParameters.clearOnClose){
- this.clearOnClose = true;
- }
- if("failOk" in keywordParameters){
- this.failOk = keywordParameters.failOk?true:false;
- }
- },
- url: "",
-
-
- _ccUrl: "",
- data: null,
- typeMap: null,
-
-
-
-
- clearOnClose: false,
-
-
-
- urlPreventCache: false,
-
- failOk: false,
-
-
-
-
-
- hierarchical: true,
- _assertIsItem: function(/* item */ item){
-
-
-
-
- if(!this.isItem(item)){
- throw new Error("dojo.data.ItemFileReadStore: Invalid item argument.");
- }
- },
- _assertIsAttribute: function(/* attribute-name-string */ attribute){
-
-
-
-
- if(typeof attribute !== "string"){
- throw new Error("dojo.data.ItemFileReadStore: Invalid attribute argument.");
- }
- },
- getValue: function( /* item */ item,
- /* attribute-name-string */ attribute,
- /* value? */ defaultValue){
-
-
- var values = this.getValues(item, attribute);
- return (values.length > 0)?values[0]:defaultValue;
- },
- getValues: function(/* item */ item,
- /* attribute-name-string */ attribute){
-
-
- this._assertIsItem(item);
- this._assertIsAttribute(attribute);
-
- return (item[attribute] || []).slice(0);
- },
- getAttributes: function(/* item */ item){
-
-
- this._assertIsItem(item);
- var attributes = [];
- for(var key in item){
-
- if((key !== this._storeRefPropName) && (key !== this._itemNumPropName) && (key !== this._rootItemPropName) && (key !== this._reverseRefMap)){
- attributes.push(key);
- }
- }
- return attributes;
- },
- hasAttribute: function( /* item */ item,
- /* attribute-name-string */ attribute){
-
-
- this._assertIsItem(item);
- this._assertIsAttribute(attribute);
- return (attribute in item);
- },
- containsValue: function(/* item */ item,
- /* attribute-name-string */ attribute,
- /* anything */ value){
-
-
- var regexp = undefined;
- if(typeof value === "string"){
- regexp = filterUtil.patternToRegExp(value, false);
- }
- return this._containsValue(item, attribute, value, regexp);
- },
- _containsValue: function( /* item */ item,
- /* attribute-name-string */ attribute,
- /* anything */ value,
- /* RegExp?*/ regexp){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return array.some(this.getValues(item, attribute), function(possibleValue){
- if(possibleValue !== null && !lang.isObject(possibleValue) && regexp){
- if(possibleValue.toString().match(regexp)){
- return true;
- }
- }else if(value === possibleValue){
- return true;
- }
- });
- },
- isItem: function(/* anything */ something){
-
-
- if(something && something[this._storeRefPropName] === this){
- if(this._arrayOfAllItems[something[this._itemNumPropName]] === something){
- return true;
- }
- }
- return false;
- },
- isItemLoaded: function(/* anything */ something){
-
-
- return this.isItem(something);
- },
- loadItem: function(/* object */ keywordArgs){
-
-
- this._assertIsItem(keywordArgs.item);
- },
- getFeatures: function(){
-
-
- return this._features;
- },
- getLabel: function(/* item */ item){
-
-
- if(this._labelAttr && this.isItem(item)){
- return this.getValue(item,this._labelAttr);
- }
- return undefined;
- },
- getLabelAttributes: function(/* item */ item){
-
-
- if(this._labelAttr){
- return [this._labelAttr];
- }
- return null;
- },
- _fetchItems: function( /* Object */ keywordArgs,
- /* Function */ findCallback,
- /* Function */ errorCallback){
-
-
- var self = this,
- filter = function(requestArgs, arrayOfItems){
- var items = [],
- i, key;
- if(requestArgs.query){
- var value,
- ignoreCase = requestArgs.queryOptions ? requestArgs.queryOptions.ignoreCase : false;
-
-
- var regexpList = {};
- for(key in requestArgs.query){
- value = requestArgs.query[key];
- if(typeof value === "string"){
- regexpList[key] = filterUtil.patternToRegExp(value, ignoreCase);
- }else if(value instanceof RegExp){
- regexpList[key] = value;
- }
- }
- for(i = 0; i < arrayOfItems.length; ++i){
- var match = true;
- var candidateItem = arrayOfItems[i];
- if(candidateItem === null){
- match = false;
- }else{
- for(key in requestArgs.query){
- value = requestArgs.query[key];
- if(!self._containsValue(candidateItem, key, value, regexpList[key])){
- match = false;
- }
- }
- }
- if(match){
- items.push(candidateItem);
- }
- }
- findCallback(items, requestArgs);
- }else{
-
-
-
-
-
- for(i = 0; i < arrayOfItems.length; ++i){
- var item = arrayOfItems[i];
- if(item !== null){
- items.push(item);
- }
- }
- findCallback(items, requestArgs);
- }
- };
- if(this._loadFinished){
- filter(keywordArgs, this._getItemsArray(keywordArgs.queryOptions));
- }else{
-
-
-
-
-
-
- if(this._jsonFileUrl !== this._ccUrl){
- kernel.deprecated("dojo.data.ItemFileReadStore: ",
- "To change the url, set the url property of the store," +
- " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0");
- this._ccUrl = this._jsonFileUrl;
- this.url = this._jsonFileUrl;
- }else if(this.url !== this._ccUrl){
- this._jsonFileUrl = this.url;
- this._ccUrl = this.url;
- }
-
- if(this.data != null){
- this._jsonData = this.data;
- this.data = null;
- }
- if(this._jsonFileUrl){
-
-
-
- if(this._loadInProgress){
- this._queuedFetches.push({args: keywordArgs, filter: filter});
- }else{
- this._loadInProgress = true;
- var getArgs = {
- url: self._jsonFileUrl,
- handleAs: "json-comment-optional",
- preventCache: this.urlPreventCache,
- failOk: this.failOk
- };
- var getHandler = xhr.get(getArgs);
- getHandler.addCallback(function(data){
- try{
- self._getItemsFromLoadedData(data);
- self._loadFinished = true;
- self._loadInProgress = false;
- filter(keywordArgs, self._getItemsArray(keywordArgs.queryOptions));
- self._handleQueuedFetches();
- }catch(e){
- self._loadFinished = true;
- self._loadInProgress = false;
- errorCallback(e, keywordArgs);
- }
- });
- getHandler.addErrback(function(error){
- self._loadInProgress = false;
- errorCallback(error, keywordArgs);
- });
-
-
-
-
- var oldAbort = null;
- if(keywordArgs.abort){
- oldAbort = keywordArgs.abort;
- }
- keywordArgs.abort = function(){
- var df = getHandler;
- if(df && df.fired === -1){
- df.cancel();
- df = null;
- }
- if(oldAbort){
- oldAbort.call(keywordArgs);
- }
- };
- }
- }else if(this._jsonData){
- try{
- this._loadFinished = true;
- this._getItemsFromLoadedData(this._jsonData);
- this._jsonData = null;
- filter(keywordArgs, this._getItemsArray(keywordArgs.queryOptions));
- }catch(e){
- errorCallback(e, keywordArgs);
- }
- }else{
- errorCallback(new Error("dojo.data.ItemFileReadStore: No JSON source data was provided as either URL or a nested Javascript object."), keywordArgs);
- }
- }
- },
- _handleQueuedFetches: function(){
-
-
-
- if(this._queuedFetches.length > 0){
- for(var i = 0; i < this._queuedFetches.length; i++){
- var fData = this._queuedFetches[i],
- delayedQuery = fData.args,
- delayedFilter = fData.filter;
- if(delayedFilter){
- delayedFilter(delayedQuery, this._getItemsArray(delayedQuery.queryOptions));
- }else{
- this.fetchItemByIdentity(delayedQuery);
- }
- }
- this._queuedFetches = [];
- }
- },
- _getItemsArray: function(/*object?*/queryOptions){
-
-
-
- if(queryOptions && queryOptions.deep){
- return this._arrayOfAllItems;
- }
- return this._arrayOfTopLevelItems;
- },
- close: function(/*dojo.data.api.Request || keywordArgs || null */ request){
-
-
- if(this.clearOnClose &&
- this._loadFinished &&
- !this._loadInProgress){
-
-
-
-
- if(((this._jsonFileUrl == "" || this._jsonFileUrl == null) &&
- (this.url == "" || this.url == null)
- ) && this.data == null){
- console.debug("dojo.data.ItemFileReadStore: WARNING! Data reload " +
- " information has not been provided." +
- " Please set 'url' or 'data' to the appropriate value before" +
- " the next fetch");
- }
- this._arrayOfAllItems = [];
- this._arrayOfTopLevelItems = [];
- this._loadFinished = false;
- this._itemsByIdentity = null;
- this._loadInProgress = false;
- this._queuedFetches = [];
- }
- },
- _getItemsFromLoadedData: function(/* Object */ dataObject){
-
-
-
-
-
-
-
-
-
-
-
- var addingArrays = false,
- self = this;
- function valueIsAnItem(/* anything */ aValue){
-
-
-
-
-
-
-
-
-
-
-
-
-
- return (aValue !== null) &&
- (typeof aValue === "object") &&
- (!lang.isArray(aValue) || addingArrays) &&
- (!lang.isFunction(aValue)) &&
- (aValue.constructor == Object || lang.isArray(aValue)) &&
- (typeof aValue._reference === "undefined") &&
- (typeof aValue._type === "undefined") &&
- (typeof aValue._value === "undefined") &&
- self.hierarchical;
- }
- function addItemAndSubItemsToArrayOfAllItems(/* Item */ anItem){
- self._arrayOfAllItems.push(anItem);
- for(var attribute in anItem){
- var valueForAttribute = anItem[attribute];
- if(valueForAttribute){
- if(lang.isArray(valueForAttribute)){
- var valueArray = valueForAttribute;
- for(var k = 0; k < valueArray.length; ++k){
- var singleValue = valueArray[k];
- if(valueIsAnItem(singleValue)){
- addItemAndSubItemsToArrayOfAllItems(singleValue);
- }
- }
- }else{
- if(valueIsAnItem(valueForAttribute)){
- addItemAndSubItemsToArrayOfAllItems(valueForAttribute);
- }
- }
- }
- }
- }
- this._labelAttr = dataObject.label;
-
-
-
-
- var i,
- item;
- this._arrayOfAllItems = [];
- this._arrayOfTopLevelItems = dataObject.items;
- for(i = 0; i < this._arrayOfTopLevelItems.length; ++i){
- item = this._arrayOfTopLevelItems[i];
- if(lang.isArray(item)){
- addingArrays = true;
- }
- addItemAndSubItemsToArrayOfAllItems(item);
- item[this._rootItemPropName]=true;
- }
-
-
-
-
-
-
-
-
- var allAttributeNames = {},
- key;
- for(i = 0; i < this._arrayOfAllItems.length; ++i){
- item = this._arrayOfAllItems[i];
- for(key in item){
- if(key !== this._rootItemPropName){
- var value = item[key];
- if(value !== null){
- if(!lang.isArray(value)){
- item[key] = [value];
- }
- }else{
- item[key] = [null];
- }
- }
- allAttributeNames[key]=key;
- }
- }
-
-
- while(allAttributeNames[this._storeRefPropName]){
- this._storeRefPropName += "_";
- }
- while(allAttributeNames[this._itemNumPropName]){
- this._itemNumPropName += "_";
- }
- while(allAttributeNames[this._reverseRefMap]){
- this._reverseRefMap += "_";
- }
-
-
-
-
- var arrayOfValues;
- var identifier = dataObject.identifier;
- if(identifier){
- this._itemsByIdentity = {};
- this._features['dojo.data.api.Identity'] = identifier;
- for(i = 0; i < this._arrayOfAllItems.length; ++i){
- item = this._arrayOfAllItems[i];
- arrayOfValues = item[identifier];
- var identity = arrayOfValues[0];
- if(!Object.hasOwnProperty.call(this._itemsByIdentity, identity)){
- this._itemsByIdentity[identity] = item;
- }else{
- if(this._jsonFileUrl){
- 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 + "]");
- }else if(this._jsonData){
- 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 + "]");
- }
- }
- }
- }else{
- this._features['dojo.data.api.Identity'] = Number;
- }
-
-
- for(i = 0; i < this._arrayOfAllItems.length; ++i){
- item = this._arrayOfAllItems[i];
- item[this._storeRefPropName] = this;
- item[this._itemNumPropName] = i;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- for(i = 0; i < this._arrayOfAllItems.length; ++i){
- item = this._arrayOfAllItems[i];
- for(key in item){
- arrayOfValues = item[key];
- for(var j = 0; j < arrayOfValues.length; ++j){
- value = arrayOfValues[j];
- if(value !== null && typeof value == "object"){
- if(("_type" in value) && ("_value" in value)){
- var type = value._type;
- var mappingObj = this._datatypeMap[type];
- if(!mappingObj){
- throw new Error("dojo.data.ItemFileReadStore: in the typeMap constructor arg, no object class was specified for the datatype '" + type + "'");
- }else if(lang.isFunction(mappingObj)){
- arrayOfValues[j] = new mappingObj(value._value);
- }else if(lang.isFunction(mappingObj.deserialize)){
- arrayOfValues[j] = mappingObj.deserialize(value._value);
- }else{
- throw new Error("dojo.data.ItemFileReadStore: Value provided in typeMap was neither a constructor, nor a an object with a deserialize function");
- }
- }
- if(value._reference){
- var referenceDescription = value._reference;
- if(!lang.isObject(referenceDescription)){
-
-
- arrayOfValues[j] = this._getItemByIdentity(referenceDescription);
- }else{
-
-
- for(var k = 0; k < this._arrayOfAllItems.length; ++k){
- var candidateItem = this._arrayOfAllItems[k],
- found = true;
- for(var refKey in referenceDescription){
- if(candidateItem[refKey] != referenceDescription[refKey]){
- found = false;
- }
- }
- if(found){
- arrayOfValues[j] = candidateItem;
- }
- }
- }
- if(this.referenceIntegrity){
- var refItem = arrayOfValues[j];
- if(this.isItem(refItem)){
- this._addReferenceToMap(refItem, item, key);
- }
- }
- }else if(this.isItem(value)){
-
-
-
- if(this.referenceIntegrity){
- this._addReferenceToMap(value, item, key);
- }
- }
- }
- }
- }
- }
- },
- _addReferenceToMap: function(/*item*/ refItem, /*item*/ parentItem, /*string*/ attribute){
-
-
-
-
-
-
-
-
-
-
-
- },
- getIdentity: function(/* item */ item){
-
-
- var identifier = this._features['dojo.data.api.Identity'];
- if(identifier === Number){
- return item[this._itemNumPropName];
- }else{
- var arrayOfValues = item[identifier];
- if(arrayOfValues){
- return arrayOfValues[0];
- }
- }
- return null;
- },
- fetchItemByIdentity: function(/* Object */ keywordArgs){
-
-
-
- var item,
- scope;
- if(!this._loadFinished){
- var self = this;
-
-
-
-
-
-
- if(this._jsonFileUrl !== this._ccUrl){
- kernel.deprecated("dojo.data.ItemFileReadStore: ",
- "To change the url, set the url property of the store," +
- " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0");
- this._ccUrl = this._jsonFileUrl;
- this.url = this._jsonFileUrl;
- }else if(this.url !== this._ccUrl){
- this._jsonFileUrl = this.url;
- this._ccUrl = this.url;
- }
-
- if(this.data != null && this._jsonData == null){
- this._jsonData = this.data;
- this.data = null;
- }
- if(this._jsonFileUrl){
- if(this._loadInProgress){
- this._queuedFetches.push({args: keywordArgs});
- }else{
- this._loadInProgress = true;
- var getArgs = {
- url: self._jsonFileUrl,
- handleAs: "json-comment-optional",
- preventCache: this.urlPreventCache,
- failOk: this.failOk
- };
- var getHandler = xhr.get(getArgs);
- getHandler.addCallback(function(data){
- var scope = keywordArgs.scope?keywordArgs.scope:window.global;
- try{
- self._getItemsFromLoadedData(data);
- self._loadFinished = true;
- self._loadInProgress = false;
- item = self._getItemByIdentity(keywordArgs.identity);
- if(keywordArgs.onItem){
- keywordArgs.onItem.call(scope, item);
- }
- self._handleQueuedFetches();
- }catch(error){
- self._loadInProgress = false;
- if(keywordArgs.onError){
- keywordArgs.onError.call(scope, error);
- }
- }
- });
- getHandler.addErrback(function(error){
- self._loadInProgress = false;
- if(keywordArgs.onError){
- var scope = keywordArgs.scope?keywordArgs.scope:window.global;
- keywordArgs.onError.call(scope, error);
- }
- });
- }
- }else if(this._jsonData){
-
- self._getItemsFromLoadedData(self._jsonData);
- self._jsonData = null;
- self._loadFinished = true;
- item = self._getItemByIdentity(keywordArgs.identity);
- if(keywordArgs.onItem){
- scope = keywordArgs.scope?keywordArgs.scope:window.global;
- keywordArgs.onItem.call(scope, item);
- }
- }
- }else{
-
- item = this._getItemByIdentity(keywordArgs.identity);
- if(keywordArgs.onItem){
- scope = keywordArgs.scope?keywordArgs.scope:window.global;
- keywordArgs.onItem.call(scope, item);
- }
- }
- },
- _getItemByIdentity: function(/* Object */ identity){
-
-
- var item = null;
- if(this._itemsByIdentity){
-
-
- if(Object.hasOwnProperty.call(this._itemsByIdentity, identity)){
- item = this._itemsByIdentity[identity];
- }
- }else if (Object.hasOwnProperty.call(this._arrayOfAllItems, identity)){
- item = this._arrayOfAllItems[identity];
- }
- if(item === undefined){
- item = null;
- }
- return item;
- },
- getIdentityAttributes: function(/* item */ item){
-
-
- var identifier = this._features['dojo.data.api.Identity'];
- if(identifier === Number){
-
-
-
-
- return null;
- }else{
- return [identifier];
- }
- },
- _forceLoad: function(){
-
-
-
- var self = this;
-
-
-
-
-
-
- if(this._jsonFileUrl !== this._ccUrl){
- kernel.deprecated("dojo.data.ItemFileReadStore: ",
- "To change the url, set the url property of the store," +
- " not _jsonFileUrl. _jsonFileUrl support will be removed in 2.0");
- this._ccUrl = this._jsonFileUrl;
- this.url = this._jsonFileUrl;
- }else if(this.url !== this._ccUrl){
- this._jsonFileUrl = this.url;
- this._ccUrl = this.url;
- }
-
- if(this.data != null){
- this._jsonData = this.data;
- this.data = null;
- }
- if(this._jsonFileUrl){
- var getArgs = {
- url: this._jsonFileUrl,
- handleAs: "json-comment-optional",
- preventCache: this.urlPreventCache,
- failOk: this.failOk,
- sync: true
- };
- var getHandler = xhr.get(getArgs);
- getHandler.addCallback(function(data){
- try{
-
-
-
-
-
-
- if(self._loadInProgress !== true && !self._loadFinished){
- self._getItemsFromLoadedData(data);
- self._loadFinished = true;
- }else if(self._loadInProgress){
-
-
-
- throw new Error("dojo.data.ItemFileReadStore: Unable to perform a synchronous load, an async load is in progress.");
- }
- }catch(e){
- console.log(e);
- throw e;
- }
- });
- getHandler.addErrback(function(error){
- throw error;
- });
- }else if(this._jsonData){
- self._getItemsFromLoadedData(self._jsonData);
- self._jsonData = null;
- self._loadFinished = true;
- }
- }
- });
- lang.extend(ItemFileReadStore,simpleFetch);
- return ItemFileReadStore;
- });
|