CategoryComboBox.js (5280B)
1 Ext.define("PartKeepr.CategoryComboBox", { 2 extend: "PartKeepr.Widgets.TreePicker", 3 alias: 'widget.CategoryComboBox', 4 5 6 editable: true, 7 8 /** 9 * @cfg {Number} typeAheadDelay 10 * The length of time in milliseconds to wait until the typeahead function is called 11 */ 12 typeAheadDelay: 250, 13 14 /** 15 * @var {Ext.util.DelayedTask} typeAheadTask 16 * The internal task for the typeAhead delay 17 */ 18 typeAheadTask: null, 19 20 /** 21 * @var {PartKeepr.StorageLocationBundle.Entity.StorageLocation} selectedStorageLocation 22 * The selected storage location 23 */ 24 selectedCategory: null, 25 26 enableKeyEvents: true, 27 28 listeners: { 29 'specialkey': { 30 fn: 'keyHandler', 31 scope: 'this' 32 } 33 }, 34 35 triggers: { 36 reload: { 37 cls: "x-form-reload-trigger", 38 weight: -1, 39 handler: function () { 40 this.store.load(); 41 }, 42 scope: 'this' 43 } 44 }, 45 46 _oldValue: null, 47 48 initComponent: function () { 49 this.store = Ext.create("PartKeepr.data.store.PartCategoryStore"); 50 51 this.on("keyup", Ext.bind(this.onFieldChange, this)); 52 this.on("blur", Ext.bind(this.onBlur, this)); 53 54 this.listenersStore = this.store.on({ 55 scope: this, 56 // Workaround to remember the value when loading 57 beforeload: function () { 58 this._oldValue = this.getValue(); 59 }, 60 // Set the old value when load is complete 61 load: function () { 62 if (this._oldValue !== null) { 63 this.setValue(this._oldValue); 64 } 65 } 66 }); 67 68 this.callParent(); 69 }, 70 onBlur: function () { 71 this.applySelection(); 72 this.validate(); 73 }, 74 onFieldChange: function () { 75 var newValue = this.inputEl.getValue(); 76 77 if (!this.typeAheadTask) { 78 this.typeAheadTask = new Ext.util.DelayedTask(this.onTypeAhead, this, [newValue]); 79 } 80 81 this.typeAheadTask.delay(this.typeAheadDelay, false, false, [newValue]); 82 }, 83 setValue: function (value) { 84 if (value !== null) { 85 this.textValue = value.get("name"); 86 } else { 87 this.textValue = ""; 88 } 89 this.callParent(arguments); 90 this.validate(); 91 }, 92 onTypeAhead: function (newValue) { 93 if (newValue !== this.textValue) { 94 var picker = this.getPicker(); 95 var store = picker.getStore(); 96 97 var node = store.findNode("name", newValue, false, false); 98 this.expand(); 99 100 if (node !== null) { 101 picker.getSelectionModel().select(node); 102 } else { 103 picker.getSelectionModel().deselectAll(); 104 } 105 106 this.inputEl.focus(); 107 108 this.textValue = newValue; 109 } 110 }, 111 /** 112 * Handles special keys used in this field. 113 * 114 * Enter: Starts the search 115 * Escape: Removes the search and clears the field contents 116 */ 117 keyHandler: function (field, e) { 118 var picker = this.getPicker(), 119 currentSelection, index; 120 switch (e.getKey()) { 121 case e.DOWN: 122 currentSelection = picker.getSelectionModel().getSelection(); 123 124 if (currentSelection.length === 0) { 125 picker.getSelectionModel().select(0); 126 } else { 127 index = picker.getStore().indexOf(currentSelection[0]) + 1; 128 129 if (index < picker.getStore().count()) { 130 picker.getSelectionModel().select(index); 131 } 132 } 133 break; 134 case e.UP: 135 currentSelection = picker.getSelectionModel().getSelection(); 136 137 if (currentSelection.length === 0) { 138 picker.getSelectionModel().select(0); 139 } else { 140 index = picker.getStore().indexOf(currentSelection[0]) - 1; 141 142 if (index >= 0) { 143 picker.getSelectionModel().select(index); 144 } 145 } 146 break; 147 case e.ENTER: 148 if (!this.isExpanded) { 149 this.expand(); 150 return; 151 } else { 152 this.applySelection(); 153 } 154 break; 155 case e.TAB: 156 this.applySelection(); 157 break; 158 } 159 160 this.inputEl.focus(); 161 }, 162 applySelection: function () { 163 var currentSelection = this.getPicker().getSelectionModel().getSelection(); 164 165 if (currentSelection.length === 1) { 166 this.setValue(currentSelection[0]); 167 } 168 169 this.collapse(); 170 }, 171 getErrors: function (value) { 172 var errors = this.callParent(arguments); 173 174 if (!this.inputEl) { 175 return errors; 176 } 177 178 if (!(this.getValue() instanceof PartKeepr.PartBundle.Entity.PartCategory) || 179 this.inputEl.getValue() !== this.getValue().get("name")) { 180 errors.push(i18n("A category must be selected")); 181 } 182 183 return errors; 184 } 185 });