ImportFieldMatcherGrid.js (6103B)
1 Ext.define("PartKeepr.Importer.ImportFieldMatcherGrid", { 2 extend: "Ext.grid.Panel", 3 xtype: 'importFieldMatcherGrid', 4 plugins: { 5 ptype: 'cellediting', 6 clicksToEdit: 1 7 }, 8 bbar: [ 9 { 10 xtype: 'button', 11 text: i18n("Add…"), 12 iconCls: 'fugue-icon flask--plus', 13 itemId: 'addImporterMatchField' 14 }, 15 { 16 xtype: 'button', 17 text: i18n("Delete"), 18 iconCls: 'fugue-icon flask--minus', 19 itemId: 'deleteImporterMatchField', 20 disabled: true 21 } 22 ], 23 store: { 24 fields: ['matchField', 'importField'] 25 }, 26 27 fieldSelectorWindow: null, 28 29 initComponent: function () 30 { 31 this.callParent(arguments); 32 this.down("#addImporterMatchField").on("click", this.onAddMatchField, this); 33 this.down("#deleteImporterMatchField").on("click", this.onDeleteMatchField, this); 34 this.on("selectionchange", this.onSelectionChange, this); 35 this.getStore().on("update", function () 36 { 37 this.fireEvent("change"); 38 }, this); 39 this.reconfigureColumns(null); 40 41 42 }, 43 onSelectionChange: function (model, selected) 44 { 45 if (selected.length === 1) { 46 this.down("#deleteImporterMatchField").enable(); 47 } else { 48 this.down("#deleteImporterMatchField").disable(); 49 } 50 }, 51 reconfigureColumns: function (columnsStore) 52 { 53 var columns = [ 54 { 55 text: i18n("Match field"), 56 dataIndex: "matchField", 57 width: 200 58 }, { 59 text: i18n("Import Field"), 60 dataIndex: "importField", 61 width: 200, 62 renderer: function (val) 63 { 64 if (columnsStore.getAt(val) !== null) { 65 return columnsStore.getAt(val).get("headerName"); 66 } else { 67 return ""; 68 } 69 }, 70 editor: { 71 xtype: "combo", 72 forceSelection: true, 73 editable: false, 74 queryMode: "local", 75 store: columnsStore, 76 displayField: "headerName", 77 valueField: "headerIndex" 78 } 79 } 80 ]; 81 this.reconfigure(null, columns); 82 }, 83 addRecord: function (matchField) 84 { 85 this.fieldSelectorWindow.close(); 86 this.store.add({"matchField": matchField, "importField": ""}); 87 this.fireEvent("change"); 88 }, 89 setModel: function (model, ignoreModel) 90 { 91 this.model = model; 92 93 if (ignoreModel === undefined) { 94 this.ignoreModel = null; 95 } else { 96 this.ignoreModel = ignoreModel; 97 } 98 }, 99 onDeleteMatchField: function () 100 { 101 if (this.getSelection().length === 1) { 102 this.getStore().remove(this.getSelection()[0]); 103 } 104 }, 105 getImporterConfig: function () 106 { 107 var data = this.getStore().getData(); 108 var serializedRecords = []; 109 var serializedRecord; 110 111 112 for (var i = 0; i < data.getCount(); i++) { 113 serializedRecord = data.getAt(i).getData(); 114 delete serializedRecord.id; 115 serializedRecords.push(serializedRecord); 116 } 117 118 return serializedRecords; 119 }, 120 setImporterConfig: function (config) 121 { 122 this.getStore().removeAll(); 123 for (var i = 0; i < config.length; i++) { 124 this.getStore().add(config[i]); 125 } 126 }, 127 onAddMatchField: function () 128 { 129 var excludeFields = []; 130 131 for (var j = 0; j < this.store.getCount(); j++) { 132 excludeFields.push(this.store.getAt(j).get("matchField")); 133 } 134 135 var modelFieldSelector = Ext.create({ 136 xtype: 'modelFieldSelector', 137 id: 'searchPartFieldSelector', 138 border: false, 139 sourceModel: this.model, 140 useCheckBoxes: false, 141 excludeFields: excludeFields, 142 excludeModels: [this.ignoreModel], 143 flex: 1, 144 listeners: { 145 selectionchange: function (selectionModel, selected) 146 { 147 var addFieldButton = this.up("#matchFieldWindow").down("#addSelectedField"); 148 149 if (selected.length == 1 && selected[0].data.data.type == "field") { 150 addFieldButton.enable(); 151 } else { 152 addFieldButton.disable(); 153 } 154 } 155 } 156 }); 157 158 modelFieldSelector.on("itemdblclick", function (view, record) 159 { 160 if (record.data.data && record.data.data.type == "field") { 161 this.addRecord(record.data.data.name); 162 } 163 }, this); 164 165 this.fieldSelectorWindow = Ext.create("Ext.window.Window", { 166 title: i18n("Select match field"), 167 itemId: 'matchFieldWindow', 168 height: 400, 169 modal: true, 170 width: 600, 171 layout: { 172 type: 'vbox', 173 pack: 'start', 174 align: 'stretch' 175 }, 176 items: [ 177 modelFieldSelector 178 ], 179 bbar: [ 180 { 181 xtype: 'button', 182 itemId: 'addSelectedField', 183 disabled: true, 184 text: i18n("Add selected Field"), 185 iconCls: 'fugue-icon flask--plus', 186 handler: function () 187 { 188 var selection = modelFieldSelector.getSelection(); 189 190 if (selection.length == 1 && selection[0].data.data.type == "field") { 191 this.addRecord(selection[0].data.data.name); 192 } 193 }, 194 scope: this 195 } 196 ] 197 }).show(); 198 } 199 });