]> git.proxmox.com Git - pve-manager.git/blame - www/manager6/Utils.js
add new tree and grid classes for the 'unknown' status
[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
1035 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
1036
b1d8e73d
DC
1037 var icon = '';
1038 var gridcls = '';
1039
1040 switch (value) {
1041 case 'lxc': icon = 'cube';
1042 gridcls = '-stopped';
1043 break;
1044 case 'qemu': icon = 'desktop';
1045 gridcls = '-stopped';
1046 break;
1047 case 'node': icon = 'building';
1048 gridcls = '-offline';
1049 break;
1050 case 'storage': icon = 'database'; break;
1051 case 'pool': icon = 'tags'; break;
1052 default: icon = 'file';
1053 }
1054
1055 if (value === 'lxc' || value === 'qemu') {
1056 if (record.data.running && record.data.status !== 'paused') {
1057 gridcls = '-running';
1058 } else if (record.data.running) {
1059 gridcls = '-paused';
1060 }
1061 if (record.data.template) {
1062 icon = 'file-o';
1063 gridcls = '-template-' + value;
1064 }
1065 } else if (value === 'node') {
1066 if (record.data.running) {
a764c5f7 1067 gridcls = '-online';
b1d8e73d 1068 }
b0a6d326
EK
1069 }
1070
2b2fe160
DC
1071 // overwrite anything else
1072 if (record.data.hastate === 'error') {
1073 gridcls = '-offline';
1074 }
1075
a764c5f7 1076 var fa = '<i class="fa fa-fw x-fa-grid' + gridcls + ' fa-' + icon + '"></i> ';
b1d8e73d 1077 return fa + value;
b0a6d326
EK
1078 },
1079
1080 render_uptime: function(value, metaData, record, rowIndex, colIndex, store) {
1081
1082 var uptime = value;
1083
1084 if (uptime === undefined) {
1085 return '';
1086 }
0be88ae1 1087
b0a6d326
EK
1088 if (uptime <= 0) {
1089 return '-';
1090 }
1091
1092 return PVE.Utils.format_duration_long(uptime);
1093 },
1094
1095 render_support_level: function(value, metaData, record) {
1096 return PVE.Utils.support_level_hash[value] || '-';
1097 },
1098
0be88ae1 1099 render_upid: function(value, metaData, record) {
b0a6d326
EK
1100 var type = record.data.type;
1101 var id = record.data.id;
1102
1103 return PVE.Utils.format_task_description(type, id);
1104 },
1105
054ac1b8
DC
1106 /* render functions for new status panel */
1107
1108 render_usage: function(val) {
1109 return (val*100).toFixed(2) + '%';
1110 },
1111
1112 render_cpu_usage: function(val, max) {
1113 return Ext.String.format(gettext('{0}% of {1}') +
1114 ' ' + gettext('CPU(s)'), (val*100).toFixed(2), max);
1115 },
1116
1117 render_size_usage: function(val, max) {
bab64974
DC
1118 if (max === 0) {
1119 return gettext('N/A');
1120 }
054ac1b8
DC
1121 return (val*100/max).toFixed(2) + '% '+ '(' +
1122 Ext.String.format(gettext('{0} of {1}'),
1123 PVE.Utils.render_size(val), PVE.Utils.render_size(max)) + ')';
1124 },
1125
1126 /* this is different for nodes */
1127 render_node_cpu_usage: function(value, record) {
1128 return PVE.Utils.render_cpu_usage(value, record.cpus);
1129 },
1130
1131 /* this is different for nodes */
1132 render_node_size_usage: function(record) {
1133 return PVE.Utils.render_size_usage(record.used, record.total);
1134 },
1135
b0a6d326
EK
1136 dialog_title: function(subject, create, isAdd) {
1137 if (create) {
1138 if (isAdd) {
1139 return gettext('Add') + ': ' + subject;
1140 } else {
1141 return gettext('Create') + ': ' + subject;
1142 }
1143 } else {
1144 return gettext('Edit') + ': ' + subject;
1145 }
1146 },
aa0819a8
WB
1147
1148 windowHostname: function() {
c15c61d3 1149 return window.location.hostname.replace(PVE.Utils.IP6_bracket_match,
aa0819a8
WB
1150 function(m, addr, offset, original) { return addr; });
1151 },
0be88ae1 1152
b0a6d326
EK
1153 openDefaultConsoleWindow: function(allowSpice, vmtype, vmid, nodename, vmname) {
1154 var dv = PVE.Utils.defaultViewer(allowSpice);
1155 PVE.Utils.openConsoleWindow(dv, vmtype, vmid, nodename, vmname);
1156 },
1157
1158 openConsoleWindow: function(viewer, vmtype, vmid, nodename, vmname) {
9e361643 1159 // kvm, lxc, shell, upgrade
b0a6d326 1160
9e361643 1161 if (vmid == undefined && (vmtype === 'kvm' || vmtype === 'lxc')) {
b0a6d326
EK
1162 throw "missing vmid";
1163 }
1164
1165 if (!nodename) {
1166 throw "no nodename specified";
1167 }
1168
c7218ab3
DC
1169 if (viewer === 'html5') {
1170 PVE.Utils.openVNCViewer(vmtype, vmid, nodename, vmname);
b0a6d326
EK
1171 } else if (viewer === 'vv') {
1172 var url;
aa0819a8 1173 var params = { proxy: PVE.Utils.windowHostname() };
b0a6d326
EK
1174 if (vmtype === 'kvm') {
1175 url = '/nodes/' + nodename + '/qemu/' + vmid.toString() + '/spiceproxy';
1176 PVE.Utils.openSpiceViewer(url, params);
9e361643
DM
1177 } else if (vmtype === 'lxc') {
1178 url = '/nodes/' + nodename + '/lxc/' + vmid.toString() + '/spiceproxy';
b0a6d326
EK
1179 PVE.Utils.openSpiceViewer(url, params);
1180 } else if (vmtype === 'shell') {
1181 url = '/nodes/' + nodename + '/spiceshell';
1182 PVE.Utils.openSpiceViewer(url, params);
1183 } else if (vmtype === 'upgrade') {
1184 url = '/nodes/' + nodename + '/spiceshell';
1185 params.upgrade = 1;
1186 PVE.Utils.openSpiceViewer(url, params);
1187 }
1188 } else {
1189 throw "unknown viewer type";
1190 }
1191 },
1192
1193 defaultViewer: function(allowSpice) {
1194 var vncdefault = 'html5';
1195 var dv = PVE.VersionInfo.console || vncdefault;
1196 if (dv === 'vv' && !allowSpice) {
1197 dv = vncdefault;
1198 }
1199
1200 return dv;
1201 },
1202
c7218ab3 1203 openVNCViewer: function(vmtype, vmid, nodename, vmname) {
b0a6d326 1204 var url = Ext.urlEncode({
9e361643 1205 console: vmtype, // kvm, lxc, upgrade or shell
c7218ab3 1206 novnc: 1,
b0a6d326
EK
1207 vmid: vmid,
1208 vmname: vmname,
1209 node: nodename
1210 });
1211 var nw = window.open("?" + url, '_blank', "innerWidth=745,innerheight=427");
1212 nw.focus();
1213 },
1214
1215 openSpiceViewer: function(url, params){
1216
1217 var downloadWithName = function(uri, name) {
1218 var link = Ext.DomHelper.append(document.body, {
1219 tag: 'a',
1220 href: uri,
1221 css : 'display:none;visibility:hidden;height:0px;'
1222 });
1223
1224 // Note: we need to tell android the correct file name extension
1225 // but we do not set 'download' tag for other environments, because
1226 // It can have strange side effects (additional user prompt on firefox)
1227 var andriod = navigator.userAgent.match(/Android/i) ? true : false;
1228 if (andriod) {
1229 link.download = name;
1230 }
1231
1232 if (link.fireEvent) {
1233 link.fireEvent('onclick');
1234 } else {
1235 var evt = document.createEvent("MouseEvents");
1236 evt.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
1237 link.dispatchEvent(evt);
1238 }
1239 };
1240
1241 PVE.Utils.API2Request({
1242 url: url,
1243 params: params,
1244 method: 'POST',
1245 failure: function(response, opts){
1246 Ext.Msg.alert('Error', response.htmlStatus);
1247 },
1248 success: function(response, opts){
1249 var raw = "[virt-viewer]\n";
1250 Ext.Object.each(response.result.data, function(k, v) {
1251 raw += k + "=" + v + "\n";
1252 });
1253 var url = 'data:application/x-virt-viewer;charset=UTF-8,' +
1254 encodeURIComponent(raw);
0be88ae1 1255
b0a6d326
EK
1256 downloadWithName(url, "pve-spice.vv");
1257 }
1258 });
1259 },
1260
0be88ae1 1261 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
b0a6d326
EK
1262 // use el.mask() instead
1263 setErrorMask: function(comp, msg) {
1264 var el = comp.el;
1265 if (!el) {
1266 return;
1267 }
1268 if (!msg) {
1269 el.unmask();
1270 } else {
1271 if (msg === true) {
1272 el.mask(gettext("Loading..."));
1273 } else {
1274 el.mask(msg);
1275 }
1276 }
1277 },
1278
1279 monStoreErrors: function(me, store) {
1280 me.mon(store, 'beforeload', function(s, operation, eOpts) {
1281 if (!me.loadCount) {
1282 me.loadCount = 0; // make sure it is numeric
1283 PVE.Utils.setErrorMask(me, true);
1284 }
1285 });
1286
1287 // only works with 'pve' proxy
1288 me.mon(store.proxy, 'afterload', function(proxy, request, success) {
1289 me.loadCount++;
1290
1291 if (success) {
1292 PVE.Utils.setErrorMask(me, false);
1293 return;
1294 }
1295
1296 var msg;
23d3881a 1297 /*jslint nomen: true */
86826854 1298 var operation = request._operation;
b0a6d326
EK
1299 var error = operation.getError();
1300 if (error.statusText) {
1301 msg = error.statusText + ' (' + error.status + ')';
1302 } else {
1303 msg = gettext('Connection error');
1304 }
1305 PVE.Utils.setErrorMask(me, msg);
1306 });
685b7aa4 1307 },
b0a6d326 1308
e3129443
DC
1309 openTreeConsole: function(tree, record, item, index, e) {
1310 e.stopEvent();
1311 var nodename = record.data.node;
1312 var vmid = record.data.vmid;
1313 var vmname = record.data.name;
1314 if (record.data.type === 'qemu' && !record.data.template) {
1315 PVE.Utils.API2Request({
1316 url: '/nodes/' + nodename + '/qemu/' + vmid + '/status/current',
1317 failure: function(response, opts) {
1318 Ext.Msg.alert('Error', response.htmlStatus);
1319 },
1320 success: function(response, opts) {
1321 var allowSpice = response.result.data.spice;
1322 PVE.Utils.openDefaultConsoleWindow(allowSpice, 'kvm', vmid, nodename, vmname);
1323 }
1324 });
1325 } else if (record.data.type === 'lxc' && !record.data.template) {
1326 PVE.Utils.openDefaultConsoleWindow(true, 'lxc', vmid, nodename, vmname);
1327 }
1328 },
1329
fbd60cfd
DM
1330 // test automation helper
1331 call_menu_handler: function(menu, text) {
1332
1333 var list = menu.query('menuitem');
1334
1335 Ext.Array.each(list, function(item) {
1336 if (item.text === text) {
1337 if (item.handler) {
1338 item.handler();
1339 return 1;
1340 } else {
1341 return undefined;
1342 }
1343 }
1344 });
1345 },
1346
685b7aa4
DC
1347 createCmdMenu: function(v, record, item, index, event) {
1348 event.stopEvent();
cc1a91be
DC
1349 if (!(v instanceof Ext.tree.View)) {
1350 v.select(record);
1351 }
685b7aa4
DC
1352 var menu;
1353
1354 if (record.data.type === 'qemu' && !record.data.template) {
1355 menu = Ext.create('PVE.qemu.CmdMenu', {
1356 pveSelNode: record
1357 });
1358 } else if (record.data.type === 'qemu' && record.data.template) {
1359 menu = Ext.create('PVE.qemu.TemplateMenu', {
1360 pveSelNode: record
1361 });
1362 } else if (record.data.type === 'lxc' && !record.data.template) {
1363 menu = Ext.create('PVE.lxc.CmdMenu', {
1364 pveSelNode: record
1365 });
1366 } else if (record.data.type === 'lxc' && record.data.template) {
1367 /* since clone does not work reliably, disable for now
1368 menu = Ext.create('PVE.lxc.TemplateMenu', {
1369 pveSelNode: record
1370 });
1371 */
1372 return;
c11ab8cb
DC
1373
1374 } else if (record.data.type === 'node' ){
1375 menu = Ext.create('PVE.node.CmdMenu', {
1376 nodename: record.data.node
1377 });
1378
685b7aa4
DC
1379 } else {
1380 return;
1381 }
1382
1383 menu.showAt(event.getXY());
9fa2e36d
EK
1384 }},
1385
fe4f00ad
TL
1386 // helper for deleting field which are set to there default values
1387 delete_if_default: function(values, fieldname, default_val, create) {
1388 if (values[fieldname] === '' || values[fieldname] === default_val) {
1389 if (!create) {
1390 if (values['delete']) {
1391 values['delete'] += ',' + fieldname;
1392 } else {
1393 values['delete'] = fieldname;
1394 }
1395 }
1396
1397 delete values[fieldname];
1398 }
1399 },
1400
9fa2e36d
EK
1401 singleton: true,
1402 constructor: function() {
1403 var me = this;
1404 Ext.apply(me, me.utilities);
c15c61d3
EK
1405
1406 var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
1407 var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
1408 var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
1409 var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
1410
1411
1412 me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
1413 me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$");
1414
1415 var IPV6_REGEXP = "(?:" +
1416 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
1417 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
1418 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
1419 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
1420 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
1421 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
1422 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
1423 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
1424 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
1425 ")";
1426
1427 me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
1428 me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$");
1429 me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
1430
1431 me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
1432
1433 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])?))";
1434 me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
1435
1436 me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$");
1437 me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$");
1438 me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$");
685b7aa4 1439 }
9fa2e36d 1440});
b0a6d326 1441