AddPart.js (6030B)
1 /** 2 * Searches the configured part field(s). If no record was found, the create part window is being opened. 3 */ 4 Ext.define("PartKeepr.BarcodeScanner.Actions.AddPart", { 5 extend: "PartKeepr.BarcodeScanner.Action", 6 7 statics: { 8 actionName: i18n("Add Part"), 9 actionDescription: i18n("Searches for a part. If the part is not found, a create part window is opened"), 10 11 /** 12 * Configures the action 13 */ 14 configure: function (configuration) 15 { 16 configuration = Ext.applyIf(configuration, { 17 searchFields: [], 18 searchMode: 'fixed' 19 }); 20 21 var modelFieldSelector = Ext.create({ 22 xtype: 'modelFieldSelector', 23 id: 'searchPartFieldSelector', 24 border: false, 25 sourceModel: PartKeepr.PartBundle.Entity.Part, 26 initiallyChecked: configuration.searchFields, 27 flex: 1 28 }); 29 30 var saveButton = Ext.create("Ext.button.Button", { 31 text: i18n("OK"), 32 iconCls: 'fugue-icon disk', 33 }); 34 35 var cancelButton = Ext.create("Ext.button.Button", { 36 text: i18n("Cancel"), 37 iconCls: 'web-icon cancel' 38 }); 39 40 var bottomToolbar = Ext.create("Ext.toolbar.Toolbar", { 41 enableOverflow: true, 42 margin: '10px', 43 defaults: {minWidth: 100}, 44 dock: 'bottom', 45 ui: 'footer', 46 items: [saveButton, cancelButton] 47 }); 48 49 var window = Ext.create('Ext.window.Window', { 50 title: i18n("Add/Remove Stock Configuration"), 51 height: 400, 52 modal: true, 53 width: 600, 54 layout: { 55 type: 'vbox', 56 pack: 'start', 57 align: 'stretch' 58 }, 59 items: [ 60 { 61 html: i18n("Select the field(s) to be searched"), 62 border: false, 63 bodyStyle: 'padding: 5px; background:transparent;', 64 }, 65 modelFieldSelector, 66 { 67 xtype: 'radiogroup', 68 layout: 'vbox', 69 itemId: 'searchMode', 70 items: [ 71 { 72 boxLabel: i18n("Search string as-is"), 73 name: 'searchMode', 74 inputValue: "fixed", 75 checked: configuration.searchMode == "fixed" ? true : false 76 }, 77 { 78 boxLabel: i18n("Search beginning of string (string*)"), 79 name: 'searchMode', 80 inputValue: "beginning", 81 checked: configuration.searchMode == "beginning" ? true : false 82 }, { 83 boxLabel: i18n("Search middle of string (*string*)"), 84 name: 'searchMode', 85 inputValue: "any", 86 checked: configuration.searchMode == "any" ? true : false 87 88 } 89 ] 90 } 91 ], 92 dockedItems: bottomToolbar 93 } 94 ).show(); 95 96 saveButton.setHandler(function () 97 { 98 var selection = modelFieldSelector.getChecked(); 99 var fields = []; 100 101 for (var i = 0; i < selection.length; i++) { 102 fields.push(selection[i].data.data.name); 103 } 104 configuration.searchFields = fields; 105 106 configuration.searchMode = this.down("#searchMode").getValue().searchMode; 107 this.close(); 108 }, window); 109 110 cancelButton.setHandler(function () 111 { 112 this.close(); 113 114 }, window); 115 116 } 117 }, 118 119 execute: function () 120 { 121 this.searchStore = Ext.create("Ext.data.Store", { 122 model: 'PartKeepr.PartBundle.Entity.Part', 123 autoLoad: false, 124 autoSync: false, 125 remoteFilter: true, 126 remoteSort: true 127 }); 128 129 var subFilters = []; 130 var searchValue; 131 132 switch (this.config.searchMode) { 133 case "beginning": 134 searchValue = this.data + "%"; 135 break; 136 case "any": 137 searchValue = "%" + this.data + "%"; 138 break; 139 default: 140 searchValue = this.data; 141 break; 142 } 143 144 for (var i = 0; i < this.config.searchFields.length; i++) { 145 subFilters.push(Ext.create("PartKeepr.util.Filter", { 146 property: this.config.searchFields[i], 147 operator: "LIKE", 148 value: searchValue 149 })); 150 } 151 152 this.filter = Ext.create("PartKeepr.util.Filter", { 153 type: "OR", 154 subfilters: subFilters 155 }); 156 157 this.searchStore.on("load", this.onDataLoaded, this); 158 this.searchStore.addFilter(this.filter, true); 159 this.searchStore.load({start: 0}); 160 }, 161 onDataLoaded: function () 162 { 163 if (this.searchStore.getCount() === 0) { 164 var defaults = {}, i; 165 166 for (i = 0; i < this.config.searchFields.length; i++) { 167 defaults[this.config.searchFields[i]] = this.data; 168 169 } 170 171 PartKeepr.getApplication().getPartManager().onItemAdd(defaults); 172 173 174 } 175 } 176 });