From 3d3e31b7f80be8dbb0404a4bd94f301206b108e1 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 25 Feb 2021 11:52:42 +0100 Subject: [PATCH] ui: factor out common code --- www/Utils.js | 173 ++++++++++++++++++++++++++++++++++ www/tape/ChangerStatus.js | 192 ++++---------------------------------- www/tape/DriveConfig.js | 138 +++++---------------------- 3 files changed, 211 insertions(+), 292 deletions(-) diff --git a/www/Utils.js b/www/Utils.js index 3ce119a7..9861d40f 100644 --- a/www/Utils.js +++ b/www/Utils.js @@ -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', { diff --git a/www/tape/ChangerStatus.js b/www/tape/ChangerStatus.js index 2c814340..1a7bcf6a 100644 --- a/www/tape/ChangerStatus.js +++ b/www/tape/ChangerStatus.js @@ -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, }); }, diff --git a/www/tape/DriveConfig.js b/www/tape/DriveConfig.js index 0a28b089..7433f37c 100644 --- a/www/tape/DriveConfig.js +++ b/www/tape/DriveConfig.js @@ -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('
'); - - 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('
'); - - 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, }); },