//>>built
define("dgrid/Selection",["dojo/_base/kernel","dojo/_base/declare","dojo/_base/Deferred","dojo/on","dojo/has","dojo/aspect","./List","dojo/has!touch?./util/touch","put-selector/put","dojo/query"],function(_1,_2,_3,on,_4,_5,_6,_7,_8){var _9=_4("mac")?"metaKey":"ctrlKey";return _2("dgrid.Selection",null,{selectionDelegate:".dgrid-row",selectionEvents:"mousedown,mouseup,dgrid-cellfocusin",deselectOnRefresh:true,allowSelectAll:false,create:function(){this.selection={};return this.inherited(arguments);},postCreate:function(){this.inherited(arguments);this._initSelectionEvents();},selection:{},selectionMode:"extended",_setSelectionMode:function(_a){if(_a==this.selectionMode){return;}this.clearSelection();this.selectionMode=_a;},setSelectionMode:function(_b){_1.deprecated("setSelectionMode(...)","use set(\"selectionMode\", ...) instead","dgrid 1.0");this.set("selectionMode",_b);},_handleSelect:function(_c,_d){if(this.selectionMode=="none"||(_c.type=="dgrid-cellfocusin"&&_c.parentType=="mousedown")||(_c.type=="mouseup"&&_d!=this._waitForMouseUp)){return;}this._waitForMouseUp=null;this._selectionTriggerEvent=_c;var _e=!_c.keyCode?_c[_9]:_c.ctrlKey;if(!_c.keyCode||!_c.ctrlKey||_c.keyCode==32){var _f=this.selectionMode,row=_d,_10=this.row(row),_11=this._lastSelected;if(_f=="single"){if(_11===row){this.select(row,null,!_e||!this.isSelected(row));}else{this.clearSelection();this.select(row);this._lastSelected=row;}}else{if(this.selection[_10.id]&&!_c.shiftKey&&_c.type=="mousedown"){this._waitForMouseUp=row;}else{var _12;if((_c.button!=2&&_f=="extended"&&!_e)||(_c.button==2&&!(this.selection[_10.id]))){this.clearSelection(_10.id,true);}if(!_c.shiftKey){_11=_12=_e?null:undefined;}this.select(row,_11,_12);if(!_11){this._lastSelected=row;}}}if(!_c.keyCode&&(_c.shiftKey||_e)){_c.preventDefault();}}this._selectionTriggerEvent=null;},_initSelectionEvents:function(){var _13=this,_14=this.selectionDelegate;on(this.domNode,"selectstart",function(_15){var tag=_15.target&&_15.target.tagName;if(tag!="INPUT"&&tag!="TEXTAREA"){_15.preventDefault();}});function _16(_17){_13._handleSelect(_17,this);};if(_4("touch")){on(this.contentNode,_7.selector(_14,_7.tap),function(evt){_13._handleSelect(evt,this);});}else{on(this.contentNode,on.selector(_14,this.selectionEvents),_16);}if(this.allowSelectAll){this.on("keydown",function(_18){if(_18[_9]&&_18.keyCode==65){_18.preventDefault();_13[_13.allSelected?"clearSelection":"selectAll"]();}});}_5.before(this,"removeRow",function(_19,_1a){var row;if(!_1a){row=this.row(_19);if(row&&(row.id in this.selection)){this.deselect(_19);}}});},allowSelect:function(row){return true;},_selectionEventQueue:function(_1b,_1c){var _1d=this,_1e="dgrid-"+(_1b?"select":"deselect"),_1f=this[_1e],_20=this._selectionTriggerEvent;if(_20){_20=_20.type;}if(_1f){return _1f;}setTimeout(this._fireSelectionEvent=function(){if(!_1f){return;}var _21={bubbles:true,grid:_1d};if(_20){_21.parentType=_20;}_21[_1c]=_1f;on.emit(_1d.contentNode,_1e,_21);_1f=null;delete _1d[_1e];},0);return (_1f=this[_1e]=[]);},select:function(row,_22,_23){if(_23===undefined){_23=true;}if(!row.element){row=this.row(row);}if(this.allowSelect(row)){var _24=this.selection;var _25=_24[row.id];if(_23===null){_23=!_25;}var _26=row.element;if(!_23&&!this.allSelected){delete this.selection[row.id];}else{_24[row.id]=_23;}if(_26){if(_23){_8(_26,".dgrid-selected.ui-state-active");}else{_8(_26,"!dgrid-selected!ui-state-active");}}if(_23!=_25&&_26){this._selectionEventQueue(_23,"rows").push(row);}if(_22){if(!_22.element){_22=this.row(_22);}var _27=_22.element;var _28=row.element;var _29=(_27&&(_27.compareDocumentPosition?_27.compareDocumentPosition(_28)==2:_27.sourceIndex>_28.sourceIndex))?"down":"up";while(row.element!=_27&&(row=this[_29](row))){this.select(row);}}}},deselect:function(row,_2a){this.select(row,_2a,false);},clearSelection:function(_2b,_2c){this.allSelected=false;for(var id in this.selection){if(_2b!==id){this.deselect(id);}}if(!_2c){this._lastSelected=null;}},selectAll:function(){this.allSelected=true;this.selection={};for(var i in this._rowIdToObject){var row=this.row(this._rowIdToObject[i]);this.select(row.id);}},isSelected:function(_2d){if(!_2d){return false;}if(!_2d.element){_2d=this.row(_2d);}return !!this.selection[_2d.id];},refresh:function(){if(this.deselectOnRefresh){this.clearSelection();this._fireSelectionEvent&&this._fireSelectionEvent();}this._lastSelected=null;this.inherited(arguments);},renderArray:function(){var _2e=this,_2f=this.inherited(arguments);_3.when(_2f,function(_30){var _31=_2e.selection,i,row,_32;for(i=0;i<_30.length;i++){row=_2e.row(_30[i]);_32=row.id in _31?_31[row.id]:_2e.allSelected;if(_32){_2e.select(row,null,_32);}}});return _2f;}});});