X-Git-Url: https://git.proxmox.com/?p=proxmox-widget-toolkit.git;a=blobdiff_plain;f=Utils.js;h=645c5250cf64803e280b466b9cd04309291f81f4;hp=1aff39f64f33fde078a62c8463fea8cfee5b6888;hb=72be386c64b6f30ace3ef6ecae5ddce627d707a7;hpb=28e54f37dfbd8e1bbd7172d245f315aface98857 diff --git a/Utils.js b/Utils.js index 1aff39f..645c525 100644 --- a/Utils.js +++ b/Utils.js @@ -1,10 +1,6 @@ Ext.ns('Proxmox'); Ext.ns('Proxmox.Setup'); -// TODO: implement gettext -function gettext(buf) { return buf; } - - if (!Ext.isDefined(Proxmox.Setup.auth_cookie_name)) { throw "Proxmox library not initialized"; } @@ -60,6 +56,54 @@ Ext.define('Proxmox.Utils', { utilities: { stateText: gettext('State'), groupText: gettext('Group'), + language_map: { + zh_CN: 'Chinese (Simplified)', + zh_TW: 'Chinese (Traditional)', + ca: 'Catalan', + da: 'Danish', + en: 'English', + eu: 'Euskera (Basque)', + fr: 'French', + de: 'German', + it: 'Italian', + es: 'Spanish', + ja: 'Japanese', + nb: 'Norwegian (Bokmal)', + nn: 'Norwegian (Nynorsk)', + fa: 'Persian (Farsi)', + pl: 'Polish', + pt_BR: 'Portuguese (Brazil)', + ru: 'Russian', + sl: 'Slovenian', + sv: 'Swedish', + tr: 'Turkish' + }, + + render_language: function (value) { + if (!value) { + return Proxmox.Utils.defaultText + ' (English)'; + } + var text = Proxmox.Utils.language_map[value]; + if (text) { + return text + ' (' + value + ')'; + } + return value; + }, + + language_array: function() { + var data = [['__default__', Proxmox.Utils.render_language('')]]; + Ext.Object.each(Proxmox.Utils.language_map, function(key, value) { + data.push([key, Proxmox.Utils.render_language(value)]); + }); + + return data; + }, + + getNoSubKeyHtml: function(url) { + // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans + return Ext.String.format('You do not have a valid subscription for this server. Please visit www.proxmox.com to get a list of available options.', url || 'http://www.proxmox.com'); + }, + format_boolean_with_default: function(value) { if (Ext.isDefined(value) && value !== '__default__') { return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText; @@ -79,6 +123,52 @@ Ext.define('Proxmox.Utils', { utilities: { return value ? Proxmox.Utils.enabledText : Proxmox.Utils.disabledText; }, + format_expire: function(date) { + if (!date) { + return Proxmox.Utils.neverText; + } + return Ext.Date.format(date, "Y-m-d"); + }, + + format_duration_long: function(ut) { + + var days = Math.floor(ut / 86400); + ut -= days*86400; + var hours = Math.floor(ut / 3600); + ut -= hours*3600; + var mins = Math.floor(ut / 60); + ut -= mins*60; + + var hours_str = '00' + hours.toString(); + hours_str = hours_str.substr(hours_str.length - 2); + var mins_str = "00" + mins.toString(); + mins_str = mins_str.substr(mins_str.length - 2); + var ut_str = "00" + ut.toString(); + ut_str = ut_str.substr(ut_str.length - 2); + + if (days) { + var ds = days > 1 ? Proxmox.Utils.daysText : Proxmox.Utils.dayText; + return days.toString() + ' ' + ds + ' ' + + hours_str + ':' + mins_str + ':' + ut_str; + } else { + return hours_str + ':' + mins_str + ':' + ut_str; + } + }, + + format_subscription_level: function(level) { + if (level === 'c') { + return 'Community'; + } else if (level === 'b') { + return 'Basic'; + } else if (level === 's') { + return 'Standard'; + } else if (level === 'p') { + return 'Premium'; + } else { + return Proxmox.Utils.noneText; + } + }, + compute_min_label_width: function(text, width) { if (width === undefined) { width = 100; } @@ -89,11 +179,26 @@ Ext.define('Proxmox.Utils', { utilities: { return min < width ? width : min; }, + setAuthData: function(data) { + Proxmox.CSRFPreventionToken = data.CSRFPreventionToken; + Proxmox.UserName = data.username; + Proxmox.LoggedOut = data.LoggedOut; + // creates a session cookie (expire = null) + // that way the cookie gets deleted after the browser window is closed + Ext.util.Cookies.set(Proxmox.Setup.auth_cookie_name, data.ticket, null, '/', null, true); + }, + authOK: function() { + if (Proxmox.LoggedOut) { + return undefined; + } return (Proxmox.UserName !== '') && Ext.util.Cookies.get(Proxmox.Setup.auth_cookie_name); }, authClear: function() { + if (Proxmox.LoggedOut) { + return undefined; + } Ext.util.Cookies.clear(Proxmox.Setup.auth_cookie_name); }, @@ -115,13 +220,19 @@ Ext.define('Proxmox.Utils', { utilities: { } }, - monStoreErrors: function(me, store) { - me.mon(store, 'beforeload', function(s, operation, eOpts) { - if (!me.loadCount) { - me.loadCount = 0; // make sure it is numeric - Proxmox.Utils.setErrorMask(me, true); - } - }); + monStoreErrors: function(me, store, clearMaskBeforeLoad) { + if (clearMaskBeforeLoad) { + me.mon(store, 'beforeload', function(s, operation, eOpts) { + Proxmox.Utils.setErrorMask(me, false); + }); + } else { + me.mon(store, 'beforeload', function(s, operation, eOpts) { + if (!me.loadCount) { + me.loadCount = 0; // make sure it is numeric + Proxmox.Utils.setErrorMask(me, true); + } + }); + } // only works with 'proxmox' proxy me.mon(store.proxy, 'afterload', function(proxy, request, success) { @@ -184,7 +295,11 @@ Ext.define('Proxmox.Utils', { utilities: { Ext.apply(newopts, { success: function(response, options) { if (options.waitMsgTarget) { - options.waitMsgTarget.setLoading(false); + if (Proxmox.Utils.toolkit === 'touch') { + options.waitMsgTarget.setMasked(false); + } else { + options.waitMsgTarget.setLoading(false); + } } var result = Ext.decode(response.responseText); response.result = result; @@ -199,7 +314,11 @@ Ext.define('Proxmox.Utils', { utilities: { }, failure: function(response, options) { if (options.waitMsgTarget) { - options.waitMsgTarget.setLoading(false); + if (Proxmox.Utils.toolkit === 'touch') { + options.waitMsgTarget.setMasked(false); + } else { + options.waitMsgTarget.setLoading(false); + } } response.result = {}; try { @@ -224,12 +343,47 @@ Ext.define('Proxmox.Utils', { utilities: { var target = newopts.waitMsgTarget; if (target) { - // Note: ExtJS bug - this does not work when component is not rendered - target.setLoading(newopts.waitMsg); + 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); }, + checked_command: function(orig_cmd) { + Proxmox.Utils.API2Request({ + url: '/nodes/localhost/subscription', + method: 'GET', + //waitMsgTarget: me, + failure: function(response, opts) { + Ext.Msg.alert(gettext('Error'), response.htmlStatus); + }, + success: function(response, opts) { + var data = response.result.data; + + if (data.status !== 'Active') { + Ext.Msg.show({ + title: gettext('No valid subscription'), + icon: Ext.Msg.WARNING, + msg: Proxmox.Utils.getNoSubKeyHtml(data.url), + buttons: Ext.Msg.OK, + callback: function(btn) { + if (btn !== 'ok') { + return; + } + orig_cmd(); + } + }); + } else { + orig_cmd(); + } + } + }); + }, + assemble_field_data: function(values, data) { if (Ext.isObject(data)) { Ext.Object.each(data, function(name, val) { @@ -266,6 +420,7 @@ Ext.define('Proxmox.Utils', { utilities: { eth: gettext("Network Device"), bridge: 'Linux Bridge', bond: 'Linux Bond', + vlan: 'Linux VLAN', OVSBridge: 'OVS Bridge', OVSBond: 'OVS Bond', OVSPort: 'OVS Port', @@ -277,9 +432,118 @@ Ext.define('Proxmox.Utils', { utilities: { Proxmox.Utils.unknownText; }, - // you can override this to provide nicer task descriptions + task_desc_table: { + acmenewcert: [ 'SRV', gettext('Order Certificate') ], + acmeregister: [ 'ACME Account', gettext('Register') ], + acmedeactivate: [ 'ACME Account', gettext('Deactivate') ], + acmeupdate: [ 'ACME Account', gettext('Update') ], + acmerefresh: [ 'ACME Account', gettext('Refresh') ], + acmerenew: [ 'SRV', gettext('Renew Certificate') ], + acmerevoke: [ 'SRV', gettext('Revoke Certificate') ], + 'move_volume': [ 'CT', gettext('Move Volume') ], + clustercreate: [ '', gettext('Create Cluster') ], + clusterjoin: [ '', gettext('Join Cluster') ], + diskinit: [ 'Disk', gettext('Initialize Disk with GPT') ], + vncproxy: [ 'VM/CT', gettext('Console') ], + spiceproxy: [ 'VM/CT', gettext('Console') + ' (Spice)' ], + vncshell: [ '', gettext('Shell') ], + spiceshell: [ '', gettext('Shell') + ' (Spice)' ], + qmsnapshot: [ 'VM', gettext('Snapshot') ], + qmrollback: [ 'VM', gettext('Rollback') ], + qmdelsnapshot: [ 'VM', gettext('Delete Snapshot') ], + qmcreate: [ 'VM', gettext('Create') ], + qmrestore: [ 'VM', gettext('Restore') ], + qmdestroy: [ 'VM', gettext('Destroy') ], + qmigrate: [ 'VM', gettext('Migrate') ], + qmclone: [ 'VM', gettext('Clone') ], + qmmove: [ 'VM', gettext('Move disk') ], + qmtemplate: [ 'VM', gettext('Convert to template') ], + qmstart: [ 'VM', gettext('Start') ], + qmstop: [ 'VM', gettext('Stop') ], + qmreset: [ 'VM', gettext('Reset') ], + qmshutdown: [ 'VM', gettext('Shutdown') ], + qmsuspend: [ 'VM', gettext('Hibernate') ], + qmpause: [ 'VM', gettext('Pause') ], + qmresume: [ 'VM', gettext('Resume') ], + qmconfig: [ 'VM', gettext('Configure') ], + vzsnapshot: [ 'CT', gettext('Snapshot') ], + vzrollback: [ 'CT', gettext('Rollback') ], + vzdelsnapshot: [ 'CT', gettext('Delete Snapshot') ], + vzcreate: ['CT', gettext('Create') ], + vzrestore: ['CT', gettext('Restore') ], + vzdestroy: ['CT', gettext('Destroy') ], + vzmigrate: [ 'CT', gettext('Migrate') ], + vzclone: [ 'CT', gettext('Clone') ], + vztemplate: [ 'CT', gettext('Convert to template') ], + vzstart: ['CT', gettext('Start') ], + vzstop: ['CT', gettext('Stop') ], + vzmount: ['CT', gettext('Mount') ], + vzumount: ['CT', gettext('Unmount') ], + vzshutdown: ['CT', gettext('Shutdown') ], + vzsuspend: [ 'CT', gettext('Suspend') ], + vzresume: [ 'CT', gettext('Resume') ], + hamigrate: [ 'HA', gettext('Migrate') ], + hastart: [ 'HA', gettext('Start') ], + hastop: [ 'HA', gettext('Stop') ], + srvstart: ['SRV', gettext('Start') ], + srvstop: ['SRV', gettext('Stop') ], + srvrestart: ['SRV', gettext('Restart') ], + srvreload: ['SRV', gettext('Reload') ], + cephcreatemgr: ['Ceph Manager', gettext('Create') ], + cephdestroymgr: ['Ceph Manager', gettext('Destroy') ], + cephcreatemon: ['Ceph Monitor', gettext('Create') ], + cephdestroymon: ['Ceph Monitor', gettext('Destroy') ], + cephcreateosd: ['Ceph OSD', gettext('Create') ], + cephdestroyosd: ['Ceph OSD', gettext('Destroy') ], + cephcreatepool: ['Ceph Pool', gettext('Create') ], + cephdestroypool: ['Ceph Pool', gettext('Destroy') ], + cephfscreate: ['CephFS', gettext('Create') ], + cephcreatemds: ['Ceph Metadata Server', gettext('Create') ], + cephdestroymds: ['Ceph Metadata Server', gettext('Destroy') ], + imgcopy: ['', gettext('Copy data') ], + imgdel: ['', gettext('Erase data') ], + unknownimgdel: ['', gettext('Destroy image from unknown guest') ], + download: ['', gettext('Download') ], + vzdump: ['VM/CT', gettext('Backup') ], + aptupdate: ['', gettext('Update package database') ], + startall: [ '', gettext('Start all VMs and Containers') ], + stopall: [ '', gettext('Stop all VMs and Containers') ], + migrateall: [ '', gettext('Migrate all VMs and Containers') ], + dircreate: [ gettext('Directory Storage'), gettext('Create') ], + lvmcreate: [ gettext('LVM Storage'), gettext('Create') ], + lvmthincreate: [ gettext('LVM-Thin Storage'), gettext('Create') ], + zfscreate: [ gettext('ZFS Storage'), gettext('Create') ] + }, + format_task_description: function(type, id) { - return type + ' ' + id; + var farray = Proxmox.Utils.task_desc_table[type]; + var text; + if (!farray) { + text = type; + if (id) { + type += ' ' + id; + } + return text; + } + var prefix = farray[0]; + text = farray[1]; + if (prefix) { + return prefix + ' ' + id + ' - ' + text; + } + return text; + }, + + format_size: function(size) { + /*jslint confusion: true */ + + var units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi']; + var num = 0; + + while (size >= 1024 && ((num++)+1) < units.length) { + size = size / 1024; + } + + return size.toFixed((num > 0)?2:0) + " " + units[num] + "B"; }, render_upid: function(value, metaData, record) { @@ -289,6 +553,21 @@ Ext.define('Proxmox.Utils', { utilities: { return Proxmox.Utils.format_task_description(type, id); }, + render_uptime: function(value) { + + var uptime = value; + + if (uptime === undefined) { + return ''; + } + + if (uptime <= 0) { + return '-'; + } + + return Proxmox.Utils.format_duration_long(uptime); + }, + parse_task_upid: function(upid) { var task = {}; @@ -314,7 +593,23 @@ Ext.define('Proxmox.Utils', { utilities: { return Ext.Date.format(servertime, 'Y-m-d H:i:s'); }, - }, + openXtermJsViewer: function(vmtype, vmid, nodename, vmname, cmd) { + var url = Ext.Object.toQueryString({ + console: vmtype, // kvm, lxc, upgrade or shell + xtermjs: 1, + vmid: vmid, + vmname: vmname, + node: nodename, + cmd: cmd, + + }); + var nw = window.open("?" + url, '_blank', 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420'); + if (nw) { + nw.focus(); + } + } + +}, singleton: true, constructor: function() { @@ -325,10 +620,12 @@ Ext.define('Proxmox.Utils', { utilities: { var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")"; var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})"; var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")"; + var IPV4_CIDR_MASK = "([0-9]{1,2})"; + var IPV6_CIDR_MASK = "([0-9]{1,3})"; me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$"); - me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$"); + me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/" + IPV4_CIDR_MASK + "$"); var IPV6_REGEXP = "(?:" + "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" + @@ -343,10 +640,11 @@ Ext.define('Proxmox.Utils', { utilities: { ")"; me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$"); - me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$"); + me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/" + IPV6_CIDR_MASK + "$"); me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]"); me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$"); + me.IP64_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + "\/" + IPV6_CIDR_MASK + ")|(?:" + IPV4_REGEXP + "\/" + IPV4_CIDR_MASK + ")$"); var DnsName_REGEXP = "(?:(([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)\\.)*([A-Za-z0-9]([A-Za-z0-9\\-]*[A-Za-z0-9])?))"; me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");