]>
Commit | Line | Data |
---|---|---|
0bb29d35 DM |
1 | Ext.ns('Proxmox'); |
2 | Ext.ns('Proxmox.Setup'); | |
3 | ||
757cc58a DM |
4 | if (!Ext.isDefined(Proxmox.Setup.auth_cookie_name)) { |
5 | throw "Proxmox library not initialized"; | |
0bb29d35 DM |
6 | } |
7 | ||
8 | // avoid errors related to Accessible Rich Internet Applications | |
9 | // (access for people with disabilities) | |
10 | // TODO reenable after all components are upgraded | |
11 | Ext.enableAria = false; | |
12 | Ext.enableAriaButtons = false; | |
13 | Ext.enableAriaPanels = false; | |
14 | ||
15 | // avoid errors when running without development tools | |
16 | if (!Ext.isDefined(Ext.global.console)) { | |
17 | var console = { | |
18 | dir: function() {}, | |
19 | log: function() {} | |
20 | }; | |
21 | } | |
22 | ||
23 | Ext.Ajax.defaultHeaders = { | |
24 | 'Accept': 'application/json' | |
25 | }; | |
26 | ||
27 | Ext.Ajax.on('beforerequest', function(conn, options) { | |
28 | if (Proxmox.CSRFPreventionToken) { | |
29 | if (!options.headers) { | |
30 | options.headers = {}; | |
31 | } | |
32 | options.headers.CSRFPreventionToken = Proxmox.CSRFPreventionToken; | |
33 | } | |
34 | }); | |
35 | ||
36 | Ext.define('Proxmox.Utils', { utilities: { | |
37 | ||
38 | // this singleton contains miscellaneous utilities | |
39 | ||
30f885cb DM |
40 | yesText: gettext('Yes'), |
41 | noText: gettext('No'), | |
42 | enabledText: gettext('Enabled'), | |
43 | disabledText: gettext('Disabled'), | |
44 | noneText: gettext('none'), | |
45 | errorText: gettext('Error'), | |
a58001dd | 46 | unknownText: gettext('Unknown'), |
30f885cb DM |
47 | defaultText: gettext('Default'), |
48 | daysText: gettext('days'), | |
49 | dayText: gettext('day'), | |
50 | runningText: gettext('running'), | |
51 | stoppedText: gettext('stopped'), | |
52 | neverText: gettext('never'), | |
53 | totalText: gettext('Total'), | |
54 | usedText: gettext('Used'), | |
55 | directoryText: gettext('Directory'), | |
56 | stateText: gettext('State'), | |
57 | groupText: gettext('Group'), | |
58 | ||
f6f0066a | 59 | language_map: { |
0247a07a TL |
60 | zh_CN: 'Chinese (Simplified)', |
61 | zh_TW: 'Chinese (Traditional)', | |
f97a2a34 DC |
62 | ca: 'Catalan', |
63 | da: 'Danish', | |
f6f0066a | 64 | en: 'English', |
f97a2a34 | 65 | eu: 'Euskera (Basque)', |
f6f0066a DM |
66 | fr: 'French', |
67 | de: 'German', | |
68 | it: 'Italian', | |
f97a2a34 DC |
69 | es: 'Spanish', |
70 | ja: 'Japanese', | |
71 | nb: 'Norwegian (Bokmal)', | |
72 | nn: 'Norwegian (Nynorsk)', | |
73 | fa: 'Persian (Farsi)', | |
74 | pl: 'Polish', | |
75 | pt_BR: 'Portuguese (Brazil)', | |
76 | ru: 'Russian', | |
77 | sl: 'Slovenian', | |
78 | sv: 'Swedish', | |
79 | tr: 'Turkish' | |
f6f0066a DM |
80 | }, |
81 | ||
82 | render_language: function (value) { | |
83 | if (!value) { | |
84 | return Proxmox.Utils.defaultText + ' (English)'; | |
85 | } | |
86 | var text = Proxmox.Utils.language_map[value]; | |
87 | if (text) { | |
88 | return text + ' (' + value + ')'; | |
89 | } | |
90 | return value; | |
91 | }, | |
92 | ||
93 | language_array: function() { | |
94 | var data = [['__default__', Proxmox.Utils.render_language('')]]; | |
95 | Ext.Object.each(Proxmox.Utils.language_map, function(key, value) { | |
96 | data.push([key, Proxmox.Utils.render_language(value)]); | |
97 | }); | |
98 | ||
99 | return data; | |
100 | }, | |
101 | ||
5f93e010 DM |
102 | getNoSubKeyHtml: function(url) { |
103 | // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans | |
104 | return Ext.String.format('You do not have a valid subscription for this server. Please visit <a target="_blank" href="{0}">www.proxmox.com</a> to get a list of available options.', url || 'http://www.proxmox.com'); | |
105 | }, | |
106 | ||
b0d9b5d1 DM |
107 | format_boolean_with_default: function(value) { |
108 | if (Ext.isDefined(value) && value !== '__default__') { | |
109 | return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText; | |
110 | } | |
111 | return Proxmox.Utils.defaultText; | |
112 | }, | |
113 | ||
114 | format_boolean: function(value) { | |
115 | return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText; | |
116 | }, | |
117 | ||
118 | format_neg_boolean: function(value) { | |
119 | return !value ? Proxmox.Utils.yesText : Proxmox.Utils.noText; | |
120 | }, | |
a58001dd | 121 | |
0e49da6d DM |
122 | format_enabled_toggle: function(value) { |
123 | return value ? Proxmox.Utils.enabledText : Proxmox.Utils.disabledText; | |
124 | }, | |
125 | ||
2d0153a5 DM |
126 | format_expire: function(date) { |
127 | if (!date) { | |
128 | return Proxmox.Utils.neverText; | |
129 | } | |
130 | return Ext.Date.format(date, "Y-m-d"); | |
131 | }, | |
132 | ||
452892df DM |
133 | format_duration_long: function(ut) { |
134 | ||
135 | var days = Math.floor(ut / 86400); | |
136 | ut -= days*86400; | |
137 | var hours = Math.floor(ut / 3600); | |
138 | ut -= hours*3600; | |
139 | var mins = Math.floor(ut / 60); | |
140 | ut -= mins*60; | |
141 | ||
142 | var hours_str = '00' + hours.toString(); | |
143 | hours_str = hours_str.substr(hours_str.length - 2); | |
144 | var mins_str = "00" + mins.toString(); | |
145 | mins_str = mins_str.substr(mins_str.length - 2); | |
146 | var ut_str = "00" + ut.toString(); | |
147 | ut_str = ut_str.substr(ut_str.length - 2); | |
148 | ||
149 | if (days) { | |
150 | var ds = days > 1 ? Proxmox.Utils.daysText : Proxmox.Utils.dayText; | |
151 | return days.toString() + ' ' + ds + ' ' + | |
152 | hours_str + ':' + mins_str + ':' + ut_str; | |
153 | } else { | |
154 | return hours_str + ':' + mins_str + ':' + ut_str; | |
155 | } | |
156 | }, | |
157 | ||
02ef30c9 DM |
158 | format_subscription_level: function(level) { |
159 | if (level === 'c') { | |
8f5a1a08 | 160 | return 'Community'; |
02ef30c9 | 161 | } else if (level === 'b') { |
8f5a1a08 | 162 | return 'Basic'; |
02ef30c9 | 163 | } else if (level === 's') { |
8f5a1a08 | 164 | return 'Standard'; |
02ef30c9 | 165 | } else if (level === 'p') { |
8f5a1a08 | 166 | return 'Premium'; |
02ef30c9 DM |
167 | } else { |
168 | return Proxmox.Utils.noneText; | |
169 | } | |
170 | }, | |
171 | ||
28e54f37 DM |
172 | compute_min_label_width: function(text, width) { |
173 | ||
174 | if (width === undefined) { width = 100; } | |
175 | ||
176 | var tm = new Ext.util.TextMetrics(); | |
177 | var min = tm.getWidth(text + ':'); | |
178 | ||
179 | return min < width ? width : min; | |
180 | }, | |
181 | ||
c2e25a21 WB |
182 | setAuthData: function(data) { |
183 | Proxmox.CSRFPreventionToken = data.CSRFPreventionToken; | |
184 | Proxmox.UserName = data.username; | |
185 | Proxmox.LoggedOut = data.LoggedOut; | |
186 | // creates a session cookie (expire = null) | |
187 | // that way the cookie gets deleted after the browser window is closed | |
188 | Ext.util.Cookies.set(Proxmox.Setup.auth_cookie_name, data.ticket, null, '/', null, true); | |
189 | }, | |
190 | ||
0bb29d35 | 191 | authOK: function() { |
c2e25a21 WB |
192 | if (Proxmox.LoggedOut) { |
193 | return undefined; | |
194 | } | |
0bb29d35 DM |
195 | return (Proxmox.UserName !== '') && Ext.util.Cookies.get(Proxmox.Setup.auth_cookie_name); |
196 | }, | |
197 | ||
198 | authClear: function() { | |
c2e25a21 WB |
199 | if (Proxmox.LoggedOut) { |
200 | return undefined; | |
201 | } | |
0bb29d35 DM |
202 | Ext.util.Cookies.clear(Proxmox.Setup.auth_cookie_name); |
203 | }, | |
204 | ||
205 | // comp.setLoading() is buggy in ExtJS 4.0.7, so we | |
206 | // use el.mask() instead | |
207 | setErrorMask: function(comp, msg) { | |
208 | var el = comp.el; | |
209 | if (!el) { | |
210 | return; | |
211 | } | |
212 | if (!msg) { | |
213 | el.unmask(); | |
214 | } else { | |
215 | if (msg === true) { | |
216 | el.mask(gettext("Loading...")); | |
217 | } else { | |
218 | el.mask(msg); | |
219 | } | |
220 | } | |
221 | }, | |
222 | ||
5b4b3ffd DM |
223 | monStoreErrors: function(me, store, clearMaskBeforeLoad) { |
224 | if (clearMaskBeforeLoad) { | |
225 | me.mon(store, 'beforeload', function(s, operation, eOpts) { | |
226 | Proxmox.Utils.setErrorMask(me, false); | |
e94c0767 | 227 | }); |
5b4b3ffd DM |
228 | } else { |
229 | me.mon(store, 'beforeload', function(s, operation, eOpts) { | |
230 | if (!me.loadCount) { | |
231 | me.loadCount = 0; // make sure it is numeric | |
232 | Proxmox.Utils.setErrorMask(me, true); | |
233 | } | |
234 | }); | |
235 | } | |
0bb29d35 DM |
236 | |
237 | // only works with 'proxmox' proxy | |
238 | me.mon(store.proxy, 'afterload', function(proxy, request, success) { | |
239 | me.loadCount++; | |
240 | ||
241 | if (success) { | |
242 | Proxmox.Utils.setErrorMask(me, false); | |
243 | return; | |
244 | } | |
245 | ||
246 | var msg; | |
247 | /*jslint nomen: true */ | |
248 | var operation = request._operation; | |
249 | var error = operation.getError(); | |
250 | if (error.statusText) { | |
251 | msg = error.statusText + ' (' + error.status + ')'; | |
252 | } else { | |
253 | msg = gettext('Connection error'); | |
254 | } | |
255 | Proxmox.Utils.setErrorMask(me, msg); | |
256 | }); | |
257 | }, | |
258 | ||
259 | extractRequestError: function(result, verbose) { | |
260 | var msg = gettext('Successful'); | |
261 | ||
262 | if (!result.success) { | |
263 | msg = gettext("Unknown error"); | |
264 | if (result.message) { | |
265 | msg = result.message; | |
266 | if (result.status) { | |
267 | msg += ' (' + result.status + ')'; | |
268 | } | |
269 | } | |
270 | if (verbose && Ext.isObject(result.errors)) { | |
271 | msg += "<br>"; | |
272 | Ext.Object.each(result.errors, function(prop, desc) { | |
273 | msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " + | |
274 | Ext.htmlEncode(desc); | |
275 | }); | |
276 | } | |
277 | } | |
278 | ||
279 | return msg; | |
280 | }, | |
281 | ||
282 | // Ext.Ajax.request | |
283 | API2Request: function(reqOpts) { | |
284 | ||
285 | var newopts = Ext.apply({ | |
286 | waitMsg: gettext('Please wait...') | |
287 | }, reqOpts); | |
288 | ||
289 | if (!newopts.url.match(/^\/api2/)) { | |
290 | newopts.url = '/api2/extjs' + newopts.url; | |
291 | } | |
292 | delete newopts.callback; | |
293 | ||
294 | var createWrapper = function(successFn, callbackFn, failureFn) { | |
295 | Ext.apply(newopts, { | |
296 | success: function(response, options) { | |
297 | if (options.waitMsgTarget) { | |
5812914c DC |
298 | if (Proxmox.Utils.toolkit === 'touch') { |
299 | options.waitMsgTarget.setMasked(false); | |
300 | } else { | |
301 | options.waitMsgTarget.setLoading(false); | |
302 | } | |
0bb29d35 DM |
303 | } |
304 | var result = Ext.decode(response.responseText); | |
305 | response.result = result; | |
306 | if (!result.success) { | |
307 | response.htmlStatus = Proxmox.Utils.extractRequestError(result, true); | |
308 | Ext.callback(callbackFn, options.scope, [options, false, response]); | |
309 | Ext.callback(failureFn, options.scope, [response, options]); | |
310 | return; | |
311 | } | |
312 | Ext.callback(callbackFn, options.scope, [options, true, response]); | |
313 | Ext.callback(successFn, options.scope, [response, options]); | |
314 | }, | |
315 | failure: function(response, options) { | |
316 | if (options.waitMsgTarget) { | |
5812914c DC |
317 | if (Proxmox.Utils.toolkit === 'touch') { |
318 | options.waitMsgTarget.setMasked(false); | |
319 | } else { | |
320 | options.waitMsgTarget.setLoading(false); | |
321 | } | |
0bb29d35 DM |
322 | } |
323 | response.result = {}; | |
324 | try { | |
325 | response.result = Ext.decode(response.responseText); | |
326 | } catch(e) {} | |
327 | var msg = gettext('Connection error') + ' - server offline?'; | |
328 | if (response.aborted) { | |
329 | msg = gettext('Connection error') + ' - aborted.'; | |
330 | } else if (response.timedout) { | |
331 | msg = gettext('Connection error') + ' - Timeout.'; | |
332 | } else if (response.status && response.statusText) { | |
333 | msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText; | |
334 | } | |
335 | response.htmlStatus = msg; | |
336 | Ext.callback(callbackFn, options.scope, [options, false, response]); | |
337 | Ext.callback(failureFn, options.scope, [response, options]); | |
338 | } | |
339 | }); | |
340 | }; | |
341 | ||
342 | createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure); | |
343 | ||
344 | var target = newopts.waitMsgTarget; | |
345 | if (target) { | |
5812914c DC |
346 | if (Proxmox.Utils.toolkit === 'touch') { |
347 | target.setMasked({ xtype: 'loadmask', message: newopts.waitMsg} ); | |
348 | } else { | |
349 | // Note: ExtJS bug - this does not work when component is not rendered | |
350 | target.setLoading(newopts.waitMsg); | |
351 | } | |
0bb29d35 DM |
352 | } |
353 | Ext.Ajax.request(newopts); | |
354 | }, | |
355 | ||
5f93e010 DM |
356 | checked_command: function(orig_cmd) { |
357 | Proxmox.Utils.API2Request({ | |
358 | url: '/nodes/localhost/subscription', | |
359 | method: 'GET', | |
360 | //waitMsgTarget: me, | |
361 | failure: function(response, opts) { | |
362 | Ext.Msg.alert(gettext('Error'), response.htmlStatus); | |
363 | }, | |
364 | success: function(response, opts) { | |
365 | var data = response.result.data; | |
366 | ||
367 | if (data.status !== 'Active') { | |
368 | Ext.Msg.show({ | |
369 | title: gettext('No valid subscription'), | |
370 | icon: Ext.Msg.WARNING, | |
371 | msg: Proxmox.Utils.getNoSubKeyHtml(data.url), | |
372 | buttons: Ext.Msg.OK, | |
373 | callback: function(btn) { | |
374 | if (btn !== 'ok') { | |
375 | return; | |
376 | } | |
377 | orig_cmd(); | |
378 | } | |
379 | }); | |
380 | } else { | |
381 | orig_cmd(); | |
382 | } | |
383 | } | |
384 | }); | |
385 | }, | |
386 | ||
06694509 DM |
387 | assemble_field_data: function(values, data) { |
388 | if (Ext.isObject(data)) { | |
389 | Ext.Object.each(data, function(name, val) { | |
390 | if (values.hasOwnProperty(name)) { | |
391 | var bucket = values[name]; | |
392 | if (!Ext.isArray(bucket)) { | |
393 | bucket = values[name] = [bucket]; | |
394 | } | |
395 | if (Ext.isArray(val)) { | |
396 | values[name] = bucket.concat(val); | |
397 | } else { | |
398 | bucket.push(val); | |
399 | } | |
400 | } else { | |
401 | values[name] = val; | |
402 | } | |
403 | }); | |
404 | } | |
405 | }, | |
406 | ||
407 | dialog_title: function(subject, create, isAdd) { | |
408 | if (create) { | |
409 | if (isAdd) { | |
410 | return gettext('Add') + ': ' + subject; | |
411 | } else { | |
412 | return gettext('Create') + ': ' + subject; | |
413 | } | |
414 | } else { | |
415 | return gettext('Edit') + ': ' + subject; | |
416 | } | |
417 | }, | |
418 | ||
a58001dd DM |
419 | network_iface_types: { |
420 | eth: gettext("Network Device"), | |
421 | bridge: 'Linux Bridge', | |
422 | bond: 'Linux Bond', | |
8cb1ec17 | 423 | vlan: 'Linux VLAN', |
a58001dd DM |
424 | OVSBridge: 'OVS Bridge', |
425 | OVSBond: 'OVS Bond', | |
426 | OVSPort: 'OVS Port', | |
427 | OVSIntPort: 'OVS IntPort' | |
428 | }, | |
429 | ||
430 | render_network_iface_type: function(value) { | |
431 | return Proxmox.Utils.network_iface_types[value] || | |
432 | Proxmox.Utils.unknownText; | |
433 | }, | |
434 | ||
ab7fac0b | 435 | task_desc_table: { |
18ffbaef DC |
436 | acmenewcert: [ 'SRV', gettext('Order Certificate') ], |
437 | acmeregister: [ 'ACME Account', gettext('Register') ], | |
438 | acmedeactivate: [ 'ACME Account', gettext('Deactivate') ], | |
439 | acmeupdate: [ 'ACME Account', gettext('Update') ], | |
440 | acmerefresh: [ 'ACME Account', gettext('Refresh') ], | |
441 | acmerenew: [ 'SRV', gettext('Renew Certificate') ], | |
442 | acmerevoke: [ 'SRV', gettext('Revoke Certificate') ], | |
443 | 'move_volume': [ 'CT', gettext('Move Volume') ], | |
444 | clustercreate: [ '', gettext('Create Cluster') ], | |
445 | clusterjoin: [ '', gettext('Join Cluster') ], | |
ab7fac0b DC |
446 | diskinit: [ 'Disk', gettext('Initialize Disk with GPT') ], |
447 | vncproxy: [ 'VM/CT', gettext('Console') ], | |
448 | spiceproxy: [ 'VM/CT', gettext('Console') + ' (Spice)' ], | |
449 | vncshell: [ '', gettext('Shell') ], | |
450 | spiceshell: [ '', gettext('Shell') + ' (Spice)' ], | |
451 | qmsnapshot: [ 'VM', gettext('Snapshot') ], | |
452 | qmrollback: [ 'VM', gettext('Rollback') ], | |
453 | qmdelsnapshot: [ 'VM', gettext('Delete Snapshot') ], | |
454 | qmcreate: [ 'VM', gettext('Create') ], | |
455 | qmrestore: [ 'VM', gettext('Restore') ], | |
456 | qmdestroy: [ 'VM', gettext('Destroy') ], | |
457 | qmigrate: [ 'VM', gettext('Migrate') ], | |
458 | qmclone: [ 'VM', gettext('Clone') ], | |
459 | qmmove: [ 'VM', gettext('Move disk') ], | |
460 | qmtemplate: [ 'VM', gettext('Convert to template') ], | |
461 | qmstart: [ 'VM', gettext('Start') ], | |
462 | qmstop: [ 'VM', gettext('Stop') ], | |
463 | qmreset: [ 'VM', gettext('Reset') ], | |
464 | qmshutdown: [ 'VM', gettext('Shutdown') ], | |
b7d94d3e DC |
465 | qmsuspend: [ 'VM', gettext('Hibernate') ], |
466 | qmpause: [ 'VM', gettext('Pause') ], | |
ab7fac0b DC |
467 | qmresume: [ 'VM', gettext('Resume') ], |
468 | qmconfig: [ 'VM', gettext('Configure') ], | |
469 | vzsnapshot: [ 'CT', gettext('Snapshot') ], | |
470 | vzrollback: [ 'CT', gettext('Rollback') ], | |
471 | vzdelsnapshot: [ 'CT', gettext('Delete Snapshot') ], | |
472 | vzcreate: ['CT', gettext('Create') ], | |
473 | vzrestore: ['CT', gettext('Restore') ], | |
474 | vzdestroy: ['CT', gettext('Destroy') ], | |
475 | vzmigrate: [ 'CT', gettext('Migrate') ], | |
476 | vzclone: [ 'CT', gettext('Clone') ], | |
477 | vztemplate: [ 'CT', gettext('Convert to template') ], | |
478 | vzstart: ['CT', gettext('Start') ], | |
479 | vzstop: ['CT', gettext('Stop') ], | |
480 | vzmount: ['CT', gettext('Mount') ], | |
481 | vzumount: ['CT', gettext('Unmount') ], | |
482 | vzshutdown: ['CT', gettext('Shutdown') ], | |
483 | vzsuspend: [ 'CT', gettext('Suspend') ], | |
484 | vzresume: [ 'CT', gettext('Resume') ], | |
485 | hamigrate: [ 'HA', gettext('Migrate') ], | |
486 | hastart: [ 'HA', gettext('Start') ], | |
487 | hastop: [ 'HA', gettext('Stop') ], | |
488 | srvstart: ['SRV', gettext('Start') ], | |
489 | srvstop: ['SRV', gettext('Stop') ], | |
490 | srvrestart: ['SRV', gettext('Restart') ], | |
491 | srvreload: ['SRV', gettext('Reload') ], | |
18ffbaef DC |
492 | cephcreatemgr: ['Ceph Manager', gettext('Create') ], |
493 | cephdestroymgr: ['Ceph Manager', gettext('Destroy') ], | |
ab7fac0b DC |
494 | cephcreatemon: ['Ceph Monitor', gettext('Create') ], |
495 | cephdestroymon: ['Ceph Monitor', gettext('Destroy') ], | |
496 | cephcreateosd: ['Ceph OSD', gettext('Create') ], | |
497 | cephdestroyosd: ['Ceph OSD', gettext('Destroy') ], | |
498 | cephcreatepool: ['Ceph Pool', gettext('Create') ], | |
499 | cephdestroypool: ['Ceph Pool', gettext('Destroy') ], | |
9367da29 TL |
500 | cephfscreate: ['CephFS', gettext('Create') ], |
501 | cephcreatemds: ['Ceph Metadata Server', gettext('Create') ], | |
502 | cephdestroymds: ['Ceph Metadata Server', gettext('Destroy') ], | |
ab7fac0b DC |
503 | imgcopy: ['', gettext('Copy data') ], |
504 | imgdel: ['', gettext('Erase data') ], | |
99a5979b | 505 | unknownimgdel: ['', gettext('Destroy image from unknown guest') ], |
ab7fac0b | 506 | download: ['', gettext('Download') ], |
9138244f | 507 | vzdump: ['VM/CT', gettext('Backup') ], |
ab7fac0b DC |
508 | aptupdate: ['', gettext('Update package database') ], |
509 | startall: [ '', gettext('Start all VMs and Containers') ], | |
510 | stopall: [ '', gettext('Stop all VMs and Containers') ], | |
0908d830 DC |
511 | migrateall: [ '', gettext('Migrate all VMs and Containers') ], |
512 | dircreate: [ gettext('Directory Storage'), gettext('Create') ], | |
513 | lvmcreate: [ gettext('LVM Storage'), gettext('Create') ], | |
514 | lvmthincreate: [ gettext('LVM-Thin Storage'), gettext('Create') ], | |
515 | zfscreate: [ gettext('ZFS Storage'), gettext('Create') ] | |
ab7fac0b DC |
516 | }, |
517 | ||
53ac9bca | 518 | format_task_description: function(type, id) { |
ab7fac0b | 519 | var farray = Proxmox.Utils.task_desc_table[type]; |
c5aaa502 | 520 | var text; |
ab7fac0b | 521 | if (!farray) { |
c5aaa502 | 522 | text = type; |
ab7fac0b DC |
523 | if (id) { |
524 | type += ' ' + id; | |
525 | } | |
526 | return text; | |
527 | } | |
528 | var prefix = farray[0]; | |
c5aaa502 | 529 | text = farray[1]; |
ab7fac0b DC |
530 | if (prefix) { |
531 | return prefix + ' ' + id + ' - ' + text; | |
532 | } | |
533 | return text; | |
53ac9bca DM |
534 | }, |
535 | ||
b91c7ce2 DC |
536 | format_size: function(size) { |
537 | /*jslint confusion: true */ | |
538 | ||
539 | var units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi']; | |
540 | var num = 0; | |
541 | ||
542 | while (size >= 1024 && ((num++)+1) < units.length) { | |
543 | size = size / 1024; | |
544 | } | |
545 | ||
546 | return size.toFixed((num > 0)?2:0) + " " + units[num] + "B"; | |
547 | }, | |
548 | ||
53ac9bca DM |
549 | render_upid: function(value, metaData, record) { |
550 | var type = record.data.type; | |
551 | var id = record.data.id; | |
552 | ||
553 | return Proxmox.Utils.format_task_description(type, id); | |
554 | }, | |
555 | ||
452892df DM |
556 | render_uptime: function(value) { |
557 | ||
558 | var uptime = value; | |
559 | ||
560 | if (uptime === undefined) { | |
561 | return ''; | |
562 | } | |
563 | ||
564 | if (uptime <= 0) { | |
565 | return '-'; | |
566 | } | |
567 | ||
568 | return Proxmox.Utils.format_duration_long(uptime); | |
569 | }, | |
570 | ||
06694509 DM |
571 | parse_task_upid: function(upid) { |
572 | var task = {}; | |
573 | ||
574 | 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]+):$/); | |
575 | if (!res) { | |
576 | throw "unable to parse upid '" + upid + "'"; | |
577 | } | |
578 | task.node = res[1]; | |
579 | task.pid = parseInt(res[2], 16); | |
580 | task.pstart = parseInt(res[3], 16); | |
581 | task.starttime = parseInt(res[4], 16); | |
582 | task.type = res[5]; | |
583 | task.id = res[6]; | |
584 | task.user = res[7]; | |
585 | ||
53ac9bca DM |
586 | task.desc = Proxmox.Utils.format_task_description(task.type, task.id); |
587 | ||
06694509 DM |
588 | return task; |
589 | }, | |
590 | ||
591 | render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) { | |
592 | var servertime = new Date(value * 1000); | |
593 | return Ext.Date.format(servertime, 'Y-m-d H:i:s'); | |
881c9c0c DC |
594 | }, |
595 | ||
f80f1f94 TM |
596 | openXtermJsViewer: function(vmtype, vmid, nodename, vmname, cmd) { |
597 | var url = Ext.Object.toQueryString({ | |
881c9c0c DC |
598 | console: vmtype, // kvm, lxc, upgrade or shell |
599 | xtermjs: 1, | |
600 | vmid: vmid, | |
601 | vmname: vmname, | |
f80f1f94 TM |
602 | node: nodename, |
603 | cmd: cmd, | |
604 | ||
881c9c0c DC |
605 | }); |
606 | var nw = window.open("?" + url, '_blank', 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420'); | |
9bf01f76 DC |
607 | if (nw) { |
608 | nw.focus(); | |
609 | } | |
e94c0767 | 610 | } |
06694509 | 611 | |
881c9c0c | 612 | }, |
5ffef550 | 613 | |
0bb29d35 DM |
614 | singleton: true, |
615 | constructor: function() { | |
616 | var me = this; | |
617 | Ext.apply(me, me.utilities); | |
618 | ||
619 | var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])"; | |
620 | var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")"; | |
621 | var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})"; | |
622 | var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")"; | |
577b6c75 ML |
623 | var IPV4_CIDR_MASK = "([0-9]{1,2})"; |
624 | var IPV6_CIDR_MASK = "([0-9]{1,3})"; | |
0bb29d35 DM |
625 | |
626 | ||
627 | me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$"); | |
577b6c75 | 628 | me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/" + IPV4_CIDR_MASK + "$"); |
0bb29d35 DM |
629 | |
630 | var IPV6_REGEXP = "(?:" + | |
631 | "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" + | |
632 | "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" + | |
633 | "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" + | |
634 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" + | |
635 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" + | |
636 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" + | |
637 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" + | |
638 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" + | |
639 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" + | |
640 | ")"; | |
641 | ||
642 | me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$"); | |
577b6c75 | 643 | me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/" + IPV6_CIDR_MASK + "$"); |
0bb29d35 DM |
644 | me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]"); |
645 | ||
646 | me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$"); | |
577b6c75 | 647 | me.IP64_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + "\/" + IPV6_CIDR_MASK + ")|(?:" + IPV4_REGEXP + "\/" + IPV4_CIDR_MASK + ")$"); |
0bb29d35 DM |
648 | |
649 | 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])?))"; | |
650 | me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$"); | |
651 | ||
652 | me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$"); | |
653 | me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$"); | |
654 | me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$"); | |
655 | } | |
656 | }); |