//>>built
define("dojox/grid/enhanced/plugins/filter/FilterLayer",["dojo/_base/declare","dojo/_base/lang","dojo/_base/kernel","dojo/_base/json","../_StoreLayer"],function(g,d,m,k,i){var h=function(a,b){return b?d.hitch(a||m.global,b):function(){}},n=function(a){var b={};if(a&&d.isObject(a))for(var e in a)b[e]=a[e];return b},l=g("dojox.grid.enhanced.plugins.filter._FilterLayerMixin",null,{tags:["sizeChange"],name:function(){return"filter"},onFilterDefined:function(){},onFiltered:function(){}}),o=g("dojox.grid.enhanced.plugins.filter.ServerSideFilterLayer",
[i._ServerSideLayer,l],{constructor:function(a){this._onUserCommandLoad=a.setupFilterQuery||this._onUserCommandLoad;this.filterDef(null)},filterDef:function(a){if(a){this._filter=a;var b=a.toObject();this.command("filter",this._isStateful?k.toJson(b):b);this.command("clear",null);this.useCommands(!0);this.onFilterDefined(a)}else if(null===a)this._filter=null,this.command("filter",null),this.command("clear",!0),this.useCommands(!0),this.onFilterDefined(null);return this._filter},onCommandLoad:function(a,
b){this.inherited(arguments);var e=b.onBegin;if(this._isStateful){var c;if(a){this.command("filter",null);this.command("clear",null);this.useCommands(!1);var f=a.split(",");if(2<=f.length)c=this._filteredSize=parseInt(f[0],10),this.onFiltered(c,parseInt(f[1],10));else return}else c=this._filteredSize;if(this.enabled())b.onBegin=function(a,d){h(b.scope,e)(c,d)}}else{var d=this;b.onBegin=function(a,c){if(!d._filter)d._storeSize=a;d.onFiltered(a,d._storeSize||a);c.onBegin=e;h(b.scope,e)(a,c)}}}}),g=
g("dojox.grid.enhanced.plugins.filter.ClientSideFilterLayer",[i._StoreLayer,l],{_storeSize:-1,_fetchAll:!0,constructor:function(a){this.filterDef(null);a=d.isObject(a)?a:{};this.fetchAllOnFirstFilter(a.fetchAll);this._getter=d.isFunction(a.getter)?a.getter:this._defaultGetter},_defaultGetter:function(a,b,e,c){return c.getValue(a,b)},filterDef:function(a){if(void 0!==a)this._filter=a,this.invalidate(),this.onFilterDefined(a);return this._filter},setGetter:function(a){if(d.isFunction(a))this._getter=
a},fetchAllOnFirstFilter:function(a){if(void 0!==a)this._fetchAll=!!a;return this._fetchAll},invalidate:function(){this._items=[];this._nextUnfetchedIdx=0;this._result=[];this._indexMap=[];this._resultStartIdx=0},_fetch:function(a,b){if(!this._filter){var e=a.onBegin,c=this;a.onBegin=function(b,d){h(a.scope,e)(b,d);c.onFiltered(b,b)};this.originFetch(a);return a}try{var f=b?b._nextResultItemIdx:a.start,f=f||0;if(!b){this._result=[];this._resultStartIdx=f;var g;if(d.isArray(a.sort)&&0<a.sort.length&&
(g=k.toJson(a.sort))!=this._lastSortInfo)this.invalidate(),this._lastSortInfo=g}var j="number"==typeof a.count?f+a.count-this._result.length:this._items.length;this._result=this._result.length?this._result.concat(this._items.slice(f,j)):this._items.slice(a.start,"number"==typeof a.count?a.start+a.count:this._items.length);if(this._result.length>=a.count||this._hasReachedStoreEnd())this._completeQuery(a);else{if(!b)b=n(a),b.onBegin=d.hitch(this,this._onFetchBegin),b.onComplete=d.hitch(this,function(b,
c){this._nextUnfetchedIdx+=b.length;this._doFilter(b,c.start,a);this._fetch(a,c)});b.start=this._nextUnfetchedIdx;this._fetchAll&&delete b.count;b._nextResultItemIdx=j<this._items.length?j:this._items.length;this.originFetch(b)}}catch(i){if(a.onError)h(a.scope,a.onError)(i,a);else throw i;}return a},_hasReachedStoreEnd:function(){return 0<=this._storeSize&&this._nextUnfetchedIdx>=this._storeSize},_applyFilter:function(a,b){var e=this._getter,c=this._store;try{return!!this._filter.applyRow(a,function(a,
d){return e(a,d,b,c)}).getValue()}catch(d){return!1}},_doFilter:function(a,b,e){for(var c=0,d=0;c<a.length;++c)this._applyFilter(a[c],b+c)&&(h(e.scope,e.onItem)(a[c],e),d+=this._addCachedItems(a[c],this._items.length),this._indexMap.push(b+c))},_onFetchBegin:function(a){this._storeSize=a},_completeQuery:function(a){var b=this._items.length;this._nextUnfetchedIdx<this._storeSize&&b++;h(a.scope,a.onBegin)(b,a);this.onFiltered(this._items.length,this._storeSize);h(a.scope,a.onComplete)(this._result,
a)},_addCachedItems:function(a,b){d.isArray(a)||(a=[a]);for(var e=0;e<a.length;++e)this._items[b+e]=a[e];return a.length},onRowMappingChange:function(a){if(this._filter){var b=d.clone(a),e={},c;for(c in b)c=parseInt(c,10),a[this._indexMap[c]]=this._indexMap[b[c]],e[this._indexMap[c]]||(e[this._indexMap[c]]=!0),e[c]||(e[c]=!0,delete a[c])}}});return d.mixin({ServerSideFilterLayer:o,ClientSideFilterLayer:g},i)});