StorageLocationMultiAddDialog.js (6175B)
1 /** 2 * Represents a form which is used to create multiple storage locations at once. 3 * @class PartKeepr.StorageLocationMultiAddDialog 4 */ 5 Ext.define('PartKeepr.StorageLocationMultiAddDialog', { 6 extend: 'Ext.form.Panel', 7 8 // Layout settings 9 layout: { 10 type: 'vbox', 11 align: 'stretch' 12 }, 13 14 defaults: { 15 border: false 16 }, 17 18 bodyPadding: 5, 19 20 /** 21 * Initializes the component. Adds all form fields 22 */ 23 initComponent: function () { 24 25 /** 26 * The prefix represents the first part of the storage location name, 27 * e.g. "A" for storage locations "A0001". 28 */ 29 this.storageLocationPrefix = Ext.create("Ext.form.field.Text", { 30 fieldLabel: i18n("Name Prefix"), 31 listeners: { 32 change: { 33 fn: this.onFormChange, 34 scope: this 35 } 36 } 37 }); 38 39 /** 40 * Specifies the start of the numeric range. 41 */ 42 this.storageLocationStart = Ext.create("Ext.form.field.Number", { 43 fieldLabel: i18n("Start"), 44 flex: 1, 45 value: 1, 46 minValue: 0, 47 listeners: { 48 change: { 49 fn: this.onFormChange, 50 scope: this 51 } 52 } 53 }); 54 55 /** 56 * Specifies the end of the numeric range. 57 */ 58 this.storageLocationEnd = Ext.create("Ext.form.field.Number", { 59 fieldLabel: i18n("End"), 60 flex: 1, 61 anchor: '100%', 62 value: 10, 63 minValue: 1, 64 listeners: { 65 change: { 66 fn: this.onFormChange, 67 scope: this 68 } 69 } 70 }); 71 72 /** 73 * Specifies if the storage locations should be prefixed with a zero (e.g. creates A0001 instead of A1). 74 */ 75 this.storageLocationZeroPrefix = Ext.create("Ext.form.field.Checkbox", { 76 boxLabel: i18n("Prefix with zeroes"), 77 flex: 1, 78 hideEmptyLabel: false, 79 listeners: { 80 change: { 81 fn: this.onFormChange, 82 scope: this 83 } 84 } 85 }); 86 87 /** 88 * Specifies the overall length of the storage location name. If you have a prefix "A" and numbers up to 89 * 100, you can set the overall length to 5 to achieve "A0100", or to 6 to achieve "A00100". 90 */ 91 this.storageLocationOverallLength = Ext.create("Ext.form.field.Number", { 92 fieldLabel: i18n("Length"), 93 flex: 1, 94 disabled: true, 95 listeners: { 96 change: { 97 fn: this.onFormChange, 98 scope: this 99 } 100 } 101 }); 102 103 /** 104 * Creates a field which displays the storage locations to be created. 105 */ 106 this.outputField = Ext.create("Ext.form.field.TextArea", { 107 fieldLabel: i18n("Sample"), 108 readOnly: true, 109 flex: 1 110 }); 111 112 this.items = [ 113 this.storageLocationPrefix, 114 { 115 layout: { 116 type: 'hbox', 117 align: 'stretch' 118 }, 119 items: [ 120 this.storageLocationStart, 121 this.storageLocationEnd 122 ] 123 }, 124 { 125 layout: { 126 type: 'hbox', 127 align: 'stretch' 128 }, 129 items: [ 130 this.storageLocationZeroPrefix, 131 this.storageLocationOverallLength 132 ] 133 }, 134 this.outputField 135 ]; 136 137 this.callParent(); 138 139 this.recalculateDemo(); 140 }, 141 /** 142 * Called when something in the form has changed. 143 */ 144 onFormChange: function () { 145 /** 146 * If the overall length is less than the prefix plus the length of the highest number, update it 147 */ 148 if (this.storageLocationOverallLength.getValue() < this.getMinLength()) { 149 this.storageLocationOverallLength.setValue(this.getMinLength()); 150 } 151 152 /** 153 * Make sure that the end value is bigger than the start value. 154 */ 155 if (this.storageLocationStart.getValue() > this.storageLocationEnd.getValue()) { 156 this.storageLocationEnd.setValue(this.storageLocationStart.getValue()); 157 } 158 159 /** 160 * Enable/Disable the length field if zero prefix is wanted 161 */ 162 if (this.storageLocationZeroPrefix.getValue()) { 163 this.storageLocationOverallLength.enable(); 164 } else { 165 this.storageLocationOverallLength.disable(); 166 } 167 168 this.recalculateDemo(); 169 }, 170 /** 171 * Calculates the minimum length possible 172 * @returns int The minimum length possible 173 */ 174 getMinLength: function () { 175 return strlen(this.storageLocationPrefix.getValue()) + 176 strlen((this.storageLocationEnd.getValue()).toString()); 177 }, 178 /** 179 * Updates the sample field 180 */ 181 recalculateDemo: function () { 182 this.outputField.setValue(implode("\n", this.getStorageLocations())); 183 }, 184 /** 185 * Returns all storage locations which are to be created 186 * @returns {Array} The storage locations 187 */ 188 getStorageLocations: function () { 189 var j = []; 190 191 for (var i = this.storageLocationStart.getValue(); i < this.storageLocationEnd.getValue() + 1; i++) { 192 if (!this.storageLocationZeroPrefix.getValue()) { 193 // No padding wanted 194 j.push(this.storageLocationPrefix.getValue() + i); 195 } else { 196 var padLength = this.storageLocationOverallLength.getValue() - 197 ( strlen(this.storageLocationPrefix.getValue()) + 198 strlen(i)); 199 200 j.push(this.storageLocationPrefix.getValue() + str_repeat("0", padLength) + i); 201 } 202 203 } 204 205 return j; 206 } 207 208 });