]>
Commit | Line | Data |
---|---|---|
0bb29d35 DM |
1 | Ext.ns('Proxmox'); |
2 | Ext.ns('Proxmox.Setup'); | |
3 | ||
4 | // TODO: implement gettext | |
5 | function gettext(buf) { return buf; } | |
6 | ||
7 | ||
757cc58a DM |
8 | if (!Ext.isDefined(Proxmox.Setup.auth_cookie_name)) { |
9 | throw "Proxmox library not initialized"; | |
0bb29d35 DM |
10 | } |
11 | ||
12 | // avoid errors related to Accessible Rich Internet Applications | |
13 | // (access for people with disabilities) | |
14 | // TODO reenable after all components are upgraded | |
15 | Ext.enableAria = false; | |
16 | Ext.enableAriaButtons = false; | |
17 | Ext.enableAriaPanels = false; | |
18 | ||
19 | // avoid errors when running without development tools | |
20 | if (!Ext.isDefined(Ext.global.console)) { | |
21 | var console = { | |
22 | dir: function() {}, | |
23 | log: function() {} | |
24 | }; | |
25 | } | |
26 | ||
27 | Ext.Ajax.defaultHeaders = { | |
28 | 'Accept': 'application/json' | |
29 | }; | |
30 | ||
31 | Ext.Ajax.on('beforerequest', function(conn, options) { | |
32 | if (Proxmox.CSRFPreventionToken) { | |
33 | if (!options.headers) { | |
34 | options.headers = {}; | |
35 | } | |
36 | options.headers.CSRFPreventionToken = Proxmox.CSRFPreventionToken; | |
37 | } | |
38 | }); | |
39 | ||
40 | Ext.define('Proxmox.Utils', { utilities: { | |
41 | ||
42 | // this singleton contains miscellaneous utilities | |
43 | ||
30f885cb DM |
44 | yesText: gettext('Yes'), |
45 | noText: gettext('No'), | |
46 | enabledText: gettext('Enabled'), | |
47 | disabledText: gettext('Disabled'), | |
48 | noneText: gettext('none'), | |
49 | errorText: gettext('Error'), | |
a58001dd | 50 | unknownText: gettext('Unknown'), |
30f885cb DM |
51 | defaultText: gettext('Default'), |
52 | daysText: gettext('days'), | |
53 | dayText: gettext('day'), | |
54 | runningText: gettext('running'), | |
55 | stoppedText: gettext('stopped'), | |
56 | neverText: gettext('never'), | |
57 | totalText: gettext('Total'), | |
58 | usedText: gettext('Used'), | |
59 | directoryText: gettext('Directory'), | |
60 | stateText: gettext('State'), | |
61 | groupText: gettext('Group'), | |
62 | ||
b0d9b5d1 DM |
63 | format_boolean_with_default: function(value) { |
64 | if (Ext.isDefined(value) && value !== '__default__') { | |
65 | return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText; | |
66 | } | |
67 | return Proxmox.Utils.defaultText; | |
68 | }, | |
69 | ||
70 | format_boolean: function(value) { | |
71 | return value ? Proxmox.Utils.yesText : Proxmox.Utils.noText; | |
72 | }, | |
73 | ||
74 | format_neg_boolean: function(value) { | |
75 | return !value ? Proxmox.Utils.yesText : Proxmox.Utils.noText; | |
76 | }, | |
a58001dd | 77 | |
0e49da6d DM |
78 | format_enabled_toggle: function(value) { |
79 | return value ? Proxmox.Utils.enabledText : Proxmox.Utils.disabledText; | |
80 | }, | |
81 | ||
2d0153a5 DM |
82 | format_expire: function(date) { |
83 | if (!date) { | |
84 | return Proxmox.Utils.neverText; | |
85 | } | |
86 | return Ext.Date.format(date, "Y-m-d"); | |
87 | }, | |
88 | ||
28e54f37 DM |
89 | compute_min_label_width: function(text, width) { |
90 | ||
91 | if (width === undefined) { width = 100; } | |
92 | ||
93 | var tm = new Ext.util.TextMetrics(); | |
94 | var min = tm.getWidth(text + ':'); | |
95 | ||
96 | return min < width ? width : min; | |
97 | }, | |
98 | ||
0bb29d35 DM |
99 | authOK: function() { |
100 | return (Proxmox.UserName !== '') && Ext.util.Cookies.get(Proxmox.Setup.auth_cookie_name); | |
101 | }, | |
102 | ||
103 | authClear: function() { | |
104 | Ext.util.Cookies.clear(Proxmox.Setup.auth_cookie_name); | |
105 | }, | |
106 | ||
107 | // comp.setLoading() is buggy in ExtJS 4.0.7, so we | |
108 | // use el.mask() instead | |
109 | setErrorMask: function(comp, msg) { | |
110 | var el = comp.el; | |
111 | if (!el) { | |
112 | return; | |
113 | } | |
114 | if (!msg) { | |
115 | el.unmask(); | |
116 | } else { | |
117 | if (msg === true) { | |
118 | el.mask(gettext("Loading...")); | |
119 | } else { | |
120 | el.mask(msg); | |
121 | } | |
122 | } | |
123 | }, | |
124 | ||
125 | monStoreErrors: function(me, store) { | |
126 | me.mon(store, 'beforeload', function(s, operation, eOpts) { | |
127 | if (!me.loadCount) { | |
128 | me.loadCount = 0; // make sure it is numeric | |
129 | Proxmox.Utils.setErrorMask(me, true); | |
130 | } | |
131 | }); | |
132 | ||
133 | // only works with 'proxmox' proxy | |
134 | me.mon(store.proxy, 'afterload', function(proxy, request, success) { | |
135 | me.loadCount++; | |
136 | ||
137 | if (success) { | |
138 | Proxmox.Utils.setErrorMask(me, false); | |
139 | return; | |
140 | } | |
141 | ||
142 | var msg; | |
143 | /*jslint nomen: true */ | |
144 | var operation = request._operation; | |
145 | var error = operation.getError(); | |
146 | if (error.statusText) { | |
147 | msg = error.statusText + ' (' + error.status + ')'; | |
148 | } else { | |
149 | msg = gettext('Connection error'); | |
150 | } | |
151 | Proxmox.Utils.setErrorMask(me, msg); | |
152 | }); | |
153 | }, | |
154 | ||
155 | extractRequestError: function(result, verbose) { | |
156 | var msg = gettext('Successful'); | |
157 | ||
158 | if (!result.success) { | |
159 | msg = gettext("Unknown error"); | |
160 | if (result.message) { | |
161 | msg = result.message; | |
162 | if (result.status) { | |
163 | msg += ' (' + result.status + ')'; | |
164 | } | |
165 | } | |
166 | if (verbose && Ext.isObject(result.errors)) { | |
167 | msg += "<br>"; | |
168 | Ext.Object.each(result.errors, function(prop, desc) { | |
169 | msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " + | |
170 | Ext.htmlEncode(desc); | |
171 | }); | |
172 | } | |
173 | } | |
174 | ||
175 | return msg; | |
176 | }, | |
177 | ||
178 | // Ext.Ajax.request | |
179 | API2Request: function(reqOpts) { | |
180 | ||
181 | var newopts = Ext.apply({ | |
182 | waitMsg: gettext('Please wait...') | |
183 | }, reqOpts); | |
184 | ||
185 | if (!newopts.url.match(/^\/api2/)) { | |
186 | newopts.url = '/api2/extjs' + newopts.url; | |
187 | } | |
188 | delete newopts.callback; | |
189 | ||
190 | var createWrapper = function(successFn, callbackFn, failureFn) { | |
191 | Ext.apply(newopts, { | |
192 | success: function(response, options) { | |
193 | if (options.waitMsgTarget) { | |
194 | options.waitMsgTarget.setLoading(false); | |
195 | } | |
196 | var result = Ext.decode(response.responseText); | |
197 | response.result = result; | |
198 | if (!result.success) { | |
199 | response.htmlStatus = Proxmox.Utils.extractRequestError(result, true); | |
200 | Ext.callback(callbackFn, options.scope, [options, false, response]); | |
201 | Ext.callback(failureFn, options.scope, [response, options]); | |
202 | return; | |
203 | } | |
204 | Ext.callback(callbackFn, options.scope, [options, true, response]); | |
205 | Ext.callback(successFn, options.scope, [response, options]); | |
206 | }, | |
207 | failure: function(response, options) { | |
208 | if (options.waitMsgTarget) { | |
209 | options.waitMsgTarget.setLoading(false); | |
210 | } | |
211 | response.result = {}; | |
212 | try { | |
213 | response.result = Ext.decode(response.responseText); | |
214 | } catch(e) {} | |
215 | var msg = gettext('Connection error') + ' - server offline?'; | |
216 | if (response.aborted) { | |
217 | msg = gettext('Connection error') + ' - aborted.'; | |
218 | } else if (response.timedout) { | |
219 | msg = gettext('Connection error') + ' - Timeout.'; | |
220 | } else if (response.status && response.statusText) { | |
221 | msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText; | |
222 | } | |
223 | response.htmlStatus = msg; | |
224 | Ext.callback(callbackFn, options.scope, [options, false, response]); | |
225 | Ext.callback(failureFn, options.scope, [response, options]); | |
226 | } | |
227 | }); | |
228 | }; | |
229 | ||
230 | createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure); | |
231 | ||
232 | var target = newopts.waitMsgTarget; | |
233 | if (target) { | |
234 | // Note: ExtJS bug - this does not work when component is not rendered | |
235 | target.setLoading(newopts.waitMsg); | |
236 | } | |
237 | Ext.Ajax.request(newopts); | |
238 | }, | |
239 | ||
06694509 DM |
240 | assemble_field_data: function(values, data) { |
241 | if (Ext.isObject(data)) { | |
242 | Ext.Object.each(data, function(name, val) { | |
243 | if (values.hasOwnProperty(name)) { | |
244 | var bucket = values[name]; | |
245 | if (!Ext.isArray(bucket)) { | |
246 | bucket = values[name] = [bucket]; | |
247 | } | |
248 | if (Ext.isArray(val)) { | |
249 | values[name] = bucket.concat(val); | |
250 | } else { | |
251 | bucket.push(val); | |
252 | } | |
253 | } else { | |
254 | values[name] = val; | |
255 | } | |
256 | }); | |
257 | } | |
258 | }, | |
259 | ||
260 | dialog_title: function(subject, create, isAdd) { | |
261 | if (create) { | |
262 | if (isAdd) { | |
263 | return gettext('Add') + ': ' + subject; | |
264 | } else { | |
265 | return gettext('Create') + ': ' + subject; | |
266 | } | |
267 | } else { | |
268 | return gettext('Edit') + ': ' + subject; | |
269 | } | |
270 | }, | |
271 | ||
a58001dd DM |
272 | network_iface_types: { |
273 | eth: gettext("Network Device"), | |
274 | bridge: 'Linux Bridge', | |
275 | bond: 'Linux Bond', | |
276 | OVSBridge: 'OVS Bridge', | |
277 | OVSBond: 'OVS Bond', | |
278 | OVSPort: 'OVS Port', | |
279 | OVSIntPort: 'OVS IntPort' | |
280 | }, | |
281 | ||
282 | render_network_iface_type: function(value) { | |
283 | return Proxmox.Utils.network_iface_types[value] || | |
284 | Proxmox.Utils.unknownText; | |
285 | }, | |
286 | ||
53ac9bca DM |
287 | // you can override this to provide nicer task descriptions |
288 | format_task_description: function(type, id) { | |
289 | return type + ' ' + id; | |
290 | }, | |
291 | ||
292 | render_upid: function(value, metaData, record) { | |
293 | var type = record.data.type; | |
294 | var id = record.data.id; | |
295 | ||
296 | return Proxmox.Utils.format_task_description(type, id); | |
297 | }, | |
298 | ||
06694509 DM |
299 | parse_task_upid: function(upid) { |
300 | var task = {}; | |
301 | ||
302 | 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]+):$/); | |
303 | if (!res) { | |
304 | throw "unable to parse upid '" + upid + "'"; | |
305 | } | |
306 | task.node = res[1]; | |
307 | task.pid = parseInt(res[2], 16); | |
308 | task.pstart = parseInt(res[3], 16); | |
309 | task.starttime = parseInt(res[4], 16); | |
310 | task.type = res[5]; | |
311 | task.id = res[6]; | |
312 | task.user = res[7]; | |
313 | ||
53ac9bca DM |
314 | task.desc = Proxmox.Utils.format_task_description(task.type, task.id); |
315 | ||
06694509 DM |
316 | return task; |
317 | }, | |
318 | ||
319 | render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) { | |
320 | var servertime = new Date(value * 1000); | |
321 | return Ext.Date.format(servertime, 'Y-m-d H:i:s'); | |
322 | }, | |
323 | ||
0bb29d35 | 324 | }, |
5ffef550 | 325 | |
0bb29d35 DM |
326 | singleton: true, |
327 | constructor: function() { | |
328 | var me = this; | |
329 | Ext.apply(me, me.utilities); | |
330 | ||
331 | var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])"; | |
332 | var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")"; | |
333 | var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})"; | |
334 | var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")"; | |
335 | ||
336 | ||
337 | me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$"); | |
338 | me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$"); | |
339 | ||
340 | var IPV6_REGEXP = "(?:" + | |
341 | "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" + | |
342 | "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" + | |
343 | "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" + | |
344 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" + | |
345 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" + | |
346 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" + | |
347 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" + | |
348 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" + | |
349 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" + | |
350 | ")"; | |
351 | ||
352 | me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$"); | |
353 | me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$"); | |
354 | me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]"); | |
355 | ||
356 | me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$"); | |
357 | ||
358 | 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])?))"; | |
359 | me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$"); | |
360 | ||
361 | me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$"); | |
362 | me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$"); | |
363 | me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$"); | |
364 | } | |
365 | }); |