]>
Commit | Line | Data |
---|---|---|
aff192e6 DM |
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 | ||
10b4a988 | 28 | // custom Vtypes |
aff192e6 DM |
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 | }, | |
a2dca26b | 33 | IPAddressText: gettext('Example') + ': 192.168.1.1', |
aff192e6 DM |
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:]/, | |
a2dca26b | 40 | MacAddressText: gettext('Example') + ': 01:23:45:67:89:ab', |
aff192e6 DM |
41 | |
42 | BridgeName: function(v) { | |
43 | return (/^vmbr\d{1,4}$/).test(v); | |
44 | }, | |
a2dca26b | 45 | BridgeNameText: gettext('Format') + ': vmbr<b>N</b>, where 0 <= <b>N</b> <= 9999', |
aff192e6 DM |
46 | |
47 | BondName: function(v) { | |
48 | return (/^bond\d{1,4}$/).test(v); | |
49 | }, | |
a2dca26b | 50 | BondNameText: gettext('Format') + ': bond<b>N</b>, where 0 <= <b>N</b> <= 9999', |
aff192e6 DM |
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 | }, | |
a2dca26b | 55 | QemuStartDateText: gettext('Format') + ': "now" or "2006-06-17T16:01:21" or "2006-06-17"', |
aff192e6 DM |
56 | |
57 | StorageId: function(v) { | |
58 | return (/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i).test(v); | |
59 | }, | |
a2dca26b | 60 | StorageIdText: gettext("Allowed characters") + ": 'a-z', '0-9', '-', '_', '.'", |
10b4a988 DM |
61 | |
62 | HttpProxy: function(v) { | |
63 | return (/^http:\/\/.*$/).test(v); | |
64 | }, | |
667b2be9 DM |
65 | HttpProxyText: gettext('Example') + ": http://username:password@host:port/", |
66 | ||
67 | DnsName: function(v) { | |
0d201dfe | 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); |
667b2be9 DM |
69 | }, |
70 | DnsNameText: gettext('This is not a valid DNS name') | |
aff192e6 DM |
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 | ||
16b69b6c DM |
103 | support_level_hash: { |
104 | 'c': gettext('Community'), | |
105 | 'b': gettext('Basic'), | |
106 | 's': gettext('Standard'), | |
107 | 'p': gettext('Premium') | |
108 | }, | |
109 | ||
aff192e6 | 110 | kvm_ostypes: { |
2ed04b3c | 111 | other: gettext('Other OS types'), |
aff192e6 DM |
112 | wxp: 'Microsoft Windows XP/2003', |
113 | w2k: 'Microsoft Windows 2000', | |
114 | w2k8: 'Microsoft Windows Vista/2008', | |
115 | win7: 'Microsoft Windows 7/2008r2', | |
1d7334d2 | 116 | win8: 'Microsoft Windows 8/2012', |
aff192e6 | 117 | l24: 'Linux 2.4 Kernel', |
1ea7a234 | 118 | l26: 'Linux 3.X/2.6 Kernel' |
aff192e6 DM |
119 | }, |
120 | ||
121 | render_kvm_ostype: function (value) { | |
122 | if (!value) { | |
2ed04b3c | 123 | return gettext('Other OS types'); |
aff192e6 DM |
124 | } |
125 | var text = PVE.Utils.kvm_ostypes[value]; | |
126 | if (text) { | |
127 | return text + ' (' + value + ')'; | |
128 | } | |
129 | return value; | |
130 | }, | |
131 | ||
74184048 DM |
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 | ||
aff192e6 DM |
146 | // fixme: auto-generate this |
147 | // for now, please keep in sync with PVE::Tools::kvmkeymaps | |
148 | kvm_keymaps: { | |
149 | //ar: 'Arabic', | |
5fe5283f | 150 | da: 'Danish', |
aff192e6 DM |
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', | |
b4e7aa2c | 178 | sl: 'Slovenian', |
1133bd83 | 179 | sv: 'Swedish', |
aff192e6 | 180 | //th: 'Thai', |
1133bd83 | 181 | tr: 'Turkish' |
aff192e6 DM |
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) { | |
2ed04b3c | 192 | return PVE.Utils.defaultText; |
aff192e6 DM |
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 | ||
19fa0647 | 210 | language_map: { |
d0525640 | 211 | zh_CN: 'Chinese', |
226edf6d | 212 | ca: 'Catalan', |
22217294 | 213 | ja: 'Japanese', |
19fa0647 | 214 | en: 'English', |
18fb1962 | 215 | da: 'Danish', |
2198a479 | 216 | de: 'German', |
8009570d | 217 | es: 'Spanish', |
4a3a5520 | 218 | fr: 'French', |
72da7de7 | 219 | it: 'Italian', |
2d32003f DM |
220 | nb: 'Norwegian (Bokmal)', |
221 | nn: 'Norwegian (Nynorsk)', | |
a4326a6c | 222 | ru: 'Russian', |
b4e7aa2c | 223 | sl: 'Slovenian', |
05e3e996 | 224 | sv: 'Swedish', |
fb1cf1bf | 225 | pl: 'Polish', |
d97c5983 | 226 | pt_BR: 'Portuguese (Brazil)', |
05e3e996 | 227 | tr: 'Turkish' |
19fa0647 DM |
228 | }, |
229 | ||
230 | render_language: function (value) { | |
231 | if (!value) { | |
7e6b14a8 | 232 | return PVE.Utils.defaultText + ' (English)'; |
19fa0647 DM |
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 | ||
aff192e6 DM |
250 | render_kvm_vga_driver: function (value) { |
251 | if (!value) { | |
2ed04b3c | 252 | return PVE.Utils.defaultText; |
aff192e6 DM |
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 | ||
640f8f14 DM |
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 | ||
aff192e6 DM |
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) { | |
a2dca26b | 307 | var msg = gettext('Successful'); |
aff192e6 DM |
308 | |
309 | if (!result.success) { | |
a2dca26b | 310 | msg = gettext("Unknown error"); |
aff192e6 DM |
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: | |
a2dca26b | 333 | msg = gettext('Form fields may not be submitted with invalid values'); |
aff192e6 DM |
334 | break; |
335 | case Ext.form.action.Action.CONNECT_FAILURE: | |
a2dca26b | 336 | msg = gettext('Connection error'); |
aff192e6 DM |
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({ | |
a2dca26b | 354 | waitMsg: gettext('Please wait...') |
aff192e6 DM |
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 | } | |
52406c42 DM |
383 | response.result = {}; |
384 | try { | |
385 | response.result = Ext.decode(response.responseText); | |
db16032c | 386 | } catch(e) {} |
a2dca26b | 387 | var msg = gettext('Connection error') + ' - server offline?'; |
aff192e6 | 388 | if (response.aborted) { |
a2dca26b | 389 | msg = gettext('Connection error') + ' - aborted.'; |
aff192e6 | 390 | } else if (response.timedout) { |
a2dca26b | 391 | msg = gettext('Connection error') + ' - Timeout.'; |
aff192e6 | 392 | } else if (response.status && response.statusText) { |
a2dca26b | 393 | msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText; |
aff192e6 DM |
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 | |
2c535170 | 407 | target.setLoading(newopts.waitMsg); |
aff192e6 DM |
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 | ||
acc0c13a | 432 | task_desc_table: { |
c25f9d30 | 433 | vncproxy: [ 'VM/CT', gettext('Console') ], |
2ed04b3c | 434 | vncshell: [ '', gettext('Shell') ], |
93d4a58a DM |
435 | qmsnapshot: [ 'VM', gettext('Snapshot') ], |
436 | qmrollback: [ 'VM', gettext('Rollback') ], | |
437 | qmdelsnapshot: [ 'VM', gettext('Delete Snapshot') ], | |
c25f9d30 DM |
438 | qmcreate: [ 'VM', gettext('Create') ], |
439 | qmrestore: [ 'VM', gettext('Restore') ], | |
440 | qmdestroy: [ 'VM', gettext('Destroy') ], | |
441 | qmigrate: [ 'VM', gettext('Migrate') ], | |
f16369ee | 442 | qmclone: [ 'VM', gettext('Clone') ], |
772a8ffb | 443 | qmmove: [ 'VM', gettext('Move disk') ], |
dd271a32 | 444 | qmtemplate: [ 'VM', gettext('Convert to template') ], |
c25f9d30 DM |
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') ], | |
9fb30485 | 451 | qmconfig: [ 'VM', gettext('Configure') ], |
c25f9d30 DM |
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') ], | |
8710f280 DM |
458 | vzmount: ['CT', gettext('Mount') ], |
459 | vzumount: ['CT', gettext('Unmount') ], | |
51ed1415 | 460 | vzshutdown: ['CT', gettext('Shutdown') ], |
3f76170f DM |
461 | hamigrate: [ 'HA', gettext('Migrate') ], |
462 | hastart: [ 'HA', gettext('Start') ], | |
463 | hastop: [ 'HA', gettext('Stop') ], | |
c25f9d30 DM |
464 | srvstart: ['SRV', gettext('Start') ], |
465 | srvstop: ['SRV', gettext('Stop') ], | |
466 | srvrestart: ['SRV', gettext('Restart') ], | |
467 | srvreload: ['SRV', gettext('Reload') ], | |
c9164975 | 468 | imgcopy: ['', gettext('Copy data') ], |
4a3a5520 | 469 | imgdel: ['', gettext('Erase data') ], |
c9164975 | 470 | download: ['', gettext('Download') ], |
640f8f14 DM |
471 | vzdump: ['', gettext('Backup') ], |
472 | startall: [ '', gettext('Start all VMs and Containers') ], | |
473 | stopall: [ '', gettext('Stop all VMs and Containers') ] | |
acc0c13a | 474 | }, |
d1233856 | 475 | |
acc0c13a | 476 | format_task_description: function(type, id) { |
2ed04b3c DM |
477 | var farray = PVE.Utils.task_desc_table[type]; |
478 | if (!farray) { | |
479 | return type; | |
d1233856 | 480 | } |
2ed04b3c | 481 | var prefix = farray[0]; |
c25f9d30 | 482 | var text = farray[1]; |
2ed04b3c | 483 | if (prefix) { |
c25f9d30 | 484 | return prefix + ' ' + id + ' - ' + text; |
2ed04b3c | 485 | } |
81e94b6d | 486 | return text; |
aff192e6 DM |
487 | }, |
488 | ||
aff192e6 DM |
489 | parse_task_upid: function(upid) { |
490 | var task = {}; | |
491 | ||
55d19d9b | 492 | var res = upid.match(/^UPID:(\S+):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([0-9A-Fa-f]{8}):([^:\s]+):([^:\s]*):([^:\s]+):$/); |
aff192e6 DM |
493 | if (!res) { |
494 | throw "unable to parse upid '" + upid + "'"; | |
495 | } | |
496 | task.node = res[1]; | |
497 | task.pid = parseInt(res[2], 16); | |
498 | task.pstart = parseInt(res[3], 16); | |
499 | task.starttime = parseInt(res[4], 16); | |
500 | task.type = res[5]; | |
501 | task.id = res[6]; | |
502 | task.user = res[7]; | |
503 | ||
504 | task.desc = PVE.Utils.format_task_description(task.type, task.id); | |
505 | ||
506 | return task; | |
507 | }, | |
508 | ||
509 | format_size: function(size) { | |
a2f57991 | 510 | /*jslint confusion: true */ |
aff192e6 | 511 | |
a1d849df DM |
512 | if (size < 1024) { |
513 | return size; | |
514 | } | |
515 | ||
aff192e6 DM |
516 | var kb = size / 1024; |
517 | ||
518 | if (kb < 1024) { | |
519 | return kb.toFixed(0) + "KB"; | |
520 | } | |
521 | ||
522 | var mb = size / (1024*1024); | |
523 | ||
524 | if (mb < 1024) { | |
525 | return mb.toFixed(0) + "MB"; | |
526 | } | |
527 | ||
528 | var gb = mb / 1024; | |
529 | ||
530 | if (gb < 1024) { | |
531 | return gb.toFixed(2) + "GB"; | |
532 | } | |
533 | ||
534 | var tb = gb / 1024; | |
535 | ||
536 | return tb.toFixed(2) + "TB"; | |
537 | ||
538 | }, | |
539 | ||
540 | format_html_bar: function(per, text) { | |
541 | ||
542 | return "<div class='pve-bar-wrap'>" + text + "<div class='pve-bar-border'>" + | |
543 | "<div class='pve-bar-inner' style='width:" + per + "%;'></div>" + | |
544 | "</div></div>"; | |
545 | ||
546 | }, | |
547 | ||
548 | format_cpu_bar: function(per1, per2, text) { | |
549 | ||
550 | return "<div class='pve-bar-border'>" + | |
551 | "<div class='pve-bar-inner' style='width:" + per1 + "%;'></div>" + | |
552 | "<div class='pve-bar-inner2' style='width:" + per2 + "%;'></div>" + | |
553 | "<div class='pve-bar-text'>" + text + "</div>" + | |
554 | "</div>"; | |
555 | }, | |
556 | ||
557 | format_large_bar: function(per, text) { | |
558 | ||
559 | if (!text) { | |
560 | text = per.toFixed(1) + "%"; | |
561 | } | |
562 | ||
563 | return "<div class='pve-largebar-border'>" + | |
564 | "<div class='pve-largebar-inner' style='width:" + per + "%;'></div>" + | |
565 | "<div class='pve-largebar-text'>" + text + "</div>" + | |
566 | "</div>"; | |
567 | }, | |
568 | ||
569 | format_duration_long: function(ut) { | |
570 | ||
571 | var days = Math.floor(ut / 86400); | |
572 | ut -= days*86400; | |
573 | var hours = Math.floor(ut / 3600); | |
574 | ut -= hours*3600; | |
575 | var mins = Math.floor(ut / 60); | |
576 | ut -= mins*60; | |
577 | ||
578 | var hours_str = '00' + hours.toString(); | |
579 | hours_str = hours_str.substr(hours_str.length - 2); | |
580 | var mins_str = "00" + mins.toString(); | |
581 | mins_str = mins_str.substr(mins_str.length - 2); | |
582 | var ut_str = "00" + ut.toString(); | |
583 | ut_str = ut_str.substr(ut_str.length - 2); | |
584 | ||
585 | if (days) { | |
7e6b14a8 | 586 | var ds = days > 1 ? PVE.Utils.daysText : PVE.Utils.dayText; |
aff192e6 DM |
587 | return days.toString() + ' ' + ds + ' ' + |
588 | hours_str + ':' + mins_str + ':' + ut_str; | |
589 | } else { | |
590 | return hours_str + ':' + mins_str + ':' + ut_str; | |
591 | } | |
592 | }, | |
593 | ||
594 | format_duration_short: function(ut) { | |
595 | ||
596 | if (ut < 60) { | |
597 | return ut.toString() + 's'; | |
598 | } | |
599 | ||
600 | if (ut < 3600) { | |
601 | var mins = ut / 60; | |
602 | return mins.toFixed(0) + 'm'; | |
603 | } | |
604 | ||
605 | if (ut < 86400) { | |
606 | var hours = ut / 3600; | |
607 | return hours.toFixed(0) + 'h'; | |
608 | } | |
609 | ||
610 | var days = ut / 86400; | |
611 | return days.toFixed(0) + 'd'; | |
612 | }, | |
613 | ||
7e6b14a8 DM |
614 | yesText: gettext('Yes'), |
615 | noText: gettext('No'), | |
32314691 | 616 | errorText: gettext('Error'), |
7e6b14a8 DM |
617 | unknownText: gettext('Unknown'), |
618 | defaultText: gettext('Default'), | |
619 | daysText: gettext('days'), | |
620 | dayText: gettext('day'), | |
32314691 DM |
621 | runningText: gettext('running'), |
622 | stoppedText: gettext('stopped'), | |
b62861ce DM |
623 | neverText: gettext('never'), |
624 | ||
625 | format_expire: function(date) { | |
626 | if (!date) { | |
627 | return PVE.Utils.neverText; | |
628 | } | |
629 | return Ext.Date.format(date, "Y-m-d"); | |
630 | }, | |
7e6b14a8 | 631 | |
aff192e6 DM |
632 | format_storage_type: function(value) { |
633 | if (value === 'dir') { | |
634 | return 'Directory'; | |
635 | } else if (value === 'nfs') { | |
636 | return 'NFS'; | |
637 | } else if (value === 'lvm') { | |
638 | return 'LVM'; | |
639 | } else if (value === 'iscsi') { | |
640 | return 'iSCSI'; | |
56fd4763 AD |
641 | } else if (value === 'rbd') { |
642 | return 'RBD'; | |
643 | } else if (value === 'sheepdog') { | |
644 | return 'Sheepdog'; | |
645 | } else if (value === 'nexenta') { | |
646 | return 'Nexenta'; | |
647 | } else if (value === 'iscsidirect') { | |
648 | return 'iSCSIDirect'; | |
aff192e6 | 649 | } else { |
7e6b14a8 | 650 | return PVE.Utils.unknownText; |
aff192e6 DM |
651 | } |
652 | }, | |
653 | ||
654 | format_boolean_with_default: function(value) { | |
655 | if (Ext.isDefined(value) && value !== '') { | |
a2dca26b | 656 | return value ? PVE.Utils.yesText : PVE.Utils.noText; |
aff192e6 | 657 | } |
a2dca26b | 658 | return PVE.Utils.defaultText; |
aff192e6 DM |
659 | }, |
660 | ||
661 | format_boolean: function(value) { | |
a2dca26b | 662 | return value ? PVE.Utils.yesText : PVE.Utils.noText; |
aff192e6 DM |
663 | }, |
664 | ||
665 | format_neg_boolean: function(value) { | |
a2dca26b | 666 | return !value ? PVE.Utils.yesText : PVE.Utils.noText; |
aff192e6 DM |
667 | }, |
668 | ||
669 | format_content_types: function(value) { | |
670 | var cta = []; | |
671 | ||
f29297db | 672 | Ext.each(value.split(',').sort(), function(ct) { |
aff192e6 DM |
673 | if (ct === 'images') { |
674 | cta.push('Images'); | |
675 | } else if (ct === 'backup') { | |
676 | cta.push('Backups'); | |
677 | } else if (ct === 'vztmpl') { | |
678 | cta.push('Templates'); | |
679 | } else if (ct === 'iso') { | |
680 | cta.push('ISO'); | |
9f767883 DM |
681 | } else if (ct === 'rootdir') { |
682 | cta.push('Containers'); | |
aff192e6 DM |
683 | } |
684 | }); | |
685 | ||
686 | return cta.join(', '); | |
687 | }, | |
688 | ||
689 | render_storage_content: function(value, metaData, record) { | |
690 | var data = record.data; | |
691 | if (Ext.isNumber(data.channel) && | |
692 | Ext.isNumber(data.id) && | |
693 | Ext.isNumber(data.lun)) { | |
694 | return "CH " + | |
695 | Ext.String.leftPad(data.channel,2, '0') + | |
696 | " ID " + data.id + " LUN " + data.lun; | |
697 | } | |
698 | return data.volid.replace(/^.*:(.*\/)?/,''); | |
699 | }, | |
700 | ||
701 | render_serverity: function (value) { | |
702 | return PVE.Utils.log_severity_hash[value] || value; | |
703 | }, | |
704 | ||
705 | render_cpu: function(value, metaData, record, rowIndex, colIndex, store) { | |
706 | ||
105270d3 | 707 | if (!(record.data.uptime && Ext.isNumeric(value))) { |
aff192e6 DM |
708 | return ''; |
709 | } | |
710 | ||
105270d3 DM |
711 | var maxcpu = record.data.maxcpu || 1; |
712 | ||
713 | if (!Ext.isNumeric(maxcpu) && (maxcpu >= 1)) { | |
aff192e6 DM |
714 | return ''; |
715 | } | |
105270d3 DM |
716 | |
717 | var per = value * 100; | |
aff192e6 DM |
718 | |
719 | return per.toFixed(1) + '% of ' + maxcpu.toString() + (maxcpu > 1 ? 'CPUs' : 'CPU'); | |
720 | }, | |
721 | ||
722 | render_size: function(value, metaData, record, rowIndex, colIndex, store) { | |
a2f57991 | 723 | /*jslint confusion: true */ |
aff192e6 DM |
724 | |
725 | if (!Ext.isNumeric(value)) { | |
726 | return ''; | |
727 | } | |
728 | ||
729 | return PVE.Utils.format_size(value); | |
730 | }, | |
731 | ||
732 | render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) { | |
733 | var servertime = new Date(value * 1000); | |
734 | return Ext.Date.format(servertime, 'Y-m-d H:i:s'); | |
735 | }, | |
736 | ||
737 | render_mem_usage: function(value, metaData, record, rowIndex, colIndex, store) { | |
738 | ||
739 | var mem = value; | |
740 | var maxmem = record.data.maxmem; | |
741 | ||
742 | if (!record.data.uptime) { | |
743 | return ''; | |
744 | } | |
745 | ||
746 | if (!(Ext.isNumeric(mem) && maxmem)) { | |
747 | return ''; | |
748 | } | |
749 | ||
750 | var per = (mem * 100) / maxmem; | |
751 | ||
752 | return per.toFixed(1) + '%'; | |
753 | }, | |
754 | ||
755 | render_disk_usage: function(value, metaData, record, rowIndex, colIndex, store) { | |
756 | ||
757 | var disk = value; | |
758 | var maxdisk = record.data.maxdisk; | |
759 | ||
760 | if (!(Ext.isNumeric(disk) && maxdisk)) { | |
761 | return ''; | |
762 | } | |
763 | ||
764 | var per = (disk * 100) / maxdisk; | |
765 | ||
766 | return per.toFixed(1) + '%'; | |
767 | }, | |
768 | ||
769 | render_resource_type: function(value, metaData, record, rowIndex, colIndex, store) { | |
770 | ||
771 | var cls = 'pve-itype-icon-' + value; | |
772 | ||
773 | if (record.data.running) { | |
774 | metaData.tdCls = cls + "-running"; | |
5f28da0a DM |
775 | } else if (record.data.template) { |
776 | metaData.tdCls = cls + "-template"; | |
aff192e6 DM |
777 | } else { |
778 | metaData.tdCls = cls; | |
779 | } | |
780 | ||
781 | return value; | |
782 | }, | |
783 | ||
784 | render_uptime: function(value, metaData, record, rowIndex, colIndex, store) { | |
785 | ||
786 | var uptime = value; | |
787 | ||
788 | if (uptime === undefined) { | |
789 | return ''; | |
790 | } | |
791 | ||
792 | if (uptime <= 0) { | |
793 | return '-'; | |
794 | } | |
795 | ||
796 | return PVE.Utils.format_duration_long(uptime); | |
797 | }, | |
798 | ||
16b69b6c DM |
799 | render_support_level: function(value, metaData, record) { |
800 | return PVE.Utils.support_level_hash[value] || '-'; | |
801 | }, | |
802 | ||
aff192e6 DM |
803 | render_upid: function(value, metaData, record) { |
804 | var type = record.data.type; | |
805 | var id = record.data.id; | |
806 | ||
807 | return PVE.Utils.format_task_description(type, id); | |
176eee4f DM |
808 | }, |
809 | ||
58eb5905 DM |
810 | dialog_title: function(subject, create, isAdd) { |
811 | if (create) { | |
812 | if (isAdd) { | |
813 | return gettext('Add') + ': ' + subject; | |
814 | } else { | |
815 | return gettext('Create') + ': ' + subject; | |
816 | } | |
817 | } else { | |
818 | return gettext('Edit') + ': ' + subject; | |
819 | } | |
820 | }, | |
821 | ||
fb3feb55 | 822 | openConoleWindow: function(vmtype, vmid, nodename, vmname) { |
176eee4f | 823 | var url = Ext.urlEncode({ |
fb3feb55 | 824 | console: vmtype, // kvm, openvz or shell |
176eee4f | 825 | vmid: vmid, |
fb3feb55 | 826 | vmname: vmname, |
176eee4f DM |
827 | node: nodename |
828 | }); | |
829 | var nw = window.open("?" + url, '_blank', | |
830 | "innerWidth=745,innerheight=427"); | |
831 | nw.focus(); | |
5f663f5f DM |
832 | }, |
833 | ||
834 | // comp.setLoading() is buggy in ExtJS 4.0.7, so we | |
835 | // use el.mask() instead | |
836 | setErrorMask: function(comp, msg) { | |
837 | var el = comp.el; | |
838 | if (!el) { | |
839 | return; | |
840 | } | |
841 | if (!msg) { | |
842 | el.unmask(); | |
843 | } else { | |
844 | if (msg === true) { | |
845 | el.mask(gettext("Loading...")); | |
846 | } else { | |
847 | el.mask(msg); | |
848 | } | |
849 | } | |
850 | }, | |
851 | ||
852 | monStoreErrors: function(me, store) { | |
853 | me.mon(store, 'beforeload', function(s, operation, eOpts) { | |
854 | if (!me.loadCount) { | |
855 | me.loadCount = 0; // make sure it is numeric | |
856 | PVE.Utils.setErrorMask(me, true); | |
857 | } | |
858 | }); | |
859 | ||
860 | // only works with 'pve' proxy | |
861 | me.mon(store.proxy, 'afterload', function(proxy, request, success) { | |
862 | me.loadCount++; | |
863 | ||
864 | if (success) { | |
865 | PVE.Utils.setErrorMask(me, false); | |
866 | return; | |
867 | } | |
868 | ||
869 | var msg; | |
870 | var operation = request.operation; | |
871 | var error = operation.getError(); | |
872 | if (error.statusText) { | |
873 | msg = error.statusText + ' (' + error.status + ')'; | |
874 | } else { | |
875 | msg = gettext('Connection error'); | |
876 | } | |
877 | PVE.Utils.setErrorMask(me, msg); | |
878 | }); | |
aff192e6 | 879 | } |
176eee4f | 880 | |
aff192e6 DM |
881 | }}); |
882 |