]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
dc/OptionView: cleanup & use new features from ObjectGrid
[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
17c71f27
DC
188 render_qemu_bios: function(value) {
189 if (!value) {
e7ade592 190 return Proxmox.Utils.defaultText + ' (SeaBIOS)';
17c71f27
DC
191 } else if (value === 'seabios') {
192 return "SeaBIOS";
193 } else if (value === 'ovmf') {
194 return "OVMF (UEFI)";
195 } else {
196 return value;
197 }
198 },
199
b0a6d326
EK
200 render_scsihw: function(value) {
201 if (!value) {
e7ade592 202 return Proxmox.Utils.defaultText + ' (LSI 53C895A)';
b0a6d326
EK
203 } else if (value === 'lsi') {
204 return 'LSI 53C895A';
205 } else if (value === 'lsi53c810') {
206 return 'LSI 53C810';
207 } else if (value === 'megasas') {
208 return 'MegaRAID SAS 8708EM2';
209 } else if (value === 'virtio-scsi-pci') {
49f5d1ab
EK
210 return 'VirtIO SCSI';
211 } else if (value === 'virtio-scsi-single') {
212 return 'VirtIO SCSI single';
b0a6d326
EK
213 } else if (value === 'pvscsi') {
214 return 'VMware PVSCSI';
215 } else {
216 return value;
217 }
218 },
219
220 // fixme: auto-generate this
221 // for now, please keep in sync with PVE::Tools::kvmkeymaps
222 kvm_keymaps: {
223 //ar: 'Arabic',
224 da: 'Danish',
0be88ae1
DC
225 de: 'German',
226 'de-ch': 'German (Swiss)',
227 'en-gb': 'English (UK)',
0a5b8747 228 'en-us': 'English (USA)',
b0a6d326
EK
229 es: 'Spanish',
230 //et: 'Estonia',
231 fi: 'Finnish',
0be88ae1
DC
232 //fo: 'Faroe Islands',
233 fr: 'French',
234 'fr-be': 'French (Belgium)',
b0a6d326
EK
235 'fr-ca': 'French (Canada)',
236 'fr-ch': 'French (Swiss)',
237 //hr: 'Croatia',
238 hu: 'Hungarian',
239 is: 'Icelandic',
0be88ae1 240 it: 'Italian',
b0a6d326
EK
241 ja: 'Japanese',
242 lt: 'Lithuanian',
243 //lv: 'Latvian',
0be88ae1 244 mk: 'Macedonian',
b0a6d326
EK
245 nl: 'Dutch',
246 //'nl-be': 'Dutch (Belgium)',
0be88ae1 247 no: 'Norwegian',
b0a6d326
EK
248 pl: 'Polish',
249 pt: 'Portuguese',
250 'pt-br': 'Portuguese (Brazil)',
251 //ru: 'Russian',
252 sl: 'Slovenian',
253 sv: 'Swedish',
254 //th: 'Thai',
255 tr: 'Turkish'
256 },
257
258 kvm_vga_drivers: {
259 std: gettext('Standard VGA'),
5ca366f2 260 vmware: gettext('VMware compatible'),
b0a6d326
EK
261 qxl: 'SPICE',
262 qxl2: 'SPICE dual monitor',
263 qxl3: 'SPICE three monitors',
264 qxl4: 'SPICE four monitors',
265 serial0: gettext('Serial terminal') + ' 0',
266 serial1: gettext('Serial terminal') + ' 1',
267 serial2: gettext('Serial terminal') + ' 2',
268 serial3: gettext('Serial terminal') + ' 3'
269 },
270
271 render_kvm_language: function (value) {
e7ade592
DC
272 if (!value || value === '__default__') {
273 return Proxmox.Utils.defaultText;
b0a6d326
EK
274 }
275 var text = PVE.Utils.kvm_keymaps[value];
276 if (text) {
277 return text + ' (' + value + ')';
278 }
279 return value;
280 },
281
282 kvm_keymap_array: function() {
f2782813 283 var data = [['__default__', PVE.Utils.render_kvm_language('')]];
b0a6d326
EK
284 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
285 data.push([key, PVE.Utils.render_kvm_language(value)]);
286 });
287
288 return data;
289 },
290
291 render_console_viewer: function(value) {
f2782813 292 if (!value || value === '__default__') {
e7ade592 293 return Proxmox.Utils.defaultText + ' (HTML5)';
b0a6d326
EK
294 } else if (value === 'vv') {
295 return 'SPICE (remote-viewer)';
296 } else if (value === 'html5') {
297 return 'HTML5 (noVNC)';
298 } else {
299 return value;
300 }
301 },
302
755b9083
TL
303 console_viewer_array: function() {
304 return Ext.Array.map(['__default__','vv', 'html5'], function(v) {
305 return [v, PVE.Utils.render_console_viewer(v)];
306 });
307 },
308
b0a6d326
EK
309 render_kvm_vga_driver: function (value) {
310 if (!value) {
e7ade592 311 return Proxmox.Utils.defaultText;
b0a6d326
EK
312 }
313 var text = PVE.Utils.kvm_vga_drivers[value];
0be88ae1 314 if (text) {
b0a6d326
EK
315 return text + ' (' + value + ')';
316 }
317 return value;
318 },
319
320 kvm_vga_driver_array: function() {
f2782813 321 var data = [['__default__', PVE.Utils.render_kvm_vga_driver('')]];
b0a6d326
EK
322 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
323 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
324 });
325
326 return data;
327 },
328
329 render_kvm_startup: function(value) {
330 var startup = PVE.Parser.parseStartup(value);
331
332 var res = 'order=';
333 if (startup.order === undefined) {
334 res += 'any';
335 } else {
336 res += startup.order;
337 }
338 if (startup.up !== undefined) {
339 res += ',up=' + startup.up;
340 }
341 if (startup.down !== undefined) {
342 res += ',down=' + startup.down;
343 }
344
345 return res;
346 },
347
b0a6d326
EK
348 extractFormActionError: function(action) {
349 var msg;
350 switch (action.failureType) {
351 case Ext.form.action.Action.CLIENT_INVALID:
352 msg = gettext('Form fields may not be submitted with invalid values');
353 break;
354 case Ext.form.action.Action.CONNECT_FAILURE:
355 msg = gettext('Connection error');
356 var resp = action.response;
357 if (resp.status && resp.statusText) {
358 msg += " " + resp.status + ": " + resp.statusText;
359 }
360 break;
361 case Ext.form.action.Action.LOAD_FAILURE:
362 case Ext.form.action.Action.SERVER_INVALID:
e7ade592 363 msg = Proxmox.Utils.extractRequestError(action.result, true);
b0a6d326
EK
364 break;
365 }
366 return msg;
367 },
368
b0a6d326 369 format_duration_short: function(ut) {
0be88ae1 370
b0a6d326 371 if (ut < 60) {
530ee6b7 372 return ut.toFixed(1) + 's';
b0a6d326
EK
373 }
374
375 if (ut < 3600) {
376 var mins = ut / 60;
530ee6b7 377 return mins.toFixed(1) + 'm';
b0a6d326
EK
378 }
379
380 if (ut < 86400) {
381 var hours = ut / 3600;
530ee6b7 382 return hours.toFixed(1) + 'h';
b0a6d326
EK
383 }
384
385 var days = ut / 86400;
530ee6b7 386 return days.toFixed(1) + 'd';
b0a6d326
EK
387 },
388
b0a6d326
EK
389 imagesText: gettext('Disk image'),
390 backupFileText: gettext('VZDump backup file'),
2c554952 391 vztmplText: gettext('Container template'),
b0a6d326 392 isoImageText: gettext('ISO image'),
2c554952 393 containersText: gettext('Container'),
b0a6d326 394
3c23c025 395 format_storage_type: function(value, md, record) {
6760ab92
DC
396 if (value === 'rbd' && record) {
397 value = (record.get('monhost')?'rbd_ext':'pveceph');
3c23c025 398 }
b0a6d326 399 if (value === 'dir') {
e7ade592 400 return Proxmox.Utils.directoryText;
b0a6d326
EK
401 } else if (value === 'nfs') {
402 return 'NFS';
403 } else if (value === 'glusterfs') {
404 return 'GlusterFS';
405 } else if (value === 'lvm') {
406 return 'LVM';
ffd96a3b
DM
407 } else if (value === 'lvmthin') {
408 return 'LVM-Thin';
b0a6d326
EK
409 } else if (value === 'iscsi') {
410 return 'iSCSI';
411 } else if (value === 'rbd') {
6760ab92
DC
412 return 'RBD';
413 } else if (value === 'rbd_ext') {
3c23c025
DC
414 return 'RBD (external)';
415 } else if (value === 'pveceph') {
416 return 'RBD (PVE)';
b0a6d326
EK
417 } else if (value === 'sheepdog') {
418 return 'Sheepdog';
419 } else if (value === 'zfs') {
420 return 'ZFS over iSCSI';
421 } else if (value === 'zfspool') {
422 return 'ZFS';
423 } else if (value === 'iscsidirect') {
424 return 'iSCSIDirect';
ffd96a3b
DM
425 } else if (value === 'drbd') {
426 return 'DRBD';
b0a6d326 427 } else {
e7ade592 428 return Proxmox.Utils.unknownText;
b0a6d326 429 }
a3b8efb4
EK
430 },
431
ced1677b 432 format_ha: function(value) {
e7ade592 433 var text = Proxmox.Utils.noneText;
ced1677b
TL
434
435 if (value.managed) {
e7ade592 436 text = value.state || Proxmox.Utils.noneText;
ced1677b 437
e7ade592
DC
438 text += ', ' + Proxmox.Utils.groupText + ': ';
439 text += value.group || Proxmox.Utils.noneText;
ced1677b
TL
440 }
441
442 return text;
443 },
444
b0a6d326
EK
445 format_content_types: function(value) {
446 var cta = [];
447
448 Ext.each(value.split(',').sort(), function(ct) {
449 if (ct === 'images') {
450 cta.push(PVE.Utils.imagesText);
451 } else if (ct === 'backup') {
452 cta.push(PVE.Utils.backupFileText);
453 } else if (ct === 'vztmpl') {
454 cta.push(PVE.Utils.vztmplText);
455 } else if (ct === 'iso') {
456 cta.push(PVE.Utils.isoImageText);
457 } else if (ct === 'rootdir') {
458 cta.push(PVE.Utils.containersText);
459 }
460 });
461
462 return cta.join(', ');
463 },
464
465 render_storage_content: function(value, metaData, record) {
466 var data = record.data;
467 if (Ext.isNumber(data.channel) &&
468 Ext.isNumber(data.id) &&
469 Ext.isNumber(data.lun)) {
0be88ae1
DC
470 return "CH " +
471 Ext.String.leftPad(data.channel,2, '0') +
b0a6d326
EK
472 " ID " + data.id + " LUN " + data.lun;
473 }
474 return data.volid.replace(/^.*:(.*\/)?/,'');
475 },
476
477 render_serverity: function (value) {
478 return PVE.Utils.log_severity_hash[value] || value;
479 },
480
481 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
482
483 if (!(record.data.uptime && Ext.isNumeric(value))) {
484 return '';
485 }
486
487 var maxcpu = record.data.maxcpu || 1;
488
489 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
490 return '';
491 }
0be88ae1 492
b0a6d326
EK
493 var per = value * 100;
494
495 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
496 },
497
498 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
499 /*jslint confusion: true */
500
501 if (!Ext.isNumeric(value)) {
502 return '';
503 }
504
e7ade592 505 return Proxmox.Utils.format_size(value);
b0a6d326
EK
506 },
507
946730cd
DC
508 render_bandwidth: function(value) {
509 if (!Ext.isNumeric(value)) {
510 return '';
511 }
512
e7ade592 513 return Proxmox.Utils.format_size(value) + '/s';
b0a6d326
EK
514 },
515
3f633655
EK
516 render_timestamp_human_readable: function(value) {
517 return Ext.Date.format(new Date(value * 1000), 'l d F Y H:i:s');
518 },
519
ddd26302
DC
520 render_duration: function(value) {
521 if (value === undefined) {
522 return '-';
523 }
524 return PVE.Utils.format_duration_short(value);
525 },
526
0bfc799f
DC
527 calculate_mem_usage: function(data) {
528 if (!Ext.isNumeric(data.mem) ||
529 data.maxmem === 0 ||
530 data.uptime < 1) {
531 return -1;
532 }
533
534 return (data.mem / data.maxmem);
535 },
536
537 render_mem_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
538 if (!Ext.isNumeric(value) || value === -1) {
539 return '';
540 }
541 if (value > 1 ) {
542 // we got no percentage but bytes
543 var mem = value;
544 var maxmem = record.data.maxmem;
545 if (!record.data.uptime ||
546 maxmem === 0 ||
547 !Ext.isNumeric(mem)) {
548 return '';
549 }
550
551 return ((mem*100)/maxmem).toFixed(1) + " %";
552 }
553 return (value*100).toFixed(1) + " %";
554 },
555
b0a6d326
EK
556 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
557
558 var mem = value;
559 var maxmem = record.data.maxmem;
0be88ae1 560
b0a6d326
EK
561 if (!record.data.uptime) {
562 return '';
563 }
564
565 if (!(Ext.isNumeric(mem) && maxmem)) {
566 return '';
567 }
568
728f1b97 569 return PVE.Utils.render_size(value);
b0a6d326
EK
570 },
571
0bfc799f
DC
572 calculate_disk_usage: function(data) {
573
574 if (!Ext.isNumeric(data.disk) ||
575 data.type === 'qemu' ||
576 (data.type === 'lxc' && data.uptime === 0) ||
577 data.maxdisk === 0) {
578 return -1;
579 }
580
581 return (data.disk / data.maxdisk);
582 },
583
584 render_disk_usage_percent: function(value, metaData, record, rowIndex, colIndex, store) {
585 if (!Ext.isNumeric(value) || value === -1) {
586 return '';
587 }
588
589 return (value * 100).toFixed(1) + " %";
590 },
591
b0a6d326
EK
592 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
593
594 var disk = value;
595 var maxdisk = record.data.maxdisk;
728f1b97 596 var type = record.data.type;
b0a6d326 597
728f1b97
DC
598 if (!Ext.isNumeric(disk) ||
599 type === 'qemu' ||
600 maxdisk === 0 ||
601 (type === 'lxc' && record.data.uptime === 0)) {
b0a6d326
EK
602 return '';
603 }
604
728f1b97 605 return PVE.Utils.render_size(value);
b0a6d326
EK
606 },
607
4dbc64a7
DC
608 get_object_icon_class: function(type, record) {
609 var status = '';
610 var objType = type;
611
612 if (type === 'type') {
613 // for folder view
614 objType = record.groupbyid;
615 } else if (record.template) {
616 // templates
617 objType = 'template';
618 status = type;
619 } else {
620 // everything else
621 status = record.status + ' ha-' + record.hastate;
b1d8e73d
DC
622 }
623
4dbc64a7
DC
624 var defaults = PVE.tree.ResourceTree.typeDefaults[objType];
625 if (defaults && defaults.iconCls) {
626 var retVal = defaults.iconCls + ' ' + status;
627 return retVal;
b0a6d326
EK
628 }
629
4dbc64a7
DC
630 return '';
631 },
632
633 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
634
635 var cls = PVE.Utils.get_object_icon_class(value,record.data);
2b2fe160 636
4dbc64a7 637 var fa = '<i class="fa-fw x-grid-icon-custom ' + cls + '"></i> ';
b1d8e73d 638 return fa + value;
b0a6d326
EK
639 },
640
b0a6d326
EK
641 render_support_level: function(value, metaData, record) {
642 return PVE.Utils.support_level_hash[value] || '-';
643 },
644
0be88ae1 645 render_upid: function(value, metaData, record) {
b0a6d326
EK
646 var type = record.data.type;
647 var id = record.data.id;
648
e7ade592 649 return Proxmox.Utils.format_task_description(type, id);
b0a6d326
EK
650 },
651
054ac1b8
DC
652 /* render functions for new status panel */
653
654 render_usage: function(val) {
655 return (val*100).toFixed(2) + '%';
656 },
657
658 render_cpu_usage: function(val, max) {
659 return Ext.String.format(gettext('{0}% of {1}') +
660 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
661 },
662
663 render_size_usage: function(val, max) {
bab64974
DC
664 if (max === 0) {
665 return gettext('N/A');
666 }
054ac1b8
DC
667 return (val*100/max).toFixed(2) + '% '+ '(' +
668 Ext.String.format(gettext('{0} of {1}'),
669 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
670 },
671
672 /* this is different for nodes */
673 render_node_cpu_usage: function(value, record) {
674 return PVE.Utils.render_cpu_usage(value, record.cpus);
675 },
676
677 /* this is different for nodes */
678 render_node_size_usage: function(record) {
679 return PVE.Utils.render_size_usage(record.used, record.total);
680 },
681
aa0819a8 682 windowHostname: function() {
e7ade592 683 return window.location.hostname.replace(Proxmox.Utils.IP6_bracket_match,
aa0819a8
WB
684 function(m, addr, offset, original) { return addr; });
685 },
0be88ae1 686
b0a6d326
EK
687 openDefaultConsoleWindow: function(allowSpice, vmtype, vmid, nodename, vmname) {
688 var dv = PVE.Utils.defaultViewer(allowSpice);
689 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
690 },
691
692 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
9e361643 693 // kvm, lxc, shell, upgrade
b0a6d326 694
9e361643 695 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
696 throw "missing vmid";
697 }
698
699 if (!nodename) {
700 throw "no nodename specified";
701 }
702
c7218ab3
DC
703 if (viewer === 'html5') {
704 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
c6b2336c 705 } else if (viewer === 'xtermjs') {
e7ade592 706 Proxmox.Utils.openXtermJsViewer(vmtype, vmid, nodename, vmname);
b0a6d326
EK
707 } else if (viewer === 'vv') {
708 var url;
aa0819a8 709 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
710 if (vmtype === 'kvm') {
711 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
712 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
713 } else if (vmtype === 'lxc') {
714 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
715 PVE.Utils.openSpiceViewer(url, params);
716 } else if (vmtype === 'shell') {
717 url = '/nodes/' + nodename + '/spiceshell';
718 PVE.Utils.openSpiceViewer(url, params);
719 } else if (vmtype === 'upgrade') {
720 url = '/nodes/' + nodename + '/spiceshell';
721 params.upgrade = 1;
722 PVE.Utils.openSpiceViewer(url, params);
723 }
724 } else {
725 throw "unknown viewer type";
726 }
727 },
728
729 defaultViewer: function(allowSpice) {
730 var vncdefault = 'html5';
731 var dv = PVE.VersionInfo.console || vncdefault;
732 if (dv === 'vv' && !allowSpice) {
733 dv = vncdefault;
734 }
735
736 return dv;
737 },
738
c7218ab3 739 openVNCViewer: function(vmtype, vmid, nodename, vmname) {
b0a6d326 740 var url = Ext.urlEncode({
9e361643 741 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 742 novnc: 1,
b0a6d326
EK
743 vmid: vmid,
744 vmname: vmname,
745 node: nodename
746 });
747 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
748 nw.focus();
749 },
750
751 openSpiceViewer: function(url, params){
752
753 var downloadWithName = function(uri, name) {
754 var link = Ext.DomHelper.append(document.body, {
755 tag: 'a',
756 href: uri,
757 css : 'display:none;visibility:hidden;height:0px;'
758 });
759
760 // Note: we need to tell android the correct file name extension
761 // but we do not set 'download' tag for other environments, because
762 // It can have strange side effects (additional user prompt on firefox)
763 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
764 if (andriod) {
765 link.download = name;
766 }
767
768 if (link.fireEvent) {
769 link.fireEvent('onclick');
770 } else {
771 var evt = document.createEvent("MouseEvents");
772 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
773 link.dispatchEvent(evt);
774 }
775 };
776
e7ade592 777 Proxmox.Utils.API2Request({
b0a6d326
EK
778 url: url,
779 params: params,
780 method: 'POST',
781 failure: function(response, opts){
782 Ext.Msg.alert('Error', response.htmlStatus);
783 },
784 success: function(response, opts){
785 var raw = "[virt-viewer]\n";
786 Ext.Object.each(response.result.data, function(k, v) {
787 raw += k + "=" + v + "\n";
788 });
789 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
790 encodeURIComponent(raw);
0be88ae1 791
b0a6d326
EK
792 downloadWithName(url, "pve-spice.vv");
793 }
794 });
795 },
796
e3129443
DC
797 openTreeConsole: function(tree, record, item, index, e) {
798 e.stopEvent();
799 var nodename = record.data.node;
800 var vmid = record.data.vmid;
801 var vmname = record.data.name;
802 if (record.data.type === 'qemu' && !record.data.template) {
e7ade592 803 Proxmox.Utils.API2Request({
e3129443
DC
804 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
805 failure: function(response, opts) {
806 Ext.Msg.alert('Error', response.htmlStatus);
807 },
808 success: function(response, opts) {
809 var allowSpice = response.result.data.spice;
810 PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
811 }
812 });
813 } else if (record.data.type === 'lxc' && !record.data.template) {
814 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
815 }
816 },
817
fbd60cfd
DM
818 // test automation helper
819 call_menu_handler: function(menu, text) {
820
821 var list = menu.query('menuitem');
822
823 Ext.Array.each(list, function(item) {
824 if (item.text === text) {
825 if (item.handler) {
826 item.handler();
827 return 1;
828 } else {
829 return undefined;
830 }
831 }
832 });
833 },
834
685b7aa4
DC
835 createCmdMenu: function(v, record, item, index, event) {
836 event.stopEvent();
cc1a91be
DC
837 if (!(v instanceof Ext.tree.View)) {
838 v.select(record);
839 }
685b7aa4
DC
840 var menu;
841
842 if (record.data.type === 'qemu' && !record.data.template) {
843 menu = Ext.create('PVE.qemu.CmdMenu', {
844 pveSelNode: record
845 });
846 } else if (record.data.type === 'qemu' && record.data.template) {
847 menu = Ext.create('PVE.qemu.TemplateMenu', {
848 pveSelNode: record
849 });
850 } else if (record.data.type === 'lxc' && !record.data.template) {
851 menu = Ext.create('PVE.lxc.CmdMenu', {
852 pveSelNode: record
853 });
854 } else if (record.data.type === 'lxc' && record.data.template) {
855 /* since clone does not work reliably, disable for now
856 menu = Ext.create('PVE.lxc.TemplateMenu', {
857 pveSelNode: record
858 });
859 */
860 return;
c11ab8cb
DC
861
862 } else if (record.data.type === 'node' ){
863 menu = Ext.create('PVE.node.CmdMenu', {
864 nodename: record.data.node
865 });
866
685b7aa4
DC
867 } else {
868 return;
869 }
870
871 menu.showAt(event.getXY());
e7ade592 872 },
9fa2e36d 873
fe4f00ad
TL
874 // helper for deleting field which are set to there default values
875 delete_if_default: function(values, fieldname, default_val, create) {
876 if (values[fieldname] === '' || values[fieldname] === default_val) {
877 if (!create) {
878 if (values['delete']) {
879 values['delete'] += ',' + fieldname;
880 } else {
881 values['delete'] = fieldname;
882 }
883 }
884
885 delete values[fieldname];
886 }
e7ade592
DC
887 }
888},
fe4f00ad 889
9fa2e36d
EK
890 singleton: true,
891 constructor: function() {
892 var me = this;
893 Ext.apply(me, me.utilities);
685b7aa4 894 }
e7ade592 895
9fa2e36d 896});
b0a6d326 897