Panel.js (6953B)
1 Ext.define("PartKeepr.Components.Widgets.ColumnConfigurator.Panel", { 2 extend: "Ext.panel.Panel", 3 4 layout: 'border', 5 grid: null, 6 7 originalColumnConfigurations: [], 8 9 viewModel: { 10 data: { 11 column: null 12 }, 13 formulas: { 14 isFlex: function (get) 15 { 16 17 return get("column.widthMode") === "flex"; 18 } 19 } 20 }, 21 22 initComponent: function () 23 { 24 this.columnListGrid = Ext.create("PartKeepr.Components.Widgets.ColumnConfigurator.ColumnListGrid", { 25 region: 'west', 26 width: 400, 27 split: true 28 }); 29 30 this.columnProperties = Ext.create("PartKeepr.Components.Widgets.ColumnConfigurator.ColumnProperties", { 31 region: 'center', 32 sourceModel: this.grid.getStore().getModel() 33 } 34 ); 35 36 this.items = [ 37 this.columnListGrid, 38 this.columnProperties 39 ]; 40 41 this.callParent(); 42 43 this.columnListGrid.on("select", this.onColumnSelect, this); 44 this.columnListGrid.getStore().on("add", this.onAdd, this); 45 this.down("#restoreDefaults").on("click", this.restoreDefaults, this); 46 this.columnListGrid.getStore().on("datachanged", this.preview, this); 47 this.columnListGrid.getStore().on("update", this.preview, this); 48 49 this.down("#gridPresetCombo").getStore().addFilter({ 50 property: "grid", 51 operator: "=", 52 value: this.grid.$className 53 }); 54 55 this.down("#gridPresetCombo").on("selectPreset", this.onPresetSelect, this); 56 this.down("#gridPresetCombo").on("markAsDefault", this.onMarkAsDefault, this); 57 this.down("#gridPresetCombo").setAdditionalFields([ 58 { 59 fieldName: "grid", 60 value: this.grid.$className 61 } 62 ]); 63 64 this.down("#renderers").on("change", this.preview, this); 65 66 this.autoPreviewTask = new Ext.util.DelayedTask(this.doPreview, this, null, true); 67 }, 68 restoreDefaults: function () 69 { 70 if (this.grid instanceof PartKeepr.BaseGrid) 71 { 72 this.grid.reconfigure(this.grid.store, this.grid.getDefaultColumnConfiguration()); 73 this.applyColumnConfigurationFromGrid(); 74 } 75 }, 76 onMarkAsDefault: function (gridPreset) { 77 gridPreset.callPutAction("markAsDefault", {}, Ext.bind(this.onMarkedAsDefault, this)); 78 }, 79 onMarkedAsDefault: function () { 80 this.down("#gridPresetCombo").getStore().load(); 81 }, 82 onPresetSelect: function (configuration) 83 { 84 this.grid.reconfigure(this.grid.store, configuration); 85 this.applyColumnConfigurationFromGrid(); 86 87 this.down("#gridPresetCombo").setConfiguration(configuration); 88 }, 89 onColumnSelect: function (grid, record) 90 { 91 this.editData(record); 92 }, 93 onAdd: function (store, records) 94 { 95 if (records.length === 1) 96 { 97 this.editData(records[0]); 98 } 99 }, 100 editData: function (record) 101 { 102 this.columnProperties.loadRecord(record); 103 }, 104 preview: function () 105 { 106 this.down("#gridPresetCombo").setConfiguration(this.getColumnConfigurations()); 107 this.autoPreviewTask.delay(200); 108 }, 109 doPreview: function () 110 { 111 this.grid.reconfigure(this.grid.store, this.getColumnConfigurations()); 112 }, 113 getColumnConfigurations: function () 114 { 115 var i, j, rtype; 116 117 var config = {}, columnConfigurations = [], fieldsToCopy = this.getFieldsToCopy(); 118 119 var data = this.columnListGrid.getStore().getData(); 120 121 for (i = 0; i < data.getCount(); i++) 122 { 123 config = {}; 124 for (j = 0; j < fieldsToCopy.length; j++) 125 { 126 config[fieldsToCopy[j]] = data.getAt(i).get(fieldsToCopy[j]); 127 } 128 129 if (data.getAt(i).get("widthMode") === "flex") 130 { 131 delete config.width; 132 } else 133 { 134 delete config.flex; 135 } 136 137 config.renderers = []; 138 139 for (j = 0; j < data.getAt(i).renderers().getCount(); j++) 140 { 141 142 rtype = data.getAt(i).renderers().getAt(j).get("rtype"); 143 144 if (typeof(PartKeepr.Components.Grid.Renderers.RendererRegistry.lookupRenderer(rtype)) !== "undefined") 145 { 146 config.renderers.push({ 147 rtype: rtype, 148 rendererConfig: Ext.decode(data.getAt(i).renderers().getAt(j).get("config")) 149 }); 150 } 151 } 152 columnConfigurations.push(config); 153 154 } 155 156 return columnConfigurations; 157 }, 158 applyColumnConfigurationFromGrid: function () 159 { 160 var columns = this.grid.getColumns(); 161 var i, j; 162 var columnRecord; 163 this.originalColumnConfigurations = []; 164 var startColumn = 0; 165 var columnConfig; 166 var fieldsToCopy = this.getFieldsToCopy(); 167 168 this.columnListGrid.getStore().removeAll(); 169 170 // In case we have a row expander which adds an additional column, skip the first column 171 if (this.grid.findPlugin("metapartrowexpander")) 172 { 173 startColumn++; 174 } 175 176 for (i = startColumn; i < columns.length; i++) 177 { 178 columnRecord = Ext.create("PartKeepr.Models.ColumnConfiguration"); 179 columnConfig = {}; 180 181 for (j = 0; j < fieldsToCopy.length; j++) 182 { 183 columnRecord.set(fieldsToCopy[j], columns[i][fieldsToCopy[j]]); 184 columnConfig[fieldsToCopy[j]] = columns[i][fieldsToCopy[j]]; 185 } 186 187 if (columnConfig["flex"] > 0) 188 { 189 columnRecord.set("widthMode", "flex"); 190 } else 191 { 192 columnRecord.set("widthMode", "width"); 193 } 194 195 columnConfig["renderers"] = columns[i]["renderers"]; 196 this.originalColumnConfigurations.push(columnConfig); 197 columnRecord.set("index", i); 198 199 if (columns[i].renderers instanceof Array) 200 { 201 for (j = 0; j < columns[i].renderers.length; j++) 202 { 203 columnRecord.renderers().add(Ext.create("PartKeepr.Models.ColumnRendererConfiguration", { 204 rtype: columns[i].renderers[j].rtype, 205 config: Ext.encode(columns[i].renderers[j].rendererConfig) 206 })); 207 } 208 } 209 210 columnRecord.renderers().on("datachanged", this.preview, this); 211 columnRecord.renderers().on("update", this.preview, this); 212 213 this.columnListGrid.getStore().add(columnRecord); 214 } 215 }, 216 getFieldsToCopy: function () 217 { 218 return ["dataIndex", "text", "hidden", "flex", "width", "tooltip"]; 219 } 220 });