Selection.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. define("dojox/grid/Selection", [
  2. "dojo/_base/declare",
  3. "dojo/_base/array",
  4. "dojo/_base/lang",
  5. "dojo/dom-attr"
  6. ], function(declare, array, lang, domAttr){
  7. return declare("dojox.grid.Selection", null, {
  8. // summary:
  9. // Manages row selection for grid. Owned by grid and used internally
  10. // for selection. Override to implement custom selection.
  11. constructor: function(inGrid){
  12. this.grid = inGrid;
  13. this.selected = [];
  14. this.setMode(inGrid.selectionMode);
  15. },
  16. mode: 'extended',
  17. selected: null,
  18. updating: 0,
  19. selectedIndex: -1,
  20. rangeStartIndex: -1,
  21. setMode: function(mode){
  22. if(this.selected.length){
  23. this.deselectAll();
  24. }
  25. if(mode != 'extended' && mode != 'multiple' && mode != 'single' && mode != 'none'){
  26. this.mode = 'extended';
  27. }else{
  28. this.mode = mode;
  29. }
  30. },
  31. onCanSelect: function(inIndex){
  32. return this.grid.onCanSelect(inIndex);
  33. },
  34. onCanDeselect: function(inIndex){
  35. return this.grid.onCanDeselect(inIndex);
  36. },
  37. onSelected: function(inIndex){
  38. },
  39. onDeselected: function(inIndex){
  40. },
  41. //onSetSelected: function(inIndex, inSelect) { };
  42. onChanging: function(){
  43. },
  44. onChanged: function(){
  45. },
  46. isSelected: function(inIndex){
  47. if(this.mode == 'none'){
  48. return false;
  49. }
  50. return this.selected[inIndex];
  51. },
  52. getFirstSelected: function(){
  53. if(!this.selected.length||this.mode == 'none'){ return -1; }
  54. for(var i=0, l=this.selected.length; i<l; i++){
  55. if(this.selected[i]){
  56. return i;
  57. }
  58. }
  59. return -1;
  60. },
  61. getNextSelected: function(inPrev){
  62. if(this.mode == 'none'){ return -1; }
  63. for(var i=inPrev+1, l=this.selected.length; i<l; i++){
  64. if(this.selected[i]){
  65. return i;
  66. }
  67. }
  68. return -1;
  69. },
  70. getSelected: function(){
  71. var result = [];
  72. for(var i=0, l=this.selected.length; i<l; i++){
  73. if(this.selected[i]){
  74. result.push(i);
  75. }
  76. }
  77. return result;
  78. },
  79. getSelectedCount: function(){
  80. var c = 0;
  81. for(var i=0; i<this.selected.length; i++){
  82. if(this.selected[i]){
  83. c++;
  84. }
  85. }
  86. return c;
  87. },
  88. _beginUpdate: function(){
  89. if(this.updating === 0){
  90. this.onChanging();
  91. }
  92. this.updating++;
  93. },
  94. _endUpdate: function(){
  95. this.updating--;
  96. if(this.updating === 0){
  97. this.onChanged();
  98. }
  99. },
  100. select: function(inIndex){
  101. if(this.mode == 'none'){ return; }
  102. if(this.mode != 'multiple'){
  103. this.deselectAll(inIndex);
  104. this.addToSelection(inIndex);
  105. }else{
  106. this.toggleSelect(inIndex);
  107. }
  108. },
  109. addToSelection: function(inIndex){
  110. if(this.mode == 'none'){ return; }
  111. if(lang.isArray(inIndex)){
  112. array.forEach(inIndex, this.addToSelection, this);
  113. return;
  114. }
  115. inIndex = Number(inIndex);
  116. if(this.selected[inIndex]){
  117. this.selectedIndex = inIndex;
  118. }else{
  119. if(this.onCanSelect(inIndex) !== false){
  120. this.selectedIndex = inIndex;
  121. var rowNode = this.grid.getRowNode(inIndex);
  122. if(rowNode){
  123. domAttr.set(rowNode, "aria-selected", "true");
  124. }
  125. this._beginUpdate();
  126. this.selected[inIndex] = true;
  127. //this.grid.onSelected(inIndex);
  128. this.onSelected(inIndex);
  129. //this.onSetSelected(inIndex, true);
  130. this._endUpdate();
  131. }
  132. }
  133. },
  134. deselect: function(inIndex){
  135. if(this.mode == 'none'){ return; }
  136. if(lang.isArray(inIndex)){
  137. array.forEach(inIndex, this.deselect, this);
  138. return;
  139. }
  140. inIndex = Number(inIndex);
  141. if(this.selectedIndex == inIndex){
  142. this.selectedIndex = -1;
  143. }
  144. if(this.selected[inIndex]){
  145. if(this.onCanDeselect(inIndex) === false){
  146. return;
  147. }
  148. var rowNode = this.grid.getRowNode(inIndex);
  149. if(rowNode){
  150. domAttr.set(rowNode, "aria-selected", "false");
  151. }
  152. this._beginUpdate();
  153. delete this.selected[inIndex];
  154. //this.grid.onDeselected(inIndex);
  155. this.onDeselected(inIndex);
  156. //this.onSetSelected(inIndex, false);
  157. this._endUpdate();
  158. }
  159. },
  160. setSelected: function(inIndex, inSelect){
  161. this[(inSelect ? 'addToSelection' : 'deselect')](inIndex);
  162. },
  163. toggleSelect: function(inIndex){
  164. if(lang.isArray(inIndex)){
  165. array.forEach(inIndex, this.toggleSelect, this);
  166. return;
  167. }
  168. this.setSelected(inIndex, !this.selected[inIndex]);
  169. },
  170. _range: function(inFrom, inTo, func){
  171. var s = (inFrom >= 0 ? inFrom : inTo), e = inTo;
  172. if(s > e){
  173. e = s;
  174. s = inTo;
  175. }
  176. for(var i=s; i<=e; i++){
  177. func(i);
  178. }
  179. },
  180. selectRange: function(inFrom, inTo){
  181. this._range(inFrom, inTo, lang.hitch(this, "addToSelection"));
  182. },
  183. deselectRange: function(inFrom, inTo){
  184. this._range(inFrom, inTo, lang.hitch(this, "deselect"));
  185. },
  186. insert: function(inIndex){
  187. this.selected.splice(inIndex, 0, false);
  188. if(this.selectedIndex >= inIndex){
  189. this.selectedIndex++;
  190. }
  191. },
  192. remove: function(inIndex){
  193. this.selected.splice(inIndex, 1);
  194. if(this.selectedIndex >= inIndex){
  195. this.selectedIndex--;
  196. }
  197. },
  198. deselectAll: function(inExcept){
  199. for(var i in this.selected){
  200. if((i!=inExcept)&&(this.selected[i]===true)){
  201. this.deselect(i);
  202. }
  203. }
  204. },
  205. clickSelect: function(inIndex, inCtrlKey, inShiftKey){
  206. if(this.mode == 'none'){ return; }
  207. this._beginUpdate();
  208. if(this.mode != 'extended'){
  209. this.select(inIndex);
  210. }else{
  211. if(!inShiftKey || this.rangeStartIndex < 0){
  212. this.rangeStartIndex = inIndex;
  213. }
  214. if(!inCtrlKey){
  215. this.deselectAll(inIndex);
  216. }
  217. if(inShiftKey){
  218. this.selectRange(this.rangeStartIndex, inIndex);
  219. }else if(inCtrlKey){
  220. this.toggleSelect(inIndex);
  221. }else{
  222. this.addToSelection(inIndex);
  223. }
  224. }
  225. this._endUpdate();
  226. },
  227. clickSelectEvent: function(e){
  228. this.clickSelect(e.rowIndex, dojo.isCopyKey(e), e.shiftKey);
  229. },
  230. clear: function(){
  231. this._beginUpdate();
  232. this.deselectAll();
  233. this._endUpdate();
  234. }
  235. });
  236. });