ui: rework datastore content panel controller

Mostly refactoring, but actually fixes an issue where one seldom run
into a undefined dereference due to the store onLoad callback getting
triggered after some of the componet was destroyed - on quick
switching through the datastores.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2020-05-26 12:46:45 +02:00
parent 40dc103103
commit 3f98b34705

View File

@ -29,34 +29,44 @@ Ext.define('PBS.DataStoreContent', {
throw "no datastore specified"; throw "no datastore specified";
} }
this.data_store = Ext.create('Ext.data.Store', { this.store = Ext.create('Ext.data.Store', {
model: 'pbs-data-store-snapshots', model: 'pbs-data-store-snapshots',
sorters: 'backup-group', sorters: 'backup-group',
groupField: 'backup-group', groupField: 'backup-group',
}); });
this.store.on('load', this.onLoad, this);
Proxmox.Utils.monStoreErrors(view, view.store, true); Proxmox.Utils.monStoreErrors(view, view.store, true);
this.reload(); // initial load this.reload(); // initial load
}, },
reload: function() { reload: function() {
var view = this.getView(); let view = this.getView();
if (!view.store || !this.store) {
console.warn('cannot reload, no store(s)');
return;
}
let url = `/api2/json/admin/datastore/${view.datastore}/snapshots`; let url = `/api2/json/admin/datastore/${view.datastore}/snapshots`;
this.data_store.setProxy({ this.store.setProxy({
type: 'proxmox', type: 'proxmox',
url: url url: url
}); });
this.data_store.load(function(records, operation, success) { this.store.load();
},
getRecordGroups: function(records) {
let groups = {}; let groups = {};
records.forEach(function(item) { for (const item of records) {
var btype = item.data["backup-type"]; var btype = item.data["backup-type"];
let group = btype + "/" + item.data["backup-id"]; let group = btype + "/" + item.data["backup-id"];
if (groups[group] !== undefined) if (groups[group] !== undefined) {
return; continue;
}
var cls = ''; var cls = '';
if (btype === 'vm') { if (btype === 'vm') {
@ -66,7 +76,8 @@ Ext.define('PBS.DataStoreContent', {
} else if (btype === 'host') { } else if (btype === 'host') {
cls = 'fa-building'; cls = 'fa-building';
} else { } else {
return btype + '/' + value; console.warn(`got unkown backup-type '${btype}'`);
continue; // FIXME: auto render? what do?
} }
groups[group] = { groups[group] = {
@ -78,15 +89,22 @@ Ext.define('PBS.DataStoreContent', {
backup_id: item.data["backup-id"], backup_id: item.data["backup-id"],
children: [] children: []
}; };
}); }
return groups;
},
onLoad: function(store, records, success) {
let view = this.getView();
if (!success) {
return;
}
let groups = this.getRecordGroups(records);
let backup_time_to_string = function(backup_time) { let backup_time_to_string = function(backup_time) {
let pad = function(number) { let pad = (number) => number < 10 ? '0' + number : number;
if (number < 10) {
return '0' + number;
}
return number;
};
return backup_time.getUTCFullYear() + return backup_time.getUTCFullYear() +
'-' + pad(backup_time.getUTCMonth() + 1) + '-' + pad(backup_time.getUTCMonth() + 1) +
'-' + pad(backup_time.getUTCDate()) + '-' + pad(backup_time.getUTCDate()) +
@ -96,7 +114,7 @@ Ext.define('PBS.DataStoreContent', {
'Z'; 'Z';
}; };
records.forEach(function(item) { for (const item of records) {
let group = item.data["backup-type"] + "/" + item.data["backup-id"]; let group = item.data["backup-type"] + "/" + item.data["backup-id"];
let children = groups[group].children; let children = groups[group].children;
@ -108,30 +126,27 @@ Ext.define('PBS.DataStoreContent', {
data.cls = 'no-leaf-icons'; data.cls = 'no-leaf-icons';
children.push(data); children.push(data);
}); }
let children = []; let children = [];
Ext.Object.each(groups, function(key, group) { for (const [_key, group] of Object.entries(groups)) {
let last_backup = 0; let last_backup = 0;
group.children.forEach(function(item) { for (const item of group.children) {
if (item["backup-time"] > last_backup) { if (item["backup-time"] > last_backup) {
last_backup = item["backup-time"]; last_backup = item["backup-time"];
group["backup-time"] = last_backup; group["backup-time"] = last_backup;
group.files = item.files; group.files = item.files;
group.size = item.size; group.size = item.size;
} }
}); }
group.count = group.children.length; group.count = group.children.length;
children.push(group) children.push(group);
}) }
view.setRootNode({ view.setRootNode({
expanded: true, expanded: true,
children: children children: children
}); });
});
}, },
onPrune: function() { onPrune: function() {