/*
	Copyright (c) 2004-2016, The JS Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/

//>>built
define("dojo/data/ItemFileReadStore","../_base/kernel ../_base/lang ../_base/declare ../_base/array ../_base/xhr ../Evented ./util/filter ./util/simpleFetch ../date/stamp".split(" "),function(q,m,u,B,v,C,y,D,E){u=u("dojo.data.ItemFileReadStore",[C],{constructor:function(a){this._arrayOfAllItems=[];this._arrayOfTopLevelItems=[];this._loadFinished=!1;this.url=this._ccUrl=this._jsonFileUrl=a.url;this._jsonData=a.data;this.data=null;this._datatypeMap=a.typeMap||{};this._datatypeMap.Date||(this._datatypeMap.Date=
{type:Date,deserialize:function(b){return E.fromISOString(b)}});this._features={"dojo.data.api.Read":!0,"dojo.data.api.Identity":!0};this._itemsByIdentity=null;this._storeRefPropName="_S";this._itemNumPropName="_0";this._rootItemPropName="_RI";this._reverseRefMap="_RRM";this._loadInProgress=!1;this._queuedFetches=[];void 0!==a.urlPreventCache&&(this.urlPreventCache=a.urlPreventCache?!0:!1);void 0!==a.hierarchical&&(this.hierarchical=a.hierarchical?!0:!1);a.clearOnClose&&(this.clearOnClose=!0);"failOk"in
a&&(this.failOk=a.failOk?!0:!1)},url:"",_ccUrl:"",data:null,typeMap:null,clearOnClose:!1,urlPreventCache:!1,failOk:!1,hierarchical:!0,_assertIsItem:function(a){if(!this.isItem(a))throw Error(this.declaredClass+": Invalid item argument.");},_assertIsAttribute:function(a){if("string"!==typeof a)throw Error(this.declaredClass+": Invalid attribute argument.");},getValue:function(a,b,d){a=this.getValues(a,b);return 0<a.length?a[0]:d},getValues:function(a,b){this._assertIsItem(a);this._assertIsAttribute(b);
return(a[b]||[]).slice(0)},getAttributes:function(a){this._assertIsItem(a);var b=[],d;for(d in a)d!==this._storeRefPropName&&d!==this._itemNumPropName&&d!==this._rootItemPropName&&d!==this._reverseRefMap&&b.push(d);return b},hasAttribute:function(a,b){this._assertIsItem(a);this._assertIsAttribute(b);return b in a},containsValue:function(a,b,d){var c=void 0;"string"===typeof d&&(c=y.patternToRegExp(d,!1));return this._containsValue(a,b,d,c)},_containsValue:function(a,b,d,c){return B.some(this.getValues(a,
b),function(h){if(null!==h&&!m.isObject(h)&&c){if(h.toString().match(c))return!0}else if(d===h)return!0})},isItem:function(a){return a&&a[this._storeRefPropName]===this&&this._arrayOfAllItems[a[this._itemNumPropName]]===a?!0:!1},isItemLoaded:function(a){return this.isItem(a)},loadItem:function(a){this._assertIsItem(a.item)},getFeatures:function(){return this._features},getLabel:function(a){if(this._labelAttr&&this.isItem(a))return this.getValue(a,this._labelAttr)},getLabelAttributes:function(a){return this._labelAttr?
[this._labelAttr]:null},filter:function(a,b,d){var c=[];if(a.query){var h=a.queryOptions?a.queryOptions.ignoreCase:!1;var g={};for(f in a.query){var e=a.query[f];"string"===typeof e?g[f]=y.patternToRegExp(e,h):e instanceof RegExp&&(g[f]=e)}for(h=0;h<b.length;++h){var k=!0,l=b[h];if(null===l)k=!1;else for(f in a.query)e=a.query[f],this._containsValue(l,f,e,g[f])||(k=!1);k&&c.push(l)}}else for(h=0;h<b.length;++h){var f=b[h];null!==f&&c.push(f)}d(c,a)},_fetchItems:function(a,b,d){var c=this;if(this._loadFinished)this.filter(a,
this._getItemsArray(a.queryOptions),b);else if(this._jsonFileUrl!==this._ccUrl?(q.deprecated(this.declaredClass+": ","To change the url, set the url property of the store, not _jsonFileUrl.  _jsonFileUrl support will be removed in 2.0"),this.url=this._ccUrl=this._jsonFileUrl):this.url!==this._ccUrl&&(this._ccUrl=this._jsonFileUrl=this.url),null!=this.data&&(this._jsonData=this.data,this.data=null),this._jsonFileUrl)if(this._loadInProgress)this._queuedFetches.push({args:a,filter:m.hitch(c,"filter"),
findCallback:m.hitch(c,b)});else{this._loadInProgress=!0;var h=v.get({url:c._jsonFileUrl,handleAs:"json-comment-optional",preventCache:this.urlPreventCache,failOk:this.failOk});h.addCallback(function(e){try{c._getItemsFromLoadedData(e),c._loadFinished=!0,c._loadInProgress=!1,c.filter(a,c._getItemsArray(a.queryOptions),b),c._handleQueuedFetches()}catch(k){c._loadFinished=!0,c._loadInProgress=!1,d(k,a)}});h.addErrback(function(e){c._loadInProgress=!1;d(e,a)});var g=null;a.abort&&(g=a.abort);a.abort=
function(){var e=h;e&&-1===e.fired&&(e.cancel(),e=null);g&&g.call(a)}}else if(this._jsonData)try{this._loadFinished=!0,this._getItemsFromLoadedData(this._jsonData),this._jsonData=null,c.filter(a,this._getItemsArray(a.queryOptions),b)}catch(e){d(e,a)}else d(Error(this.declaredClass+": No JSON source data was provided as either URL or a nested Javascript object."),a)},_handleQueuedFetches:function(){if(0<this._queuedFetches.length){for(var a=0;a<this._queuedFetches.length;a++){var b=this._queuedFetches[a],
d=b.args,c=b.filter;b=b.findCallback;c?c(d,this._getItemsArray(d.queryOptions),b):this.fetchItemByIdentity(d)}this._queuedFetches=[]}},_getItemsArray:function(a){return a&&a.deep?this._arrayOfAllItems:this._arrayOfTopLevelItems},close:function(a){this.clearOnClose&&this._loadFinished&&!this._loadInProgress&&(this._arrayOfAllItems=[],this._arrayOfTopLevelItems=[],this._loadFinished=!1,this._itemsByIdentity=null,this._loadInProgress=!1,this._queuedFetches=[])},_getItemsFromLoadedData:function(a){function b(n){return null!==
n&&"object"===typeof n&&(!m.isArray(n)||c)&&!m.isFunction(n)&&(n.constructor==Object||m.isArray(n))&&"undefined"===typeof n._reference&&"undefined"===typeof n._type&&"undefined"===typeof n._value&&h.hierarchical}function d(n){h._arrayOfAllItems.push(n);for(var F in n){var r=n[F];if(r)if(m.isArray(r))for(var w=0;w<r.length;++w){var z=r[w];b(z)&&d(z)}else b(r)&&d(r)}}var c=!1,h=this;this._labelAttr=a.label;var g;this._arrayOfAllItems=[];this._arrayOfTopLevelItems=a.items;for(g=0;g<this._arrayOfTopLevelItems.length;++g){var e=
this._arrayOfTopLevelItems[g];m.isArray(e)&&(c=!0);d(e);e[this._rootItemPropName]=!0}var k={},l;for(g=0;g<this._arrayOfAllItems.length;++g)for(l in e=this._arrayOfAllItems[g],e){if(l!==this._rootItemPropName){var f=e[l];null!==f?m.isArray(f)||(e[l]=[f]):e[l]=[null]}k[l]=l}for(;k[this._storeRefPropName];)this._storeRefPropName+="_";for(;k[this._itemNumPropName];)this._itemNumPropName+="_";for(;k[this._reverseRefMap];)this._reverseRefMap+="_";if(k=a.identifier)for(this._itemsByIdentity={},this._features["dojo.data.api.Identity"]=
k,g=0;g<this._arrayOfAllItems.length;++g)if(e=this._arrayOfAllItems[g],a=e[k],a=a[0],Object.hasOwnProperty.call(this._itemsByIdentity,a)){if(this._jsonFileUrl)throw Error(this.declaredClass+":  The json data as specified by: ["+this._jsonFileUrl+"] is malformed.  Items within the list have identifier: ["+k+"].  Value collided: ["+a+"]");if(this._jsonData)throw Error(this.declaredClass+":  The json data provided by the creation arguments is malformed.  Items within the list have identifier: ["+k+"].  Value collided: ["+
a+"]");}else this._itemsByIdentity[a]=e;else this._features["dojo.data.api.Identity"]=Number;for(g=0;g<this._arrayOfAllItems.length;++g)e=this._arrayOfAllItems[g],e[this._storeRefPropName]=this,e[this._itemNumPropName]=g;for(g=0;g<this._arrayOfAllItems.length;++g)for(l in e=this._arrayOfAllItems[g],e)for(a=e[l],k=0;k<a.length;++k)if(f=a[k],null!==f&&"object"==typeof f){if("_type"in f&&"_value"in f){var t=f._type,p=this._datatypeMap[t];if(p)if(m.isFunction(p))a[k]=new p(f._value);else if(m.isFunction(p.deserialize))a[k]=
p.deserialize(f._value);else throw Error("dojo.data.ItemFileReadStore: Value provided in typeMap was neither a constructor, nor a an object with a deserialize function");else throw Error("dojo.data.ItemFileReadStore: in the typeMap constructor arg, no object class was specified for the datatype '"+t+"'");}if(f._reference){f=f._reference;if(m.isObject(f))for(t=0;t<this._arrayOfAllItems.length;++t){p=this._arrayOfAllItems[t];var A=!0,x;for(x in f)p[x]!=f[x]&&(A=!1);A&&(a[k]=p)}else a[k]=this._getItemByIdentity(f);
this.referenceIntegrity&&(f=a[k],this.isItem(f)&&this._addReferenceToMap(f,e,l))}else this.isItem(f)&&this.referenceIntegrity&&this._addReferenceToMap(f,e,l)}},_addReferenceToMap:function(a,b,d){},getIdentity:function(a){var b=this._features["dojo.data.api.Identity"];return b===Number?a[this._itemNumPropName]:(a=a[b])?a[0]:null},fetchItemByIdentity:function(a){if(this._loadFinished){var b=this._getItemByIdentity(a.identity);if(a.onItem){var d=a.scope?a.scope:q.global;a.onItem.call(d,b)}}else{var c=
this;this._jsonFileUrl!==this._ccUrl?(q.deprecated(this.declaredClass+": ","To change the url, set the url property of the store, not _jsonFileUrl.  _jsonFileUrl support will be removed in 2.0"),this.url=this._ccUrl=this._jsonFileUrl):this.url!==this._ccUrl&&(this._ccUrl=this._jsonFileUrl=this.url);null!=this.data&&null==this._jsonData&&(this._jsonData=this.data,this.data=null);this._jsonFileUrl?this._loadInProgress?this._queuedFetches.push({args:a}):(this._loadInProgress=!0,d=v.get({url:c._jsonFileUrl,
handleAs:"json-comment-optional",preventCache:this.urlPreventCache,failOk:this.failOk}),d.addCallback(function(h){var g=a.scope?a.scope:q.global;try{c._getItemsFromLoadedData(h),c._loadFinished=!0,c._loadInProgress=!1,b=c._getItemByIdentity(a.identity),a.onItem&&a.onItem.call(g,b),c._handleQueuedFetches()}catch(e){c._loadInProgress=!1,a.onError&&a.onError.call(g,e)}}),d.addErrback(function(h){c._loadInProgress=!1;a.onError&&a.onError.call(a.scope?a.scope:q.global,h)})):this._jsonData&&(c._getItemsFromLoadedData(c._jsonData),
c._jsonData=null,c._loadFinished=!0,b=c._getItemByIdentity(a.identity),a.onItem&&(d=a.scope?a.scope:q.global,a.onItem.call(d,b)))}},_getItemByIdentity:function(a){var b=null;this._itemsByIdentity?Object.hasOwnProperty.call(this._itemsByIdentity,a)&&(b=this._itemsByIdentity[a]):Object.hasOwnProperty.call(this._arrayOfAllItems,a)&&(b=this._arrayOfAllItems[a]);void 0===b&&(b=null);return b},getIdentityAttributes:function(a){a=this._features["dojo.data.api.Identity"];return a===Number?null:[a]},_forceLoad:function(){var a=
this;this._jsonFileUrl!==this._ccUrl?(q.deprecated(this.declaredClass+": ","To change the url, set the url property of the store, not _jsonFileUrl.  _jsonFileUrl support will be removed in 2.0"),this.url=this._ccUrl=this._jsonFileUrl):this.url!==this._ccUrl&&(this._ccUrl=this._jsonFileUrl=this.url);null!=this.data&&(this._jsonData=this.data,this.data=null);if(this._jsonFileUrl){var b=v.get({url:this._jsonFileUrl,handleAs:"json-comment-optional",preventCache:this.urlPreventCache,failOk:this.failOk,
sync:!0});b.addCallback(function(d){try{if(!0!==a._loadInProgress&&!a._loadFinished)a._getItemsFromLoadedData(d),a._loadFinished=!0;else if(a._loadInProgress)throw Error(this.declaredClass+":  Unable to perform a synchronous load, an async load is in progress.");}catch(c){throw c;}});b.addErrback(function(d){throw d;})}else this._jsonData&&(a._getItemsFromLoadedData(a._jsonData),a._jsonData=null,a._loadFinished=!0)}});m.extend(u,D);return u});