d5de2e2ee93e665df9f27a9e7f7fa8908063c51c
[pve-manager.git] / www / manager / Utils.js
1 Ext.ns('PVE');
2
3 // avoid errors when running without development tools
4 if (!Ext.isDefined(Ext.global.console)) {
5 var console = {
6 dir: function() {},
7 log: function() {}
8 };
9 }
10 console.log("Starting PVE Manager");
11
12 Ext.Ajax.defaultHeaders = {
13 'Accept': 'application/json'
14 };
15
16 // do not send '_dc' parameter
17 Ext.Ajax.disableCaching = false;
18
19 Ext.Ajax.on('beforerequest', function(conn, options) {
20 if (PVE.CSRFPreventionToken) {
21 if (!options.headers) {
22 options.headers = {};
23 }
24 options.headers.CSRFPreventionToken = PVE.CSRFPreventionToken;
25 }
26 });
27
28 // custom Vtypes
29 Ext.apply(Ext.form.field.VTypes, {
30 IPAddress: function(v) {
31 return (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/).test(v);
32 },
33 IPAddressText: gettext('Example') + ': 192.168.1.1',
34 IPAddressMask: /[\d\.]/i,
35
36 MacAddress: function(v) {
37 return (/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/).test(v);
38 },
39 MacAddressMask: /[a-fA-F0-9:]/,
40 MacAddressText: gettext('Example') + ': 01:23:45:67:89:ab',
41
42 BridgeName: function(v) {
43 return (/^vmbr\d{1,4}$/).test(v);
44 },
45 BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999',
46
47 BondName: function(v) {
48 return (/^bond\d{1,4}$/).test(v);
49 },
50 BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999',
51
52 QemuStartDate: function(v) {
53 return (/^(now|\d{4}-\d{1,2}-\d{1,2}(T\d{1,2}:\d{1,2}:\d{1,2})?)$/).test(v);
54 },
55 QemuStartDateText: gettext('Format') + ': "now" or "2006-06-17T16:01:21" or "2006-06-17"',
56
57 StorageId: function(v) {
58 return (/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i).test(v);
59 },
60 StorageIdText: gettext("Allowed characters") + ": 'a-z', '0-9', '-', '_', '.'",
61
62 HttpProxy: function(v) {
63 return (/^http:\/\/.*$/).test(v);
64 },
65 HttpProxyText: gettext('Example') + ": http://username:password&#64;host:port/",
66
67 DnsName: function(v) {
68 return (/^(([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)\.)*([A-Za-z0-9]([A-Za-z0-9\-]*[A-Za-z0-9])?)$/).test(v);
69 },
70 DnsNameText: gettext('This is not a valid DNS name')
71 });
72
73 // we dont want that a displayfield set the form dirty flag!
74 Ext.override(Ext.form.field.Display, {
75 isDirty: function() { return false; }
76 });
77
78 // hack: ExtJS does not display the correct value if we
79 // call setValue while the store is loading, so we need
80 // to call it again after loading
81 Ext.override(Ext.form.field.ComboBox, {
82 onLoad: function() {
83 this.setValue(this.value, false);
84 this.callOverridden(arguments);
85 }
86 });
87
88 Ext.define('PVE.Utils', { statics: {
89
90 // this class only contains static functions
91
92 log_severity_hash: {
93 0: "panic",
94 1: "alert",
95 2: "critical",
96 3: "error",
97 4: "warning",
98 5: "notice",
99 6: "info",
100 7: "debug"
101 },
102
103 support_level_hash: {
104 'c': gettext('Community'),
105 'b': gettext('Basic'),
106 's': gettext('Standard'),
107 'p': gettext('Premium')
108 },
109
110 kvm_ostypes: {
111 other: gettext('Other OS types'),
112 wxp: 'Microsoft Windows XP/2003',
113 w2k: 'Microsoft Windows 2000',
114 w2k8: 'Microsoft Windows Vista/2008',
115 win7: 'Microsoft Windows 7/2008r2',
116 win8: 'Microsoft Windows 8/2012',
117 l24: 'Linux 2.4 Kernel',
118 l26: 'Linux 3.X/2.6 Kernel'
119 },
120
121 render_kvm_ostype: function (value) {
122 if (!value) {
123 return gettext('Other OS types');
124 }
125 var text = PVE.Utils.kvm_ostypes[value];
126 if (text) {
127 return text + ' (' + value + ')';
128 }
129 return value;
130 },
131
132 render_scsihw: function(value) {
133 if (!value) {
134 return PVE.Utils.defaultText + ' (lsi)';
135 } else if (value === 'lsi') {
136 return 'LSI 53C895A';
137 } else if (value === 'megasas') {
138 return 'MegaRAID SAS 8708EM2';
139 } else if (value === 'virtio-scsi-pci') {
140 return 'VIRTIO';
141 } else {
142 return value;
143 }
144 },
145
146 // fixme: auto-generate this
147 // for now, please keep in sync with PVE::Tools::kvmkeymaps
148 kvm_keymaps: {
149 //ar: 'Arabic',
150 da: 'Danish',
151 de: 'German',
152 'de-ch': 'German (Swiss)',
153 'en-gb': 'English (UK)',
154 'en-us': 'English (USA',
155 es: 'Spanish',
156 //et: 'Estonia',
157 fi: 'Finnish',
158 //fo: 'Faroe Islands',
159 fr: 'French',
160 'fr-be': 'French (Belgium)',
161 'fr-ca': 'French (Canada)',
162 'fr-ch': 'French (Swiss)',
163 //hr: 'Croatia',
164 hu: 'Hungarian',
165 is: 'Icelandic',
166 it: 'Italian',
167 ja: 'Japanese',
168 lt: 'Lithuanian',
169 //lv: 'Latvian',
170 mk: 'Macedonian',
171 nl: 'Dutch',
172 //'nl-be': 'Dutch (Belgium)',
173 no: 'Norwegian',
174 pl: 'Polish',
175 pt: 'Portuguese',
176 'pt-br': 'Portuguese (Brazil)',
177 //ru: 'Russian',
178 sl: 'Slovenian',
179 sv: 'Swedish',
180 //th: 'Thai',
181 tr: 'Turkish'
182 },
183
184 kvm_vga_drivers: {
185 std: 'Standard VGA',
186 vmware: 'VMWare compatible',
187 cirrus: 'Cirrus Logic GD5446'
188 },
189
190 render_kvm_language: function (value) {
191 if (!value) {
192 return PVE.Utils.defaultText;
193 }
194 var text = PVE.Utils.kvm_keymaps[value];
195 if (text) {
196 return text + ' (' + value + ')';
197 }
198 return value;
199 },
200
201 kvm_keymap_array: function() {
202 var data = [['', PVE.Utils.render_kvm_language('')]];
203 Ext.Object.each(PVE.Utils.kvm_keymaps, function(key, value) {
204 data.push([key, PVE.Utils.render_kvm_language(value)]);
205 });
206
207 return data;
208 },
209
210 language_map: {
211 zh_CN: 'Chinese',
212 ca: 'Catalan',
213 ja: 'Japanese',
214 en: 'English',
215 da: 'Danish',
216 de: 'German',
217 es: 'Spanish',
218 fr: 'French',
219 it: 'Italian',
220 nb: 'Norwegian (Bokmal)',
221 nn: 'Norwegian (Nynorsk)',
222 ru: 'Russian',
223 sl: 'Slovenian',
224 sv: 'Swedish',
225 pl: 'Polish',
226 pt_BR: 'Portuguese (Brazil)',
227 tr: 'Turkish'
228 },
229
230 render_language: function (value) {
231 if (!value) {
232 return PVE.Utils.defaultText + ' (English)';
233 }
234 var text = PVE.Utils.language_map[value];
235 if (text) {
236 return text + ' (' + value + ')';
237 }
238 return value;
239 },
240
241 language_array: function() {
242 var data = [['', PVE.Utils.render_language('')]];
243 Ext.Object.each(PVE.Utils.language_map, function(key, value) {
244 data.push([key, PVE.Utils.render_language(value)]);
245 });
246
247 return data;
248 },
249
250 render_kvm_vga_driver: function (value) {
251 if (!value) {
252 return PVE.Utils.defaultText;
253 }
254 var text = PVE.Utils.kvm_vga_drivers[value];
255 if (text) {
256 return text + ' (' + value + ')';
257 }
258 return value;
259 },
260
261 kvm_vga_driver_array: function() {
262 var data = [['', PVE.Utils.render_kvm_vga_driver('')]];
263 Ext.Object.each(PVE.Utils.kvm_vga_drivers, function(key, value) {
264 data.push([key, PVE.Utils.render_kvm_vga_driver(value)]);
265 });
266
267 return data;
268 },
269
270 render_kvm_startup: function(value) {
271 var startup = PVE.Parser.parseStartup(value);
272
273 var res = 'order=';
274 if (startup.order === undefined) {
275 res += 'any';
276 } else {
277 res += startup.order;
278 }
279 if (startup.up !== undefined) {
280 res += ',up=' + startup.up;
281 }
282 if (startup.down !== undefined) {
283 res += ',down=' + startup.down;
284 }
285
286 return res;
287 },
288
289 authOK: function() {
290 return Ext.util.Cookies.get('PVEAuthCookie');
291 },
292
293 authClear: function() {
294 Ext.util.Cookies.clear("PVEAuthCookie");
295 },
296
297 // fixme: remove - not needed?
298 gridLineHeigh: function() {
299 return 21;
300
301 //if (Ext.isGecko)
302 //return 23;
303 //return 21;
304 },
305
306 extractRequestError: function(result, verbose) {
307 var msg = gettext('Successful');
308
309 if (!result.success) {
310 msg = gettext("Unknown error");
311 if (result.message) {
312 msg = result.message;
313 if (result.status) {
314 msg += ' (' + result.status + ')';
315 }
316 }
317 if (verbose && Ext.isObject(result.errors)) {
318 msg += "<br>";
319 Ext.Object.each(result.errors, function(prop, desc) {
320 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
321 Ext.htmlEncode(desc);
322 });
323 }
324 }
325
326 return msg;
327 },
328
329 extractFormActionError: function(action) {
330 var msg;
331 switch (action.failureType) {
332 case Ext.form.action.Action.CLIENT_INVALID:
333 msg = gettext('Form fields may not be submitted with invalid values');
334 break;
335 case Ext.form.action.Action.CONNECT_FAILURE:
336 msg = gettext('Connection error');
337 var resp = action.response;
338 if (resp.status && resp.statusText) {
339 msg += " " + resp.status + ": " + resp.statusText;
340 }
341 break;
342 case Ext.form.action.Action.LOAD_FAILURE:
343 case Ext.form.action.Action.SERVER_INVALID:
344 msg = PVE.Utils.extractRequestError(action.result, true);
345 break;
346 }
347 return msg;
348 },
349
350 // Ext.Ajax.request
351 API2Request: function(reqOpts) {
352
353 var newopts = Ext.apply({
354 waitMsg: gettext('Please wait...')
355 }, reqOpts);
356
357 if (!newopts.url.match(/^\/api2/)) {
358 newopts.url = '/api2/extjs' + newopts.url;
359 }
360 delete newopts.callback;
361
362 var createWrapper = function(successFn, callbackFn, failureFn) {
363 Ext.apply(newopts, {
364 success: function(response, options) {
365 if (options.waitMsgTarget) {
366 options.waitMsgTarget.setLoading(false);
367 }
368 var result = Ext.decode(response.responseText);
369 response.result = result;
370 if (!result.success) {
371 response.htmlStatus = PVE.Utils.extractRequestError(result, true);
372 Ext.callback(callbackFn, options.scope, [options, false, response]);
373 Ext.callback(failureFn, options.scope, [response, options]);
374 return;
375 }
376 Ext.callback(callbackFn, options.scope, [options, true, response]);
377 Ext.callback(successFn, options.scope, [response, options]);
378 },
379 failure: function(response, options) {
380 if (options.waitMsgTarget) {
381 options.waitMsgTarget.setLoading(false);
382 }
383 response.result = {};
384 try {
385 response.result = Ext.decode(response.responseText);
386 } catch(e) {}
387 var msg = gettext('Connection error') + ' - server offline?';
388 if (response.aborted) {
389 msg = gettext('Connection error') + ' - aborted.';
390 } else if (response.timedout) {
391 msg = gettext('Connection error') + ' - Timeout.';
392 } else if (response.status && response.statusText) {
393 msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText;
394 }
395 response.htmlStatus = msg;
396 Ext.callback(callbackFn, options.scope, [options, false, response]);
397 Ext.callback(failureFn, options.scope, [response, options]);
398 }
399 });
400 };
401
402 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
403
404 var target = newopts.waitMsgTarget;
405 if (target) {
406 // Note: ExtJS bug - this does not work when component is not rendered
407 target.setLoading(newopts.waitMsg);
408 }
409 Ext.Ajax.request(newopts);
410 },
411
412 assemble_field_data: function(values, data) {
413 if (Ext.isObject(data)) {
414 Ext.Object.each(data, function(name, val) {
415 if (values.hasOwnProperty(name)) {
416 var bucket = values[name];
417 if (!Ext.isArray(bucket)) {
418 bucket = values[name] = [bucket];
419 }
420 if (Ext.isArray(val)) {
421 values[name] = bucket.concat(val);
422 } else {
423 bucket.push(val);
424 }
425 } else {
426 values[name] = val;
427 }
428 });
429 }
430 },
431
432 task_desc_table: {
433 vncproxy: [ 'VM/CT', gettext('Console') ],
434 vncshell: [ '', gettext('Shell') ],
435 qmsnapshot: [ 'VM', gettext('Snapshot') ],
436 qmrollback: [ 'VM', gettext('Rollback') ],
437 qmdelsnapshot: [ 'VM', gettext('Delete Snapshot') ],
438 qmcreate: [ 'VM', gettext('Create') ],
439 qmrestore: [ 'VM', gettext('Restore') ],
440 qmdestroy: [ 'VM', gettext('Destroy') ],
441 qmigrate: [ 'VM', gettext('Migrate') ],
442 qmclone: [ 'VM', gettext('Clone') ],
443 qmmove: [ 'VM', gettext('Move disk') ],
444 qmtemplate: [ 'VM', gettext('Convert to template') ],
445 qmstart: [ 'VM', gettext('Start') ],
446 qmstop: [ 'VM', gettext('Stop') ],
447 qmreset: [ 'VM', gettext('Reset') ],
448 qmshutdown: [ 'VM', gettext('Shutdown') ],
449 qmsuspend: [ 'VM', gettext('Suspend') ],
450 qmresume: [ 'VM', gettext('Resume') ],
451 qmconfig: [ 'VM', gettext('Configure') ],
452 vzcreate: ['CT', gettext('Create') ],
453 vzrestore: ['CT', gettext('Restore') ],
454 vzdestroy: ['CT', gettext('Destroy') ],
455 vzmigrate: [ 'CT', gettext('Migrate') ],
456 vzstart: ['CT', gettext('Start') ],
457 vzstop: ['CT', gettext('Stop') ],
458 vzmount: ['CT', gettext('Mount') ],
459 vzumount: ['CT', gettext('Unmount') ],
460 vzshutdown: ['CT', gettext('Shutdown') ],
461 hamigrate: [ 'HA', gettext('Migrate') ],
462 hastart: [ 'HA', gettext('Start') ],
463 hastop: [ 'HA', gettext('Stop') ],
464 srvstart: ['SRV', gettext('Start') ],
465 srvstop: ['SRV', gettext('Stop') ],
466 srvrestart: ['SRV', gettext('Restart') ],
467 srvreload: ['SRV', gettext('Reload') ],
468 imgcopy: ['', gettext('Copy data') ],
469 imgdel: ['', gettext('Erase data') ],
470 download: ['', gettext('Download') ],
471 vzdump: ['', gettext('Backup') ],
472 aptupdate: ['', gettext('Update package database') ],
473 aptupgrade: ['', gettext('System upgrade') ],
474 startall: [ '', gettext('Start all VMs and Containers') ],
475 stopall: [ '', gettext('Stop all VMs and Containers') ]
476 },
477
478 format_task_description: function(type, id) {
479 var farray = PVE.Utils.task_desc_table[type];
480 if (!farray) {
481 return type;
482 }
483 var prefix = farray[0];
484 var text = farray[1];
485 if (prefix) {
486 return prefix + ' ' + id + ' - ' + text;
487 }
488 return text;
489 },
490
491 parse_task_upid: function(upid) {
492 var task = {};
493
494 var res = upid.match(/^UPID:(\S+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/);
495 if (!res) {
496 throw "unable to parse upid '" + upid + "'";
497 }
498 task.node = res[1];
499 task.pid = parseInt(res[2], 16);
500 task.pstart = parseInt(res[3], 16);
501 task.starttime = parseInt(res[4], 16);
502 task.type = res[5];
503 task.id = res[6];
504 task.user = res[7];
505
506 task.desc = PVE.Utils.format_task_description(task.type, task.id);
507
508 return task;
509 },
510
511 format_size: function(size) {
512 /*jslint confusion: true */
513
514 if (size < 1024) {
515 return size;
516 }
517
518 var kb = size / 1024;
519
520 if (kb < 1024) {
521 return kb.toFixed(0) + "KB";
522 }
523
524 var mb = size / (1024*1024);
525
526 if (mb < 1024) {
527 return mb.toFixed(0) + "MB";
528 }
529
530 var gb = mb / 1024;
531
532 if (gb < 1024) {
533 return gb.toFixed(2) + "GB";
534 }
535
536 var tb = gb / 1024;
537
538 return tb.toFixed(2) + "TB";
539
540 },
541
542 format_html_bar: function(per, text) {
543
544 return "<div class='pve-bar-wrap'>" + text + "<div class='pve-bar-border'>" +
545 "<div class='pve-bar-inner' style='width:" + per + "%;'></div>" +
546 "</div></div>";
547
548 },
549
550 format_cpu_bar: function(per1, per2, text) {
551
552 return "<div class='pve-bar-border'>" +
553 "<div class='pve-bar-inner' style='width:" + per1 + "%;'></div>" +
554 "<div class='pve-bar-inner2' style='width:" + per2 + "%;'></div>" +
555 "<div class='pve-bar-text'>" + text + "</div>" +
556 "</div>";
557 },
558
559 format_large_bar: function(per, text) {
560
561 if (!text) {
562 text = per.toFixed(1) + "%";
563 }
564
565 return "<div class='pve-largebar-border'>" +
566 "<div class='pve-largebar-inner' style='width:" + per + "%;'></div>" +
567 "<div class='pve-largebar-text'>" + text + "</div>" +
568 "</div>";
569 },
570
571 format_duration_long: function(ut) {
572
573 var days = Math.floor(ut / 86400);
574 ut -= days*86400;
575 var hours = Math.floor(ut / 3600);
576 ut -= hours*3600;
577 var mins = Math.floor(ut / 60);
578 ut -= mins*60;
579
580 var hours_str = '00' + hours.toString();
581 hours_str = hours_str.substr(hours_str.length - 2);
582 var mins_str = "00" + mins.toString();
583 mins_str = mins_str.substr(mins_str.length - 2);
584 var ut_str = "00" + ut.toString();
585 ut_str = ut_str.substr(ut_str.length - 2);
586
587 if (days) {
588 var ds = days > 1 ? PVE.Utils.daysText : PVE.Utils.dayText;
589 return days.toString() + ' ' + ds + ' ' +
590 hours_str + ':' + mins_str + ':' + ut_str;
591 } else {
592 return hours_str + ':' + mins_str + ':' + ut_str;
593 }
594 },
595
596 format_duration_short: function(ut) {
597
598 if (ut < 60) {
599 return ut.toString() + 's';
600 }
601
602 if (ut < 3600) {
603 var mins = ut / 60;
604 return mins.toFixed(0) + 'm';
605 }
606
607 if (ut < 86400) {
608 var hours = ut / 3600;
609 return hours.toFixed(0) + 'h';
610 }
611
612 var days = ut / 86400;
613 return days.toFixed(0) + 'd';
614 },
615
616 yesText: gettext('Yes'),
617 noText: gettext('No'),
618 errorText: gettext('Error'),
619 unknownText: gettext('Unknown'),
620 defaultText: gettext('Default'),
621 daysText: gettext('days'),
622 dayText: gettext('day'),
623 runningText: gettext('running'),
624 stoppedText: gettext('stopped'),
625 neverText: gettext('never'),
626
627 format_expire: function(date) {
628 if (!date) {
629 return PVE.Utils.neverText;
630 }
631 return Ext.Date.format(date, "Y-m-d");
632 },
633
634 format_storage_type: function(value) {
635 if (value === 'dir') {
636 return 'Directory';
637 } else if (value === 'nfs') {
638 return 'NFS';
639 } else if (value === 'lvm') {
640 return 'LVM';
641 } else if (value === 'iscsi') {
642 return 'iSCSI';
643 } else if (value === 'rbd') {
644 return 'RBD';
645 } else if (value === 'sheepdog') {
646 return 'Sheepdog';
647 } else if (value === 'nexenta') {
648 return 'Nexenta';
649 } else if (value === 'iscsidirect') {
650 return 'iSCSIDirect';
651 } else {
652 return PVE.Utils.unknownText;
653 }
654 },
655
656 format_boolean_with_default: function(value) {
657 if (Ext.isDefined(value) && value !== '') {
658 return value ? PVE.Utils.yesText : PVE.Utils.noText;
659 }
660 return PVE.Utils.defaultText;
661 },
662
663 format_boolean: function(value) {
664 return value ? PVE.Utils.yesText : PVE.Utils.noText;
665 },
666
667 format_neg_boolean: function(value) {
668 return !value ? PVE.Utils.yesText : PVE.Utils.noText;
669 },
670
671 format_content_types: function(value) {
672 var cta = [];
673
674 Ext.each(value.split(',').sort(), function(ct) {
675 if (ct === 'images') {
676 cta.push('Images');
677 } else if (ct === 'backup') {
678 cta.push('Backups');
679 } else if (ct === 'vztmpl') {
680 cta.push('Templates');
681 } else if (ct === 'iso') {
682 cta.push('ISO');
683 } else if (ct === 'rootdir') {
684 cta.push('Containers');
685 }
686 });
687
688 return cta.join(', ');
689 },
690
691 render_storage_content: function(value, metaData, record) {
692 var data = record.data;
693 if (Ext.isNumber(data.channel) &&
694 Ext.isNumber(data.id) &&
695 Ext.isNumber(data.lun)) {
696 return "CH " +
697 Ext.String.leftPad(data.channel,2, '0') +
698 " ID " + data.id + " LUN " + data.lun;
699 }
700 return data.volid.replace(/^.*:(.*\/)?/,'');
701 },
702
703 render_serverity: function (value) {
704 return PVE.Utils.log_severity_hash[value] || value;
705 },
706
707 render_cpu: function(value, metaData, record, rowIndex, colIndex, store) {
708
709 if (!(record.data.uptime && Ext.isNumeric(value))) {
710 return '';
711 }
712
713 var maxcpu = record.data.maxcpu || 1;
714
715 if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) {
716 return '';
717 }
718
719 var per = value * 100;
720
721 return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU');
722 },
723
724 render_size: function(value, metaData, record, rowIndex, colIndex, store) {
725 /*jslint confusion: true */
726
727 if (!Ext.isNumeric(value)) {
728 return '';
729 }
730
731 return PVE.Utils.format_size(value);
732 },
733
734 render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
735 var servertime = new Date(value * 1000);
736 return Ext.Date.format(servertime, 'Y-m-d H:i:s');
737 },
738
739 render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) {
740
741 var mem = value;
742 var maxmem = record.data.maxmem;
743
744 if (!record.data.uptime) {
745 return '';
746 }
747
748 if (!(Ext.isNumeric(mem) && maxmem)) {
749 return '';
750 }
751
752 var per = (mem * 100) / maxmem;
753
754 return per.toFixed(1) + '%';
755 },
756
757 render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) {
758
759 var disk = value;
760 var maxdisk = record.data.maxdisk;
761
762 if (!(Ext.isNumeric(disk) && maxdisk)) {
763 return '';
764 }
765
766 var per = (disk * 100) / maxdisk;
767
768 return per.toFixed(1) + '%';
769 },
770
771 render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) {
772
773 var cls = 'pve-itype-icon-' + value;
774
775 if (record.data.running) {
776 metaData.tdCls = cls + "-running";
777 } else if (record.data.template) {
778 metaData.tdCls = cls + "-template";
779 } else {
780 metaData.tdCls = cls;
781 }
782
783 return value;
784 },
785
786 render_uptime: function(value, metaData, record, rowIndex, colIndex, store) {
787
788 var uptime = value;
789
790 if (uptime === undefined) {
791 return '';
792 }
793
794 if (uptime <= 0) {
795 return '-';
796 }
797
798 return PVE.Utils.format_duration_long(uptime);
799 },
800
801 render_support_level: function(value, metaData, record) {
802 return PVE.Utils.support_level_hash[value] || '-';
803 },
804
805 render_upid: function(value, metaData, record) {
806 var type = record.data.type;
807 var id = record.data.id;
808
809 return PVE.Utils.format_task_description(type, id);
810 },
811
812 dialog_title: function(subject, create, isAdd) {
813 if (create) {
814 if (isAdd) {
815 return gettext('Add') + ': ' + subject;
816 } else {
817 return gettext('Create') + ': ' + subject;
818 }
819 } else {
820 return gettext('Edit') + ': ' + subject;
821 }
822 },
823
824 openConoleWindow: function(vmtype, vmid, nodename, vmname) {
825 var url = Ext.urlEncode({
826 console: vmtype, // kvm, openvz or shell
827 vmid: vmid,
828 vmname: vmname,
829 node: nodename
830 });
831 var nw = window.open("?" + url, '_blank',
832 "innerWidth=745,innerheight=427");
833 nw.focus();
834 },
835
836 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
837 // use el.mask() instead
838 setErrorMask: function(comp, msg) {
839 var el = comp.el;
840 if (!el) {
841 return;
842 }
843 if (!msg) {
844 el.unmask();
845 } else {
846 if (msg === true) {
847 el.mask(gettext("Loading..."));
848 } else {
849 el.mask(msg);
850 }
851 }
852 },
853
854 monStoreErrors: function(me, store) {
855 me.mon(store, 'beforeload', function(s, operation, eOpts) {
856 if (!me.loadCount) {
857 me.loadCount = 0; // make sure it is numeric
858 PVE.Utils.setErrorMask(me, true);
859 }
860 });
861
862 // only works with 'pve' proxy
863 me.mon(store.proxy, 'afterload', function(proxy, request, success) {
864 me.loadCount++;
865
866 if (success) {
867 PVE.Utils.setErrorMask(me, false);
868 return;
869 }
870
871 var msg;
872 var operation = request.operation;
873 var error = operation.getError();
874 if (error.statusText) {
875 msg = error.statusText + ' (' + error.status + ')';
876 } else {
877 msg = gettext('Connection error');
878 }
879 PVE.Utils.setErrorMask(me, msg);
880 });
881 }
882
883 }});
884