]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - src/Utils.js
language_map: add Dutch
[proxmox-widget-toolkit.git] / src / Utils.js
1 Ext.ns('Proxmox');
2 Ext.ns('Proxmox.Setup');
3
4 if (!Ext.isDefined(Proxmox.Setup.auth_cookie_name)) {
5 throw "Proxmox library not initialized";
6 }
7
8 // avoid errors related to Accessible Rich Internet Applications
9 // (access for people with disabilities)
10 // TODO reenable after all components are upgraded
11 Ext.enableAria = false;
12 Ext.enableAriaButtons = false;
13 Ext.enableAriaPanels = false;
14
15 // avoid errors when running without development tools
16 if (!Ext.isDefined(Ext.global.console)) {
17 let console = {
18 dir: function() {
19 // do nothing
20 },
21 log: function() {
22 // do nothing
23 },
24 warn: function() {
25 // do nothing
26 },
27 };
28 Ext.global.console = console;
29 }
30
31 Ext.Ajax.defaultHeaders = {
32 'Accept': 'application/json',
33 };
34
35 Ext.Ajax.on('beforerequest', function(conn, options) {
36 if (Proxmox.CSRFPreventionToken) {
37 if (!options.headers) {
38 options.headers = {};
39 }
40 options.headers.CSRFPreventionToken = Proxmox.CSRFPreventionToken;
41 }
42 let storedAuth = Proxmox.Utils.getStoredAuth();
43 if (storedAuth.token) {
44 options.headers.Authorization = storedAuth.token;
45 }
46 });
47
48 Ext.define('Proxmox.Utils', { // a singleton
49 utilities: {
50
51 yesText: gettext('Yes'),
52 noText: gettext('No'),
53 enabledText: gettext('Enabled'),
54 disabledText: gettext('Disabled'),
55 noneText: gettext('none'),
56 NoneText: gettext('None'),
57 errorText: gettext('Error'),
58 unknownText: gettext('Unknown'),
59 defaultText: gettext('Default'),
60 daysText: gettext('days'),
61 dayText: gettext('day'),
62 runningText: gettext('running'),
63 stoppedText: gettext('stopped'),
64 neverText: gettext('never'),
65 totalText: gettext('Total'),
66 usedText: gettext('Used'),
67 directoryText: gettext('Directory'),
68 stateText: gettext('State'),
69 groupText: gettext('Group'),
70
71 language_map: {
72 ar: 'Arabic',
73 ca: 'Catalan',
74 zh_CN: 'Chinese (Simplified)',
75 zh_TW: 'Chinese (Traditional)',
76 da: 'Danish',
77 nl: 'Dutch',
78 en: 'English',
79 eu: 'Euskera (Basque)',
80 fr: 'French',
81 de: 'German',
82 he: 'Hebrew',
83 it: 'Italian',
84 ja: 'Japanese',
85 nb: 'Norwegian (Bokmal)',
86 nn: 'Norwegian (Nynorsk)',
87 fa: 'Persian (Farsi)',
88 pl: 'Polish',
89 pt_BR: 'Portuguese (Brazil)',
90 ru: 'Russian',
91 sl: 'Slovenian',
92 es: 'Spanish',
93 sv: 'Swedish',
94 tr: 'Turkish',
95 },
96
97 render_language: function(value) {
98 if (!value) {
99 return Proxmox.Utils.defaultText + ' (English)';
100 }
101 let text = Proxmox.Utils.language_map[value];
102 if (text) {
103 return text + ' (' + value + ')';
104 }
105 return value;
106 },
107
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)]);
112 });
113
114 return data;
115 },
116
117 bond_mode_gettext_map: {
118 '802.3ad': 'LACP (802.3ad)',
119 'lacp-balance-slb': 'LACP (balance-slb)',
120 'lacp-balance-tcp': 'LACP (balance-tcp)',
121 },
122
123 render_bond_mode: value => Proxmox.Utils.bond_mode_gettext_map[value] || value || '',
124
125 bond_mode_array: function(modes) {
126 return modes.map(mode => [mode, Proxmox.Utils.render_bond_mode(mode)]);
127 },
128
129 getNoSubKeyHtml: function(url) {
130 // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans
131 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');
132 },
133
134 format_boolean_with_default: function(value) {
135 if (Ext.isDefined(value) && value !== '__default__') {
136 return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText;
137 }
138 return Proxmox.Utils.defaultText;
139 },
140
141 format_boolean: function(value) {
142 return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText;
143 },
144
145 format_neg_boolean: function(value) {
146 return !value ? Proxmox.Utils.yesText : Proxmox.Utils.noText;
147 },
148
149 format_enabled_toggle: function(value) {
150 return value ? Proxmox.Utils.enabledText : Proxmox.Utils.disabledText;
151 },
152
153 format_expire: function(date) {
154 if (!date) {
155 return Proxmox.Utils.neverText;
156 }
157 return Ext.Date.format(date, "Y-m-d");
158 },
159
160 // somewhat like a human would tell durations, omit zero values and do not
161 // give seconds precision if we talk days already
162 format_duration_human: function(ut) {
163 let seconds = 0, minutes = 0, hours = 0, days = 0;
164
165 if (ut <= 0.1) {
166 return '<0.1s';
167 }
168
169 let remaining = ut;
170 seconds = Number((remaining % 60).toFixed(1));
171 remaining = Math.trunc(remaining / 60);
172 if (remaining > 0) {
173 minutes = remaining % 60;
174 remaining = Math.trunc(remaining / 60);
175 if (remaining > 0) {
176 hours = remaining % 24;
177 remaining = Math.trunc(remaining / 24);
178 if (remaining > 0) {
179 days = remaining;
180 }
181 }
182 }
183
184 let res = [];
185 let add = (t, unit) => {
186 if (t > 0) res.push(t + unit);
187 return t > 0;
188 };
189
190 let addSeconds = !add(days, 'd');
191 add(hours, 'h');
192 add(minutes, 'm');
193 if (addSeconds) {
194 add(seconds, 's');
195 }
196 return res.join(' ');
197 },
198
199 format_duration_long: function(ut) {
200 let days = Math.floor(ut / 86400);
201 ut -= days*86400;
202 let hours = Math.floor(ut / 3600);
203 ut -= hours*3600;
204 let mins = Math.floor(ut / 60);
205 ut -= mins*60;
206
207 let hours_str = '00' + hours.toString();
208 hours_str = hours_str.substr(hours_str.length - 2);
209 let mins_str = "00" + mins.toString();
210 mins_str = mins_str.substr(mins_str.length - 2);
211 let ut_str = "00" + ut.toString();
212 ut_str = ut_str.substr(ut_str.length - 2);
213
214 if (days) {
215 let ds = days > 1 ? Proxmox.Utils.daysText : Proxmox.Utils.dayText;
216 return days.toString() + ' ' + ds + ' ' +
217 hours_str + ':' + mins_str + ':' + ut_str;
218 } else {
219 return hours_str + ':' + mins_str + ':' + ut_str;
220 }
221 },
222
223 format_subscription_level: function(level) {
224 if (level === 'c') {
225 return 'Community';
226 } else if (level === 'b') {
227 return 'Basic';
228 } else if (level === 's') {
229 return 'Standard';
230 } else if (level === 'p') {
231 return 'Premium';
232 } else {
233 return Proxmox.Utils.noneText;
234 }
235 },
236
237 compute_min_label_width: function(text, width) {
238 if (width === undefined) { width = 100; }
239
240 let tm = new Ext.util.TextMetrics();
241 let min = tm.getWidth(text + ':');
242
243 return min < width ? width : min;
244 },
245
246 getStoredAuth: function() {
247 let storedAuth = JSON.parse(window.localStorage.getItem('ProxmoxUser'));
248 return storedAuth || {};
249 },
250
251 setAuthData: function(data) {
252 Proxmox.UserName = data.username;
253 Proxmox.LoggedOut = data.LoggedOut;
254 // creates a session cookie (expire = null)
255 // that way the cookie gets deleted after the browser window is closed
256 if (data.ticket) {
257 Proxmox.CSRFPreventionToken = data.CSRFPreventionToken;
258 Ext.util.Cookies.set(Proxmox.Setup.auth_cookie_name, data.ticket, null, '/', null, true);
259 }
260
261 if (data.token) {
262 window.localStorage.setItem('ProxmoxUser', JSON.stringify(data));
263 }
264 },
265
266 authOK: function() {
267 if (Proxmox.LoggedOut) {
268 return undefined;
269 }
270 let storedAuth = Proxmox.Utils.getStoredAuth();
271 let cookie = Ext.util.Cookies.get(Proxmox.Setup.auth_cookie_name);
272 if ((Proxmox.UserName !== '' && cookie && !cookie.startsWith("PVE:tfa!")) || storedAuth.token) {
273 return cookie || storedAuth.token;
274 } else {
275 return false;
276 }
277 },
278
279 authClear: function() {
280 if (Proxmox.LoggedOut) {
281 return;
282 }
283 Ext.util.Cookies.clear(Proxmox.Setup.auth_cookie_name);
284 window.localStorage.removeItem("ProxmoxUser");
285 },
286
287 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
288 // use el.mask() instead
289 setErrorMask: function(comp, msg) {
290 let el = comp.el;
291 if (!el) {
292 return;
293 }
294 if (!msg) {
295 el.unmask();
296 } else if (msg === true) {
297 el.mask(gettext("Loading..."));
298 } else {
299 el.mask(msg);
300 }
301 },
302
303 getResponseErrorMessage: (err) => {
304 if (!err.statusText) {
305 return gettext('Connection error');
306 }
307 let msg = [`${err.statusText} (${err.status})`];
308 if (err.response && err.response.responseText) {
309 let txt = err.response.responseText;
310 try {
311 let res = JSON.parse(txt);
312 if (res.errors && typeof res.errors === 'object') {
313 for (let [key, value] of Object.entries(res.errors)) {
314 msg.push(Ext.String.htmlEncode(`${key}: ${value}`));
315 }
316 }
317 } catch (e) {
318 // fallback to string
319 msg.push(Ext.String.htmlEncode(txt));
320 }
321 }
322 return msg.join('<br>');
323 },
324
325 monStoreErrors: function(component, store, clearMaskBeforeLoad) {
326 if (clearMaskBeforeLoad) {
327 component.mon(store, 'beforeload', function(s, operation, eOpts) {
328 Proxmox.Utils.setErrorMask(component, false);
329 });
330 } else {
331 component.mon(store, 'beforeload', function(s, operation, eOpts) {
332 if (!component.loadCount) {
333 component.loadCount = 0; // make sure it is nucomponent.ic
334 Proxmox.Utils.setErrorMask(component, true);
335 }
336 });
337 }
338
339 // only works with 'proxmox' proxy
340 component.mon(store.proxy, 'afterload', function(proxy, request, success) {
341 component.loadCount++;
342
343 if (success) {
344 Proxmox.Utils.setErrorMask(component, false);
345 return;
346 }
347
348 let error = request._operation.getError();
349 let msg = Proxmox.Utils.getResponseErrorMessage(error);
350 Proxmox.Utils.setErrorMask(component, msg);
351 });
352 },
353
354 extractRequestError: function(result, verbose) {
355 let msg = gettext('Successful');
356
357 if (!result.success) {
358 msg = gettext("Unknown error");
359 if (result.message) {
360 msg = result.message;
361 if (result.status) {
362 msg += ' (' + result.status + ')';
363 }
364 }
365 if (verbose && Ext.isObject(result.errors)) {
366 msg += "<br>";
367 Ext.Object.each(result.errors, function(prop, desc) {
368 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
369 Ext.htmlEncode(desc);
370 });
371 }
372 }
373
374 return msg;
375 },
376
377 // Ext.Ajax.request
378 API2Request: function(reqOpts) {
379 let newopts = Ext.apply({
380 waitMsg: gettext('Please wait...'),
381 }, reqOpts);
382
383 if (!newopts.url.match(/^\/api2/)) {
384 newopts.url = '/api2/extjs' + newopts.url;
385 }
386 delete newopts.callback;
387
388 let createWrapper = function(successFn, callbackFn, failureFn) {
389 Ext.apply(newopts, {
390 success: function(response, options) {
391 if (options.waitMsgTarget) {
392 if (Proxmox.Utils.toolkit === 'touch') {
393 options.waitMsgTarget.setMasked(false);
394 } else {
395 options.waitMsgTarget.setLoading(false);
396 }
397 }
398 let result = Ext.decode(response.responseText);
399 response.result = result;
400 if (!result.success) {
401 response.htmlStatus = Proxmox.Utils.extractRequestError(result, true);
402 Ext.callback(callbackFn, options.scope, [options, false, response]);
403 Ext.callback(failureFn, options.scope, [response, options]);
404 return;
405 }
406 Ext.callback(callbackFn, options.scope, [options, true, response]);
407 Ext.callback(successFn, options.scope, [response, options]);
408 },
409 failure: function(response, options) {
410 if (options.waitMsgTarget) {
411 if (Proxmox.Utils.toolkit === 'touch') {
412 options.waitMsgTarget.setMasked(false);
413 } else {
414 options.waitMsgTarget.setLoading(false);
415 }
416 }
417 response.result = {};
418 try {
419 response.result = Ext.decode(response.responseText);
420 } catch (e) {
421 // ignore
422 }
423 let msg = gettext('Connection error') + ' - server offline?';
424 if (response.aborted) {
425 msg = gettext('Connection error') + ' - aborted.';
426 } else if (response.timedout) {
427 msg = gettext('Connection error') + ' - Timeout.';
428 } else if (response.status && response.statusText) {
429 msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText;
430 }
431 response.htmlStatus = msg;
432 Ext.callback(callbackFn, options.scope, [options, false, response]);
433 Ext.callback(failureFn, options.scope, [response, options]);
434 },
435 });
436 };
437
438 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
439
440 let target = newopts.waitMsgTarget;
441 if (target) {
442 if (Proxmox.Utils.toolkit === 'touch') {
443 target.setMasked({ xtype: 'loadmask', message: newopts.waitMsg });
444 } else {
445 // Note: ExtJS bug - this does not work when component is not rendered
446 target.setLoading(newopts.waitMsg);
447 }
448 }
449 Ext.Ajax.request(newopts);
450 },
451
452 checked_command: function(orig_cmd) {
453 Proxmox.Utils.API2Request(
454 {
455 url: '/nodes/localhost/subscription',
456 method: 'GET',
457 failure: function(response, opts) {
458 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
459 },
460 success: function(response, opts) {
461 let res = response.result;
462 if (res === null || res === undefined || !res || res
463 .data.status !== 'Active') {
464 Ext.Msg.show({
465 title: gettext('No valid subscription'),
466 icon: Ext.Msg.WARNING,
467 message: Proxmox.Utils.getNoSubKeyHtml(res.data.url),
468 buttons: Ext.Msg.OK,
469 callback: function(btn) {
470 if (btn !== 'ok') {
471 return;
472 }
473 orig_cmd();
474 },
475 });
476 } else {
477 orig_cmd();
478 }
479 },
480 },
481 );
482 },
483
484 assemble_field_data: function(values, data) {
485 if (!Ext.isObject(data)) {
486 return;
487 }
488 Ext.Object.each(data, function(name, val) {
489 if (Object.prototype.hasOwnProperty.call(values, name)) {
490 let bucket = values[name];
491 if (!Ext.isArray(bucket)) {
492 bucket = values[name] = [bucket];
493 }
494 if (Ext.isArray(val)) {
495 values[name] = bucket.concat(val);
496 } else {
497 bucket.push(val);
498 }
499 } else {
500 values[name] = val;
501 }
502 });
503 },
504
505 updateColumnWidth: function(container) {
506 let mode = Ext.state.Manager.get('summarycolumns') || 'auto';
507 let factor;
508 if (mode !== 'auto') {
509 factor = parseInt(mode, 10);
510 if (Number.isNaN(factor)) {
511 factor = 1;
512 }
513 } else {
514 factor = container.getSize().width < 1600 ? 1 : 2;
515 }
516
517 if (container.oldFactor === factor) {
518 return;
519 }
520
521 let items = container.query('>'); // direct childs
522 factor = Math.min(factor, items.length);
523 container.oldFactor = factor;
524
525 items.forEach((item) => {
526 item.columnWidth = 1 / factor;
527 });
528
529 // we have to update the layout twice, since the first layout change
530 // can trigger the scrollbar which reduces the amount of space left
531 container.updateLayout();
532 container.updateLayout();
533 },
534
535 dialog_title: function(subject, create, isAdd) {
536 if (create) {
537 if (isAdd) {
538 return gettext('Add') + ': ' + subject;
539 } else {
540 return gettext('Create') + ': ' + subject;
541 }
542 } else {
543 return gettext('Edit') + ': ' + subject;
544 }
545 },
546
547 network_iface_types: {
548 eth: gettext("Network Device"),
549 bridge: 'Linux Bridge',
550 bond: 'Linux Bond',
551 vlan: 'Linux VLAN',
552 OVSBridge: 'OVS Bridge',
553 OVSBond: 'OVS Bond',
554 OVSPort: 'OVS Port',
555 OVSIntPort: 'OVS IntPort',
556 },
557
558 render_network_iface_type: function(value) {
559 return Proxmox.Utils.network_iface_types[value] ||
560 Proxmox.Utils.unknownText;
561 },
562
563 task_desc_table: {
564 acmenewcert: ['SRV', gettext('Order Certificate')],
565 acmeregister: ['ACME Account', gettext('Register')],
566 acmedeactivate: ['ACME Account', gettext('Deactivate')],
567 acmeupdate: ['ACME Account', gettext('Update')],
568 acmerefresh: ['ACME Account', gettext('Refresh')],
569 acmerenew: ['SRV', gettext('Renew Certificate')],
570 acmerevoke: ['SRV', gettext('Revoke Certificate')],
571 'auth-realm-sync': [gettext('Realm'), gettext('Sync')],
572 'auth-realm-sync-test': [gettext('Realm'), gettext('Sync Preview')],
573 'move_volume': ['CT', gettext('Move Volume')],
574 clustercreate: ['', gettext('Create Cluster')],
575 clusterjoin: ['', gettext('Join Cluster')],
576 diskinit: ['Disk', gettext('Initialize Disk with GPT')],
577 vncproxy: ['VM/CT', gettext('Console')],
578 spiceproxy: ['VM/CT', gettext('Console') + ' (Spice)'],
579 vncshell: ['', gettext('Shell')],
580 spiceshell: ['', gettext('Shell') + ' (Spice)'],
581 qmsnapshot: ['VM', gettext('Snapshot')],
582 qmrollback: ['VM', gettext('Rollback')],
583 qmdelsnapshot: ['VM', gettext('Delete Snapshot')],
584 qmcreate: ['VM', gettext('Create')],
585 qmrestore: ['VM', gettext('Restore')],
586 qmdestroy: ['VM', gettext('Destroy')],
587 qmigrate: ['VM', gettext('Migrate')],
588 qmclone: ['VM', gettext('Clone')],
589 qmmove: ['VM', gettext('Move disk')],
590 qmtemplate: ['VM', gettext('Convert to template')],
591 qmstart: ['VM', gettext('Start')],
592 qmstop: ['VM', gettext('Stop')],
593 qmreset: ['VM', gettext('Reset')],
594 qmshutdown: ['VM', gettext('Shutdown')],
595 qmreboot: ['VM', gettext('Reboot')],
596 qmsuspend: ['VM', gettext('Hibernate')],
597 qmpause: ['VM', gettext('Pause')],
598 qmresume: ['VM', gettext('Resume')],
599 qmconfig: ['VM', gettext('Configure')],
600 vzsnapshot: ['CT', gettext('Snapshot')],
601 vzrollback: ['CT', gettext('Rollback')],
602 vzdelsnapshot: ['CT', gettext('Delete Snapshot')],
603 vzcreate: ['CT', gettext('Create')],
604 vzrestore: ['CT', gettext('Restore')],
605 vzdestroy: ['CT', gettext('Destroy')],
606 vzmigrate: ['CT', gettext('Migrate')],
607 vzclone: ['CT', gettext('Clone')],
608 vztemplate: ['CT', gettext('Convert to template')],
609 vzstart: ['CT', gettext('Start')],
610 vzstop: ['CT', gettext('Stop')],
611 vzmount: ['CT', gettext('Mount')],
612 vzumount: ['CT', gettext('Unmount')],
613 vzshutdown: ['CT', gettext('Shutdown')],
614 vzreboot: ['CT', gettext('Reboot')],
615 vzsuspend: ['CT', gettext('Suspend')],
616 vzresume: ['CT', gettext('Resume')],
617 push_file: ['CT', gettext('Push file')],
618 pull_file: ['CT', gettext('Pull file')],
619 hamigrate: ['HA', gettext('Migrate')],
620 hastart: ['HA', gettext('Start')],
621 hastop: ['HA', gettext('Stop')],
622 hashutdown: ['HA', gettext('Shutdown')],
623 srvstart: ['SRV', gettext('Start')],
624 srvstop: ['SRV', gettext('Stop')],
625 srvrestart: ['SRV', gettext('Restart')],
626 srvreload: ['SRV', gettext('Reload')],
627 cephcreatemgr: ['Ceph Manager', gettext('Create')],
628 cephdestroymgr: ['Ceph Manager', gettext('Destroy')],
629 cephcreatemon: ['Ceph Monitor', gettext('Create')],
630 cephdestroymon: ['Ceph Monitor', gettext('Destroy')],
631 cephcreateosd: ['Ceph OSD', gettext('Create')],
632 cephdestroyosd: ['Ceph OSD', gettext('Destroy')],
633 cephcreatepool: ['Ceph Pool', gettext('Create')],
634 cephdestroypool: ['Ceph Pool', gettext('Destroy')],
635 cephfscreate: ['CephFS', gettext('Create')],
636 cephcreatemds: ['Ceph Metadata Server', gettext('Create')],
637 cephdestroymds: ['Ceph Metadata Server', gettext('Destroy')],
638 imgcopy: ['', gettext('Copy data')],
639 imgdel: ['', gettext('Erase data')],
640 unknownimgdel: ['', gettext('Destroy image from unknown guest')],
641 download: ['', gettext('Download')],
642 vzdump: (type, id) => id ? `VM/CT ${id} - ${gettext('Backup')}` : gettext('Backup Job'),
643 aptupdate: ['', gettext('Update package database')],
644 startall: ['', gettext('Start all VMs and Containers')],
645 stopall: ['', gettext('Stop all VMs and Containers')],
646 migrateall: ['', gettext('Migrate all VMs and Containers')],
647 dircreate: [gettext('Directory Storage'), gettext('Create')],
648 lvmcreate: [gettext('LVM Storage'), gettext('Create')],
649 lvmthincreate: [gettext('LVM-Thin Storage'), gettext('Create')],
650 zfscreate: [gettext('ZFS Storage'), gettext('Create')],
651 },
652
653 // to add or change existing for product specific ones
654 override_task_descriptions: function(extra) {
655 for (const [key, value] of Object.entries(extra)) {
656 Proxmox.Utils.task_desc_table[key] = value;
657 }
658 },
659
660 format_task_description: function(type, id) {
661 let farray = Proxmox.Utils.task_desc_table[type];
662 let text;
663 if (!farray) {
664 text = type;
665 if (id) {
666 type += ' ' + id;
667 }
668 return text;
669 } else if (Ext.isFunction(farray)) {
670 return farray(type, id);
671 }
672 let prefix = farray[0];
673 text = farray[1];
674 if (prefix && id !== undefined) {
675 return prefix + ' ' + id + ' - ' + text;
676 }
677 return text;
678 },
679
680 format_size: function(size) {
681 let units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
682 let num = 0;
683 while (size >= 1024 && num++ <= units.length) {
684 size = size / 1024;
685 }
686
687 return size.toFixed(num > 0?2:0) + " " + units[num] + "B";
688 },
689
690 render_upid: function(value, metaData, record) {
691 let task = record.data;
692 let type = task.type || task.worker_type;
693 let id = task.id || task.worker_id;
694
695 return Proxmox.Utils.format_task_description(type, id);
696 },
697
698 render_uptime: function(value) {
699 let uptime = value;
700
701 if (uptime === undefined) {
702 return '';
703 }
704
705 if (uptime <= 0) {
706 return '-';
707 }
708
709 return Proxmox.Utils.format_duration_long(uptime);
710 },
711
712 parse_task_upid: function(upid) {
713 let task = {};
714
715 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]+):$/);
716 if (!res) {
717 throw "unable to parse upid '" + upid + "'";
718 }
719 task.node = res[1];
720 task.pid = parseInt(res[2], 16);
721 task.pstart = parseInt(res[3], 16);
722 if (res[5] !== undefined) {
723 task.task_id = parseInt(res[5], 16);
724 }
725 task.starttime = parseInt(res[6], 16);
726 task.type = res[7];
727 task.id = res[8];
728 task.user = res[9];
729
730 task.desc = Proxmox.Utils.format_task_description(task.type, task.id);
731
732 return task;
733 },
734
735 parse_task_status: function(status) {
736 if (status === 'OK') {
737 return 'ok';
738 }
739
740 if (status === 'unknown') {
741 return 'unknown';
742 }
743
744 let match = status.match(/^WARNINGS: (.*)$/);
745 if (match) {
746 return 'warning';
747 }
748
749 return 'error';
750 },
751
752 render_duration: function(value) {
753 if (value === undefined) {
754 return '-';
755 }
756 return Proxmox.Utils.format_duration_human(value);
757 },
758
759 render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
760 let servertime = new Date(value * 1000);
761 return Ext.Date.format(servertime, 'Y-m-d H:i:s');
762 },
763
764 render_zfs_health: function(value) {
765 if (typeof value === 'undefined') {
766 return "";
767 }
768 var iconCls = 'question-circle';
769 switch (value) {
770 case 'AVAIL':
771 case 'ONLINE':
772 iconCls = 'check-circle good';
773 break;
774 case 'REMOVED':
775 case 'DEGRADED':
776 iconCls = 'exclamation-circle warning';
777 break;
778 case 'UNAVAIL':
779 case 'FAULTED':
780 case 'OFFLINE':
781 iconCls = 'times-circle critical';
782 break;
783 default: //unknown
784 }
785
786 return '<i class="fa fa-' + iconCls + '"></i> ' + value;
787 },
788
789 get_help_info: function(section) {
790 let helpMap;
791 if (typeof proxmoxOnlineHelpInfo !== 'undefined') {
792 helpMap = proxmoxOnlineHelpInfo; // eslint-disable-line no-undef
793 } else if (typeof pveOnlineHelpInfo !== 'undefined') {
794 // be backward compatible with older pve-doc-generators
795 helpMap = pveOnlineHelpInfo; // eslint-disable-line no-undef
796 } else {
797 throw "no global OnlineHelpInfo map declared";
798 }
799
800 if (helpMap[section]) {
801 return helpMap[section];
802 }
803 // try to normalize - and _ separators, to support asciidoc and sphinx
804 // references at the same time.
805 let section_minus_normalized = section.replace(/_/, '-');
806 if (helpMap[section_minus_normalized]) {
807 return helpMap[section_minus_normalized];
808 }
809 let section_underscore_normalized = section.replace(/-/, '_');
810 return helpMap[section_underscore_normalized];
811 },
812
813 get_help_link: function(section) {
814 let info = Proxmox.Utils.get_help_info(section);
815 if (!info) {
816 return undefined;
817 }
818 return window.location.origin + info.link;
819 },
820
821 openXtermJsViewer: function(vmtype, vmid, nodename, vmname, cmd) {
822 let url = Ext.Object.toQueryString({
823 console: vmtype, // kvm, lxc, upgrade or shell
824 xtermjs: 1,
825 vmid: vmid,
826 vmname: vmname,
827 node: nodename,
828 cmd: cmd,
829
830 });
831 let nw = window.open("?" + url, '_blank', 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420');
832 if (nw) {
833 nw.focus();
834 }
835 },
836
837 },
838
839 singleton: true,
840 constructor: function() {
841 let me = this;
842 Ext.apply(me, me.utilities);
843
844 let IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
845 let IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
846 let IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
847 let IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
848 let IPV4_CIDR_MASK = "([0-9]{1,2})";
849 let IPV6_CIDR_MASK = "([0-9]{1,3})";
850
851
852 me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
853 me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")/" + IPV4_CIDR_MASK + "$");
854
855 /* eslint-disable no-useless-concat,no-multi-spaces */
856 let IPV6_REGEXP = "(?:" +
857 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
858 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
859 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
860 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
861 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
862 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
863 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
864 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
865 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
866 ")";
867 /* eslint-enable no-useless-concat,no-multi-spaces */
868
869 me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
870 me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")/" + IPV6_CIDR_MASK + "$");
871 me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
872
873 me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
874 me.IP64_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + "/" + IPV6_CIDR_MASK + ")|(?:" + IPV4_REGEXP + "/" + IPV4_CIDR_MASK + ")$");
875
876 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])?))";
877 me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
878
879 me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$");
880 me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$");
881 me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$");
882 me.Vlan_match = /^vlan(\\d+)/;
883 me.VlanInterface_match = /(\\w+)\\.(\\d+)/;
884 },
885 });