1 // NOTE: copied from widget-toolkit's Utils.js, as we only need a few utils
2 // from there, and loading the whole proxmoxlib creates more trouble..
5 Ext
.ns('Proxmox.Setup');
7 if (!Ext
.isDefined(Proxmox
.Setup
.auth_cookie_name
)) {
8 throw "Proxmox library not initialized";
11 // avoid errors when running without development tools
12 if (!Ext
.isDefined(Ext
.global
.console
)) {
24 Ext
.global
.console
= console
;
27 Ext
.Ajax
.defaultHeaders
= {
28 'Accept': 'application/json',
31 Ext
.Ajax
.on('beforerequest', function(conn
, options
) {
32 if (Proxmox
.CSRFPreventionToken
) {
33 if (!options
.headers
) {
36 options
.headers
.CSRFPreventionToken
= Proxmox
.CSRFPreventionToken
;
38 let storedAuth
= Proxmox
.Utils
.getStoredAuth();
39 if (storedAuth
.token
) {
40 options
.headers
.Authorization
= storedAuth
.token
;
44 Ext
.define('Proxmox.Utils', { // a singleton
47 yesText
: gettext('Yes'),
48 noText
: gettext('No'),
49 enabledText
: gettext('Enabled'),
50 disabledText
: gettext('Disabled'),
51 noneText
: gettext('none'),
52 NoneText
: gettext('None'),
53 errorText
: gettext('Error'),
54 warningsText
: gettext('Warnings'),
55 unknownText
: gettext('Unknown'),
56 defaultText
: gettext('Default'),
57 daysText
: gettext('days'),
58 dayText
: gettext('day'),
59 runningText
: gettext('running'),
60 stoppedText
: gettext('stopped'),
61 neverText
: gettext('never'),
62 totalText
: gettext('Total'),
63 usedText
: gettext('Used'),
64 directoryText
: gettext('Directory'),
65 stateText
: gettext('State'),
66 groupText
: gettext('Group'),
68 language_map
: { //language map is sorted alphabetically by iso 639-1
69 ar
: `العربية - ${gettext("Arabic")}`,
70 ca
: `Català - ${gettext("Catalan")}`,
71 da
: `Dansk - ${gettext("Danish")}`,
72 de
: `Deutsch - ${gettext("German")}`,
73 en
: `English - ${gettext("English")}`,
74 es
: `Español - ${gettext("Spanish")}`,
75 eu
: `Euskera (Basque) - ${gettext("Euskera (Basque)")}`,
76 fa
: `فارسی - ${gettext("Persian (Farsi)")}`,
77 fr
: `Français - ${gettext("French")}`,
78 he
: `עברית - ${gettext("Hebrew")}`,
79 it
: `Italiano - ${gettext("Italian")}`,
80 ja
: `日本語 - ${gettext("Japanese")}`,
81 kr
: `한국어 - ${gettext("Korean")}`,
82 nb
: `Bokmål - ${gettext("Norwegian (Bokmal)")}`,
83 nl
: `Nederlands - ${gettext("Dutch")}`,
84 nn
: `Nynorsk - ${gettext("Norwegian (Nynorsk)")}`,
85 pl
: `Polski - ${gettext("Polish")}`,
86 pt_BR
: `Português Brasileiro - ${gettext("Portuguese (Brazil)")}`,
87 ru
: `Русский - ${gettext("Russian")}`,
88 sl
: `Slovenščina - ${gettext("Slovenian")}`,
89 sv
: `Svenska - ${gettext("Swedish")}`,
90 tr
: `Türkçe - ${gettext("Turkish")}`,
91 zh_CN
: `中文(简体)- ${gettext("Chinese (Simplified)")}`,
92 zh_TW
: `中文(繁體)- ${gettext("Chinese (Traditional)")}`,
95 render_language: function(value
) {
96 if (!value
|| value
=== '__default__') {
97 return Proxmox
.Utils
.defaultText
+ ' (English)';
99 let text
= Proxmox
.Utils
.language_map
[value
];
101 return text
+ ' (' + value
+ ')';
106 renderEnabledIcon
: enabled
=> `<i class="fa fa-${enabled ? 'check' : 'minus'}"></i>`,
108 language_array: function() {
109 let data
= [['__default__', Proxmox
.Utils
.render_language('')]];
110 Ext
.Object
.each(Proxmox
.Utils
.language_map
, function(key
, value
) {
111 data
.push([key
, Proxmox
.Utils
.render_language(value
)]);
117 getNoSubKeyHtml: function(url
) {
118 return Ext
.String
.format('You do not have a valid subscription for this server. Please visit <a target="_blank" href="{0}">www.proxmox.com</a> to get a list of available options.', url
|| 'https://www.proxmox.com');
121 format_boolean_with_default: function(value
) {
122 if (Ext
.isDefined(value
) && value
!== '__default__') {
123 return value
? Proxmox
.Utils
.yesText
: Proxmox
.Utils
.noText
;
125 return Proxmox
.Utils
.defaultText
;
128 format_boolean: function(value
) {
129 return value
? Proxmox
.Utils
.yesText
: Proxmox
.Utils
.noText
;
132 format_neg_boolean: function(value
) {
133 return !value
? Proxmox
.Utils
.yesText
: Proxmox
.Utils
.noText
;
136 format_enabled_toggle: function(value
) {
137 return value
? Proxmox
.Utils
.enabledText
: Proxmox
.Utils
.disabledText
;
140 format_expire: function(date
) {
142 return Proxmox
.Utils
.neverText
;
144 return Ext
.Date
.format(date
, "Y-m-d");
147 // somewhat like a human would tell durations, omit zero values and do not
148 // give seconds precision if we talk days already
149 format_duration_human: function(ut
) {
150 let seconds
= 0, minutes
= 0, hours
= 0, days
= 0, years
= 0;
157 seconds
= Number((remaining
% 60).toFixed(1));
158 remaining
= Math
.trunc(remaining
/ 60);
160 minutes
= remaining
% 60;
161 remaining
= Math
.trunc(remaining
/ 60);
163 hours
= remaining
% 24;
164 remaining
= Math
.trunc(remaining
/ 24);
166 days
= remaining
% 365;
167 remaining
= Math
.trunc(remaining
/ 365); // yea, just lets ignore leap years...
176 let add
= (t
, unit
) => {
177 if (t
> 0) res
.push(t
+ unit
);
181 let addMinutes
= !add(years
, 'y');
182 let addSeconds
= !add(days
, 'd');
190 return res
.join(' ');
193 format_duration_long: function(ut
) {
194 let days
= Math
.floor(ut
/ 86400);
196 let hours
= Math
.floor(ut
/ 3600);
198 let mins
= Math
.floor(ut
/ 60);
201 let hours_str
= '00' + hours
.toString();
202 hours_str
= hours_str
.substr(hours_str
.length
- 2);
203 let mins_str
= "00" + mins
.toString();
204 mins_str
= mins_str
.substr(mins_str
.length
- 2);
205 let ut_str
= "00" + ut
.toString();
206 ut_str
= ut_str
.substr(ut_str
.length
- 2);
209 let ds
= days
> 1 ? Proxmox
.Utils
.daysText
: Proxmox
.Utils
.dayText
;
210 return days
.toString() + ' ' + ds
+ ' ' +
211 hours_str
+ ':' + mins_str
+ ':' + ut_str
;
213 return hours_str
+ ':' + mins_str
+ ':' + ut_str
;
217 format_subscription_level: function(level
) {
220 } else if (level
=== 'b') {
222 } else if (level
=== 's') {
224 } else if (level
=== 'p') {
227 return Proxmox
.Utils
.noneText
;
231 compute_min_label_width: function(text
, width
) {
232 if (width
=== undefined) { width
= 100; }
234 let tm
= new Ext
.util
.TextMetrics();
235 let min
= tm
.getWidth(text
+ ':');
237 return min
< width
? width
: min
;
240 // returns username + realm
241 parse_userid: function(userid
) {
242 if (!Ext
.isString(userid
)) {
243 return [undefined, undefined];
246 let match
= userid
.match(/^(.+)@([^@]+)$/);
247 if (match
!== null) {
248 return [match
[1], match
[2]];
251 return [undefined, undefined];
254 render_username: function(userid
) {
255 let username
= Proxmox
.Utils
.parse_userid(userid
)[0] || "";
256 return Ext
.htmlEncode(username
);
259 render_realm: function(userid
) {
260 let username
= Proxmox
.Utils
.parse_userid(userid
)[1] || "";
261 return Ext
.htmlEncode(username
);
264 getStoredAuth: function() {
265 let storedAuth
= JSON
.parse(window
.localStorage
.getItem('ProxmoxUser'));
266 return storedAuth
|| {};
269 setAuthData: function(data
) {
270 Proxmox
.UserName
= data
.username
;
271 Proxmox
.LoggedOut
= data
.LoggedOut
;
272 // creates a session cookie (expire = null)
273 // that way the cookie gets deleted after the browser window is closed
275 Proxmox
.CSRFPreventionToken
= data
.CSRFPreventionToken
;
276 Ext
.util
.Cookies
.set(Proxmox
.Setup
.auth_cookie_name
, data
.ticket
, null, '/', null, true);
280 window
.localStorage
.setItem('ProxmoxUser', JSON
.stringify(data
));
285 if (Proxmox
.LoggedOut
) {
288 let storedAuth
= Proxmox
.Utils
.getStoredAuth();
289 let cookie
= Ext
.util
.Cookies
.get(Proxmox
.Setup
.auth_cookie_name
);
290 if ((Proxmox
.UserName
!== '' && cookie
&& !cookie
.startsWith("PVE:tfa!")) || storedAuth
.token
) {
291 return cookie
|| storedAuth
.token
;
297 authClear: function() {
298 if (Proxmox
.LoggedOut
) {
301 // ExtJS clear is basically the same, but browser may complain if any cookie isn't "secure"
302 Ext
.util
.Cookies
.set(Proxmox
.Setup
.auth_cookie_name
, "", new Date(0), null, null, true);
303 window
.localStorage
.removeItem("ProxmoxUser");
306 // The End-User gets redirected back here after login on the OpenID auth. portal, and in the
307 // redirection URL the state and auth.code are passed as URL GET params, this helper parses those
308 getOpenIDRedirectionAuthorization: function() {
309 const auth
= Ext
.Object
.fromQueryString(window
.location
.search
);
310 if (auth
.state
!== undefined && auth
.code
!== undefined) {
316 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
317 // use el.mask() instead
318 setErrorMask: function(comp
, msg
) {
325 } else if (msg
=== true) {
326 el
.mask(gettext("Loading..."));
332 getResponseErrorMessage
: (err
) => {
333 if (!err
.statusText
) {
334 return gettext('Connection error');
336 let msg
= [`${err.statusText} (${err.status})`];
337 if (err
.response
&& err
.response
.responseText
) {
338 let txt
= err
.response
.responseText
;
340 let res
= JSON
.parse(txt
);
341 if (res
.errors
&& typeof res
.errors
=== 'object') {
342 for (let [key
, value
] of Object
.entries(res
.errors
)) {
343 msg
.push(Ext
.String
.htmlEncode(`${key}: ${value}`));
347 // fallback to string
348 msg
.push(Ext
.String
.htmlEncode(txt
));
351 return msg
.join('<br>');
354 monStoreErrors: function(component
, store
, clearMaskBeforeLoad
, errorCallback
) {
355 if (clearMaskBeforeLoad
) {
356 component
.mon(store
, 'beforeload', function(s
, operation
, eOpts
) {
357 Proxmox
.Utils
.setErrorMask(component
, false);
360 component
.mon(store
, 'beforeload', function(s
, operation
, eOpts
) {
361 if (!component
.loadCount
) {
362 component
.loadCount
= 0; // make sure it is nucomponent.ic
363 Proxmox
.Utils
.setErrorMask(component
, true);
368 // only works with 'proxmox' proxy
369 component
.mon(store
.proxy
, 'afterload', function(proxy
, request
, success
) {
370 component
.loadCount
++;
373 Proxmox
.Utils
.setErrorMask(component
, false);
377 let error
= request
._operation
.getError();
378 let msg
= Proxmox
.Utils
.getResponseErrorMessage(error
);
379 if (!errorCallback
|| !errorCallback(error
, msg
)) {
380 Proxmox
.Utils
.setErrorMask(component
, msg
);
385 extractRequestError: function(result
, verbose
) {
386 let msg
= gettext('Successful');
388 if (!result
.success
) {
389 msg
= gettext("Unknown error");
390 if (result
.message
) {
391 msg
= Ext
.htmlEncode(result
.message
);
393 msg
+= ` (${result.status})`;
396 if (verbose
&& Ext
.isObject(result
.errors
)) {
398 Ext
.Object
.each(result
.errors
, (prop
, desc
) => {
399 msg
+= `<br><b>${Ext.htmlEncode(prop)}</b>: ${Ext.htmlEncode(desc)}`;
408 API2Request: function(reqOpts
) {
409 let newopts
= Ext
.apply({
410 waitMsg
: gettext('Please wait...'),
413 // default to enable if user isn't handling the failure already explicitly
414 let autoErrorAlert
= reqOpts
.autoErrorAlert
??
415 (typeof reqOpts
.failure
!== 'function' && typeof reqOpts
.callback
!== 'function');
417 if (!newopts
.url
.match(/^\/api2/)) {
418 newopts
.url
= '/api2/extjs' + newopts
.url
;
420 delete newopts
.callback
;
422 let createWrapper = function(successFn
, callbackFn
, failureFn
) {
424 success: function(response
, options
) {
425 if (options
.waitMsgTarget
) {
426 if (Proxmox
.Utils
.toolkit
=== 'touch') {
427 options
.waitMsgTarget
.setMasked(false);
429 options
.waitMsgTarget
.setLoading(false);
432 let result
= Ext
.decode(response
.responseText
);
433 response
.result
= result
;
434 if (!result
.success
) {
435 response
.htmlStatus
= Proxmox
.Utils
.extractRequestError(result
, true);
436 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
437 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
438 if (autoErrorAlert
) {
439 Ext
.Msg
.alert(gettext('Error'), response
.htmlStatus
);
443 Ext
.callback(callbackFn
, options
.scope
, [options
, true, response
]);
444 Ext
.callback(successFn
, options
.scope
, [response
, options
]);
446 failure: function(response
, options
) {
447 if (options
.waitMsgTarget
) {
448 if (Proxmox
.Utils
.toolkit
=== 'touch') {
449 options
.waitMsgTarget
.setMasked(false);
451 options
.waitMsgTarget
.setLoading(false);
454 response
.result
= {};
456 response
.result
= Ext
.decode(response
.responseText
);
460 let msg
= gettext('Connection error') + ' - server offline?';
461 if (response
.aborted
) {
462 msg
= gettext('Connection error') + ' - aborted.';
463 } else if (response
.timedout
) {
464 msg
= gettext('Connection error') + ' - Timeout.';
465 } else if (response
.status
&& response
.statusText
) {
466 msg
= gettext('Connection error') + ' ' + response
.status
+ ': ' + response
.statusText
;
468 response
.htmlStatus
= msg
;
469 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
470 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
475 createWrapper(reqOpts
.success
, reqOpts
.callback
, reqOpts
.failure
);
477 let target
= newopts
.waitMsgTarget
;
479 if (Proxmox
.Utils
.toolkit
=== 'touch') {
480 target
.setMasked({ xtype
: 'loadmask', message
: newopts
.waitMsg
});
482 // Note: ExtJS bug - this does not work when component is not rendered
483 target
.setLoading(newopts
.waitMsg
);
486 Ext
.Ajax
.request(newopts
);
489 // can be useful for catching displaying errors from the API, e.g.:
490 // Proxmox.Async.api2({
492 // }).catch(Proxmox.Utils.alertResponseFailure);
493 alertResponseFailure
: (response
) => {
496 response
.htmlStatus
|| response
.result
.message
,
500 checked_command: function(orig_cmd
) {
501 Proxmox
.Utils
.API2Request(
503 url
: '/nodes/localhost/subscription',
505 failure: function(response
, opts
) {
506 Ext
.Msg
.alert(gettext('Error'), response
.htmlStatus
);
508 success: function(response
, opts
) {
509 let res
= response
.result
;
510 if (res
=== null || res
=== undefined || !res
|| res
511 .data
.status
.toLowerCase() !== 'active') {
513 title
: gettext('No valid subscription'),
514 icon
: Ext
.Msg
.WARNING
,
515 message
: Proxmox
.Utils
.getNoSubKeyHtml(res
.data
.url
),
517 callback: function(btn
) {
532 assemble_field_data: function(values
, data
) {
533 if (!Ext
.isObject(data
)) {
536 Ext
.Object
.each(data
, function(name
, val
) {
537 if (Object
.prototype.hasOwnProperty
.call(values
, name
)) {
538 let bucket
= values
[name
];
539 if (!Ext
.isArray(bucket
)) {
540 bucket
= values
[name
] = [bucket
];
542 if (Ext
.isArray(val
)) {
543 values
[name
] = bucket
.concat(val
);
553 network_iface_types
: {
554 eth
: gettext("Network Device"),
555 bridge
: 'Linux Bridge',
558 OVSBridge
: 'OVS Bridge',
561 OVSIntPort
: 'OVS IntPort',
564 render_network_iface_type: function(value
) {
565 return Proxmox
.Utils
.network_iface_types
[value
] ||
566 Proxmox
.Utils
.unknownText
;
569 // NOTE: only add general, product agnostic, ones here! Else use override helper in product repos
571 aptupdate
: ['', gettext('Update package database')],
572 diskinit
: ['Disk', gettext('Initialize Disk with GPT')],
573 spiceshell
: ['', gettext('Shell') + ' (Spice)'],
574 srvreload
: ['SRV', gettext('Reload')],
575 srvrestart
: ['SRV', gettext('Restart')],
576 srvstart
: ['SRV', gettext('Start')],
577 srvstop
: ['SRV', gettext('Stop')],
578 termproxy
: ['', gettext('Console') + ' (xterm.js)'],
579 vncshell
: ['', gettext('Shell')],
582 // to add or change existing for product specific ones
583 override_task_descriptions: function(extra
) {
584 for (const [key
, value
] of Object
.entries(extra
)) {
585 Proxmox
.Utils
.task_desc_table
[key
] = value
;
589 format_task_description: function(type
, id
) {
590 let farray
= Proxmox
.Utils
.task_desc_table
[type
];
598 } else if (Ext
.isFunction(farray
)) {
599 return farray(type
, id
);
601 let prefix
= farray
[0];
603 if (prefix
&& id
!== undefined) {
604 return prefix
+ ' ' + id
+ ' - ' + text
;
609 format_size: function(size
, useSI
) {
610 let units
= ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
612 const baseValue
= useSI
? 1000 : 1024;
613 while (size
>= baseValue
&& order
< units
.length
) {
614 size
= size
/ baseValue
;
618 let unit
= units
[order
], commaDigits
= 2;
624 return `${size.toFixed(commaDigits)} ${unit}B`;
632 'GiB': 1024*1024*1024,
633 'TiB': 1024*1024*1024*1024,
634 'PiB': 1024*1024*1024*1024*1024,
638 'GB': 1000*1000*1000,
639 'TB': 1000*1000*1000*1000,
640 'PB': 1000*1000*1000*1000*1000,
643 parse_size_unit: function(val
) {
644 //let m = val.match(/([.\d])+\s?([KMGTP]?)(i?)B?\s*$/i);
645 let m
= val
.match(/(\d+(?:\.\d+)?)\s?([KMGTP]?)(i?)B?\s*$/i);
646 let size
= parseFloat(m
[1]);
647 let scale
= m
[2].toUpperCase();
648 let binary
= m
[3].toLowerCase();
650 let unit
= `${scale}${binary}B`;
651 let factor
= Proxmox
.Utils
.SizeUnits
[unit
];
653 return { size
, factor
, unit
, binary
}; // for convenience return all we got
656 size_unit_to_bytes: function(val
) {
657 let { size
, factor
} = Proxmox
.Utils
.parse_size_unit(val
);
658 return size
* factor
;
661 autoscale_size_unit: function(val
) {
662 let { size
, factor
, binary
} = Proxmox
.Utils
.parse_size_unit(val
);
663 return Proxmox
.Utils
.format_size(size
* factor
, binary
!== "i");
666 size_unit_ratios: function(a
, b
) {
667 a
= typeof a
!== "undefined" ? a
: 0;
668 b
= typeof b
!== "undefined" ? b
: Infinity
;
669 let aBytes
= typeof a
=== "number" ? a
: Proxmox
.Utils
.size_unit_to_bytes(a
);
670 let bBytes
= typeof b
=== "number" ? b
: Proxmox
.Utils
.size_unit_to_bytes(b
);
671 return aBytes
/ (bBytes
|| Infinity
); // avoid division by zero
674 render_upid: function(value
, metaData
, record
) {
675 let task
= record
.data
;
676 let type
= task
.type
|| task
.worker_type
;
677 let id
= task
.id
|| task
.worker_id
;
679 return Proxmox
.Utils
.format_task_description(type
, id
);
682 render_uptime: function(value
) {
685 if (uptime
=== undefined) {
693 return Proxmox
.Utils
.format_duration_long(uptime
);
696 systemd_unescape: function(string_value
) {
697 const charcode_0
= '0'.charCodeAt(0);
698 const charcode_9
= '9'.charCodeAt(0);
699 const charcode_A
= 'A'.charCodeAt(0);
700 const charcode_F
= 'F'.charCodeAt(0);
701 const charcode_a
= 'a'.charCodeAt(0);
702 const charcode_f
= 'f'.charCodeAt(0);
703 const charcode_x
= 'x'.charCodeAt(0);
704 const charcode_minus
= '-'.charCodeAt(0);
705 const charcode_slash
= '/'.charCodeAt(0);
706 const charcode_backslash
= '\\'.charCodeAt(0);
708 let parse_hex_digit = function(d
) {
709 if (d
>= charcode_0
&& d
<= charcode_9
) {
710 return d
- charcode_0
;
712 if (d
>= charcode_A
&& d
<= charcode_F
) {
713 return d
- charcode_A
+ 10;
715 if (d
>= charcode_a
&& d
<= charcode_f
) {
716 return d
- charcode_a
+ 10;
718 throw "got invalid hex digit";
721 let value
= new TextEncoder().encode(string_value
);
722 let result
= new Uint8Array(value
.length
);
727 while (i
< value
.length
) {
729 if (c0
=== charcode_minus
) {
730 result
.set([charcode_slash
], result_len
);
735 if ((i
+ 4) < value
.length
) {
737 if (c0
=== charcode_backslash
&& c1
=== charcode_x
) {
738 let h1
= parse_hex_digit(value
[i
+2]);
739 let h0
= parse_hex_digit(value
[i
+3]);
741 result
.set([ord
], result_len
);
747 result
.set([c0
], result_len
);
752 return new TextDecoder().decode(result
.slice(0, result
.len
));
755 parse_task_upid: function(upid
) {
758 let res
= upid
.match(/^UPID:([^\s:]+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8,9}):(([0-9A-Fa-f]{8,16}):)?([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/);
760 throw "unable to parse upid '" + upid
+ "'";
763 task
.pid
= parseInt(res
[2], 16);
764 task
.pstart
= parseInt(res
[3], 16);
765 if (res
[5] !== undefined) {
766 task
.task_id
= parseInt(res
[5], 16);
768 task
.starttime
= parseInt(res
[6], 16);
770 task
.id
= Proxmox
.Utils
.systemd_unescape(res
[8]);
773 task
.desc
= Proxmox
.Utils
.format_task_description(task
.type
, task
.id
);
778 parse_task_status: function(status
) {
779 if (status
=== 'OK') {
783 if (status
=== 'unknown') {
787 let match
= status
.match(/^WARNINGS: (.*)$/);
795 format_task_status: function(status
) {
796 let parsed
= Proxmox
.Utils
.parse_task_status(status
);
798 case 'unknown': return Proxmox
.Utils
.unknownText
;
799 case 'error': return Proxmox
.Utils
.errorText
+ ': ' + status
;
800 case 'warning': return status
.replace('WARNINGS', Proxmox
.Utils
.warningsText
);
801 case 'ok': // fall-through
802 default: return status
;
806 render_duration: function(value
) {
807 if (value
=== undefined) {
810 return Proxmox
.Utils
.format_duration_human(value
);
813 render_timestamp: function(value
, metaData
, record
, rowIndex
, colIndex
, store
) {
814 let servertime
= new Date(value
* 1000);
815 return Ext
.Date
.format(servertime
, 'Y-m-d H:i:s');
818 render_zfs_health: function(value
) {
819 if (typeof value
=== 'undefined') {
822 var iconCls
= 'question-circle';
826 iconCls
= 'check-circle good';
830 iconCls
= 'exclamation-circle warning';
835 iconCls
= 'times-circle critical';
840 return '<i class="fa fa-' + iconCls
+ '"></i> ' + value
;
843 get_help_info: function(section
) {
845 if (typeof proxmoxOnlineHelpInfo
!== 'undefined') {
846 helpMap
= proxmoxOnlineHelpInfo
; // eslint-disable-line no-undef
847 } else if (typeof pveOnlineHelpInfo
!== 'undefined') {
848 // be backward compatible with older pve-doc-generators
849 helpMap
= pveOnlineHelpInfo
; // eslint-disable-line no-undef
851 throw "no global OnlineHelpInfo map declared";
854 if (helpMap
[section
]) {
855 return helpMap
[section
];
857 // try to normalize - and _ separators, to support asciidoc and sphinx
858 // references at the same time.
859 let section_minus_normalized
= section
.replace(/_
/g
, '-');
860 if (helpMap
[section_minus_normalized
]) {
861 return helpMap
[section_minus_normalized
];
863 let section_underscore_normalized
= section
.replace(/-/g
, '_');
864 return helpMap
[section_underscore_normalized
];
867 get_help_link: function(section
) {
868 let info
= Proxmox
.Utils
.get_help_info(section
);
872 return window
.location
.origin
+ info
.link
;
875 openXtermJsViewer: function(vmtype
, vmid
, nodename
, vmname
, cmd
) {
876 let url
= Ext
.Object
.toQueryString({
877 console
: vmtype
, // kvm, lxc, upgrade or shell
885 let nw
= window
.open("?" + url
, '_blank', 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420');
891 render_optional_url: function(value
) {
892 if (value
&& value
.match(/^https?:\/\//) !== null) {
893 return '<a target="_blank" href="' + value
+ '">' + value
+ '</a>';
898 render_san: function(value
) {
900 if (Ext
.isArray(value
)) {
901 value
.forEach(function(val
) {
902 if (!Ext
.isNumber(val
)) {
906 return names
.join('<br>');
911 render_usage
: val
=> (val
* 100).toFixed(2) + '%',
913 render_cpu_usage: function(val
, max
) {
914 return Ext
.String
.format(
915 `${gettext('{0}% of {1}')} ${gettext('CPU(s)')}`,
916 (val
*100).toFixed(2),
921 render_size_usage: function(val
, max
, useSI
) {
923 return gettext('N/A');
925 let fmt
= v
=> Proxmox
.Utils
.format_size(v
, useSI
);
926 let ratio
= (val
* 100 / max
).toFixed(2);
927 return ratio
+ '% (' + Ext
.String
.format(gettext('{0} of {1}'), fmt(val
), fmt(max
)) + ')';
930 render_cpu: function(value
, metaData
, record
, rowIndex
, colIndex
, store
) {
931 if (!(record
.data
.uptime
&& Ext
.isNumeric(value
))) {
935 let maxcpu
= record
.data
.maxcpu
|| 1;
936 if (!Ext
.isNumeric(maxcpu
) || maxcpu
< 1) {
939 let cpuText
= maxcpu
> 1 ? 'CPUs' : 'CPU';
940 let ratio
= (value
* 100).toFixed(1);
941 return `${ratio}% of ${maxcpu.toString()} ${cpuText}`;
944 render_size: function(value
, metaData
, record
, rowIndex
, colIndex
, store
) {
945 if (!Ext
.isNumeric(value
)) {
948 return Proxmox
.Utils
.format_size(value
);
951 render_cpu_model: function(cpu
) {
952 let socketText
= cpu
.sockets
> 1 ? gettext('Sockets') : gettext('Socket');
953 return `${cpu.cpus} x ${cpu.model} (${cpu.sockets.toString()} ${socketText})`;
956 /* this is different for nodes */
957 render_node_cpu_usage: function(value
, record
) {
958 return Proxmox
.Utils
.render_cpu_usage(value
, record
.cpus
);
961 render_node_size_usage: function(record
) {
962 return Proxmox
.Utils
.render_size_usage(record
.used
, record
.total
);
965 loadTextFromFile: function(file
, callback
, maxBytes
) {
966 let maxSize
= maxBytes
|| 8192;
967 if (file
.size
> maxSize
) {
968 Ext
.Msg
.alert(gettext('Error'), gettext("Invalid file size: ") + file
.size
);
971 let reader
= new FileReader();
972 reader
.onload
= evt
=> callback(evt
.target
.result
);
973 reader
.readAsText(file
);
976 parsePropertyString: function(value
, defaultKey
) {
980 if (typeof value
!== 'string' || value
=== '') {
984 Ext
.Array
.each(value
.split(','), function(p
) {
985 var kv
= p
.split('=', 2);
986 if (Ext
.isDefined(kv
[1])) {
988 } else if (Ext
.isDefined(defaultKey
)) {
989 if (Ext
.isDefined(res
[defaultKey
])) {
990 error
= 'defaultKey may be only defined once in propertyString';
991 return false; // break
993 res
[defaultKey
] = kv
[0];
995 error
= 'invalid propertyString, not a key=value pair and no defaultKey defined';
996 return false; // break
1001 if (error
!== undefined) {
1002 console
.error(error
);
1009 printPropertyString: function(data
, defaultKey
) {
1010 var stringparts
= [],
1011 gotDefaultKeyVal
= false,
1014 Ext
.Object
.each(data
, function(key
, value
) {
1015 if (defaultKey
!== undefined && key
=== defaultKey
) {
1016 gotDefaultKeyVal
= true;
1017 defaultKeyVal
= value
;
1018 } else if (Ext
.isArray(value
)) {
1019 stringparts
.push(key
+ '=' + value
.join(';'));
1020 } else if (value
!== '') {
1021 stringparts
.push(key
+ '=' + value
);
1025 stringparts
= stringparts
.sort();
1026 if (gotDefaultKeyVal
) {
1027 stringparts
.unshift(defaultKeyVal
);
1030 return stringparts
.join(',');
1033 acmedomain_count
: 5,
1035 parseACMEPluginData: function(data
) {
1038 data
.split('\n').forEach((line
) => {
1039 // capture everything after the first = as value
1040 let [key
, value
] = line
.split('=');
1041 if (value
!== undefined) {
1044 extradata
.push(line
);
1047 return [res
, extradata
];
1050 delete_if_default: function(values
, fieldname
, default_val
, create
) {
1051 if (values
[fieldname
] === '' || values
[fieldname
] === default_val
) {
1053 if (values
.delete) {
1054 if (Ext
.isArray(values
.delete)) {
1055 values
.delete.push(fieldname
);
1057 values
.delete += ',' + fieldname
;
1060 values
.delete = fieldname
;
1064 delete values
[fieldname
];
1068 printACME: function(value
) {
1069 if (Ext
.isArray(value
.domains
)) {
1070 value
.domains
= value
.domains
.join(';');
1072 return Proxmox
.Utils
.printPropertyString(value
);
1075 parseACME: function(value
) {
1083 Ext
.Array
.each(value
.split(','), function(p
) {
1084 var kv
= p
.split('=', 2);
1085 if (Ext
.isDefined(kv
[1])) {
1088 error
= 'Failed to parse key-value pair: '+p
;
1094 if (error
!== undefined) {
1095 console
.error(error
);
1099 if (res
.domains
!== undefined) {
1100 res
.domains
= res
.domains
.split(/;/);
1106 add_domain_to_acme: function(acme
, domain
) {
1107 if (acme
.domains
=== undefined) {
1108 acme
.domains
= [domain
];
1110 acme
.domains
.push(domain
);
1111 acme
.domains
= acme
.domains
.filter((value
, index
, self
) => self
.indexOf(value
) === index
);
1116 remove_domain_from_acme: function(acme
, domain
) {
1117 if (acme
.domains
!== undefined) {
1118 acme
.domains
= acme
.domains
.filter(
1119 (value
, index
, self
) => self
.indexOf(value
) === index
&& value
!== domain
,
1125 get_health_icon: function(state
, circle
) {
1126 if (circle
=== undefined) {
1130 if (state
=== undefined) {
1134 var icon
= 'faded fa-question';
1137 icon
= 'good fa-check';
1140 icon
= 'warning fa-upload';
1143 icon
= 'warning fa-refresh';
1146 icon
= 'warning fa-exclamation';
1149 icon
= 'critical fa-times';
1161 formatNodeRepoStatus: function(status
, product
) {
1162 let fmt
= (txt
, cls
) => `<i class="fa fa-fw fa-lg fa-${cls}"></i>${txt}`;
1164 let getUpdates
= Ext
.String
.format(gettext('{0} updates'), product
);
1165 let noRepo
= Ext
.String
.format(gettext('No {0} repository enabled!'), product
);
1167 if (status
=== 'ok') {
1168 return fmt(getUpdates
, 'check-circle good') + ' ' +
1169 fmt(gettext('Production-ready Enterprise repository enabled'), 'check-circle good');
1170 } else if (status
=== 'no-sub') {
1171 return fmt(gettext('Production-ready Enterprise repository enabled'), 'check-circle good') + ' ' +
1172 fmt(gettext('Enterprise repository needs valid subscription'), 'exclamation-circle warning');
1173 } else if (status
=== 'non-production') {
1174 return fmt(getUpdates
, 'check-circle good') + ' ' +
1175 fmt(gettext('Non production-ready repository enabled!'), 'exclamation-circle warning');
1176 } else if (status
=== 'no-repo') {
1177 return fmt(noRepo
, 'exclamation-circle critical');
1180 return Proxmox
.Utils
.unknownText
;
1183 render_u2f_error: function(error
) {
1185 '1': gettext('Other Error'),
1186 '2': gettext('Bad Request'),
1187 '3': gettext('Configuration Unsupported'),
1188 '4': gettext('Device Ineligible'),
1189 '5': gettext('Timeout'),
1191 return "U2F Error: " + ErrorNames
[error
] || Proxmox
.Utils
.unknownText
;
1194 // Convert an ArrayBuffer to a base64url encoded string.
1195 // A `null` value will be preserved for convenience.
1196 bytes_to_base64url: function(bytes
) {
1197 if (bytes
=== null) {
1202 .from(new Uint8Array(bytes
))
1203 .map(val
=> String
.fromCharCode(val
))
1206 .replace(/\+/g, '-')
1207 .replace(/\//g, '_')
1208 .replace(/[=]/g, '');
1211 // Convert an a base64url string to an ArrayBuffer.
1212 // A `null` value will be preserved for convenience.
1213 base64url_to_bytes: function(b64u
) {
1214 if (b64u
=== null) {
1218 return new Uint8Array(
1221 .replace(/_/g, '/'),
1224 .map(val => val.charCodeAt(0)),
1228 stringToRGB: function(string) {
1233 string += 'prox
'; // give short strings more variance
1234 for (let i = 0; i < string.length; i++) {
1235 hash = string.charCodeAt(i) + ((hash << 5) - hash);
1236 hash = hash & hash; // to int
1239 let alpha = 0.7; // make the color a bit brighter
1240 let bg = 255; // assume white background
1243 (hash & 255) * alpha + bg * (1 - alpha),
1244 ((hash >> 8) & 255) * alpha + bg * (1 - alpha),
1245 ((hash >> 16) & 255) * alpha + bg * (1 - alpha),
1249 rgbToCss: function(rgb) {
1250 return `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
1253 rgbToHex: function(rgb) {
1254 let r = Math.round(rgb[0]).toString(16);
1255 let g = Math.round(rgb[1]).toString(16);
1256 let b = Math.round(rgb[2]).toString(16);
1257 return `${r}${g}${b}`;
1260 hexToRGB: function(hex) {
1264 if (hex.length === 7) {
1267 let r = parseInt(hex.slice(0, 2), 16);
1268 let g = parseInt(hex.slice(2, 4), 16);
1269 let b = parseInt(hex.slice(4, 6), 16);
1273 // optimized & simplified SAPC function
1274 // https://github.com/Myndex/SAPC-APCA
1275 getTextContrastClass: function(rgb) {
1276 const blkThrs = 0.022;
1277 const blkClmp = 1.414;
1279 // linearize & gamma correction
1280 let r = (rgb[0] / 255) ** 2.4;
1281 let g = (rgb[1] / 255) ** 2.4;
1282 let b = (rgb[2] / 255) ** 2.4;
1284 // relative luminance sRGB
1285 let bg = r * 0.2126729 + g * 0.7151522 + b * 0.0721750;
1288 bg = bg > blkThrs ? bg : bg + (blkThrs - bg) ** blkClmp;
1290 // SAPC with white text
1291 let contrastLight = bg ** 0.65 - 1;
1292 // SAPC with black text
1293 let contrastDark = bg ** 0.56 - 0.046134502;
1295 if (Math.abs(contrastLight) >= Math.abs(contrastDark)) {
1302 getTagElement: function(string, color_overrides) {
1303 let rgb = color_overrides?.[string] || Proxmox.Utils.stringToRGB(string);
1304 let style = `background-color: ${Proxmox.Utils.rgbToCss(rgb)};`;
1306 if (rgb.length > 3) {
1307 style += `color: ${Proxmox.Utils.rgbToCss([rgb[3], rgb[4], rgb[5]])}`;
1308 cls = "proxmox-tag-dark";
1310 let txtCls = Proxmox.Utils.getTextContrastClass(rgb);
1311 cls = `proxmox-tag-${txtCls}`;
1313 return `<span class="${cls}" style="${style}">${string}</span>`;
1316 // Setting filename here when downloading from a remote url sometimes fails in chromium browsers
1317 // because of a bug when using attribute download in conjunction with a self signed certificate.
1318 // For more info see https://bugs.chromium.org/p/chromium/issues/detail?id=993362
1319 downloadAsFile: function(source, fileName) {
1320 let hiddenElement = document.createElement('a
');
1321 hiddenElement.href = source;
1322 hiddenElement.target = '_blank
';
1324 hiddenElement.download = fileName;
1326 hiddenElement.click();
1331 constructor: function() {
1333 Ext.apply(me, me.utilities);
1335 let IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
1336 let IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
1337 let IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
1338 let IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
1339 let IPV4_CIDR_MASK = "([0-9]{1,2})";
1340 let IPV6_CIDR_MASK = "([0-9]{1,3})";
1343 me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
1344 me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")/" + IPV4_CIDR_MASK + "$");
1346 /* eslint-disable no-useless-concat,no-multi-spaces */
1347 let IPV6_REGEXP = "(?:" +
1348 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
1349 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
1350 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
1351 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
1352 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
1353 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
1354 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
1355 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
1356 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
1358 /* eslint-enable no-useless-concat,no-multi-spaces */
1360 me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
1361 me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")/" + IPV6_CIDR_MASK + "$");
1362 me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
1364 me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
1365 me.IP64_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + "/" + IPV6_CIDR_MASK + ")|(?:" + IPV4_REGEXP + "/" + IPV4_CIDR_MASK + ")$");
1367 let 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])?))";
1368 me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
1369 me.DnsName_or_Wildcard_match = new RegExp("^(?:\\*\\.)?" + DnsName_REGEXP + "$");
1371 me.CpuSet_match = /^[0-9]+(?:-[0-9]+)?(?:,[0-9]+(?:-[0-9]+)?)*$/;
1373 me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(?::(\\d+))?$");
1374 me.HostPortBrackets_match = new RegExp("^\\[(" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](?::(\\d+))?$");
1375 me.IP6_dotnotation_match = new RegExp("^(" + IPV6_REGEXP + ")(?:\\.(\\d+))?$");
1376 me.Vlan_match = /^vlan(\d+)/;
1377 me.VlanInterface_match = /(\w+)\.(\d+)/;
1381 Ext.define('Proxmox
.Async
', {
1384 // Returns a Promise resolving to the result of an `API2Request` or rejecting to the error
1385 // response on failure
1386 api2: function(reqOpts) {
1387 return new Promise((resolve, reject) => {
1388 delete reqOpts.callback; // not allowed in this api
1389 reqOpts.success = response => resolve(response);
1390 reqOpts.failure = response => reject(response);
1391 Proxmox.Utils.API2Request(reqOpts);
1395 // Delay for a number of milliseconds.
1396 sleep: function(millis) {
1397 return new Promise((resolve, _reject) => setTimeout(resolve, millis));
1401 Ext.override(Ext.data.Store, {
1402 // If the store's proxy is changed
while it is waiting
for an AJAX
1403 // response, `onProxyLoad` will still be called for the outdated response.
1404 // To avoid displaying inconsistent information, only process responses
1405 // belonging to the current proxy. However, do not apply this workaround
1406 // to the mobile UI, as Sencha Touch has an incompatible internal API.
1407 onProxyLoad: function(operation
) {
1409 if (Proxmox
.Utils
.toolkit
=== 'touch' || operation
.getProxy() === me
.getProxy()) {
1410 me
.callParent(arguments
);
1412 console
.log(`ignored outdated response: ${operation.getRequest().getUrl()}`);