]>
Commit | Line | Data |
---|---|---|
458e534c EK |
1 | /* |
2 | * Base class for all the multitab config panels | |
9a4c279d DC |
3 | * |
4 | * How to use this: | |
5 | * | |
6 | * You create a subclass of this, and then define your wanted tabs | |
7 | * as items like this: | |
8 | * | |
9 | * items: [{ | |
10 | * title: "myTitle", | |
11 | * xytpe: "somextype", | |
12 | * iconCls: 'fa fa-icon', | |
13 | * groups: ['somegroup'], | |
14 | * expandedOnInit: true, | |
15 | * itemId: 'someId' | |
16 | * }] | |
17 | * | |
18 | * this has to be in the declarative syntax, else we | |
19 | * cannot save them for later | |
20 | * (so no Ext.create or Ext.apply of an item in the subclass) | |
21 | * | |
22 | * the groups array expects the itemids of the items | |
23 | * which are the parents, which have to come before they | |
24 | * are used | |
25 | * | |
26 | * if you want following the tree: | |
27 | * | |
28 | * Option1 | |
29 | * Option2 | |
30 | * -> SubOption1 | |
31 | * -> SubSubOption1 | |
32 | * | |
33 | * the suboption1 group array has to look like this: | |
34 | * groups: ['itemid-of-option2'] | |
35 | * | |
36 | * and of subsuboption1: | |
37 | * groups: ['itemid-of-option2', 'itemid-of-suboption1'] | |
38 | * | |
39 | * setting the expandedOnInit determines if the item/group is expanded | |
40 | * initially (false by default) | |
458e534c | 41 | */ |
55ed1090 DM |
42 | Ext.define('PVE.panel.Config', { |
43 | extend: 'Ext.panel.Panel', | |
44 | alias: 'widget.pvePanelConfig', | |
45 | ||
ec505260 TL |
46 | showSearch: true, // add a resource grid with a search button as first tab |
47 | viewFilter: undefined, // a filter to pass to that resource grid | |
458e534c | 48 | |
375d2918 DC |
49 | tbarSpacing: true, // if true, adds a spacer after the title in tbar |
50 | ||
9a4c279d DC |
51 | dockedItems: [{ |
52 | // this is needed for the overflow handler | |
53 | xtype: 'toolbar', | |
54 | overflowHandler: 'scroller', | |
55 | dock: 'left', | |
56 | style: { | |
57 | backgroundColor: '#f5f5f5', | |
58 | padding: 0, | |
f6710aac | 59 | margin: 0, |
9a4c279d DC |
60 | }, |
61 | items: { | |
62 | xtype: 'treelist', | |
63 | itemId: 'menu', | |
64 | ui: 'nav', | |
65 | expanderOnly: true, | |
66 | expanderFirst: false, | |
67 | animation: false, | |
68 | singleExpand: false, | |
69 | listeners: { | |
70 | selectionchange: function(treeList, selection) { | |
71 | var me = this.up('panel'); | |
72 | me.suspendLayout = true; | |
73 | me.activateCard(selection.data.id); | |
74 | me.suspendLayout = false; | |
75 | me.updateLayout(); | |
76 | }, | |
77 | itemclick: function(treelist, info) { | |
78 | var olditem = treelist.getSelection(); | |
79 | var newitem = info.node; | |
80 | ||
81 | // when clicking on the expand arrow, | |
ec505260 | 82 | // we don't select items, but still want |
9a4c279d DC |
83 | // the original behaviour |
84 | if (info.select === false) { | |
85 | return; | |
86 | } | |
87 | ||
88 | // if you click on a different item which is open, | |
89 | // leave it open | |
90 | // else toggle the clicked item | |
91 | if (olditem.data.id !== newitem.data.id && | |
92 | newitem.data.expanded === true) { | |
93 | info.toggle = false; | |
94 | } else { | |
95 | info.toggle = true; | |
96 | } | |
f6710aac TL |
97 | }, |
98 | }, | |
99 | }, | |
9a4c279d DC |
100 | }, |
101 | { | |
102 | xtype: 'toolbar', | |
103 | itemId: 'toolbar', | |
104 | dock: 'top', | |
105 | height: 36, | |
f6710aac | 106 | overflowHandler: 'scroller', |
9a4c279d DC |
107 | }], |
108 | ||
109 | firstItem: '', | |
110 | layout: 'card', | |
111 | border: 0, | |
112 | ||
86ca2794 DM |
113 | // used for automated test |
114 | selectById: function(cardid) { | |
115 | var me = this; | |
116 | ||
117 | var root = me.store.getRoot(); | |
118 | var selection = root.findChild('id', cardid, true); | |
119 | ||
120 | if (selection) { | |
63b6b7ce | 121 | selection.expand(); |
86ca2794 DM |
122 | var menu = me.down('#menu'); |
123 | menu.setSelection(selection); | |
124 | return cardid; | |
125 | } | |
126 | }, | |
127 | ||
9a4c279d DC |
128 | activateCard: function(cardid) { |
129 | var me = this; | |
130 | if (me.savedItems[cardid]) { | |
131 | var curcard = me.getLayout().getActiveItem(); | |
132 | var newcard = me.add(me.savedItems[cardid]); | |
a50de124 | 133 | me.helpButton.setOnlineHelp(newcard.onlineHelp || me.onlineHelp); |
9a4c279d DC |
134 | if (curcard) { |
135 | me.setActiveItem(cardid); | |
136 | me.remove(curcard, true); | |
137 | ||
138 | // trigger state change | |
139 | ||
140 | var ncard = cardid; | |
141 | // Note: '' is alias for first tab. | |
142 | // First tab can be 'search' or something else | |
143 | if (cardid === me.firstItem) { | |
144 | ncard = ''; | |
145 | } | |
146 | if (me.hstateid) { | |
147 | me.sp.set(me.hstateid, { value: ncard }); | |
148 | } | |
149 | } | |
150 | } | |
151 | }, | |
152 | ||
55ed1090 DM |
153 | initComponent: function() { |
154 | var me = this; | |
155 | ||
156 | var stateid = me.hstateid; | |
157 | ||
9a4c279d | 158 | me.sp = Ext.state.Manager.getProvider(); |
55ed1090 | 159 | |
cececa1d | 160 | var activeTab; // leaving this undefined means items[0] will be the default tab |
55ed1090 | 161 | |
55ed1090 | 162 | if (stateid) { |
9a4c279d | 163 | var state = me.sp.get(stateid); |
55ed1090 | 164 | if (state && state.value) { |
8dc7c7af | 165 | // if this tab does not exist, it chooses the first |
9a4c279d | 166 | activeTab = state.value; |
55ed1090 DM |
167 | } |
168 | } | |
169 | ||
9a4c279d DC |
170 | // get title |
171 | var title = me.title || me.pveSelNode.data.text; | |
172 | me.title = undefined; | |
55ed1090 | 173 | |
9a4c279d | 174 | // create toolbar |
55ed1090 DM |
175 | var tbar = me.tbar || []; |
176 | me.tbar = undefined; | |
177 | ||
9cc74dfe | 178 | if (!me.onlineHelp) { |
8058410f TL |
179 | switch (me.pveSelNode.data.id) { |
180 | case 'type/storage': me.onlineHelp = 'chapter-pvesm.html'; break; | |
181 | case 'type/qemu': me.onlineHelp = 'chapter-qm.html'; break; | |
182 | case 'type/lxc': me.onlineHelp = 'chapter-pct.html'; break; | |
183 | case 'type/pool': me.onlineHelp = 'chapter-pveum.html#_pools'; break; | |
184 | case 'type/node': me.onlineHelp = 'chapter-sysadmin.html'; break; | |
9cc74dfe DC |
185 | } |
186 | } | |
187 | ||
375d2918 DC |
188 | if (me.tbarSpacing) { |
189 | tbar.unshift('->'); | |
190 | } | |
55ed1090 DM |
191 | tbar.unshift({ |
192 | xtype: 'tbtext', | |
193 | text: title, | |
f6710aac | 194 | baseCls: 'x-panel-header-text', |
55ed1090 DM |
195 | }); |
196 | ||
672a6270 | 197 | me.helpButton = Ext.create('Proxmox.button.Help', { |
9cc74dfe DC |
198 | hidden: false, |
199 | listenToGlobalEvent: false, | |
f6710aac | 200 | onlineHelp: me.onlineHelp || undefined, |
9cc74dfe DC |
201 | }); |
202 | ||
203 | tbar.push(me.helpButton); | |
204 | ||
9a4c279d | 205 | me.dockedItems[1].items = tbar; |
55ed1090 | 206 | |
9a4c279d DC |
207 | // include search tab |
208 | me.items = me.items || []; | |
55ed1090 | 209 | if (me.showSearch) { |
9a4c279d | 210 | me.items.unshift({ |
7e27f5d1 DC |
211 | itemId: 'search', |
212 | title: gettext('Search'), | |
9a4c279d DC |
213 | iconCls: 'fa fa-search', |
214 | xtype: 'pveResourceGrid', | |
f6710aac | 215 | pveSelNode: me.pveSelNode, |
55ed1090 DM |
216 | }); |
217 | } | |
218 | ||
9a4c279d | 219 | me.savedItems = {}; |
9a4c279d DC |
220 | if (me.items[0]) { |
221 | me.firstItem = me.items[0].itemId; | |
222 | } | |
55ed1090 | 223 | |
9a4c279d DC |
224 | me.store = Ext.create('Ext.data.TreeStore', { |
225 | root: { | |
f6710aac TL |
226 | expanded: true, |
227 | }, | |
9a4c279d DC |
228 | }); |
229 | var root = me.store.getRoot(); | |
a83c659a | 230 | me.insertNodes(me.items); |
55ed1090 | 231 | |
9a4c279d DC |
232 | delete me.items; |
233 | me.defaults = me.defaults || {}; | |
234 | Ext.apply(me.defaults, { | |
235 | pveSelNode: me.pveSelNode, | |
236 | viewFilter: me.viewFilter, | |
237 | workspace: me.workspace, | |
f6710aac | 238 | border: 0, |
55ed1090 DM |
239 | }); |
240 | ||
241 | me.callParent(); | |
242 | ||
9a4c279d DC |
243 | var menu = me.down('#menu'); |
244 | var selection = root.findChild('id', activeTab, true) || root.firstChild; | |
245 | var node = selection; | |
246 | while (node !== root) { | |
247 | node.expand(); | |
248 | node = node.parentNode; | |
249 | } | |
250 | menu.setStore(me.store); | |
251 | menu.setSelection(selection); | |
252 | ||
253 | // on a state change, | |
254 | // select the new item | |
55ed1090 | 255 | var statechange = function(sp, key, state) { |
9a4c279d | 256 | // it the state change is for this panel |
55ed1090 | 257 | if (stateid && (key === stateid) && state) { |
9a4c279d DC |
258 | // get active item |
259 | var acard = me.getLayout().getActiveItem().itemId; | |
260 | // get the itemid of the new value | |
261 | var ncard = state.value || me.firstItem; | |
262 | if (ncard && (acard != ncard)) { | |
263 | // select the chosen item | |
264 | menu.setSelection(root.findChild('id', ncard, true) || root.firstChild); | |
55ed1090 DM |
265 | } |
266 | } | |
267 | }; | |
268 | ||
269 | if (stateid) { | |
9a4c279d | 270 | me.mon(me.sp, 'statechange', statechange); |
55ed1090 | 271 | } |
a83c659a FE |
272 | }, |
273 | ||
274 | insertNodes: function(items) { | |
275 | var me = this; | |
276 | var root = me.store.getRoot(); | |
277 | ||
278 | items.forEach(function(item) { | |
f6710aac | 279 | var treeitem = Ext.create('Ext.data.TreeModel', { |
a83c659a FE |
280 | id: item.itemId, |
281 | text: item.title, | |
282 | iconCls: item.iconCls, | |
283 | leaf: true, | |
f6710aac | 284 | expanded: item.expandedOnInit, |
a83c659a FE |
285 | }); |
286 | item.header = false; | |
287 | if (me.savedItems[item.itemId] !== undefined) { | |
288 | throw "itemId already exists, please use another"; | |
289 | } | |
290 | me.savedItems[item.itemId] = item; | |
291 | ||
292 | var group; | |
293 | var curnode = root; | |
294 | ||
295 | // get/create the group items | |
296 | while (Ext.isArray(item.groups) && item.groups.length > 0) { | |
297 | group = item.groups.shift(); | |
298 | ||
299 | var child = curnode.findChild('id', group); | |
300 | if (child === null) { | |
301 | // did not find the group item | |
302 | // so add it where we are | |
303 | break; | |
304 | } | |
305 | curnode = child; | |
306 | } | |
307 | ||
308 | // insert the item | |
309 | ||
310 | // lets see if it already exists | |
311 | var node = curnode.findChild('id', item.itemId); | |
312 | ||
313 | if (node === null) { | |
314 | curnode.appendChild(treeitem); | |
315 | } else { | |
316 | // should not happen! | |
317 | throw "id already exists"; | |
318 | } | |
319 | }); | |
320 | }, | |
55ed1090 | 321 | }); |