IndirectSelection.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*******************************************************************************
  2. * IBM Confidential
  3. *
  4. * OCO Source Materials
  5. *
  6. * A and PM: PD
  7. *
  8. * (c) Copyright IBM Corp. 2014
  9. *
  10. * The source code for this program is not published or otherwise divested of
  11. * its trade secrets, irrespective of what has been deposited with the U.S.
  12. * Copyright Office.
  13. ******************************************************************************/
  14. define([
  15. "dojo/_base/declare",
  16. "dojo/dom",
  17. "dojo/dom-attr",
  18. "dojo/_base/array",
  19. "dojo/_base/lang",
  20. "dojo/string",
  21. "dojox/grid/EnhancedGrid",
  22. "dojox/grid/enhanced/plugins/IndirectSelection"
  23. ], function(declare, dom, domAttr, array, lang, string, EnhancedGrid){
  24. var RowSelector = declare("pd/widgets/grid/cells/RowSelector", [dojox.grid.cells.RowSelector], {
  25. formatter: function(data, rowIndex, scope){
  26. //overwrite dojo class
  27. scope.toggleRow(rowIndex, !(data == "true")); //bundle selector with hidden attribute.
  28. var self = scope;
  29. var baseClass = self.baseClass;
  30. var checked = self.getValue(rowIndex);
  31. var disabled = !!self.disabledMap[rowIndex];
  32. if(checked){
  33. baseClass += self.checkedClass;
  34. if(disabled){ baseClass += self.checkedDisabledClass; }
  35. }else if(disabled){
  36. baseClass += self.disabledClass;
  37. }
  38. return ["<div tabindex = -1 ", "id = '" + self.grid.id + "_rowSelector_" + rowIndex + "' ",
  39. "name = '" + self.grid.id + "_rowSelector' class = '" + baseClass + "' ",
  40. "role = 'button' aria-pressed = '" + checked + "' aria-disabled = '" + disabled +
  41. "' aria-label = '" + string.substitute(self.grid._nls["indirectSelection" + self.inputType], [rowIndex + 1]) + "'>",
  42. "<span class = '" + self.statusTextClass + "'>" + (checked ? self.checkedText : self.unCheckedText) + "</span>",
  43. "</div>"].join("");
  44. },
  45. _toggleCheckedStyle: function(index, value){
  46. this.inherited(arguments);
  47. var selector = this._getSelector(index);
  48. if(selector){
  49. if (index == -1){
  50. selector.setAttribute("title", this._getHeaderSelectorTooltip());
  51. }
  52. }
  53. }
  54. });
  55. var SingleRowSelector = declare("pd/widgets/grid/cells/pd/widgets/grid/cells/RowSelector",
  56. [dojox.grid.cells.SingleRowSelector, RowSelector], {});
  57. var MultipleRowSelector = declare("pd/widgets/grid/cells/MultipleRowSelector",
  58. [dojox.grid.cells.MultipleRowSelector, RowSelector], {
  59. _selectRow: function(e){
  60. this.inherited(arguments);
  61. //scroll to the selected column in preview
  62. if (this.grid.selection.selected[e.rowIndex]){
  63. this.grid.pdToggleColumn({
  64. index: e.rowIndex,
  65. hidden: false
  66. });
  67. }
  68. },
  69. _getHeaderSelectorTooltip: function () {
  70. // Summary:
  71. // Add a tooltip to header selector according to check state.
  72. return this.getValue(-1) ? PDMSG.IPT.IDS_IPT_TOOLTIP_EXCLUDE_ALL : PDMSG.IPT.IDS_IPT_TOOLTIP_INCLUDE_ALL;
  73. },
  74. _addHeaderSelector: function(){
  75. this.inherited(arguments);
  76. var headerCellNode = this.view.getHeaderCellNode(this.index);
  77. var divNode = dom.byId(this.grid.id + "_rowSelector_-1");
  78. domAttr.set(divNode, "title", this._getHeaderSelectorTooltip());
  79. domAttr.set(divNode, "role", "button");
  80. }
  81. });
  82. var IndirectSelection = declare("pd/widgets/grid/IndirectSelection", [dojox.grid.enhanced.plugins.IndirectSelection], {
  83. addRowSelectCell: function(option){
  84. if(!this.grid.indirectSelection || this.grid.selectionMode == 'none'){
  85. return;
  86. }
  87. var rowSelectCellAdded = false;
  88. var inValidFields = ['get', 'formatter', 'fields'];
  89. var defaultCellDef = {
  90. type: MultipleRowSelector,
  91. name: '',
  92. width:'30px',
  93. styles:'text-align: center;',
  94. noresize: true
  95. };
  96. if(option.headerSelector){ option.name = ''; }
  97. if(this.grid.rowSelectCell){
  98. this.grid.rowSelectCell.destroy();
  99. }
  100. array.forEach(this.structure, function(view){
  101. var cells = view.cells;
  102. if(cells && cells.length > 0 && !rowSelectCellAdded){
  103. var firstRow = cells[0];
  104. if(firstRow[0] && firstRow[0].isRowSelector){
  105. rowSelectCellAdded = true;
  106. return;
  107. }
  108. var selectDef;
  109. var cellType = this.grid.selectionMode == 'single' ? SingleRowSelector : MultipleRowSelector;
  110. selectDef = lang.mixin(defaultCellDef, option, {
  111. type: cellType,
  112. editable: false,
  113. notselectable: true,
  114. filterable: false,
  115. navigatable: true,
  116. nosort: true
  117. });
  118. array.forEach(inValidFields, function(field){
  119. if(field in selectDef){ delete selectDef[field]; }
  120. });
  121. if(cells.length > 1){ selectDef.rowSpan = cells.length; }
  122. array.forEach(this.cells, function(cell, i){
  123. if(cell.index >= 0){
  124. cell.index += 1;
  125. }
  126. });
  127. var rowSelectCell = this.addCellDef(0, 0, selectDef);
  128. rowSelectCell.index = 0;
  129. firstRow.unshift(rowSelectCell);
  130. this.cells.unshift(rowSelectCell);
  131. this.grid.rowSelectCell = rowSelectCell;
  132. rowSelectCellAdded = true;
  133. }
  134. }, this);
  135. this.cellCount = this.cells.length;
  136. }
  137. });
  138. EnhancedGrid.registerPlugin(IndirectSelection/*name:'indirectSelection'*/, {"preInit": true});
  139. return IndirectSelection;
  140. });