From 03862a2eeb31de76f28a935881d16d90727238ac Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 5 Dec 2018 12:39:40 +0100 Subject: [PATCH] Utils.js: add code to make api3 request --- www/Makefile | 1 + www/Utils.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++ www/VersionInfo.js | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 www/Utils.js diff --git a/www/Makefile b/www/Makefile index 7260e1e9..ec41e3f8 100644 --- a/www/Makefile +++ b/www/Makefile @@ -1,4 +1,5 @@ JSSRC= \ + Utils.js \ Logo.js \ VersionInfo.js \ Application.js \ diff --git a/www/Utils.js b/www/Utils.js new file mode 100644 index 00000000..8841b0cb --- /dev/null +++ b/www/Utils.js @@ -0,0 +1,88 @@ +/*global Proxmox */ +Ext.ns('PBS'); + +console.log("Starting Backup Server GUI"); + +Ext.define('PBS.Utils', { + singleton: true, + + // Ext.Ajax.request + API3Request: function(reqOpts) { + + var newopts = Ext.apply({ + waitMsg: gettext('Please wait...') + }, reqOpts); + + if (!newopts.url.match(/^\/api3/)) { + newopts.url = '/api3/extjs' + newopts.url; + } + delete newopts.callback; + + var createWrapper = function(successFn, callbackFn, failureFn) { + Ext.apply(newopts, { + success: function(response, options) { + if (options.waitMsgTarget) { + if (Proxmox.Utils.toolkit === 'touch') { + options.waitMsgTarget.setMasked(false); + } else { + options.waitMsgTarget.setLoading(false); + } + } + var result = Ext.decode(response.responseText); + response.result = result; + if (!result.success) { + response.htmlStatus = Proxmox.Utils.extractRequestError(result, true); + Ext.callback(callbackFn, options.scope, [options, false, response]); + Ext.callback(failureFn, options.scope, [response, options]); + return; + } + Ext.callback(callbackFn, options.scope, [options, true, response]); + Ext.callback(successFn, options.scope, [response, options]); + }, + failure: function(response, options) { + if (options.waitMsgTarget) { + if (Proxmox.Utils.toolkit === 'touch') { + options.waitMsgTarget.setMasked(false); + } else { + options.waitMsgTarget.setLoading(false); + } + } + response.result = {}; + try { + response.result = Ext.decode(response.responseText); + } catch(e) {} + var msg = gettext('Connection error') + ' - server offline?'; + if (response.aborted) { + msg = gettext('Connection error') + ' - aborted.'; + } else if (response.timedout) { + msg = gettext('Connection error') + ' - Timeout.'; + } else if (response.status && response.statusText) { + msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText; + } + response.htmlStatus = msg; + Ext.callback(callbackFn, options.scope, [options, false, response]); + Ext.callback(failureFn, options.scope, [response, options]); + } + }); + }; + + createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure); + + var target = newopts.waitMsgTarget; + if (target) { + if (Proxmox.Utils.toolkit === 'touch') { + target.setMasked({ xtype: 'loadmask', message: newopts.waitMsg} ); + } else { + // Note: ExtJS bug - this does not work when component is not rendered + target.setLoading(newopts.waitMsg); + } + } + Ext.Ajax.request(newopts); + }, + + constructor: function() { + var me = this; + + // do whatever you want here + } +}); diff --git a/www/VersionInfo.js b/www/VersionInfo.js index 0112825c..a0298d33 100644 --- a/www/VersionInfo.js +++ b/www/VersionInfo.js @@ -21,7 +21,7 @@ Ext.define('PBS.view.main.VersionInfo',{ me.callParent(); if (me.makeApiCall) { - Proxmox.Utils.API2Request({ + PBS.Utils.API3Request({ url: '/version', method: 'GET', success: function(response) {