Ext.define("Ext.selection.Model",{extend:"Ext.util.Observable",alternateClassName:"Ext.AbstractSelectionModel",requires:["Ext.data.StoreManager"],mixins:{bindable:"Ext.util.Bindable"},allowDeselect:undefined,toggleOnClick:true,selected:null,pruneRemoved:true,suspendChange:0,constructor:function(a){var b=this;a=a||{};Ext.apply(b,a);b.addEvents("selectionchange","focuschange");b.modes={SINGLE:true,SIMPLE:true,MULTI:true};b.setSelectionMode(a.mode||b.mode);b.selected=new Ext.util.MixedCollection(null,b.getSelectionId);b.callParent(arguments)},bindStore:function(a,b){var c=this;c.mixins.bindable.bindStore.apply(c,arguments);if(c.store&&!b){c.refresh()}},getStoreListeners:function(){var a=this;return{add:a.onStoreAdd,clear:a.onStoreClear,bulkremove:a.onStoreRemove,update:a.onStoreUpdate,load:a.onStoreLoad,idchanged:a.onModelIdChanged,refresh:a.onStoreRefresh}},suspendChanges:function(){++this.suspendChange},resumeChanges:function(){if(this.suspendChange){--this.suspendChange}},selectAll:function(b){var e=this,d=e.store.getRange(),c=0,a=d.length,f=e.getSelection().length;e.suspendChanges();for(;c<a;c++){e.doSelect(d[c],true,b)}e.resumeChanges();if(!b){e.maybeFireSelectionChange(e.getSelection().length!==f)}},deselectAll:function(j){var f=this,b=f.getSelection(),g={},h=f.store,a=b.length,e,c,d;for(e=0,c=b.length;e<c;e++){d=b[e];g[d.internalId]=h.indexOf(d)}b=Ext.Array.sort(b,function(l,i){var m=g[l.internalId],k=g[i.internalId];return m<k?-1:1});f.suspendChanges();f.doDeselect(b,j);f.resumeChanges();if(!j){f.maybeFireSelectionChange(f.getSelection().length!==a)}},selectWithEvent:function(j,l){var m=this,d=m.isSelected(j),f=l.shiftKey,a=l.ctrlKey,c=m.selectionStart,g=m.getSelection(),k=g.length,n=m.allowDeselect,b,h,o;switch(m.selectionMode){case"MULTI":if(f&&c){m.selectRange(c,j,a)}else{if(a&&d){m.doDeselect(j,false)}else{if(a){m.doSelect(j,true,false)}else{if(d&&!f&&!a&&k>1){b=[];for(h=0;h<k;++h){o=g[h];if(o!==j){b.push(o)}}m.doDeselect(b)}else{if(!d){m.doSelect(j,false)}}}}}break;case"SIMPLE":if(d){m.doDeselect(j)}else{m.doSelect(j,true)}break;case"SINGLE":if(n&&!a){n=m.toggleOnClick}if(n&&d){m.doDeselect(j)}else{m.doSelect(j,false)}break}if(!f){if(m.isSelected(j)){m.selectionStart=j}else{m.selectionStart=null}}},afterKeyNavigate:function(f,d){var g=this,c,a,b=g.isSelected(d),i=(g.selectionStart&&g.isSelected(g.lastFocused))?g.selectionStart:(g.selectionStart=g.lastFocused),j=f.getCharCode(),k=j===f.SPACE,h=j===f.UP||j===f.PAGE_UP?"up":(j===f.DOWN||j===f.DOWN?"down":null);switch(g.selectionMode){case"MULTI":if(k){if(f.shiftKey){g.selectRange(i,d,f.ctrlKey)}else{if(b){g.doDeselect(d,f.ctrlKey);g.setLastFocused(null);g.setLastFocused(d)}else{g.doSelect(d,f.ctrlKey)}}}else{if(f.shiftKey&&i){a=g.store.indexOf(i);c=g.store.indexOf(d);if(h==="up"&&a<=c){g.deselectRange(g.lastFocused,c+1)}else{if(h==="down"&&a>=c){g.deselectRange(g.lastFocused,c-1)}else{if(i!==d){g.selectRange(i,d,f.ctrlKey)}}}g.lastSelected=d;g.setLastFocused(d)}else{if(f.ctrlKey&&b){g.setLastFocused(d)}else{if(f.ctrlKey){g.setLastFocused(d)}else{g.doSelect(d,false)}}}}break;case"SIMPLE":if(b){g.doDeselect(d)}else{g.doSelect(d,true)}break;case"SINGLE":if(k){if(b){g.doDeselect(d);g.setLastFocused(d)}else{g.doSelect(d)}}else{if(f.ctrlKey){g.setLastFocused(d)}else{if(g.allowDeselect&&b){g.doDeselect(d)}else{g.doSelect(d,false)}}}break}if(!f.shiftKey){if(g.isSelected(d)){g.selectionStart=d}}},selectRange:function(l,d,m){var h=this,k=h.store,c=h.selected.items,n,f,g,e,a,j,b;if(h.isLocked()){return}n=h.normalizeRowRange(l,d);l=n[0];d=n[1];e=[];for(f=l;f<=d;f++){if(!h.isSelected(k.getAt(f))){e.push(k.getAt(f))}}if(!m){a=[];h.suspendChanges();for(f=0,g=c.length;f<g;++f){b=c[f];j=k.indexOf(b);if(j<l||j>d){a.push(b)}}for(f=0,g=a.length;f<g;++f){h.doDeselect(a[f])}h.resumeChanges()}h.doMultiSelect(e,true)},deselectRange:function(e,d){var h=this,c=h.store,a,g,f,b;if(h.isLocked()){return}a=h.normalizeRowRange(e,d);e=a[0];d=a[1];f=[];for(g=e;g<=d;g++){b=c.getAt(g);if(h.isSelected(b)){f.push(b)}}h.doDeselect(f)},normalizeRowRange:function(c,b){var a=this.store,d;if(!Ext.isNumber(c)){c=a.indexOf(c)}c=Math.max(0,c);if(!Ext.isNumber(b)){b=a.indexOf(b)}b=Math.min(b,a.getCount()-1);if(c>b){d=b;b=c;c=d}return[c,b]},onModelIdChanged:function(a,d,e,c,b){this.selected.updateKey(b,c)},select:function(b,c,a){if(Ext.isDefined(b)){this.doSelect(b,c,a)}},deselect:function(b,a){this.doDeselect(b,a)},doSelect:function(c,e,b){var d=this,a;if(d.locked||!d.store){return}if(typeof c==="number"){a=d.store.getAt(c);if(!a){return}c=[a]}if(d.selectionMode=="SINGLE"&&c){a=c.length?c[0]:c;d.doSingleSelect(a,b)}else{d.doMultiSelect(c,e,b)}},doMultiSelect:function(a,k,j){var g=this,b=g.selected,h=false,l,d,f,e,c;if(g.locked){return}a=!Ext.isArray(a)?[a]:a;f=a.length;if(!k&&b.getCount()>0){l=g.deselectDuringSelect(a,b.getRange(),j);if(l[0]){g.maybeFireSelectionChange(l[1]>0&&!j);return}}c=function(){b.add(e);h=true};for(d=0;d<f;d++){e=a[d];if(g.isSelected(e)){continue}g.lastSelected=e;g.onSelectChange(e,true,j,c)}if(!g.preventFocus){g.setLastFocused(e,j)}g.maybeFireSelectionChange(h&&!j)},deselectDuringSelect:function(d,a,h){var g=this,f=a.length,c=0,e=false,j,b;g.suspendChanges();for(b=0;b<f;++b){j=a[b];if(!Ext.Array.contains(d,j)){if(g.doDeselect(j,h)){++c}else{e=true}}}g.resumeChanges();return[e,c]},doDeselect:function(a,j){var h=this,b=h.selected,d=0,g,e,k=0,f=0,c;if(h.locked||!h.store){return false}if(typeof a==="number"){e=h.store.getAt(a);if(!e){return false}a=[e]}else{if(!Ext.isArray(a)){a=[a]}}c=function(){++f;b.remove(e)};g=a.length;h.suspendChanges();for(;d<g;d++){e=a[d];if(h.isSelected(e)){if(h.lastSelected===e){h.lastSelected=b.last();if(h.lastFocused===e){h.setLastFocused(null)}}++k;h.onSelectChange(e,false,j,c)}}h.resumeChanges();h.maybeFireSelectionChange(f>0&&!j);return f===k},doSingleSelect:function(a,b){var d=this,f=false,c=d.selected,e;if(d.locked){return}if(d.isSelected(a)){return}if(c.getCount()){d.suspendChanges();if(!d.doDeselect(d.lastSelected,b)){d.resumeChanges();return}d.resumeChanges()}e=function(){c.add(a);d.lastSelected=a;f=true};d.onSelectChange(a,true,b,e);if(f){if(!b&&!d.preventFocus){d.setLastFocused(a)}d.maybeFireSelectionChange(!b)}},setLastFocused:function(c,b){var d=this,a=d.lastFocused;if(c!==a){d.lastFocused=c;d.onLastFocusChanged(a,c,b)}},isFocused:function(a){return a===this.getLastFocused()},maybeFireSelectionChange:function(a){var b=this;if(a&&!b.suspendChange){b.fireEvent("selectionchange",b,b.getSelection())}},getLastSelected:function(){return this.lastSelected},getLastFocused:function(){return this.lastFocused},getSelection:function(){return this.selected.getRange()},getSelectionMode:function(){return this.selectionMode},setSelectionMode:function(a){a=a?a.toUpperCase():"SINGLE";this.selectionMode=this.modes[a]?a:"SINGLE"},isLocked:function(){return this.locked},setLocked:function(a){this.locked=!!a},isRangeSelected:function(d,c){var f=this,b=f.store,e,a;a=f.normalizeRowRange(d,c);d=a[0];c=a[1];for(e=d;e<=c;e++){if(!f.isSelected(b.getAt(e))){return false}}return true},isSelected:function(a){a=Ext.isNumber(a)?this.store.getAt(a):a;return this.selected.contains(a)},hasSelection:function(){return this.selected.getCount()>0},getSelectionId:function(a){return a.internalId},pruneIf:function(){var f=this,d=f.selected,c=[],a=d.length,b,e;if(f.pruneRemoved){for(b=0;b<a;b++){e=d.getAt(b);if(!this.storeHasSelected(e)){c.push(e)}}if(c.length){for(b=0,a=c.length;b<a;b++){d.remove(c[b])}f.maybeFireSelectionChange(true)}}},storeHasSelected:function(b){var d=this.store,c,a,f,e;if(b.hasId()&&d.getById(b)){return true}else{c=d.data.items;a=c.length;f=b.internalId;for(e=0;e<a;++e){if(f===c[e].internalId){return true}}}return false},refresh:function(){var g=this,k=g.store,a,d=[],f=[],c=g.getSelection(),e=c.length,j,h,b=0,l=g.getLastFocused();if(!k){return}for(;b<e;b++){j=c[b];if(k.indexOf(j)!==-1){d.push(j)}else{if(!g.pruneRemoved){a=k.getById(j.getId());if(a){d.push(a)}else{f.push(j)}}}if(g.mode==="SINGLE"&&f.length){break}}if(g.selected.getCount()!=(d.length+f.length)){h=true}g.clearSelections();if(k.indexOf(l)!==-1){g.setLastFocused(l,true)}if(d.length){g.doSelect(d,false,true)}if(f.length){g.selected.addAll(f);if(!g.lastSelected){g.lastSelected=f[f.length-1]}}g.maybeFireSelectionChange(h)},clearSelections:function(){this.selected.clear();this.lastSelected=null;this.setLastFocused(null)},onStoreAdd:Ext.emptyFn,onStoreClear:function(){if(this.selected.getCount()>0){this.clearSelections();this.maybeFireSelectionChange(true)}},onStoreRemove:function(c,b,d,a){var e=this;if(e.selectionStart&&Ext.Array.contains(b,e.selectionStart)){e.selectionStart=null}if(a||e.locked||!e.pruneRemoved){return}e.deselectDeletedRecords(b)},deselectDeletedRecords:function(b){var f=this,d=f.selected,c,e=b.length,g=0,a;for(c=0;c<e;c++){a=b[c];if(d.remove(a)){if(f.lastSelected==a){f.lastSelected=null}if(f.getLastFocused()==a){f.setLastFocused(null)}++g}}if(g){f.maybeFireSelectionChange(true)}},getCount:function(){return this.selected.getCount()},onUpdate:Ext.emptyFn,destroy:function(){this.clearListeners()},onStoreUpdate:Ext.emptyFn,onStoreRefresh:Ext.emptyFn,onStoreLoad:Ext.emptyFn,onSelectChange:function(a,d,c,f){var e=this,b=d?"select":"deselect";if((c||e.fireEvent("before"+b,e,a))!==false&&f()!==false){if(!c){e.fireEvent(b,e,a)}}},onLastFocusChanged:function(b,a){this.fireEvent("focuschange",this,b,a)},onEditorKey:Ext.emptyFn,beforeViewRender:function(a){this.views=this.views||[];this.views.push(a);this.bindStore(a.getStore(),true)},bindComponent:Ext.emptyFn});