123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- define("dojox/grid/Selection", [
- "dojo/_base/declare",
- "dojo/_base/array",
- "dojo/_base/lang",
- "dojo/dom-attr"
- ], function(declare, array, lang, domAttr){
- return declare("dojox.grid.Selection", null, {
- // summary:
- // Manages row selection for grid. Owned by grid and used internally
- // for selection. Override to implement custom selection.
- constructor: function(inGrid){
- this.grid = inGrid;
- this.selected = [];
- this.setMode(inGrid.selectionMode);
- },
- mode: 'extended',
- selected: null,
- updating: 0,
- selectedIndex: -1,
- rangeStartIndex: -1,
- setMode: function(mode){
- if(this.selected.length){
- this.deselectAll();
- }
- if(mode != 'extended' && mode != 'multiple' && mode != 'single' && mode != 'none'){
- this.mode = 'extended';
- }else{
- this.mode = mode;
- }
- },
- onCanSelect: function(inIndex){
- return this.grid.onCanSelect(inIndex);
- },
- onCanDeselect: function(inIndex){
- return this.grid.onCanDeselect(inIndex);
- },
- onSelected: function(inIndex){
- },
- onDeselected: function(inIndex){
- },
- //onSetSelected: function(inIndex, inSelect) { };
- onChanging: function(){
- },
- onChanged: function(){
- },
- isSelected: function(inIndex){
- if(this.mode == 'none'){
- return false;
- }
- return this.selected[inIndex];
- },
- getFirstSelected: function(){
- if(!this.selected.length||this.mode == 'none'){ return -1; }
- for(var i=0, l=this.selected.length; i<l; i++){
- if(this.selected[i]){
- return i;
- }
- }
- return -1;
- },
- getNextSelected: function(inPrev){
- if(this.mode == 'none'){ return -1; }
- for(var i=inPrev+1, l=this.selected.length; i<l; i++){
- if(this.selected[i]){
- return i;
- }
- }
- return -1;
- },
- getSelected: function(){
- var result = [];
- for(var i=0, l=this.selected.length; i<l; i++){
- if(this.selected[i]){
- result.push(i);
- }
- }
- return result;
- },
- getSelectedCount: function(){
- var c = 0;
- for(var i=0; i<this.selected.length; i++){
- if(this.selected[i]){
- c++;
- }
- }
- return c;
- },
- _beginUpdate: function(){
- if(this.updating === 0){
- this.onChanging();
- }
- this.updating++;
- },
- _endUpdate: function(){
- this.updating--;
- if(this.updating === 0){
- this.onChanged();
- }
- },
- select: function(inIndex){
- if(this.mode == 'none'){ return; }
- if(this.mode != 'multiple'){
- this.deselectAll(inIndex);
- this.addToSelection(inIndex);
- }else{
- this.toggleSelect(inIndex);
- }
- },
- addToSelection: function(inIndex){
- if(this.mode == 'none'){ return; }
- if(lang.isArray(inIndex)){
- array.forEach(inIndex, this.addToSelection, this);
- return;
- }
- inIndex = Number(inIndex);
- if(this.selected[inIndex]){
- this.selectedIndex = inIndex;
- }else{
- if(this.onCanSelect(inIndex) !== false){
- this.selectedIndex = inIndex;
- var rowNode = this.grid.getRowNode(inIndex);
- if(rowNode){
- domAttr.set(rowNode, "aria-selected", "true");
- }
- this._beginUpdate();
- this.selected[inIndex] = true;
- //this.grid.onSelected(inIndex);
- this.onSelected(inIndex);
- //this.onSetSelected(inIndex, true);
- this._endUpdate();
- }
- }
- },
- deselect: function(inIndex){
- if(this.mode == 'none'){ return; }
- if(lang.isArray(inIndex)){
- array.forEach(inIndex, this.deselect, this);
- return;
- }
- inIndex = Number(inIndex);
- if(this.selectedIndex == inIndex){
- this.selectedIndex = -1;
- }
- if(this.selected[inIndex]){
- if(this.onCanDeselect(inIndex) === false){
- return;
- }
- var rowNode = this.grid.getRowNode(inIndex);
- if(rowNode){
- domAttr.set(rowNode, "aria-selected", "false");
- }
- this._beginUpdate();
- delete this.selected[inIndex];
- //this.grid.onDeselected(inIndex);
- this.onDeselected(inIndex);
- //this.onSetSelected(inIndex, false);
- this._endUpdate();
- }
- },
- setSelected: function(inIndex, inSelect){
- this[(inSelect ? 'addToSelection' : 'deselect')](inIndex);
- },
- toggleSelect: function(inIndex){
- if(lang.isArray(inIndex)){
- array.forEach(inIndex, this.toggleSelect, this);
- return;
- }
- this.setSelected(inIndex, !this.selected[inIndex]);
- },
- _range: function(inFrom, inTo, func){
- var s = (inFrom >= 0 ? inFrom : inTo), e = inTo;
- if(s > e){
- e = s;
- s = inTo;
- }
- for(var i=s; i<=e; i++){
- func(i);
- }
- },
- selectRange: function(inFrom, inTo){
- this._range(inFrom, inTo, lang.hitch(this, "addToSelection"));
- },
- deselectRange: function(inFrom, inTo){
- this._range(inFrom, inTo, lang.hitch(this, "deselect"));
- },
- insert: function(inIndex){
- this.selected.splice(inIndex, 0, false);
- if(this.selectedIndex >= inIndex){
- this.selectedIndex++;
- }
- },
- remove: function(inIndex){
- this.selected.splice(inIndex, 1);
- if(this.selectedIndex >= inIndex){
- this.selectedIndex--;
- }
- },
- deselectAll: function(inExcept){
- for(var i in this.selected){
- if((i!=inExcept)&&(this.selected[i]===true)){
- this.deselect(i);
- }
- }
- },
- clickSelect: function(inIndex, inCtrlKey, inShiftKey){
- if(this.mode == 'none'){ return; }
- this._beginUpdate();
- if(this.mode != 'extended'){
- this.select(inIndex);
- }else{
- if(!inShiftKey || this.rangeStartIndex < 0){
- this.rangeStartIndex = inIndex;
- }
- if(!inCtrlKey){
- this.deselectAll(inIndex);
- }
- if(inShiftKey){
- this.selectRange(this.rangeStartIndex, inIndex);
- }else if(inCtrlKey){
- this.toggleSelect(inIndex);
- }else{
- this.addToSelection(inIndex);
- }
- }
- this._endUpdate();
- },
- clickSelectEvent: function(e){
- this.clickSelect(e.rowIndex, dojo.isCopyKey(e), e.shiftKey);
- },
- clear: function(){
- this._beginUpdate();
- this.deselectAll();
- this._endUpdate();
- }
- });
- });
|