Selection.js 5.7 KB

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