ui: nav tree: code cleanup and unification between datastore and tapes

datastore and tape entries are very similar but differ in some points
in such a way that a nice unification is not really that helpful, but
making similar key parts the same is still nice when reading the code

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2021-04-12 14:09:29 +02:00
parent 0417e9af1b
commit 94b7f56e65
1 changed files with 26 additions and 28 deletions

View File

@ -145,9 +145,9 @@ Ext.define('PBS.view.main.NavigationTree', {
let root = view.getStore().getRoot(); let root = view.getStore().getRoot();
records.sort((a, b) => a.data.name.localeCompare(b.data.name)); records.sort((a, b) => a.data.name.localeCompare(b.data.name));
let list = root.findChild('id', 'tape_management', false);
let newSet = {};
let list = root.findChild('id', 'tape_management', false);
let existingChildren = {};
for (const drive of records) { for (const drive of records) {
let path, text, iconCls; let path, text, iconCls;
if (drive.data.changer !== undefined) { if (drive.data.changer !== undefined) {
@ -159,7 +159,7 @@ Ext.define('PBS.view.main.NavigationTree', {
path = `Drive-${text}`; path = `Drive-${text}`;
iconCls = 'pbs-icon-tape-drive'; iconCls = 'pbs-icon-tape-drive';
} }
newSet[path] = { existingChildren[path] = {
text, text,
path, path,
iconCls, iconCls,
@ -167,7 +167,7 @@ Ext.define('PBS.view.main.NavigationTree', {
}; };
} }
let paths = Object.keys(newSet).sort(); let paths = Object.keys(existingChildren).sort();
let oldIdx = 0; let oldIdx = 0;
for (let newIdx = 0; newIdx < paths.length; newIdx++) { for (let newIdx = 0; newIdx < paths.length; newIdx++) {
@ -178,17 +178,17 @@ Ext.define('PBS.view.main.NavigationTree', {
} }
if (oldIdx >= list.childNodes.length || list.getChildAt(oldIdx).data.path !== newPath) { if (oldIdx >= list.childNodes.length || list.getChildAt(oldIdx).data.path !== newPath) {
list.insertChild(oldIdx, newSet[newPath]); list.insertChild(oldIdx, existingChildren[newPath]);
} }
} }
let toremove = []; let toRemove = [];
list.eachChild((child) => { list.eachChild((child) => {
if (!newSet[child.data.path]) { if (!existingChildren[child.data.path]) {
toremove.push(child); toRemove.push(child);
} }
}); });
toremove.forEach((child) => list.removeChild(child, true)); toRemove.forEach((child) => list.removeChild(child, true));
if (view.pathToSelect !== undefined) { if (view.pathToSelect !== undefined) {
let path = view.pathToSelect; let path = view.pathToSelect;
@ -198,27 +198,26 @@ Ext.define('PBS.view.main.NavigationTree', {
}, },
onLoad: function(store, records, success) { onLoad: function(store, records, success) {
if (!success) return; if (!success) {
var view = this.getView(); return;
}
let view = this.getView();
let root = view.getStore().getRoot(); let root = view.getStore().getRoot();
records.sort((a, b) => a.id.localeCompare(b.id)); records.sort((a, b) => a.id.localeCompare(b.id));
var list = root.findChild('id', 'datastores', false); let list = root.findChild('id', 'datastores', false);
var length = records.length; let getChildTextAt = i => list.getChildAt(i).data.text;
var lookup_hash = {}; let existingChildren = {};
let j = 0; for (let i = 0, j = 0, length = records.length; i < length; i++) {
for (let i = 0; i < length; i++) {
let name = records[i].id; let name = records[i].id;
lookup_hash[name] = true; existingChildren[name] = true;
while (name.localeCompare(list.getChildAt(j).data.text) > 0 && while (name.localeCompare(getChildTextAt(j)) > 0 && (j+1) < list.childNodes.length) {
(j + 1) < list.childNodes.length) {
j++; j++;
} }
if (list.getChildAt(j).data.text.localeCompare(name) !== 0) { if (getChildTextAt(j).localeCompare(name) !== 0) {
list.insertChild(j, { list.insertChild(j, {
text: name, text: name,
path: `DataStore-${name}`, path: `DataStore-${name}`,
@ -228,15 +227,14 @@ Ext.define('PBS.view.main.NavigationTree', {
} }
} }
var erase_list = []; // remove entries which are not existing anymore
list.eachChild(function(node) { let toRemove = [];
let name = node.data.text; list.eachChild(child => {
if (!lookup_hash[name] && node.data.id !== 'addbutton') { if (!existingChildren[child.data.text] && child.data.id !== 'addbutton') {
erase_list.push(node); toRemove.push(child);
} }
}); });
toRemove.forEach(child => list.removeChild(child, true));
Ext.Array.forEach(erase_list, function(node) { list.removeChild(node, true); });
if (view.pathToSelect !== undefined) { if (view.pathToSelect !== undefined) {
let path = view.pathToSelect; let path = view.pathToSelect;