]>
Commit | Line | Data |
---|---|---|
5289a1b8 | 1 | /* |
ec505260 | 2 | * Left Treepanel, containing all the resources we manage in this datacenter: server nodes, server storages, VMs and Containers |
5289a1b8 | 3 | */ |
83726b9d DM |
4 | Ext.define('PVE.tree.ResourceTree', { |
5 | extend: 'Ext.tree.TreePanel', | |
6 | alias: ['widget.pveResourceTree'], | |
7 | ||
366558a7 DC |
8 | userCls: 'proxmox-tags-circle', |
9 | ||
83726b9d DM |
10 | statics: { |
11 | typeDefaults: { | |
2a4971d8 | 12 | node: { |
4dbc64a7 | 13 | iconCls: 'fa fa-building', |
f6710aac | 14 | text: gettext('Nodes'), |
83726b9d | 15 | }, |
2a4971d8 | 16 | pool: { |
4dbc64a7 | 17 | iconCls: 'fa fa-tags', |
f6710aac | 18 | text: gettext('Resource Pool'), |
83726b9d DM |
19 | }, |
20 | storage: { | |
4dbc64a7 | 21 | iconCls: 'fa fa-database', |
f6710aac | 22 | text: gettext('Storage'), |
83726b9d | 23 | }, |
9233148b | 24 | sdn: { |
90f7cb68 | 25 | iconCls: 'fa fa-th', |
f6710aac | 26 | text: gettext('SDN'), |
9233148b | 27 | }, |
83726b9d | 28 | qemu: { |
4dbc64a7 | 29 | iconCls: 'fa fa-desktop', |
f6710aac | 30 | text: gettext('Virtual Machine'), |
83726b9d DM |
31 | }, |
32 | lxc: { | |
b1d8e73d | 33 | //iconCls: 'x-tree-node-lxc', |
4dbc64a7 | 34 | iconCls: 'fa fa-cube', |
f6710aac | 35 | text: gettext('LXC Container'), |
b1d8e73d DC |
36 | }, |
37 | template: { | |
f6710aac TL |
38 | iconCls: 'fa fa-file-o', |
39 | }, | |
40 | }, | |
83726b9d DM |
41 | }, |
42 | ||
b1d8e73d DC |
43 | useArrows: true, |
44 | ||
83726b9d DM |
45 | // private |
46 | nodeSortFn: function(node1, node2) { | |
8e9267f3 | 47 | let me = this; |
ff0777c5 TL |
48 | let n1 = node1.data, n2 = node2.data; |
49 | ||
50 | if (!n1.groupbyid === !n2.groupbyid) { | |
8e9267f3 DC |
51 | let n1IsGuest = n1.type === 'qemu' || n1.type === 'lxc'; |
52 | let n2IsGuest = n2.type === 'qemu' || n2.type === 'lxc'; | |
53 | if (me['group-guest-types'] || !n1IsGuest || !n2IsGuest) { | |
54 | // first sort (group) by type | |
55 | if (n1.type > n2.type) { | |
56 | return 1; | |
57 | } else if (n1.type < n2.type) { | |
58 | return -1; | |
59 | } | |
83726b9d | 60 | } |
8e9267f3 | 61 | |
ff0777c5 | 62 | // then sort (group) by ID |
8e9267f3 DC |
63 | if (n1IsGuest) { |
64 | if (me['group-templates'] && (!n1.template !== !n2.template)) { | |
ff0777c5 TL |
65 | return n1.template ? 1 : -1; // sort templates after regular VMs |
66 | } | |
8e9267f3 DC |
67 | if (me['sort-field'] === 'vmid') { |
68 | if (n1.vmid > n2.vmid) { // prefer VMID as metric for guests | |
69 | return 1; | |
70 | } else if (n1.vmid < n2.vmid) { | |
71 | return -1; | |
72 | } | |
73 | } else { | |
74 | return n1.name.localeCompare(n2.name); | |
83726b9d | 75 | } |
83726b9d | 76 | } |
ff0777c5 | 77 | // same types but not a guest |
53e3ea84 | 78 | return n1.id > n2.id ? 1 : n1.id < n2.id ? -1 : 0; |
83726b9d DM |
79 | } else if (n1.groupbyid) { |
80 | return -1; | |
81 | } else if (n2.groupbyid) { | |
82 | return 1; | |
83 | } | |
ff0777c5 | 84 | return 0; // should not happen |
83726b9d DM |
85 | }, |
86 | ||
87 | // private: fast binary search | |
88 | findInsertIndex: function(node, child, start, end) { | |
ff0777c5 | 89 | let me = this; |
83726b9d | 90 | |
ff0777c5 | 91 | let diff = end - start; |
83726b9d DM |
92 | if (diff <= 0) { |
93 | return start; | |
94 | } | |
ff0777c5 | 95 | let mid = start + (diff >> 1); |
83726b9d | 96 | |
ff0777c5 | 97 | let res = me.nodeSortFn(child, node.childNodes[mid]); |
83726b9d DM |
98 | if (res <= 0) { |
99 | return me.findInsertIndex(node, child, start, mid); | |
100 | } else { | |
101 | return me.findInsertIndex(node, child, mid + 1, end); | |
102 | } | |
103 | }, | |
104 | ||
105 | setIconCls: function(info) { | |
ff0777c5 | 106 | let cls = PVE.Utils.get_object_icon_class(info.type, info); |
4dbc64a7 DC |
107 | if (cls !== '') { |
108 | info.iconCls = cls; | |
83726b9d DM |
109 | } |
110 | }, | |
111 | ||
ff0777c5 | 112 | // add additional elements to text. Currently only the usage indicator for storages |
a19652db | 113 | setText: function(info) { |
ff0777c5 | 114 | let me = this; |
a19652db | 115 | |
ff0777c5 | 116 | let status = ''; |
a19652db | 117 | if (info.type === 'storage') { |
ff0777c5 TL |
118 | let usage = info.disk / info.maxdisk; |
119 | if (usage >= 0.0 && usage <= 1.0) { | |
120 | let barHeight = (usage * 100).toFixed(0); | |
121 | let remainingHeight = (100 - barHeight).toFixed(0); | |
a19652db | 122 | status = '<div class="usage-wrapper">'; |
ff0777c5 TL |
123 | status += `<div class="usage-negative" style="height: ${remainingHeight}%"></div>`; |
124 | status += `<div class="usage" style="height: ${barHeight}%"></div>`; | |
a19652db DC |
125 | status += '</div> '; |
126 | } | |
127 | } | |
8e9267f3 DC |
128 | if (Ext.isNumeric(info.vmid) && info.vmid > 0) { |
129 | if (PVE.UIOptions.getTreeSortingValue('sort-field') !== 'vmid') { | |
130 | info.text = `${info.name} (${String(info.vmid)})`; | |
131 | } | |
132 | } | |
a19652db | 133 | |
731436ee | 134 | info.text += PVE.Utils.renderTags(info.tags, PVE.UIOptions.tagOverrides); |
366558a7 | 135 | |
a19652db DC |
136 | info.text = status + info.text; |
137 | }, | |
138 | ||
dca23c54 DC |
139 | setToolTip: function(info) { |
140 | if (info.type === 'pool' || info.groupbyid !== undefined) { | |
141 | return; | |
142 | } | |
143 | ||
ff0777c5 | 144 | let qtips = [gettext('Status') + ': ' + (info.qmpstatus || info.status)]; |
82a33d4e | 145 | if (info.lock) { |
ff0777c5 | 146 | qtips.push(Ext.String.format(gettext('Config locked ({0})'), info.lock)); |
82a33d4e | 147 | } |
ff0777c5 | 148 | if (info.hastate !== 'unmanaged') { |
dca23c54 DC |
149 | qtips.push(gettext('HA State') + ": " + info.hastate); |
150 | } | |
151 | ||
152 | info.qtip = qtips.join(', '); | |
153 | }, | |
154 | ||
83726b9d DM |
155 | // private |
156 | addChildSorted: function(node, info) { | |
ff0777c5 | 157 | let me = this; |
83726b9d DM |
158 | |
159 | me.setIconCls(info); | |
a19652db | 160 | me.setText(info); |
dca23c54 | 161 | me.setToolTip(info); |
83726b9d | 162 | |
83726b9d | 163 | if (info.groupbyid) { |
0c686cac | 164 | info.text = info.groupbyid; |
83726b9d | 165 | if (info.type === 'type') { |
ff0777c5 | 166 | let defaults = PVE.tree.ResourceTree.typeDefaults[info.groupbyid]; |
83726b9d DM |
167 | if (defaults && defaults.text) { |
168 | info.text = defaults.text; | |
169 | } | |
170 | } | |
171 | } | |
ff0777c5 | 172 | let child = Ext.create('PVETree', info); |
83726b9d | 173 | |
ff0777c5 TL |
174 | if (node.childNodes) { |
175 | let pos = me.findInsertIndex(node, child, 0, node.childNodes.length); | |
176 | node.insertBefore(child, node.childNodes[pos]); | |
177 | } else { | |
178 | node.insertBefore(child); | |
83726b9d DM |
179 | } |
180 | ||
83726b9d DM |
181 | return child; |
182 | }, | |
183 | ||
184 | // private | |
185 | groupChild: function(node, info, groups, level) { | |
ff0777c5 | 186 | let me = this; |
83726b9d | 187 | |
ff0777c5 TL |
188 | let groupBy = groups[level]; |
189 | let v = info[groupBy]; | |
83726b9d DM |
190 | |
191 | if (v) { | |
ff0777c5 | 192 | let group = node.findChild('groupbyid', v); |
83726b9d | 193 | if (!group) { |
ff0777c5 TL |
194 | let groupinfo; |
195 | if (info.type === groupBy) { | |
83726b9d DM |
196 | groupinfo = info; |
197 | } else { | |
198 | groupinfo = { | |
ff0777c5 TL |
199 | type: groupBy, |
200 | id: groupBy + "/" + v, | |
83726b9d | 201 | }; |
ff0777c5 TL |
202 | if (groupBy !== 'type') { |
203 | groupinfo[groupBy] = v; | |
83726b9d DM |
204 | } |
205 | } | |
206 | groupinfo.leaf = false; | |
2a4971d8 | 207 | groupinfo.groupbyid = v; |
83726b9d | 208 | group = me.addChildSorted(node, groupinfo); |
83726b9d | 209 | } |
ff0777c5 | 210 | if (info.type === groupBy) { |
83726b9d DM |
211 | return group; |
212 | } | |
213 | if (group) { | |
214 | return me.groupChild(group, info, groups, level + 1); | |
215 | } | |
216 | } | |
217 | ||
218 | return me.addChildSorted(node, info); | |
219 | }, | |
220 | ||
8e9267f3 DC |
221 | saveSortingOptions: function() { |
222 | let me = this; | |
223 | let changed = false; | |
224 | for (const key of ['sort-field', 'group-templates', 'group-guest-types']) { | |
225 | let newValue = PVE.UIOptions.getTreeSortingValue(key); | |
226 | if (me[key] !== newValue) { | |
227 | me[key] = newValue; | |
228 | changed = true; | |
229 | } | |
230 | } | |
231 | return changed; | |
232 | }, | |
233 | ||
8058410f | 234 | initComponent: function() { |
ff0777c5 | 235 | let me = this; |
8e9267f3 | 236 | me.saveSortingOptions(); |
83726b9d | 237 | |
ff0777c5 TL |
238 | let rstore = PVE.data.ResourceStore; |
239 | let sp = Ext.state.Manager.getProvider(); | |
83726b9d DM |
240 | |
241 | if (!me.viewFilter) { | |
242 | me.viewFilter = {}; | |
243 | } | |
244 | ||
ff0777c5 | 245 | let pdata = { |
83726b9d | 246 | dataIndex: {}, |
f6710aac | 247 | updateCount: 0, |
83726b9d DM |
248 | }; |
249 | ||
ff0777c5 | 250 | let store = Ext.create('Ext.data.TreeStore', { |
83726b9d DM |
251 | model: 'PVETree', |
252 | root: { | |
253 | expanded: true, | |
254 | id: 'root', | |
4dbc64a7 | 255 | text: gettext('Datacenter'), |
f6710aac TL |
256 | iconCls: 'fa fa-server', |
257 | }, | |
83726b9d DM |
258 | }); |
259 | ||
ff0777c5 | 260 | let stateid = 'rid'; |
83726b9d | 261 | |
366558a7 DC |
262 | const changedFields = [ |
263 | 'text', 'running', 'template', 'status', 'qmpstatus', 'hastate', 'lock', 'tags', | |
264 | ]; | |
265 | ||
ff0777c5 | 266 | let updateTree = function() { |
397dfdd3 | 267 | store.suspendEvents(); |
83726b9d | 268 | |
ff0777c5 | 269 | let rootnode = me.store.getRootNode(); |
83726b9d | 270 | // remember selected node (and all parents) |
ff0777c5 TL |
271 | let sm = me.getSelectionModel(); |
272 | let lastsel = sm.getSelection()[0]; | |
273 | let parents = []; | |
8e9267f3 | 274 | let sorting_changed = me.saveSortingOptions(); |
ff0777c5 TL |
275 | for (let node = lastsel; node; node = node.parentNode) { |
276 | parents.push(node); | |
83726b9d DM |
277 | } |
278 | ||
ff0777c5 | 279 | let groups = me.viewFilter.groups || []; |
d8da5538 | 280 | // explicitly check for node/template, as those are not always grouping attributes |
c04b8fea DC |
281 | // also check for name for when the tree is sorted by name |
282 | let moveCheckAttrs = groups.concat(['node', 'template', 'name']); | |
ff0777c5 TL |
283 | let filterfn = me.viewFilter.filterfn; |
284 | ||
285 | let reselect = false; // for disappeared nodes | |
286 | let index = pdata.dataIndex; | |
287 | // remove vanished or moved items and update changed items in-place | |
288 | for (const [key, olditem] of Object.entries(index)) { | |
289 | // getById() use find(), which is slow (ExtJS4 DP5) | |
290 | let item = rstore.data.get(olditem.data.id); | |
291 | ||
8e9267f3 | 292 | let changed = sorting_changed, moved = sorting_changed; |
ff0777c5 TL |
293 | if (item) { |
294 | // test if any grouping attributes changed, catches migrated tree-nodes in server view too | |
d8da5538 | 295 | for (const attr of moveCheckAttrs) { |
ff0777c5 | 296 | if (item.data[attr] !== olditem.data[attr]) { |
6ae31fc1 | 297 | moved = true; |
ff0777c5 | 298 | break; |
6ae31fc1 | 299 | } |
ff0777c5 | 300 | } |
6ae31fc1 | 301 | |
ff0777c5 | 302 | // tree item has been updated |
366558a7 | 303 | for (const field of changedFields) { |
ff0777c5 TL |
304 | if (item.data[field] !== olditem.data[field]) { |
305 | changed = true; | |
306 | break; | |
83726b9d | 307 | } |
83726b9d | 308 | } |
ff0777c5 TL |
309 | // FIXME: also test filterfn()? |
310 | } | |
83726b9d | 311 | |
ff0777c5 TL |
312 | if (changed) { |
313 | olditem.beginEdit(); | |
314 | let info = olditem.data; | |
315 | Ext.apply(info, item.data); | |
316 | me.setIconCls(info); | |
317 | me.setText(info); | |
318 | me.setToolTip(info); | |
319 | olditem.commit(); | |
320 | } | |
321 | if ((!item || moved) && olditem.isLeaf()) { | |
322 | delete index[key]; | |
323 | let parentNode = olditem.parentNode; | |
324 | // a selected item moved (migration) or disappeared (destroyed), so deselect that | |
325 | // node now and try to reselect the moved (or its parent) node later | |
326 | if (lastsel && olditem.data.id === lastsel.data.id) { | |
327 | reselect = true; | |
328 | sm.deselect(olditem); | |
83726b9d | 329 | } |
ff0777c5 TL |
330 | // store events are suspended, so remove the item manually |
331 | store.remove(olditem); | |
332 | parentNode.removeChild(olditem, true); | |
83726b9d DM |
333 | } |
334 | } | |
335 | ||
ff0777c5 TL |
336 | rstore.each(function(item) { // add new items |
337 | let olditem = index[item.data.id]; | |
83726b9d DM |
338 | if (olditem) { |
339 | return; | |
340 | } | |
83726b9d DM |
341 | if (filterfn && !filterfn(item)) { |
342 | return; | |
343 | } | |
ff0777c5 | 344 | let info = Ext.apply({ leaf: true }, item.data); |
83726b9d | 345 | |
ff0777c5 | 346 | let child = me.groupChild(rootnode, info, groups, 0); |
83726b9d DM |
347 | if (child) { |
348 | index[item.data.id] = child; | |
349 | } | |
350 | }); | |
351 | ||
0f3e4bc2 | 352 | store.resumeEvents(); |
cc3bcee0 | 353 | store.fireEvent('refresh', store); |
0f3e4bc2 | 354 | |
ff0777c5 | 355 | // select parent node if original selected node vanished |
83726b9d DM |
356 | if (lastsel && !rootnode.findChild('id', lastsel.data.id, true)) { |
357 | lastsel = rootnode; | |
ff0777c5 TL |
358 | for (const node of parents) { |
359 | if (rootnode.findChild('id', node.data.id, true)) { | |
360 | lastsel = node; | |
83726b9d DM |
361 | break; |
362 | } | |
363 | } | |
364 | me.selectById(lastsel.data.id); | |
d5a7996b DC |
365 | } else if (lastsel && reselect) { |
366 | me.selectById(lastsel.data.id); | |
83726b9d DM |
367 | } |
368 | ||
d0e96c0d | 369 | // on first tree load set the selection from the stateful provider |
83726b9d | 370 | if (!pdata.updateCount) { |
83726b9d DM |
371 | rootnode.expand(); |
372 | me.applyState(sp.get(stateid)); | |
373 | } | |
374 | ||
375 | pdata.updateCount++; | |
376 | }; | |
377 | ||
ff0777c5 | 378 | sp.on('statechange', (_sp, key, value) => { |
83726b9d DM |
379 | if (key === stateid) { |
380 | me.applyState(value); | |
381 | } | |
ff0777c5 | 382 | }); |
83726b9d DM |
383 | |
384 | Ext.apply(me, { | |
91994a49 | 385 | allowSelection: true, |
83726b9d DM |
386 | store: store, |
387 | viewConfig: { | |
ff0777c5 | 388 | animate: false, // note: animate cause problems with applyState |
83726b9d | 389 | }, |
83726b9d | 390 | listeners: { |
685b7aa4 | 391 | itemcontextmenu: PVE.Utils.createCmdMenu, |
83726b9d DM |
392 | destroy: function() { |
393 | rstore.un("load", updateTree); | |
91994a49 | 394 | }, |
8058410f | 395 | beforecellmousedown: function(tree, td, cellIndex, record, tr, rowIndex, ev) { |
ff0777c5 TL |
396 | let sm = me.getSelectionModel(); |
397 | // disable selection when right clicking except if the record is already selected | |
53e3ea84 | 398 | me.allowSelection = ev.button !== 2 || sm.isSelected(record); |
91994a49 | 399 | }, |
8058410f | 400 | beforeselect: function(tree, record, index, eopts) { |
ff0777c5 | 401 | let allow = me.allowSelection; |
91994a49 DC |
402 | me.allowSelection = true; |
403 | return allow; | |
e3129443 | 404 | }, |
f6710aac | 405 | itemdblclick: PVE.Utils.openTreeConsole, |
83726b9d DM |
406 | }, |
407 | setViewFilter: function(view) { | |
408 | me.viewFilter = view; | |
409 | me.clearTree(); | |
410 | updateTree(); | |
411 | }, | |
3d7b2aa9 | 412 | setDatacenterText: function(clustername) { |
ff0777c5 | 413 | let rootnode = me.store.getRootNode(); |
3d7b2aa9 | 414 | |
ff0777c5 | 415 | let rnodeText = gettext('Datacenter'); |
3d7b2aa9 TL |
416 | if (clustername !== undefined) { |
417 | rnodeText += ' (' + clustername + ')'; | |
418 | } | |
419 | ||
420 | rootnode.beginEdit(); | |
421 | rootnode.data.text = rnodeText; | |
422 | rootnode.commit(); | |
423 | }, | |
83726b9d DM |
424 | clearTree: function() { |
425 | pdata.updateCount = 0; | |
ff0777c5 | 426 | let rootnode = me.store.getRootNode(); |
83726b9d | 427 | rootnode.collapse(); |
08801a5d | 428 | rootnode.removeAll(); |
83726b9d DM |
429 | pdata.dataIndex = {}; |
430 | me.getSelectionModel().deselectAll(); | |
431 | }, | |
432 | selectExpand: function(node) { | |
ff0777c5 | 433 | let sm = me.getSelectionModel(); |
83726b9d DM |
434 | if (!sm.isSelected(node)) { |
435 | sm.select(node); | |
ff0777c5 TL |
436 | for (let iter = node; iter; iter = iter.parentNode) { |
437 | if (!iter.isExpanded()) { | |
438 | iter.expand(); | |
83726b9d DM |
439 | } |
440 | } | |
9f950723 | 441 | me.getView().focusRow(node); |
83726b9d DM |
442 | } |
443 | }, | |
444 | selectById: function(nodeid) { | |
ff0777c5 TL |
445 | let rootnode = me.store.getRootNode(); |
446 | let node; | |
83726b9d DM |
447 | if (nodeid === 'root') { |
448 | node = rootnode; | |
449 | } else { | |
450 | node = rootnode.findChild('id', nodeid, true); | |
451 | } | |
452 | if (node) { | |
453 | me.selectExpand(node); | |
454 | } | |
cfffc271 | 455 | return node; |
83726b9d | 456 | }, |
8058410f | 457 | applyState: function(state) { |
83726b9d DM |
458 | if (state && state.value) { |
459 | me.selectById(state.value); | |
460 | } else { | |
ff0777c5 | 461 | me.getSelectionModel().deselectAll(); |
83726b9d | 462 | } |
f6710aac | 463 | }, |
83726b9d DM |
464 | }); |
465 | ||
466 | me.callParent(); | |
467 | ||
ff0777c5 | 468 | me.getSelectionModel().on('select', (_sm, n) => sp.set(stateid, { value: n.data.id })); |
83726b9d DM |
469 | |
470 | rstore.on("load", updateTree); | |
471 | rstore.startUpdate(); | |
f6710aac | 472 | }, |
83726b9d DM |
473 | |
474 | }); |