_Selector.js 7.6 KB

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