StorageLocationNavigation.js (5388B)
1 Ext.define("PartKeepr.StorageLocationNavigation", { 2 extend: 'Ext.panel.Panel', 3 4 layout: 'border', 5 6 /** 7 * @var {Ext.data.Store} 8 */ 9 store: null, 10 verticalLayout: false, 11 dragAndDrop: true, 12 categoryEditActions: true, 13 itemEditActions: true, 14 editItemAsObject: false, 15 16 initComponent: function () 17 { 18 var gridConfig = { 19 xtype: 'partkeepr.StorageLocationGrid', 20 resizable: true, 21 split: true, 22 titleProperty: "name", 23 store: this.store 24 }; 25 26 if (this.verticalLayout) { 27 gridConfig.region = "east"; 28 gridConfig.width = "75%"; 29 } else { 30 gridConfig.region = "south"; 31 gridConfig.height = "50%"; 32 } 33 34 if (this.dragAndDrop) { 35 gridConfig.viewConfig = { 36 plugins: { 37 ddGroup: 'StorageLocationTree', 38 ptype: 'gridviewdragdrop', 39 enableDrop: false 40 } 41 }; 42 43 gridConfig.enableDragDrop = true; 44 } 45 46 gridConfig.enableEditing = this.itemEditActions; 47 gridConfig.editItemAsObject = this.editItemAsObject; 48 49 this.items = [ 50 { 51 xtype: 'partkeepr.StorageLocationTree', 52 region: 'center', 53 categoryEditActions: this.categoryEditActions 54 }, gridConfig 55 ]; 56 57 this.callParent(arguments); 58 59 this.getTree().on("itemclick", this.onCategoryClick, this); 60 61 this.getGrid().on("storageLocationMultiAdd", this.onMultiAddStorageLocation, 62 this); 63 this.getGrid().on("itemAdd", this.onAddStorageLocation, this); 64 this.getGrid().on("itemDelete", function (id) 65 { 66 this.fireEvent("itemDelete", id); 67 }, this 68 ); 69 this.getGrid().on("itemEdit", function (id) 70 { 71 this.fireEvent("itemEdit", id); 72 }, this 73 ); 74 }, 75 getGrid: function () { 76 return this.down("partkeepr\\.StorageLocationGrid"); 77 }, 78 getTree: function () { 79 return this.down("partkeepr\\.StorageLocationTree"); 80 }, 81 setSearchValue: function (val) { 82 var searchField = this.getGrid().searchField; 83 searchField.setValue(val); 84 searchField.startSearch(); 85 }, 86 /** 87 * Applies the category filter to the store when a category is selected 88 * 89 * @param {Ext.tree.View} tree The tree view 90 * @param {Ext.data.Model} record the selected record 91 */ 92 onCategoryClick: function (tree, record) 93 { 94 this.setCategoryFilter(record); 95 }, 96 setCategoryFilter: function (record) { 97 var filter = Ext.create("PartKeepr.util.Filter", { 98 property: 'category', 99 operator: 'IN', 100 value: this.getChildrenIds(record) 101 }); 102 103 this.store.addFilter(filter); 104 105 }, 106 /** 107 * Returns the ID for this node and all child nodes 108 * 109 * @param {Ext.data.Model} The node 110 * @return Array 111 */ 112 getChildrenIds: function (node) 113 { 114 var childNodes = [node.getId()]; 115 116 if (node.hasChildNodes()) { 117 for (var i = 0; i < node.childNodes.length; i++) { 118 childNodes = childNodes.concat(this.getChildrenIds(node.childNodes[i])); 119 } 120 } 121 122 return childNodes; 123 }, 124 /** 125 * Called when a storage location is about to be added. This prepares the to-be-edited record with the proper category id. 126 */ 127 onAddStorageLocation: function () 128 { 129 var selection = this.getTree().getSelection(); 130 131 var category; 132 if (selection.length === 0) { 133 category = this.getTree().getRootNode().firstChild.getId(); 134 } else { 135 var item = selection.shift(); 136 category = item.getId(); 137 } 138 this.fireEvent("itemAdd", { 139 category: category 140 }); 141 }, 142 /** 143 * Called when a storage location is about to be added. This prepares the to-be-edited record with the proper category id. 144 */ 145 onMultiAddStorageLocation: function () 146 { 147 var selection = this.getTree().getSelection(); 148 149 var category; 150 if (selection.length === 0) { 151 category = this.getTree().getRootNode().firstChild.getId(); 152 } else { 153 var item = selection.shift(); 154 category = item.getId(); 155 } 156 157 var j = Ext.create("PartKeepr.StorageLocationMultiCreateWindow", { 158 category: category, 159 listeners: { 160 destroy: { 161 fn: this.onMultiCreateWindowDestroy, 162 scope: this 163 } 164 } 165 }); 166 j.show(); 167 168 }, 169 /** 170 * Reloads the store after the multi-create window was closed 171 */ 172 onMultiCreateWindowDestroy: function () 173 { 174 this.store.load(); 175 }, 176 /** 177 * Triggers a reload of the store when an edited record affects the store 178 */ 179 syncChanges: function () 180 { 181 this.getGrid().getStore().load(); 182 }, 183 /** 184 * Returns the selection model of the storage location grid 185 * @return {Ext.selection.Model} The selection model 186 */ 187 getSelectionModel: function () 188 { 189 return this.getGrid().getSelectionModel(); 190 } 191 192 193 });