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