_Selector.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. define("dojox/grid/_Selector", [
  2. "../main",
  3. "dojo/_base/declare",
  4. "dojo/_base/lang",
  5. "dojo/query",
  6. "dojo/dom-class",
  7. "./Selection",
  8. "./_View",
  9. "./_Builder",
  10. "./util"
  11. ], function(dojox, declare, lang, query, domClass, Selection, _View, _Builder, util){
  12. var _InputSelectorHeaderBuilder = dojox.grid._InputSelectorHeaderBuilder = lang.extend(function(view){
  13. _Builder._HeaderBuilder.call(this, view);
  14. },_Builder._HeaderBuilder.prototype,{
  15. generateHtml: function(){
  16. var w = this.view.contentWidth || 0;
  17. var selectedCount = this.view.grid.selection.getSelectedCount();
  18. var checked = (selectedCount && selectedCount == this.view.grid.rowCount) ? ' dijitCheckBoxChecked dijitChecked' : '';
  19. return '<table style="width:' + w + 'px;" ' +
  20. 'border="0" cellspacing="0" cellpadding="0" ' +
  21. 'role="presentation"><tr><th style="text-align: center;">' +
  22. '<div class="dojoxGridCheckSelector dijitReset dijitInline dijitCheckBox' + checked + '"></div></th></tr></table>';
  23. },
  24. doclick: function(e){
  25. var selectedCount = this.view.grid.selection.getSelectedCount();
  26. this.view._selectionChanging = true;
  27. if(selectedCount==this.view.grid.rowCount){
  28. this.view.grid.selection.deselectAll();
  29. }else{
  30. this.view.grid.selection.selectRange(0, this.view.grid.rowCount-1);
  31. }
  32. this.view._selectionChanging = false;
  33. this.view.onSelectionChanged();
  34. return true;
  35. }
  36. });
  37. var _SelectorContentBuilder = dojox.grid._SelectorContentBuilder = lang.extend(function(view){
  38. _Builder._ContentBuilder.call(this, view);
  39. },_Builder._ContentBuilder.prototype,{
  40. generateHtml: function(inDataIndex, inRowIndex){
  41. var w = this.view.contentWidth || 0;
  42. return '<table class="dojoxGridRowbarTable" style="width:' + w + 'px;" border="0" ' +
  43. 'cellspacing="0" cellpadding="0" role="presentation"><tr>' +
  44. '<td style="text-align: center;" class="dojoxGridRowbarInner">' + this.getCellContent(inRowIndex) + '</td></tr></table>';
  45. },
  46. getCellContent: function(inRowIndex){
  47. return '&nbsp;';
  48. },
  49. findTarget: function(){
  50. var t = _Builder._ContentBuilder.prototype.findTarget.apply(this, arguments);
  51. return t;
  52. },
  53. domouseover: function(e){
  54. this.view.grid.onMouseOverRow(e);
  55. },
  56. domouseout: function(e){
  57. if(!this.isIntraRowEvent(e)){
  58. this.view.grid.onMouseOutRow(e);
  59. }
  60. },
  61. doclick: function(e){
  62. var idx = e.rowIndex;
  63. var selected = this.view.grid.selection.isSelected(idx);
  64. var mode = this.view.grid.selection.mode;
  65. if(!selected){
  66. if(mode == 'single'){
  67. this.view.grid.selection.select(idx);
  68. }else if(mode != 'none'){
  69. this.view.grid.selection.addToSelection(idx);
  70. }
  71. }else{
  72. this.view.grid.selection.deselect(idx);
  73. }
  74. return true;
  75. }
  76. });
  77. var _InputSelectorContentBuilder = dojox.grid._InputSelectorContentBuilder = lang.extend(function(view){
  78. _SelectorContentBuilder.call(this, view);
  79. },_SelectorContentBuilder.prototype,{
  80. getCellContent: function(rowIndex){
  81. var v = this.view;
  82. var type = v.inputType == "checkbox" ? "CheckBox" : "Radio";
  83. var checked = !!v.grid.selection.isSelected(rowIndex) ? ' dijit' + type + 'Checked dijitChecked' : '';
  84. return '<div class="dojoxGridCheckSelector dijitReset dijitInline dijit' + type + checked + '"></div>';
  85. }
  86. });
  87. var _Selector = declare("dojox.grid._Selector", _View, {
  88. inputType: '',
  89. selectionMode: '',
  90. // summary:
  91. // Custom grid view. If used in a grid structure, provides a small selectable region for grid rows.
  92. defaultWidth: "2em",
  93. noscroll: true,
  94. padBorderWidth: 2,
  95. _contentBuilderClass: _SelectorContentBuilder,
  96. postCreate: function(){
  97. this.inherited(arguments);
  98. if(this.selectionMode){
  99. this.grid.selection.mode = this.selectionMode;
  100. }
  101. this.connect(this.grid.selection, 'onSelected', 'onSelected');
  102. this.connect(this.grid.selection, 'onDeselected', 'onDeselected');
  103. },
  104. buildRendering: function(){
  105. this.inherited(arguments);
  106. this.scrollboxNode.style.overflow = "hidden";
  107. },
  108. getWidth: function(){
  109. return this.viewWidth || this.defaultWidth;
  110. },
  111. resize: function(){
  112. this.adaptHeight();
  113. },
  114. setStructure: function(s){
  115. this.inherited(arguments);
  116. if(s.defaultWidth){
  117. this.defaultWidth = s.defaultWidth;
  118. }
  119. },
  120. adaptWidth: function(){
  121. // Only calculate this here - rather than every call to buildRowContent
  122. if(!("contentWidth" in this) && this.contentNode){
  123. this.contentWidth = this.contentNode.offsetWidth - this.padBorderWidth;
  124. }
  125. },
  126. // styling
  127. doStyleRowNode: function(inRowIndex, inRowNode){
  128. var n = [ "dojoxGridRowbar dojoxGridNonNormalizedCell" ];
  129. if(this.grid.rows.isOver(inRowIndex)){
  130. n.push("dojoxGridRowbarOver");
  131. }
  132. if(this.grid.selection.isSelected(inRowIndex)){
  133. n.push("dojoxGridRowbarSelected");
  134. }
  135. inRowNode.className = n.join(" ");
  136. },
  137. // event handlers
  138. onSelected: function(inIndex){
  139. this.grid.updateRow(inIndex);
  140. },
  141. onDeselected: function(inIndex){
  142. this.grid.updateRow(inIndex);
  143. }
  144. });
  145. if(!_View.prototype._headerBuilderClass &&
  146. !_View.prototype._contentBuilderClass){
  147. _Selector.prototype.postCreate = function(){
  148. this.connect(this.scrollboxNode,"onscroll","doscroll");
  149. util.funnelEvents(this.contentNode, this, "doContentEvent", [ 'mouseover', 'mouseout', 'click', 'dblclick', 'contextmenu', 'mousedown' ]);
  150. util.funnelEvents(this.headerNode, this, "doHeaderEvent", [ 'dblclick', 'mouseover', 'mouseout', 'mousemove', 'mousedown', 'click', 'contextmenu' ]);
  151. if(this._contentBuilderClass){
  152. this.content = new this._contentBuilderClass(this);
  153. }else{
  154. this.content = new _Builder._ContentBuilder(this);
  155. }
  156. if(this._headerBuilderClass){
  157. this.header = new this._headerBuilderClass(this);
  158. }else{
  159. this.header = new _Builder._HeaderBuilder(this);
  160. }
  161. //BiDi: in RTL case, style width='9000em' causes scrolling problem in head node
  162. if(!this.grid.isLeftToRight()){
  163. this.headerNodeContainer.style.width = "";
  164. }
  165. this.connect(this.grid.selection, 'onSelected', 'onSelected');
  166. this.connect(this.grid.selection, 'onDeselected', 'onDeselected');
  167. };
  168. }
  169. declare("dojox.grid._RadioSelector", _Selector, {
  170. inputType: 'radio',
  171. selectionMode: 'single',
  172. _contentBuilderClass: _InputSelectorContentBuilder,
  173. buildRendering: function(){
  174. this.inherited(arguments);
  175. this.headerNode.style.visibility = "hidden";
  176. },
  177. renderHeader: function(){}
  178. });
  179. declare("dojox.grid._CheckBoxSelector", _Selector, {
  180. inputType: 'checkbox',
  181. _headerBuilderClass: _InputSelectorHeaderBuilder,
  182. _contentBuilderClass: _InputSelectorContentBuilder,
  183. postCreate: function(){
  184. this.inherited(arguments);
  185. this.connect(this.grid, 'onSelectionChanged', 'onSelectionChanged');
  186. this.connect(this.grid, 'updateRowCount', '_updateVisibility');
  187. },
  188. renderHeader: function(){
  189. this.inherited(arguments);
  190. this._updateVisibility(this.grid.rowCount);
  191. },
  192. _updateVisibility: function(rowCount){
  193. this.headerNode.style.visibility = rowCount ? "" : "hidden";
  194. },
  195. onSelectionChanged: function(){
  196. if(this._selectionChanging){ return; }
  197. var inputDiv = query('.dojoxGridCheckSelector', this.headerNode)[0];
  198. var g = this.grid;
  199. var s = (g.rowCount && g.rowCount == g.selection.getSelectedCount());
  200. g.allItemsSelected = s||false;
  201. domClass.toggle(inputDiv, "dijitChecked", g.allItemsSelected);
  202. domClass.toggle(inputDiv, "dijitCheckBoxChecked", g.allItemsSelected);
  203. }
  204. });
  205. return _Selector;
  206. });