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