From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9534C9157D for ; Wed, 1 Feb 2023 16:49:50 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 76BC42CB1 for ; Wed, 1 Feb 2023 16:49:20 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 1 Feb 2023 16:49:19 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BCC2745A70 for ; Wed, 1 Feb 2023 16:49:18 +0100 (CET) From: Dominik Csapak To: pve-devel@lists.proxmox.com Date: Wed, 1 Feb 2023 16:49:17 +0100 Message-Id: <20230201154917.1632212-5-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201154917.1632212-1-d.csapak@proxmox.com> References: <20230201154917.1632212-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.062 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH manager 3/3] fix #1408: ui: ResourceTree: sort the tree according to tree-sorting options X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Feb 2023 15:49:50 -0000 Considers the newly added options from both browser local storage(preferred) or from the datacenter config we have to save the last sorting mechanism there, so we can detect if it changes and trigger the movement/text changes (otherwise the tree nodes won't be updated/moved) Signed-off-by: Dominik Csapak --- www/manager6/Utils.js | 12 +++++++ www/manager6/tree/ResourceTree.js | 54 ++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js index 0c57e0844..14f03e6d1 100644 --- a/www/manager6/Utils.js +++ b/www/manager6/Utils.js @@ -1930,6 +1930,18 @@ Ext.define('PVE.Utils', { Ext.GlobalEvents.fireEvent('loadedUiOptions'); }, + getTreeSortingValue: function(key) { + let sp = Ext.state.Manager.getProvider(); + let browserValues = sp.get('pve-tree-sorting'); + let defaults = { + 'sort-field': 'vmid', + 'group-templates': true, + 'group-guest-types': true, + }; + + return browserValues?.[key] ?? PVE.UIOptions?.['tree-sorting']?.[key] ?? defaults[key]; + }, + tagTreeStyles: { '__default__': `${Proxmox.Utils.defaultText} (${gettext('Circle')})`, 'full': gettext('Full'), diff --git a/www/manager6/tree/ResourceTree.js b/www/manager6/tree/ResourceTree.js index 5c92d4128..5456a7eda 100644 --- a/www/manager6/tree/ResourceTree.js +++ b/www/manager6/tree/ResourceTree.js @@ -44,24 +44,34 @@ Ext.define('PVE.tree.ResourceTree', { // private nodeSortFn: function(node1, node2) { + let me = this; let n1 = node1.data, n2 = node2.data; if (!n1.groupbyid === !n2.groupbyid) { - // first sort (group) by type - if (n1.type > n2.type) { - return 1; - } else if (n1.type < n2.type) { - return -1; + let n1IsGuest = n1.type === 'qemu' || n1.type === 'lxc'; + let n2IsGuest = n2.type === 'qemu' || n2.type === 'lxc'; + if (me['group-guest-types'] || !n1IsGuest || !n2IsGuest) { + // first sort (group) by type + if (n1.type > n2.type) { + return 1; + } else if (n1.type < n2.type) { + return -1; + } } + // then sort (group) by ID - if (n1.type === 'qemu' || n2.type === 'lxc') { - if (!n1.template !== !n2.template) { + if (n1IsGuest) { + if (me['group-templates'] && (!n1.template !== !n2.template)) { return n1.template ? 1 : -1; // sort templates after regular VMs } - if (n1.vmid > n2.vmid) { // prefer VMID as metric for guests - return 1; - } else if (n1.vmid < n2.vmid) { - return -1; + if (me['sort-field'] === 'vmid') { + if (n1.vmid > n2.vmid) { // prefer VMID as metric for guests + return 1; + } else if (n1.vmid < n2.vmid) { + return -1; + } + } else { + return n1.name.localeCompare(n2.name); } } // same types but not a guest @@ -115,6 +125,11 @@ Ext.define('PVE.tree.ResourceTree', { status += ' '; } } + if (Ext.isNumeric(info.vmid) && info.vmid > 0) { + if (PVE.Utils.getTreeSortingValue('sort-field') !== 'vmid') { + info.text = `${info.name} (${String(info.vmid)})`; + } + } info.text += PVE.Utils.renderTags(info.tags, PVE.Utils.tagOverrides); @@ -203,8 +218,22 @@ Ext.define('PVE.tree.ResourceTree', { return me.addChildSorted(node, info); }, + saveSortingOptions: function() { + let me = this; + let changed = false; + for (const key of ['sort-field', 'group-templates', 'group-guest-types']) { + let newValue = PVE.Utils.getTreeSortingValue(key); + if (me[key] !== newValue) { + me[key] = newValue; + changed = true; + } + } + return changed; + }, + initComponent: function() { let me = this; + me.saveSortingOptions(); let rstore = PVE.data.ResourceStore; let sp = Ext.state.Manager.getProvider(); @@ -242,6 +271,7 @@ Ext.define('PVE.tree.ResourceTree', { let sm = me.getSelectionModel(); let lastsel = sm.getSelection()[0]; let parents = []; + let sorting_changed = me.saveSortingOptions(); for (let node = lastsel; node; node = node.parentNode) { parents.push(node); } @@ -258,7 +288,7 @@ Ext.define('PVE.tree.ResourceTree', { // getById() use find(), which is slow (ExtJS4 DP5) let item = rstore.data.get(olditem.data.id); - let changed = false, moved = false; + let changed = sorting_changed, moved = sorting_changed; if (item) { // test if any grouping attributes changed, catches migrated tree-nodes in server view too for (const attr of moveCheckAttrs) { -- 2.30.2