ui: factor out common code

This commit is contained in:
Dietmar Maurer 2021-02-25 11:52:42 +01:00
parent 8730cfcc3e
commit 3d3e31b7f8
3 changed files with 211 additions and 292 deletions

View File

@ -428,6 +428,179 @@ Ext.define('PBS.Utils', {
.map(val => val.charCodeAt(0)),
);
},
driveCommand: function(driveid, command, reqOpts) {
let params = Ext.apply(reqOpts, {
url: `/api2/extjs/tape/drive/${driveid}/${command}`,
timeout: 5*60*1000,
failure: function(response) {
Ext.Msg.alert(gettext('Error'), response.htmlStatus);
},
});
Proxmox.Utils.API2Request(params);
},
showMediaLabelWindow: function(response) {
let list = [];
for (let [key, val] of Object.entries(response.result.data)) {
if (key === 'ctime' || key === 'media-set-ctime') {
val = Proxmox.Utils.render_timestamp(val);
}
list.push({ key: key, value: val });
}
Ext.create('Ext.window.Window', {
title: gettext('Label Information'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: list,
},
columns: [
{
text: gettext('Property'),
dataIndex: 'key',
width: 120,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
},
showCartridgeMemoryWindow: function(response) {
Ext.create('Ext.window.Window', {
title: gettext('Cartridge Memory'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: response.result.data,
},
columns: [
{
text: gettext('ID'),
dataIndex: 'id',
width: 60,
},
{
text: gettext('Name'),
dataIndex: 'name',
flex: 2,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
},
showVolumeStatisticsWindow: function(response) {
let list = [];
for (let [key, val] of Object.entries(response.result.data)) {
if (key === 'total-native-capacity' ||
key === 'total-used-native-capacity' ||
key === 'lifetime-bytes-read' ||
key === 'lifetime-bytes-written' ||
key === 'last-mount-bytes-read' ||
key === 'last-mount-bytes-written') {
val = Proxmox.Utils.format_size(val);
}
list.push({ key: key, value: val });
}
Ext.create('Ext.window.Window', {
title: gettext('Volume Statistics'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: list,
},
columns: [
{
text: gettext('Property'),
dataIndex: 'key',
flex: 1,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
},
showDriveStatusWindow: function(response) {
let list = [];
for (let [key, val] of Object.entries(response.result.data)) {
if (key === 'manufactured') {
val = Proxmox.Utils.render_timestamp(val);
}
if (key === 'bytes-read' || key === 'bytes-written') {
val = Proxmox.Utils.format_size(val);
}
list.push({ key: key, value: val });
}
Ext.create('Ext.window.Window', {
title: gettext('Status'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: list,
},
columns: [
{
text: gettext('Property'),
dataIndex: 'key',
width: 120,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
},
});
Ext.define('PBS.Async', {

View File

@ -178,207 +178,49 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
me.reload();
},
driveCommand: function(driveid, command, callback, params, method) {
let me = this;
let view = me.getView();
params = params || {};
method = method || 'GET';
Proxmox.Utils.API2Request({
url: `/api2/extjs/tape/drive/${driveid}/${command}`,
timeout: 5*60*1000,
method,
waitMsgTarget: view,
params,
success: function(response) {
callback(response);
},
failure: function(response) {
Ext.Msg.alert(gettext('Error'), response.htmlStatus);
},
});
},
cartridgeMemory: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'cartridge-memory', function(response) {
Ext.create('Ext.window.Window', {
title: gettext('Cartridge Memory'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: response.result.data,
},
columns: [
{
text: gettext('ID'),
dataIndex: 'id',
width: 60,
},
{
text: gettext('Name'),
dataIndex: 'name',
flex: 2,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
PBS.Utils.driveCommand(drive, 'cartridge-memory', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showCartridgeMemoryWindow,
});
},
cleanDrive: function(view, rI, cI, button, el, record) {
let me = this;
me.driveCommand(record.data.name, 'clean', function(response) {
me.reload();
}, {}, 'PUT');
PBS.Utils.driveCommand(record.data.name, 'clean', {
waitMsgTarget: me.getView(),
callback: me.reload,
method: 'PUT',
});
},
volumeStatistics: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'volume-statistics', function(response) {
let list = [];
for (let [key, val] of Object.entries(response.result.data)) {
if (key === 'total-native-capacity' ||
key === 'total-used-native-capacity' ||
key === 'lifetime-bytes-read' ||
key === 'lifetime-bytes-written' ||
key === 'last-mount-bytes-read' ||
key === 'last-mount-bytes-written')
{
val = Proxmox.Utils.format_size(val);
}
list.push({ key: key, value: val });
}
Ext.create('Ext.window.Window', {
title: gettext('Volume Statistics'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: list,
},
columns: [
{
text: gettext('Property'),
dataIndex: 'key',
flex: 1,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
PBS.Utils.driveCommand(drive, 'volume-statistics', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showVolumeStatisticsWindow,
});
},
readLabel: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'read-label', function(response) {
let list = [];
for (let [key, val] of Object.entries(response.result.data)) {
if (key === 'ctime' || key === 'media-set-ctime') {
val = Proxmox.Utils.render_timestamp(val);
}
list.push({ key: key, value: val });
}
Ext.create('Ext.window.Window', {
title: gettext('Label Information'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: list,
},
columns: [
{
text: gettext('Property'),
dataIndex: 'key',
width: 120,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
PBS.Utils.driveCommand(drive, 'read-label', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showMediaLabelWindow,
});
},
status: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'status', function(response) {
let list = [];
for (let [key, val] of Object.entries(response.result.data)) {
if (key === 'manufactured') {
val = Proxmox.Utils.render_timestamp(val);
}
if (key === 'bytes-read' || key === 'bytes-written') {
val = Proxmox.Utils.format_size(val);
}
list.push({ key: key, value: val });
}
Ext.create('Ext.window.Window', {
title: gettext('Status'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: list,
},
columns: [
{
text: gettext('Property'),
dataIndex: 'key',
width: 120,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
PBS.Utils.driveCommand(drive, 'status', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showDriveStatusWindow,
});
},

View File

@ -41,147 +41,51 @@ Ext.define('PBS.TapeManagement.DrivePanel', {
status: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'status', function(response) {
let lines = [];
for (const [key, val] of Object.entries(response.result.data)) {
lines.push(`${key}: ${val}`);
}
let txt = lines.join('<br>');
Ext.Msg.show({
title: gettext('Label Information'),
message: txt,
icon: undefined,
});
PBS.Utils.driveCommand(drive, 'status', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showDriveStatusWindow,
});
},
catalog: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'catalog', function(response) {
Ext.create('Proxmox.window.TaskViewer', {
upid: response.result.data,
}).show();
}, {}, 'POST');
PBS.Utils.driveCommand(drive, 'catalog', {
waitMsgTarget: me.getView(),
method: 'POST',
success: function(response) {
Ext.create('Proxmox.window.TaskViewer', {
upid: response.result.data,
}).show();
},
});
},
readLabel: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'read-label', function(response) {
let lines = [];
for (const [key, val] of Object.entries(response.result.data)) {
lines.push(`${key}: ${val}`);
}
let txt = lines.join('<br>');
Ext.Msg.show({
title: gettext('Label Information'),
message: txt,
icon: undefined,
});
PBS.Utils.driveCommand(drive, 'read-label', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showMediaLabelWindow,
});
},
volumeStatistics: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'volume-statistics', function(response) {
Ext.create('Ext.window.Window', {
title: gettext('Volume Statistics'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: response.result.data,
},
columns: [
{
text: gettext('ID'),
dataIndex: 'id',
width: 60,
},
{
text: gettext('Name'),
dataIndex: 'name',
flex: 2,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
PBS.Utils.driveCommand(drive, 'volume-statistics', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showVolumeStatisticsWindow,
});
},
cartridgeMemory: function(view, rI, cI, button, el, record) {
let me = this;
let drive = record.data.name;
me.driveCommand(drive, 'cartridge-memory', function(response) {
Ext.create('Ext.window.Window', {
title: gettext('Cartridge Memory'),
modal: true,
width: 600,
height: 450,
layout: 'fit',
scrollable: true,
items: [
{
xtype: 'grid',
store: {
data: response.result.data,
},
columns: [
{
text: gettext('ID'),
dataIndex: 'id',
width: 60,
},
{
text: gettext('Name'),
dataIndex: 'name',
flex: 2,
},
{
text: gettext('Value'),
dataIndex: 'value',
flex: 1,
},
],
},
],
}).show();
});
},
driveCommand: function(driveid, command, callback, params, method) {
let me = this;
let view = me.getView();
params = params || {};
method = method || 'GET';
Proxmox.Utils.API2Request({
url: `/api2/extjs/tape/drive/${driveid}/${command}`,
method,
waitMsgTarget: view,
params,
success: function(response) {
callback(response);
},
failure: function(response) {
Ext.Msg.alert(gettext('Error'), response.htmlStatus);
},
PBS.Utils.driveCommand(drive, 'cartridge-memory', {
waitMsgTarget: me.getView(),
success: PBS.Utils.showCartridgeMemoryWindow,
});
},