/******************************************************************************* * IBM Confidential * * OCO Source Materials * * A and PM: PD * * (c) Copyright IBM Corp. 2014 * * The source code for this program is not published or otherwise divested of * its trade secrets, irrespective of what has been deposited with the U.S. * Copyright Office. ******************************************************************************/ define([ "dojo/_base/declare", "dojox/grid/EnhancedGrid", "dojo/_base/array", "pd/model/pdSpec", "dojox/xml/parser", "dojo/query", "dojox/timing", "dojo/keys", "pd/widgets/grid/IndirectSelection", "pd/widgets/grid/cells" ],function(declare, EnhancedGrid, array, model, xmlParser, query, timing, keys){ var AUTO_PREVIEW_DELAY = 100; var ColumnGrid = declare("pd/widgets/ColumnGrid", [EnhancedGrid], { rowsPerPage: 150, keepRows: 300, autoHeight: false, singleClickEdit: true, firstTimeRun: true, plugins: { indirectSelection: { headerSelector:true, width:dojo.isSafari?"26px":"16px", styles:"text-align: center;", name: " ", field: "hidden" } }, constructor: function(args){ this.pdSelection = []; this.structure = this.pdGetStructure(); this.previewGrid = args.previewGrid; var self = this; this.timer = new timing.Timer(AUTO_PREVIEW_DELAY); this.timer.onTick = function(){ if (self.firstTimeRun) { //initiate the selected column in preview table is the first selected one. self.previewGrid.pdToggleColumns(self.pdSelection.reverse()); self.firstTimeRun = false; } else { self.previewGrid.pdToggleColumns(self.pdSelection); } self._pdTogglePublishBtn(); this.stop(); }; this.timer.onStop = function() { self.pdSelection = []; }; }, pdToggleSelected: function(idx) { this.focus.setFocusIndex(idx, 0); this.selection.toggleSelect(idx); this.pdToggleColumn({ index: idx, hidden: !this.selection.selected[idx] }); }, pdGetStructure: function() { var structure = []; array.forEach(model, function(item, idx){ var structureItem = {}; if (!item.hidden){ structureItem.name = item.label; structureItem.noresize = item.noresize; structureItem.field = item.itemName; structureItem.width = item.width; structureItem.formatter = item.formatter; if (item.editable){ structureItem.editable = true; structureItem.type = item.type; structureItem.options = item.options; structureItem.values = item.values; } structure.push(structureItem); } }); return {cells: [structure]}; }, postCreate: function(){ this.inherited(arguments); this.connect(this.selection, 'onSelected', this._onSelectedHandler); this.connect(this.selection, 'onDeselected', this._onDeselectedHandler); this.connect(this, 'onRowClick', this._onRowClickHandler); this.connect(this, 'dokeyup', this._onKeyupHandler); }, _onSelectedHandler: function(rowIndex) { this.getItem(rowIndex).hidden = "false"; this.pdSelection.push({index: rowIndex, hidden: false}); }, _onDeselectedHandler: function(rowIndex) { this.getItem(rowIndex).hidden = "true"; this.pdSelection.push({index: rowIndex, hidden: true}); }, onSelectionChanged: function() { this.inherited(arguments); if(this.timer.isRunning){ this.timer.setInterval(AUTO_PREVIEW_DELAY); } else { this.timer.start(); } }, _pdTogglePublishBtn: function() { //disable publish button in case of non column selected. if (this.selection.getSelected().length == 0){ pd_publishBtn.set("disabled", true); } else { pd_publishBtn.set("disabled", false); } }, _doPreveiwScroll: function(rowIndex) { //select and scroll the preview when column clicked if (this.selection.selected[rowIndex]){ this.previewGrid.pdSelectColumn(rowIndex); } }, _onRowClickHandler: function(e){ if (e.cell.field != "hidden") { this._doPreveiwScroll(e.rowIndex); } }, _onKeyupHandler: function(e) { if (e.cell.field != "hidden" && e.rowIndex >= 0 && e.keyCode == keys.SPACE) { this._doPreveiwScroll(e.rowIndex); } }, pdToggleColumn: function(sel){ this.previewGrid.pdToggleColumn(sel, true); this.timer.stop(); this._pdTogglePublishBtn(); }, canSort: function(colIndex, field){ // summary: // Overwritten return false; }, canEdit: function(inCell, inRowIndex) { var item = this.getItem(inRowIndex); if (item[inCell.field + "Editable"] != undefined) { return item[inCell.field + "Editable"]; } return inCell.editable; }, onApplyCellEdit: function(inValue, inRowIndex, inFieldIndex) { var item = this.getItem(inRowIndex); array.forEach(model, function (modelItem){ if (modelItem.listener && (modelItem.listener.itemName == inFieldIndex)){ var map = modelItem.listener.map; array.forEach(map, function (mapItem) { if (mapItem[inValue]){ item[modelItem.itemName] = mapItem[inValue]; } else { item[modelItem.itemName] = modelItem.listener.defaultValue; } }); } }); }, pdGetXml: function() { return this.store.objectStore.generateXmlFromData(this.selection.selected); }, hasFactSelected: function() { var returnVal = false; array.forEach(this.selection.getSelected(), function(item){ if (item.usage == "fact") { returnVal = true; return; } }); return returnVal; } }); ColumnGrid.markupFactory = EnhancedGrid.markupFactory; ColumnGrid.registerPlugin = EnhancedGrid.registerPlugin; return ColumnGrid; });