ColumnGrid.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "dojox/grid/EnhancedGrid",
  17. "dojo/_base/array",
  18. "pd/model/pdSpec",
  19. "dojox/xml/parser",
  20. "dojo/query",
  21. "dojox/timing",
  22. "dojo/keys",
  23. "pd/widgets/grid/IndirectSelection",
  24. "pd/widgets/grid/cells"
  25. ],function(declare, EnhancedGrid, array, model, xmlParser, query, timing, keys){
  26. var AUTO_PREVIEW_DELAY = 100;
  27. var ColumnGrid = declare("pd/widgets/ColumnGrid", [EnhancedGrid], {
  28. rowsPerPage: 150,
  29. keepRows: 300,
  30. autoHeight: false,
  31. singleClickEdit: true,
  32. firstTimeRun: true,
  33. plugins: {
  34. indirectSelection: {
  35. headerSelector:true,
  36. width:dojo.isSafari?"26px":"16px",
  37. styles:"text-align: center;",
  38. name: " ",
  39. field: "hidden"
  40. }
  41. },
  42. constructor: function(args){
  43. this.pdSelection = [];
  44. this.structure = this.pdGetStructure();
  45. this.previewGrid = args.previewGrid;
  46. var self = this;
  47. this.timer = new timing.Timer(AUTO_PREVIEW_DELAY);
  48. this.timer.onTick = function(){
  49. if (self.firstTimeRun) {
  50. //initiate the selected column in preview table is the first selected one.
  51. self.previewGrid.pdToggleColumns(self.pdSelection.reverse());
  52. self.firstTimeRun = false;
  53. } else {
  54. self.previewGrid.pdToggleColumns(self.pdSelection);
  55. }
  56. self._pdTogglePublishBtn();
  57. this.stop();
  58. };
  59. this.timer.onStop = function() {
  60. self.pdSelection = [];
  61. };
  62. },
  63. pdToggleSelected: function(idx) {
  64. this.focus.setFocusIndex(idx, 0);
  65. this.selection.toggleSelect(idx);
  66. this.pdToggleColumn({
  67. index: idx,
  68. hidden: !this.selection.selected[idx]
  69. });
  70. },
  71. pdGetStructure: function() {
  72. var structure = [];
  73. array.forEach(model, function(item, idx){
  74. var structureItem = {};
  75. if (!item.hidden){
  76. structureItem.name = item.label;
  77. structureItem.noresize = item.noresize;
  78. structureItem.field = item.itemName;
  79. structureItem.width = item.width;
  80. structureItem.formatter = item.formatter;
  81. if (item.editable){
  82. structureItem.editable = true;
  83. structureItem.type = item.type;
  84. structureItem.options = item.options;
  85. structureItem.values = item.values;
  86. }
  87. structure.push(structureItem);
  88. }
  89. });
  90. return {cells: [structure]};
  91. },
  92. postCreate: function(){
  93. this.inherited(arguments);
  94. this.connect(this.selection, 'onSelected', this._onSelectedHandler);
  95. this.connect(this.selection, 'onDeselected', this._onDeselectedHandler);
  96. this.connect(this, 'onRowClick', this._onRowClickHandler);
  97. this.connect(this, 'dokeyup', this._onKeyupHandler);
  98. },
  99. _onSelectedHandler: function(rowIndex) {
  100. this.getItem(rowIndex).hidden = "false";
  101. this.pdSelection.push({index: rowIndex, hidden: false});
  102. },
  103. _onDeselectedHandler: function(rowIndex) {
  104. this.getItem(rowIndex).hidden = "true";
  105. this.pdSelection.push({index: rowIndex, hidden: true});
  106. },
  107. onSelectionChanged: function() {
  108. this.inherited(arguments);
  109. if(this.timer.isRunning){
  110. this.timer.setInterval(AUTO_PREVIEW_DELAY);
  111. } else {
  112. this.timer.start();
  113. }
  114. },
  115. _pdTogglePublishBtn: function() {
  116. //disable publish button in case of non column selected.
  117. if (this.selection.getSelected().length == 0){
  118. pd_publishBtn.set("disabled", true);
  119. } else {
  120. pd_publishBtn.set("disabled", false);
  121. }
  122. },
  123. _doPreveiwScroll: function(rowIndex) {
  124. //select and scroll the preview when column clicked
  125. if (this.selection.selected[rowIndex]){
  126. this.previewGrid.pdSelectColumn(rowIndex);
  127. }
  128. },
  129. _onRowClickHandler: function(e){
  130. if (e.cell.field != "hidden") {
  131. this._doPreveiwScroll(e.rowIndex);
  132. }
  133. },
  134. _onKeyupHandler: function(e) {
  135. if (e.cell.field != "hidden" && e.rowIndex >= 0 && e.keyCode == keys.SPACE) {
  136. this._doPreveiwScroll(e.rowIndex);
  137. }
  138. },
  139. pdToggleColumn: function(sel){
  140. this.previewGrid.pdToggleColumn(sel, true);
  141. this.timer.stop();
  142. this._pdTogglePublishBtn();
  143. },
  144. canSort: function(colIndex, field){
  145. // summary:
  146. // Overwritten
  147. return false;
  148. },
  149. canEdit: function(inCell, inRowIndex) {
  150. var item = this.getItem(inRowIndex);
  151. if (item[inCell.field + "Editable"] != undefined) {
  152. return item[inCell.field + "Editable"];
  153. }
  154. return inCell.editable;
  155. },
  156. onApplyCellEdit: function(inValue, inRowIndex, inFieldIndex) {
  157. var item = this.getItem(inRowIndex);
  158. array.forEach(model, function (modelItem){
  159. if (modelItem.listener && (modelItem.listener.itemName == inFieldIndex)){
  160. var map = modelItem.listener.map;
  161. array.forEach(map, function (mapItem) {
  162. if (mapItem[inValue]){
  163. item[modelItem.itemName] = mapItem[inValue];
  164. } else {
  165. item[modelItem.itemName] = modelItem.listener.defaultValue;
  166. }
  167. });
  168. }
  169. });
  170. },
  171. pdGetXml: function() {
  172. return this.store.objectStore.generateXmlFromData(this.selection.selected);
  173. },
  174. hasFactSelected: function() {
  175. var returnVal = false;
  176. array.forEach(this.selection.getSelected(), function(item){
  177. if (item.usage == "fact") {
  178. returnVal = true;
  179. return;
  180. }
  181. });
  182. return returnVal;
  183. }
  184. });
  185. ColumnGrid.markupFactory = EnhancedGrid.markupFactory;
  186. ColumnGrid.registerPlugin = EnhancedGrid.registerPlugin;
  187. return ColumnGrid;
  188. });