]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
add xtermjs to pveproxy dirs
[pve-manager.git] / www / manager6 / Utils.js
CommitLineData
b0a6d326
EK
1Ext.ns('PVE');
2
63b9faae
EK
3// avoid errors related to Accessible Rich Internet Applications
4// (access for people with disabilities)
0be88ae1 5// TODO reenable after all components are upgraded
63b9faae
EK
6Ext.enableAria = false;
7Ext.enableAriaButtons = false;
8Ext.enableAriaPanels = false;
9
b0a6d326 10// avoid errors when running without development tools
0be88ae1
DC
11if (!Ext.isDefined(Ext.global.console)) {
12 var console = {
13 dir: function() {},
14 log: function() {}
b0a6d326
EK
15 };
16}
0be88ae1 17console.log("Starting PVE Manager");
b0a6d326
EK
18
19Ext.Ajax.defaultHeaders = {
20 'Accept': 'application/json'
21};
22
23Ext.Ajax.on('beforerequest', function(conn, options) {
24 if (PVE.CSRFPreventionToken) {
0be88ae1 25 if (!options.headers) {
b0a6d326
EK
26 options.headers = {};
27 }
28 options.headers.CSRFPreventionToken = PVE.CSRFPreventionToken;
29 }
30});
31
9fa2e36d 32Ext.define('PVE.Utils', { utilities: {
b0a6d326 33
9fa2e36d 34 // this singleton contains miscellaneous utilities
b0a6d326 35
0be88ae1 36 toolkit: undefined, // (extjs|touch), set inside Toolkit.js
b0a6d326 37
98a01af2
EK
38 bus_match: /^(ide|sata|virtio|scsi)\d+$/,
39
b0a6d326
EK
40 log_severity_hash: {
41 0: "panic",
42 1: "alert",
43 2: "critical",
44 3: "error",
45 4: "warning",
46 5: "notice",
47 6: "info",
48 7: "debug"
49 },
50
51 support_level_hash: {
52 'c': gettext('Community'),
53 'b': gettext('Basic'),
54 's': gettext('Standard'),
55 'p': gettext('Premium')
56 },
57
58 noSubKeyHtml: 'You do not have a valid subscription for this server. Please visit <a target="_blank" href="http://www.proxmox.com/products/proxmox-ve/subscription-service-plans">www.proxmox.com</a> to get a list of available options.',
59
60 kvm_ostypes: {
45d6c71a
TL
61 'Linux': [
62 { desc: '4.X/3.X/2.6 Kernel', val: 'l26' },
63 { desc: '2.4 Kernel', val: 'l24' }
64 ],
65 'Microsoft Windows': [
66 { desc: '10/2016', val: 'win10' },
67 { desc: '8.x/2012/2012r2', val: 'win8' },
68 { desc: '7/2008r2', val: 'win7' },
69 { desc: 'Vista/2008', val: 'w2k8' },
70 { desc: 'XP/2003', val: 'wxp' },
71 { desc: '2000', val: 'w2k' }
72 ],
73 'Solaris Kernel': [
74 { desc: '-', val: 'solaris'}
75 ],
76 'Other': [
77 { desc: '-', val: 'other'}
78 ]
b0a6d326
EK
79 },
80
428bc4a2
DC
81 get_health_icon: function(state, circle) {
82 if (circle === undefined) {
83 circle = false;
84 }
85
86 if (state === undefined) {
87 state = 'uknown';
88 }
89
90 var icon = 'faded fa-question';
91 switch(state) {
92 case 'good':
93 icon = 'good fa-check';
94 break;
95 case 'warning':
96 icon = 'warning fa-exclamation';
97 break;
98 case 'critical':
99 icon = 'critical fa-times';
100 break;
101 default: break;
102 }
103
104 if (circle) {
105 icon += '-circle';
106 }
107
108 return icon;
109 },
110
046e640c
DC
111 map_ceph_health: {
112 'HEALTH_OK':'good',
113 'HEALTH_WARN':'warning',
114 'HEALTH_ERR':'critical'
115 },
116
dfe6d184 117 render_ceph_health: function(healthObj) {
046e640c
DC
118 var state = {
119 iconCls: PVE.Utils.get_health_icon(),
120 text: ''
121 };
122
dfe6d184 123 if (!healthObj || !healthObj.status) {
046e640c
DC
124 return state;
125 }
126
dfe6d184 127 var health = PVE.Utils.map_ceph_health[healthObj.status];
046e640c
DC
128
129 state.iconCls = PVE.Utils.get_health_icon(health, true);
dfe6d184 130 state.text = healthObj.status;
046e640c
DC
131
132 return state;
133 },
134
45d6c71a
TL
135 get_kvm_osinfo: function(value) {
136 var info = { base: 'Other' }; // default
137 if (value) {
138 Ext.each(Object.keys(PVE.Utils.kvm_ostypes), function(k) {
139 Ext.each(PVE.Utils.kvm_ostypes[k], function(e) {
140 if (e.val === value) {
141 info = { desc: e.desc, base: k };
142 }
143 });
144 });
b0a6d326 145 }
45d6c71a
TL
146 return info;
147 },
148
149 render_kvm_ostype: function (value) {
150 var osinfo = PVE.Utils.get_kvm_osinfo(value);
151 if (osinfo.desc && osinfo.desc !== '-') {
152 return osinfo.base + ' ' + osinfo.desc;
153 } else {
154 return osinfo.base;
b0a6d326 155 }
b0a6d326
EK
156 },
157
158 render_hotplug_features: function (value) {
23d3881a 159 var fa = [];
b0a6d326
EK
160
161 if (!value || (value === '0')) {
ec99b1c3 162 return gettext('Disabled');
b0a6d326
EK
163 }
164
4dcca8a4
DC
165 if (value === '1') {
166 value = 'disk,network,usb';
167 }
168
b0a6d326
EK
169 Ext.each(value.split(','), function(el) {
170 if (el === 'disk') {
171 fa.push(gettext('Disk'));
172 } else if (el === 'network') {
173 fa.push(gettext('Network'));
174 } else if (el === 'usb') {
b9628aa5 175 fa.push('USB');
b0a6d326
EK
176 } else if (el === 'memory') {
177 fa.push(gettext('Memory'));
178 } else if (el === 'cpu') {
179 fa.push(gettext('CPU'));
180 } else {
181 fa.push(el);
182 }
183 });
184
185 return fa.join(', ');
186 },
187
188 network_iface_types: {
189 eth: gettext("Network Device"),
190 bridge: 'Linux Bridge',
191 bond: 'Linux Bond',
192 OVSBridge: 'OVS Bridge',
193 OVSBond: 'OVS Bond',
194 OVSPort: 'OVS Port',
195 OVSIntPort: 'OVS IntPort'
196 },
197
198 render_network_iface_type: function(value) {
0be88ae1 199 return PVE.Utils.network_iface_types[value] ||
b0a6d326
EK
200 PVE.Utils.unknownText;
201 },
202
17c71f27
DC
203 render_qemu_bios: function(value) {
204 if (!value) {
205 return PVE.Utils.defaultText + ' (SeaBIOS)';
206 } else if (value === 'seabios') {
207 return "SeaBIOS";
208 } else if (value === 'ovmf') {
209 return "OVMF (UEFI)";
210 } else {
211 return value;
212 }
213 },
214
b0a6d326
EK
215 render_scsihw: function(value) {
216 if (!value) {
217 return PVE.Utils.defaultText + ' (LSI 53C895A)';
218 } else if (value === 'lsi') {
219 return 'LSI 53C895A';
220 } else if (value === 'lsi53c810') {
221 return 'LSI 53C810';
222 } else if (value === 'megasas') {
223 return 'MegaRAID SAS 8708EM2';
224 } else if (value === 'virtio-scsi-pci') {
49f5d1ab
EK
225 return 'VirtIO SCSI';
226 } else if (value === 'virtio-scsi-single') {
227 return 'VirtIO SCSI single';
b0a6d326
EK
228 } else if (value === 'pvscsi') {
229 return 'VMware PVSCSI';
230 } else {
231 return value;
232 }
233 },
234
235 // fixme: auto-generate this
236 // for now, please keep in sync with PVE::Tools::kvmkeymaps
237 kvm_keymaps: {
238 //ar: 'Arabic',
239 da: 'Danish',
0be88ae1
DC
240 de: 'German',
241 'de-ch': 'German (Swiss)',
242 'en-gb': 'English (UK)',
0a5b8747 243 'en-us': 'English (USA)',
b0a6d326
EK
244 es: 'Spanish',
245 //et: 'Estonia',
246 fi: 'Finnish',
0be88ae1
DC
247 //fo: 'Faroe Islands',
248 fr: 'French',
249 'fr-be': 'French (Belgium)',
b0a6d326
EK
250 'fr-ca': 'French (Canada)',
251 'fr-ch': 'French (Swiss)',
252 //hr: 'Croatia',
253 hu: 'Hungarian',
254 is: 'Icelandic',
0be88ae1 255 it: 'Italian',
b0a6d326
EK
256 ja: 'Japanese',
257 lt: 'Lithuanian',
258 //lv: 'Latvian',
0be88ae1 259 mk: 'Macedonian',
b0a6d326
EK
260 nl: 'Dutch',
261 //'nl-be': 'Dutch (Belgium)',
0be88ae1 262 no: 'Norwegian',
b0a6d326
EK
263 pl: 'Polish',
264 pt: 'Portuguese',
265 'pt-br': 'Portuguese (Brazil)',
266 //ru: 'Russian',
267 sl: 'Slovenian',
268 sv: 'Swedish',
269 //th: 'Thai',
270 tr: 'Turkish'
271 },
272
273 kvm_vga_drivers: {
274 std: gettext('Standard VGA'),
5ca366f2 275 vmware: gettext('VMware compatible'),
b0a6d326
EK
276 qxl: 'SPICE',
277 qxl2: 'SPICE dual monitor',
278 qxl3: 'SPICE three monitors',
279 qxl4: 'SPICE four monitors',
280 serial0: gettext('Serial terminal') + ' 0',
281 serial1: gettext('Serial terminal') + ' 1',
282 serial2: gettext('Serial terminal') + ' 2',
283 serial3: gettext('Serial terminal') + ' 3'
284 },
285
286 render_kvm_language: function (value) {
287 if (!value) {
288 return PVE.Utils.defaultText;
289 }
290 var text = PVE.Utils.kvm_keymaps[value];
291 if (text) {
292 return text + ' (' + value + ')';
293 }
294 return value;
295 },
296
297 kvm_keymap_array: function() {
f2782813 298 var data = [['__default__', PVE.Utils.render_kvm_language('')]];
b0a6d326
EK
299 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
300 data.push([key, PVE.Utils.render_kvm_language(value)]);
301 });
302
303 return data;
304 },
305
306 render_console_viewer: function(value) {
f2782813 307 if (!value || value === '__default__') {
b0a6d326 308 return PVE.Utils.defaultText + ' (HTML5)';
b0a6d326
EK
309 } else if (value === 'vv') {
310 return 'SPICE (remote-viewer)';
311 } else if (value === 'html5') {
312 return 'HTML5 (noVNC)';
313 } else {
314 return value;
315 }
316 },
317
318 language_map: {
319 zh_CN: 'Chinese',
320 ca: 'Catalan',
321 da: 'Danish',
322 en: 'English',
323 eu: 'Euskera (Basque)',
324 fr: 'French',
325 de: 'German',
326 it: 'Italian',
327 ja: 'Japanese',
328 nb: 'Norwegian (Bokmal)',
329 nn: 'Norwegian (Nynorsk)',
330 fa: 'Persian (Farsi)',
331 pl: 'Polish',
332 pt_BR: 'Portuguese (Brazil)',
333 ru: 'Russian',
334 sl: 'Slovenian',
335 es: 'Spanish',
336 sv: 'Swedish',
337 tr: 'Turkish'
338 },
339
340 render_language: function (value) {
341 if (!value) {
342 return PVE.Utils.defaultText + ' (English)';
343 }
344 var text = PVE.Utils.language_map[value];
345 if (text) {
346 return text + ' (' + value + ')';
347 }
348 return value;
349 },
350
351 language_array: function() {
d98a2c3c 352 var data = [['__default__', PVE.Utils.render_language('')]];
b0a6d326
EK
353 Ext.Object.each(PVE.Utils.language_map, function(key, value) {
354 data.push([key, PVE.Utils.render_language(value)]);
355 });
356
357 return data;
358 },
359
360 render_kvm_vga_driver: function (value) {
361 if (!value) {
362 return PVE.Utils.defaultText;
363 }
364 var text = PVE.Utils.kvm_vga_drivers[value];
0be88ae1 365 if (text) {
b0a6d326
EK
366 return text + ' (' + value + ')';
367 }
368 return value;
369 },
370
371 kvm_vga_driver_array: function() {
f2782813 372 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
373 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
374 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
375 });
376
377 return data;
378 },
379
380 render_kvm_startup: function(value) {
381 var startup = PVE.Parser.parseStartup(value);
382
383 var res = 'order=';
384 if (startup.order === undefined) {
385 res += 'any';
386 } else {
387 res += startup.order;
388 }
389 if (startup.up !== undefined) {
390 res += ',up=' + startup.up;
391 }
392 if (startup.down !== undefined) {
393 res += ',down=' + startup.down;
394 }
395
396 return res;
397 },
398
399 authOK: function() {
400 return Ext.util.Cookies.get('PVEAuthCookie');
401 },
402
403 authClear: function() {
404 Ext.util.Cookies.clear("PVEAuthCookie");
405 },
406
407 // fixme: remove - not needed?
408 gridLineHeigh: function() {
409 return 21;
0be88ae1 410
b0a6d326
EK
411 //if (Ext.isGecko)
412 //return 23;
413 //return 21;
414 },
415
416 extractRequestError: function(result, verbose) {
417 var msg = gettext('Successful');
418
419 if (!result.success) {
420 msg = gettext("Unknown error");
421 if (result.message) {
422 msg = result.message;
423 if (result.status) {
424 msg += ' (' + result.status + ')';
425 }
426 }
427 if (verbose && Ext.isObject(result.errors)) {
428 msg += "<br>";
429 Ext.Object.each(result.errors, function(prop, desc) {
0be88ae1 430 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
b0a6d326
EK
431 Ext.htmlEncode(desc);
432 });
0be88ae1 433 }
b0a6d326
EK
434 }
435
436 return msg;
437 },
438
439 extractFormActionError: function(action) {
440 var msg;
441 switch (action.failureType) {
442 case Ext.form.action.Action.CLIENT_INVALID:
443 msg = gettext('Form fields may not be submitted with invalid values');
444 break;
445 case Ext.form.action.Action.CONNECT_FAILURE:
446 msg = gettext('Connection error');
447 var resp = action.response;
448 if (resp.status && resp.statusText) {
449 msg += " " + resp.status + ": " + resp.statusText;
450 }
451 break;
452 case Ext.form.action.Action.LOAD_FAILURE:
453 case Ext.form.action.Action.SERVER_INVALID:
454 msg = PVE.Utils.extractRequestError(action.result, true);
455 break;
456 }
457 return msg;
458 },
459
460 // Ext.Ajax.request
461 API2Request: function(reqOpts) {
462
463 var newopts = Ext.apply({
464 waitMsg: gettext('Please wait...')
465 }, reqOpts);
466
467 if (!newopts.url.match(/^\/api2/)) {
468 newopts.url = '/api2/extjs' + newopts.url;
469 }
470 delete newopts.callback;
471
472 var createWrapper = function(successFn, callbackFn, failureFn) {
473 Ext.apply(newopts, {
474 success: function(response, options) {
475 if (options.waitMsgTarget) {
476 if (PVE.Utils.toolkit === 'touch') {
477 options.waitMsgTarget.setMasked(false);
478 } else {
479 options.waitMsgTarget.setLoading(false);
480 }
481 }
482 var result = Ext.decode(response.responseText);
483 response.result = result;
484 if (!result.success) {
485 response.htmlStatus = PVE.Utils.extractRequestError(result, true);
486 Ext.callback(callbackFn, options.scope, [options, false, response]);
487 Ext.callback(failureFn, options.scope, [response, options]);
488 return;
489 }
490 Ext.callback(callbackFn, options.scope, [options, true, response]);
491 Ext.callback(successFn, options.scope, [response, options]);
492 },
493 failure: function(response, options) {
494 if (options.waitMsgTarget) {
495 if (PVE.Utils.toolkit === 'touch') {
496 options.waitMsgTarget.setMasked(false);
497 } else {
498 options.waitMsgTarget.setLoading(false);
499 }
500 }
501 response.result = {};
502 try {
503 response.result = Ext.decode(response.responseText);
504 } catch(e) {}
505 var msg = gettext('Connection error') + ' - server offline?';
506 if (response.aborted) {
507 msg = gettext('Connection error') + ' - aborted.';
508 } else if (response.timedout) {
509 msg = gettext('Connection error') + ' - Timeout.';
510 } else if (response.status && response.statusText) {
511 msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText;
512 }
513 response.htmlStatus = msg;
514 Ext.callback(callbackFn, options.scope, [options, false, response]);
515 Ext.callback(failureFn, options.scope, [response, options]);
516 }
517 });
518 };
519
520 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
521
522 var target = newopts.waitMsgTarget;
523 if (target) {
524 if (PVE.Utils.toolkit === 'touch') {
525 target.setMasked({ xtype: 'loadmask', message: newopts.waitMsg} );
526 } else {
527 // Note: ExtJS bug - this does not work when component is not rendered
528 target.setLoading(newopts.waitMsg);
529 }
530 }
531 Ext.Ajax.request(newopts);
532 },
533
534 assemble_field_data: function(values, data) {
535 if (Ext.isObject(data)) {
536 Ext.Object.each(data, function(name, val) {
537 if (values.hasOwnProperty(name)) {
538 var bucket = values[name];
539 if (!Ext.isArray(bucket)) {
540 bucket = values[name] = [bucket];
541 }
542 if (Ext.isArray(val)) {
543 values[name] = bucket.concat(val);
544 } else {
545 bucket.push(val);
546 }
547 } else {
548 values[name] = val;
549 }
550 });
551 }
552 },
553
554 checked_command: function(orig_cmd) {
555 PVE.Utils.API2Request({
556 url: '/nodes/localhost/subscription',
557 method: 'GET',
558 //waitMsgTarget: me,
559 failure: function(response, opts) {
560 Ext.Msg.alert(gettext('Error'), response.htmlStatus);
561 },
562 success: function(response, opts) {
563 var data = response.result.data;
564
565 if (data.status !== 'Active') {
566 Ext.Msg.show({
567 title: gettext('No valid subscription'),
568 icon: Ext.Msg.WARNING,
569 msg: PVE.Utils.noSubKeyHtml,
570 buttons: Ext.Msg.OK,
571 callback: function(btn) {
572 if (btn !== 'ok') {
573 return;
574 }
575 orig_cmd();
576 }
577 });
578 } else {
579 orig_cmd();
580 }
581 }
582 });
583 },
584
585 task_desc_table: {
b9628aa5 586 diskinit: [ 'Disk', gettext('Initialize Disk with GPT') ],
b0a6d326
EK
587 vncproxy: [ 'VM/CT', gettext('Console') ],
588 spiceproxy: [ 'VM/CT', gettext('Console') + ' (Spice)' ],
589 vncshell: [ '', gettext('Shell') ],
590 spiceshell: [ '', gettext('Shell') + ' (Spice)' ],
591 qmsnapshot: [ 'VM', gettext('Snapshot') ],
592 qmrollback: [ 'VM', gettext('Rollback') ],
593 qmdelsnapshot: [ 'VM', gettext('Delete Snapshot') ],
594 qmcreate: [ 'VM', gettext('Create') ],
595 qmrestore: [ 'VM', gettext('Restore') ],
596 qmdestroy: [ 'VM', gettext('Destroy') ],
597 qmigrate: [ 'VM', gettext('Migrate') ],
598 qmclone: [ 'VM', gettext('Clone') ],
599 qmmove: [ 'VM', gettext('Move disk') ],
600 qmtemplate: [ 'VM', gettext('Convert to template') ],
601 qmstart: [ 'VM', gettext('Start') ],
602 qmstop: [ 'VM', gettext('Stop') ],
603 qmreset: [ 'VM', gettext('Reset') ],
604 qmshutdown: [ 'VM', gettext('Shutdown') ],
605 qmsuspend: [ 'VM', gettext('Suspend') ],
606 qmresume: [ 'VM', gettext('Resume') ],
607 qmconfig: [ 'VM', gettext('Configure') ],
16152937
DM
608 vzsnapshot: [ 'CT', gettext('Snapshot') ],
609 vzrollback: [ 'CT', gettext('Rollback') ],
610 vzdelsnapshot: [ 'CT', gettext('Delete Snapshot') ],
b0a6d326
EK
611 vzcreate: ['CT', gettext('Create') ],
612 vzrestore: ['CT', gettext('Restore') ],
613 vzdestroy: ['CT', gettext('Destroy') ],
614 vzmigrate: [ 'CT', gettext('Migrate') ],
bfade4b4
DM
615 vzclone: [ 'CT', gettext('Clone') ],
616 vztemplate: [ 'CT', gettext('Convert to template') ],
b0a6d326
EK
617 vzstart: ['CT', gettext('Start') ],
618 vzstop: ['CT', gettext('Stop') ],
619 vzmount: ['CT', gettext('Mount') ],
620 vzumount: ['CT', gettext('Unmount') ],
621 vzshutdown: ['CT', gettext('Shutdown') ],
622 vzsuspend: [ 'CT', gettext('Suspend') ],
623 vzresume: [ 'CT', gettext('Resume') ],
624 hamigrate: [ 'HA', gettext('Migrate') ],
625 hastart: [ 'HA', gettext('Start') ],
626 hastop: [ 'HA', gettext('Stop') ],
627 srvstart: ['SRV', gettext('Start') ],
628 srvstop: ['SRV', gettext('Stop') ],
629 srvrestart: ['SRV', gettext('Restart') ],
630 srvreload: ['SRV', gettext('Reload') ],
631 cephcreatemon: ['Ceph Monitor', gettext('Create') ],
632 cephdestroymon: ['Ceph Monitor', gettext('Destroy') ],
633 cephcreateosd: ['Ceph OSD', gettext('Create') ],
634 cephdestroyosd: ['Ceph OSD', gettext('Destroy') ],
fe15d330 635 cephcreatepool: ['Ceph Pool', gettext('Create') ],
2125c18a 636 cephdestroypool: ['Ceph Pool', gettext('Destroy') ],
b0a6d326
EK
637 imgcopy: ['', gettext('Copy data') ],
638 imgdel: ['', gettext('Erase data') ],
639 download: ['', gettext('Download') ],
640 vzdump: ['', gettext('Backup') ],
641 aptupdate: ['', gettext('Update package database') ],
642 startall: [ '', gettext('Start all VMs and Containers') ],
643 stopall: [ '', gettext('Stop all VMs and Containers') ],
644 migrateall: [ '', gettext('Migrate all VMs and Containers') ]
645 },
646
0be88ae1 647 format_task_description: function(type, id) {
b0a6d326
EK
648 var farray = PVE.Utils.task_desc_table[type];
649 if (!farray) {
650 return type;
651 }
652 var prefix = farray[0];
653 var text = farray[1];
654 if (prefix) {
0be88ae1 655 return prefix + ' ' + id + ' - ' + text;
b0a6d326
EK
656 }
657 return text;
658 },
659
660 parse_task_upid: function(upid) {
661 var task = {};
662
663 var res = upid.match(/^UPID:(\S+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8,9}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/);
664 if (!res) {
665 throw "unable to parse upid '" + upid + "'";
666 }
667 task.node = res[1];
668 task.pid = parseInt(res[2], 16);
669 task.pstart = parseInt(res[3], 16);
670 task.starttime = parseInt(res[4], 16);
671 task.type = res[5];
672 task.id = res[6];
673 task.user = res[7];
674
675 task.desc = PVE.Utils.format_task_description(task.type, task.id);
676
677 return task;
678 },
679
680 format_size: function(size) {
681 /*jslint confusion: true */
682
02f47fe8
DC
683 var units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
684 var num = 0;
b0a6d326 685
02f47fe8
DC
686 while (size >= 1024 && ((num++)+1) < units.length) {
687 size = size / 1024;
b0a6d326
EK
688 }
689
02f47fe8 690 return size.toFixed((num > 0)?2:0) + " " + units[num] + "B";
b0a6d326
EK
691 },
692
693 format_html_bar: function(per, text) {
694
695 return "<div class='pve-bar-wrap'>" + text + "<div class='pve-bar-border'>" +
696 "<div class='pve-bar-inner' style='width:" + per + "%;'></div>" +
697 "</div></div>";
0be88ae1 698
b0a6d326
EK
699 },
700
701 format_cpu_bar: function(per1, per2, text) {
702
703 return "<div class='pve-bar-border'>" +
704 "<div class='pve-bar-inner' style='width:" + per1 + "%;'></div>" +
705 "<div class='pve-bar-inner2' style='width:" + per2 + "%;'></div>" +
0be88ae1 706 "<div class='pve-bar-text'>" + text + "</div>" +
b0a6d326
EK
707 "</div>";
708 },
709
710 format_large_bar: function(per, text) {
711
712 if (!text) {
713 text = per.toFixed(1) + "%";
714 }
715
716 return "<div class='pve-largebar-border'>" +
717 "<div class='pve-largebar-inner' style='width:" + per + "%;'></div>" +
0be88ae1 718 "<div class='pve-largebar-text'>" + text + "</div>" +
b0a6d326
EK
719 "</div>";
720 },
721
722 format_duration_long: function(ut) {
723
724 var days = Math.floor(ut / 86400);
725 ut -= days*86400;
726 var hours = Math.floor(ut / 3600);
727 ut -= hours*3600;
728 var mins = Math.floor(ut / 60);
729 ut -= mins*60;
730
731 var hours_str = '00' + hours.toString();
732 hours_str = hours_str.substr(hours_str.length - 2);
733 var mins_str = "00" + mins.toString();
734 mins_str = mins_str.substr(mins_str.length - 2);
735 var ut_str = "00" + ut.toString();
736 ut_str = ut_str.substr(ut_str.length - 2);
737
738 if (days) {
739 var ds = days > 1 ? PVE.Utils.daysText : PVE.Utils.dayText;
0be88ae1 740 return days.toString() + ' ' + ds + ' ' +
b0a6d326
EK
741 hours_str + ':' + mins_str + ':' + ut_str;
742 } else {
743 return hours_str + ':' + mins_str + ':' + ut_str;
744 }
745 },
746
747 format_duration_short: function(ut) {
0be88ae1 748
b0a6d326 749 if (ut < 60) {
530ee6b7 750 return ut.toFixed(1) + 's';
b0a6d326
EK
751 }
752
753 if (ut < 3600) {
754 var mins = ut / 60;
530ee6b7 755 return mins.toFixed(1) + 'm';
b0a6d326
EK
756 }
757
758 if (ut < 86400) {
759 var hours = ut / 3600;
530ee6b7 760 return hours.toFixed(1) + 'h';
b0a6d326
EK
761 }
762
763 var days = ut / 86400;
530ee6b7 764 return days.toFixed(1) + 'd';
b0a6d326
EK
765 },
766
767 yesText: gettext('Yes'),
768 noText: gettext('No'),
a3b8efb4
EK
769 enabledText: gettext('Enabled'),
770 disabledText: gettext('Disabled'),
b0a6d326
EK
771 noneText: gettext('none'),
772 errorText: gettext('Error'),
773 unknownText: gettext('Unknown'),
774 defaultText: gettext('Default'),
775 daysText: gettext('days'),
776 dayText: gettext('day'),
777 runningText: gettext('running'),
778 stoppedText: gettext('stopped'),
779 neverText: gettext('never'),
780 totalText: gettext('Total'),
781 usedText: gettext('Used'),
782 directoryText: gettext('Directory'),
783 imagesText: gettext('Disk image'),
784 backupFileText: gettext('VZDump backup file'),
2c554952 785 vztmplText: gettext('Container template'),
b0a6d326 786 isoImageText: gettext('ISO image'),
2c554952 787 containersText: gettext('Container'),
45ab85bb
DM
788 stateText: gettext('State'),
789 groupText: gettext('Group'),
b0a6d326
EK
790
791 format_expire: function(date) {
792 if (!date) {
793 return PVE.Utils.neverText;
794 }
795 return Ext.Date.format(date, "Y-m-d");
796 },
797
3c23c025 798 format_storage_type: function(value, md, record) {
6760ab92
DC
799 if (value === 'rbd' && record) {
800 value = (record.get('monhost')?'rbd_ext':'pveceph');
3c23c025 801 }
b0a6d326
EK
802 if (value === 'dir') {
803 return PVE.Utils.directoryText;
804 } else if (value === 'nfs') {
805 return 'NFS';
806 } else if (value === 'glusterfs') {
807 return 'GlusterFS';
808 } else if (value === 'lvm') {
809 return 'LVM';
ffd96a3b
DM
810 } else if (value === 'lvmthin') {
811 return 'LVM-Thin';
b0a6d326
EK
812 } else if (value === 'iscsi') {
813 return 'iSCSI';
814 } else if (value === 'rbd') {
6760ab92
DC
815 return 'RBD';
816 } else if (value === 'rbd_ext') {
3c23c025
DC
817 return 'RBD (external)';
818 } else if (value === 'pveceph') {
819 return 'RBD (PVE)';
b0a6d326
EK
820 } else if (value === 'sheepdog') {
821 return 'Sheepdog';
822 } else if (value === 'zfs') {
823 return 'ZFS over iSCSI';
824 } else if (value === 'zfspool') {
825 return 'ZFS';
826 } else if (value === 'iscsidirect') {
827 return 'iSCSIDirect';
ffd96a3b
DM
828 } else if (value === 'drbd') {
829 return 'DRBD';
b0a6d326
EK
830 } else {
831 return PVE.Utils.unknownText;
832 }
833 },
834
835 format_boolean_with_default: function(value) {
f2782813 836 if (Ext.isDefined(value) && value !== '__default__') {
b0a6d326
EK
837 return value ? PVE.Utils.yesText : PVE.Utils.noText;
838 }
839 return PVE.Utils.defaultText;
840 },
841
842 format_boolean: function(value) {
843 return value ? PVE.Utils.yesText : PVE.Utils.noText;
844 },
845
846 format_neg_boolean: function(value) {
847 return !value ? PVE.Utils.yesText : PVE.Utils.noText;
848 },
849
a3b8efb4
EK
850 format_enabled_toggle: function(value) {
851 return value ? PVE.Utils.enabledText :PVE.Utils.disabledText;
852 },
853
ced1677b 854 format_ha: function(value) {
50713765 855 var text = PVE.Utils.noneText;
ced1677b
TL
856
857 if (value.managed) {
50713765 858 text = value.state || PVE.Utils.noneText;
ced1677b 859
45ab85bb 860 text += ', ' + PVE.Utils.groupText + ': ';
f25d7c4a 861 text += value.group || PVE.Utils.noneText;
ced1677b
TL
862 }
863
864 return text;
865 },
866
b0a6d326
EK
867 format_content_types: function(value) {
868 var cta = [];
869
870 Ext.each(value.split(',').sort(), function(ct) {
871 if (ct === 'images') {
872 cta.push(PVE.Utils.imagesText);
873 } else if (ct === 'backup') {
874 cta.push(PVE.Utils.backupFileText);
875 } else if (ct === 'vztmpl') {
876 cta.push(PVE.Utils.vztmplText);
877 } else if (ct === 'iso') {
878 cta.push(PVE.Utils.isoImageText);
879 } else if (ct === 'rootdir') {
880 cta.push(PVE.Utils.containersText);
881 }
882 });
883
884 return cta.join(', ');
885 },
886
887 render_storage_content: function(value, metaData, record) {
888 var data = record.data;
889 if (Ext.isNumber(data.channel) &&
890 Ext.isNumber(data.id) &&
891 Ext.isNumber(data.lun)) {
0be88ae1
DC
892 return "CH " +
893 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
894 " ID " + data.id + " LUN " + data.lun;
895 }
896 return data.volid.replace(/^.*:(.*\/)?/,'');
897 },
898
899 render_serverity: function (value) {
900 return PVE.Utils.log_severity_hash[value] || value;
901 },
902
903 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
904
905 if (!(record.data.uptime && Ext.isNumeric(value))) {
906 return '';
907 }
908
909 var maxcpu = record.data.maxcpu || 1;
910
911 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
912 return '';
913 }
0be88ae1 914
b0a6d326
EK
915 var per = value * 100;
916
917 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
918 },
919
920 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
921 /*jslint confusion: true */
922
923 if (!Ext.isNumeric(value)) {
924 return '';
925 }
926
927 return PVE.Utils.format_size(value);
928 },
929
946730cd
DC
930 render_bandwidth: function(value) {
931 if (!Ext.isNumeric(value)) {
932 return '';
933 }
934
935 return PVE.Utils.format_size(value) + '/s';
936 },
937
b0a6d326
EK
938 render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
939 var servertime = new Date(value * 1000);
940 return Ext.Date.format(servertime, 'Y-m-d H:i:s');
941 },
942
3f633655
EK
943 render_timestamp_human_readable: function(value) {
944 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
945 },
946
ddd26302
DC
947 render_duration: function(value) {
948 if (value === undefined) {
949 return '-';
950 }
951 return PVE.Utils.format_duration_short(value);
952 },
953
0bfc799f
DC
954 calculate_mem_usage: function(data) {
955 if (!Ext.isNumeric(data.mem) ||
956 data.maxmem === 0 ||
957 data.uptime < 1) {
958 return -1;
959 }
960
961 return (data.mem / data.maxmem);
962 },
963
964 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
965 if (!Ext.isNumeric(value) || value === -1) {
966 return '';
967 }
968 if (value > 1 ) {
969 // we got no percentage but bytes
970 var mem = value;
971 var maxmem = record.data.maxmem;
972 if (!record.data.uptime ||
973 maxmem === 0 ||
974 !Ext.isNumeric(mem)) {
975 return '';
976 }
977
978 return ((mem*100)/maxmem).toFixed(1) + " %";
979 }
980 return (value*100).toFixed(1) + " %";
981 },
982
b0a6d326
EK
983 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
984
985 var mem = value;
986 var maxmem = record.data.maxmem;
0be88ae1 987
b0a6d326
EK
988 if (!record.data.uptime) {
989 return '';
990 }
991
992 if (!(Ext.isNumeric(mem) && maxmem)) {
993 return '';
994 }
995
728f1b97 996 return PVE.Utils.render_size(value);
b0a6d326
EK
997 },
998
0bfc799f
DC
999 calculate_disk_usage: function(data) {
1000
1001 if (!Ext.isNumeric(data.disk) ||
1002 data.type === 'qemu' ||
1003 (data.type === 'lxc' && data.uptime === 0) ||
1004 data.maxdisk === 0) {
1005 return -1;
1006 }
1007
1008 return (data.disk / data.maxdisk);
1009 },
1010
1011 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
1012 if (!Ext.isNumeric(value) || value === -1) {
1013 return '';
1014 }
1015
1016 return (value * 100).toFixed(1) + " %";
1017 },
1018
b0a6d326
EK
1019 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
1020
1021 var disk = value;
1022 var maxdisk = record.data.maxdisk;
728f1b97 1023 var type = record.data.type;
b0a6d326 1024
728f1b97
DC
1025 if (!Ext.isNumeric(disk) ||
1026 type === 'qemu' ||
1027 maxdisk === 0 ||
1028 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
1029 return '';
1030 }
1031
728f1b97 1032 return PVE.Utils.render_size(value);
b0a6d326
EK
1033 },
1034
4dbc64a7
DC
1035 get_object_icon_class: function(type, record) {
1036 var status = '';
1037 var objType = type;
1038
1039 if (type === 'type') {
1040 // for folder view
1041 objType = record.groupbyid;
1042 } else if (record.template) {
1043 // templates
1044 objType = 'template';
1045 status = type;
1046 } else {
1047 // everything else
1048 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
1049 }
1050
4dbc64a7
DC
1051 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
1052 if (defaults && defaults.iconCls) {
1053 var retVal = defaults.iconCls + ' ' + status;
1054 return retVal;
b0a6d326
EK
1055 }
1056
4dbc64a7
DC
1057 return '';
1058 },
1059
1060 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
1061
1062 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 1063
4dbc64a7 1064 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 1065 return fa + value;
b0a6d326
EK
1066 },
1067
1068 render_uptime: function(value, metaData, record, rowIndex, colIndex, store) {
1069
1070 var uptime = value;
1071
1072 if (uptime === undefined) {
1073 return '';
1074 }
0be88ae1 1075
b0a6d326
EK
1076 if (uptime <= 0) {
1077 return '-';
1078 }
1079
1080 return PVE.Utils.format_duration_long(uptime);
1081 },
1082
1083 render_support_level: function(value, metaData, record) {
1084 return PVE.Utils.support_level_hash[value] || '-';
1085 },
1086
0be88ae1 1087 render_upid: function(value, metaData, record) {
b0a6d326
EK
1088 var type = record.data.type;
1089 var id = record.data.id;
1090
1091 return PVE.Utils.format_task_description(type, id);
1092 },
1093
054ac1b8
DC
1094 /* render functions for new status panel */
1095
1096 render_usage: function(val) {
1097 return (val*100).toFixed(2) + '%';
1098 },
1099
1100 render_cpu_usage: function(val, max) {
1101 return Ext.String.format(gettext('{0}% of {1}') +
1102 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
1103 },
1104
1105 render_size_usage: function(val, max) {
bab64974
DC
1106 if (max === 0) {
1107 return gettext('N/A');
1108 }
054ac1b8
DC
1109 return (val*100/max).toFixed(2) + '% '+ '(' +
1110 Ext.String.format(gettext('{0} of {1}'),
1111 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
1112 },
1113
1114 /* this is different for nodes */
1115 render_node_cpu_usage: function(value, record) {
1116 return PVE.Utils.render_cpu_usage(value, record.cpus);
1117 },
1118
1119 /* this is different for nodes */
1120 render_node_size_usage: function(record) {
1121 return PVE.Utils.render_size_usage(record.used, record.total);
1122 },
1123
b0a6d326
EK
1124 dialog_title: function(subject, create, isAdd) {
1125 if (create) {
1126 if (isAdd) {
1127 return gettext('Add') + ': ' + subject;
1128 } else {
1129 return gettext('Create') + ': ' + subject;
1130 }
1131 } else {
1132 return gettext('Edit') + ': ' + subject;
1133 }
1134 },
aa0819a8
WB
1135
1136 windowHostname: function() {
c15c61d3 1137 return window.location.hostname.replace(PVE.Utils.IP6_bracket_match,
aa0819a8
WB
1138 function(m, addr, offset, original) { return addr; });
1139 },
0be88ae1 1140
b0a6d326
EK
1141 openDefaultConsoleWindow: function(allowSpice, vmtype, vmid, nodename, vmname) {
1142 var dv = PVE.Utils.defaultViewer(allowSpice);
1143 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
1144 },
1145
1146 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
9e361643 1147 // kvm, lxc, shell, upgrade
b0a6d326 1148
9e361643 1149 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
1150 throw "missing vmid";
1151 }
1152
1153 if (!nodename) {
1154 throw "no nodename specified";
1155 }
1156
c7218ab3
DC
1157 if (viewer === 'html5') {
1158 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
b0a6d326
EK
1159 } else if (viewer === 'vv') {
1160 var url;
aa0819a8 1161 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
1162 if (vmtype === 'kvm') {
1163 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
1164 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
1165 } else if (vmtype === 'lxc') {
1166 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
1167 PVE.Utils.openSpiceViewer(url, params);
1168 } else if (vmtype === 'shell') {
1169 url = '/nodes/' + nodename + '/spiceshell';
1170 PVE.Utils.openSpiceViewer(url, params);
1171 } else if (vmtype === 'upgrade') {
1172 url = '/nodes/' + nodename + '/spiceshell';
1173 params.upgrade = 1;
1174 PVE.Utils.openSpiceViewer(url, params);
1175 }
1176 } else {
1177 throw "unknown viewer type";
1178 }
1179 },
1180
1181 defaultViewer: function(allowSpice) {
1182 var vncdefault = 'html5';
1183 var dv = PVE.VersionInfo.console || vncdefault;
1184 if (dv === 'vv' && !allowSpice) {
1185 dv = vncdefault;
1186 }
1187
1188 return dv;
1189 },
1190
c7218ab3 1191 openVNCViewer: function(vmtype, vmid, nodename, vmname) {
b0a6d326 1192 var url = Ext.urlEncode({
9e361643 1193 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 1194 novnc: 1,
b0a6d326
EK
1195 vmid: vmid,
1196 vmname: vmname,
1197 node: nodename
1198 });
1199 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
1200 nw.focus();
1201 },
1202
1203 openSpiceViewer: function(url, params){
1204
1205 var downloadWithName = function(uri, name) {
1206 var link = Ext.DomHelper.append(document.body, {
1207 tag: 'a',
1208 href: uri,
1209 css : 'display:none;visibility:hidden;height:0px;'
1210 });
1211
1212 // Note: we need to tell android the correct file name extension
1213 // but we do not set 'download' tag for other environments, because
1214 // It can have strange side effects (additional user prompt on firefox)
1215 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
1216 if (andriod) {
1217 link.download = name;
1218 }
1219
1220 if (link.fireEvent) {
1221 link.fireEvent('onclick');
1222 } else {
1223 var evt = document.createEvent("MouseEvents");
1224 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
1225 link.dispatchEvent(evt);
1226 }
1227 };
1228
1229 PVE.Utils.API2Request({
1230 url: url,
1231 params: params,
1232 method: 'POST',
1233 failure: function(response, opts){
1234 Ext.Msg.alert('Error', response.htmlStatus);
1235 },
1236 success: function(response, opts){
1237 var raw = "[virt-viewer]\n";
1238 Ext.Object.each(response.result.data, function(k, v) {
1239 raw += k + "=" + v + "\n";
1240 });
1241 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1242 encodeURIComponent(raw);
0be88ae1 1243
b0a6d326
EK
1244 downloadWithName(url, "pve-spice.vv");
1245 }
1246 });
1247 },
1248
0be88ae1 1249 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
b0a6d326
EK
1250 // use el.mask() instead
1251 setErrorMask: function(comp, msg) {
1252 var el = comp.el;
1253 if (!el) {
1254 return;
1255 }
1256 if (!msg) {
1257 el.unmask();
1258 } else {
1259 if (msg === true) {
1260 el.mask(gettext("Loading..."));
1261 } else {
1262 el.mask(msg);
1263 }
1264 }
1265 },
1266
1267 monStoreErrors: function(me, store) {
1268 me.mon(store, 'beforeload', function(s, operation, eOpts) {
1269 if (!me.loadCount) {
1270 me.loadCount = 0; // make sure it is numeric
1271 PVE.Utils.setErrorMask(me, true);
1272 }
1273 });
1274
1275 // only works with 'pve' proxy
1276 me.mon(store.proxy, 'afterload', function(proxy, request, success) {
1277 me.loadCount++;
1278
1279 if (success) {
1280 PVE.Utils.setErrorMask(me, false);
1281 return;
1282 }
1283
1284 var msg;
23d3881a 1285 /*jslint nomen: true */
86826854 1286 var operation = request._operation;
b0a6d326
EK
1287 var error = operation.getError();
1288 if (error.statusText) {
1289 msg = error.statusText + ' (' + error.status + ')';
1290 } else {
1291 msg = gettext('Connection error');
1292 }
1293 PVE.Utils.setErrorMask(me, msg);
1294 });
685b7aa4 1295 },
b0a6d326 1296
e3129443
DC
1297 openTreeConsole: function(tree, record, item, index, e) {
1298 e.stopEvent();
1299 var nodename = record.data.node;
1300 var vmid = record.data.vmid;
1301 var vmname = record.data.name;
1302 if (record.data.type === 'qemu' && !record.data.template) {
1303 PVE.Utils.API2Request({
1304 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
1305 failure: function(response, opts) {
1306 Ext.Msg.alert('Error', response.htmlStatus);
1307 },
1308 success: function(response, opts) {
1309 var allowSpice = response.result.data.spice;
1310 PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
1311 }
1312 });
1313 } else if (record.data.type === 'lxc' && !record.data.template) {
1314 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
1315 }
1316 },
1317
fbd60cfd
DM
1318 // test automation helper
1319 call_menu_handler: function(menu, text) {
1320
1321 var list = menu.query('menuitem');
1322
1323 Ext.Array.each(list, function(item) {
1324 if (item.text === text) {
1325 if (item.handler) {
1326 item.handler();
1327 return 1;
1328 } else {
1329 return undefined;
1330 }
1331 }
1332 });
1333 },
1334
685b7aa4
DC
1335 createCmdMenu: function(v, record, item, index, event) {
1336 event.stopEvent();
cc1a91be
DC
1337 if (!(v instanceof Ext.tree.View)) {
1338 v.select(record);
1339 }
685b7aa4
DC
1340 var menu;
1341
1342 if (record.data.type === 'qemu' && !record.data.template) {
1343 menu = Ext.create('PVE.qemu.CmdMenu', {
1344 pveSelNode: record
1345 });
1346 } else if (record.data.type === 'qemu' && record.data.template) {
1347 menu = Ext.create('PVE.qemu.TemplateMenu', {
1348 pveSelNode: record
1349 });
1350 } else if (record.data.type === 'lxc' && !record.data.template) {
1351 menu = Ext.create('PVE.lxc.CmdMenu', {
1352 pveSelNode: record
1353 });
1354 } else if (record.data.type === 'lxc' && record.data.template) {
1355 /* since clone does not work reliably, disable for now
1356 menu = Ext.create('PVE.lxc.TemplateMenu', {
1357 pveSelNode: record
1358 });
1359 */
1360 return;
c11ab8cb
DC
1361
1362 } else if (record.data.type === 'node' ){
1363 menu = Ext.create('PVE.node.CmdMenu', {
1364 nodename: record.data.node
1365 });
1366
685b7aa4
DC
1367 } else {
1368 return;
1369 }
1370
1371 menu.showAt(event.getXY());
9fa2e36d
EK
1372 }},
1373
fe4f00ad
TL
1374 // helper for deleting field which are set to there default values
1375 delete_if_default: function(values, fieldname, default_val, create) {
1376 if (values[fieldname] === '' || values[fieldname] === default_val) {
1377 if (!create) {
1378 if (values['delete']) {
1379 values['delete'] += ',' + fieldname;
1380 } else {
1381 values['delete'] = fieldname;
1382 }
1383 }
1384
1385 delete values[fieldname];
1386 }
1387 },
1388
9fa2e36d
EK
1389 singleton: true,
1390 constructor: function() {
1391 var me = this;
1392 Ext.apply(me, me.utilities);
c15c61d3
EK
1393
1394 var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
1395 var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
1396 var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
1397 var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
1398
1399
1400 me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
1401 me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$");
1402
1403 var IPV6_REGEXP = "(?:" +
1404 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
1405 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
1406 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
1407 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
1408 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
1409 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
1410 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
1411 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
1412 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
1413 ")";
1414
1415 me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
1416 me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$");
1417 me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
1418
1419 me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
1420
1421 var DnsName_REGEXP = "(?:(([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)\\.)*([A-Za-z0-9]([A-Za-z0-9\\-]*[A-Za-z0-9])?))";
1422 me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
1423
1424 me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$");
1425 me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$");
1426 me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$");
685b7aa4 1427 }
9fa2e36d 1428});
b0a6d326 1429