]>
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 | ||
28e54f37 DM |
82 | compute_min_label_width: function(text, width) { |
83 | ||
84 | if (width === undefined) { width = 100; } | |
85 | ||
86 | var tm = new Ext.util.TextMetrics(); | |
87 | var min = tm.getWidth(text + ':'); | |
88 | ||
89 | return min < width ? width : min; | |
90 | }, | |
91 | ||
0bb29d35 DM |
92 | authOK: function() { |
93 | return (Proxmox.UserName !== '') && Ext.util.Cookies.get(Proxmox.Setup.auth_cookie_name); | |
94 | }, | |
95 | ||
96 | authClear: function() { | |
97 | Ext.util.Cookies.clear(Proxmox.Setup.auth_cookie_name); | |
98 | }, | |
99 | ||
100 | // comp.setLoading() is buggy in ExtJS 4.0.7, so we | |
101 | // use el.mask() instead | |
102 | setErrorMask: function(comp, msg) { | |
103 | var el = comp.el; | |
104 | if (!el) { | |
105 | return; | |
106 | } | |
107 | if (!msg) { | |
108 | el.unmask(); | |
109 | } else { | |
110 | if (msg === true) { | |
111 | el.mask(gettext("Loading...")); | |
112 | } else { | |
113 | el.mask(msg); | |
114 | } | |
115 | } | |
116 | }, | |
117 | ||
118 | monStoreErrors: function(me, store) { | |
119 | me.mon(store, 'beforeload', function(s, operation, eOpts) { | |
120 | if (!me.loadCount) { | |
121 | me.loadCount = 0; // make sure it is numeric | |
122 | Proxmox.Utils.setErrorMask(me, true); | |
123 | } | |
124 | }); | |
125 | ||
126 | // only works with 'proxmox' proxy | |
127 | me.mon(store.proxy, 'afterload', function(proxy, request, success) { | |
128 | me.loadCount++; | |
129 | ||
130 | if (success) { | |
131 | Proxmox.Utils.setErrorMask(me, false); | |
132 | return; | |
133 | } | |
134 | ||
135 | var msg; | |
136 | /*jslint nomen: true */ | |
137 | var operation = request._operation; | |
138 | var error = operation.getError(); | |
139 | if (error.statusText) { | |
140 | msg = error.statusText + ' (' + error.status + ')'; | |
141 | } else { | |
142 | msg = gettext('Connection error'); | |
143 | } | |
144 | Proxmox.Utils.setErrorMask(me, msg); | |
145 | }); | |
146 | }, | |
147 | ||
148 | extractRequestError: function(result, verbose) { | |
149 | var msg = gettext('Successful'); | |
150 | ||
151 | if (!result.success) { | |
152 | msg = gettext("Unknown error"); | |
153 | if (result.message) { | |
154 | msg = result.message; | |
155 | if (result.status) { | |
156 | msg += ' (' + result.status + ')'; | |
157 | } | |
158 | } | |
159 | if (verbose && Ext.isObject(result.errors)) { | |
160 | msg += "<br>"; | |
161 | Ext.Object.each(result.errors, function(prop, desc) { | |
162 | msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " + | |
163 | Ext.htmlEncode(desc); | |
164 | }); | |
165 | } | |
166 | } | |
167 | ||
168 | return msg; | |
169 | }, | |
170 | ||
171 | // Ext.Ajax.request | |
172 | API2Request: function(reqOpts) { | |
173 | ||
174 | var newopts = Ext.apply({ | |
175 | waitMsg: gettext('Please wait...') | |
176 | }, reqOpts); | |
177 | ||
178 | if (!newopts.url.match(/^\/api2/)) { | |
179 | newopts.url = '/api2/extjs' + newopts.url; | |
180 | } | |
181 | delete newopts.callback; | |
182 | ||
183 | var createWrapper = function(successFn, callbackFn, failureFn) { | |
184 | Ext.apply(newopts, { | |
185 | success: function(response, options) { | |
186 | if (options.waitMsgTarget) { | |
187 | options.waitMsgTarget.setLoading(false); | |
188 | } | |
189 | var result = Ext.decode(response.responseText); | |
190 | response.result = result; | |
191 | if (!result.success) { | |
192 | response.htmlStatus = Proxmox.Utils.extractRequestError(result, true); | |
193 | Ext.callback(callbackFn, options.scope, [options, false, response]); | |
194 | Ext.callback(failureFn, options.scope, [response, options]); | |
195 | return; | |
196 | } | |
197 | Ext.callback(callbackFn, options.scope, [options, true, response]); | |
198 | Ext.callback(successFn, options.scope, [response, options]); | |
199 | }, | |
200 | failure: function(response, options) { | |
201 | if (options.waitMsgTarget) { | |
202 | options.waitMsgTarget.setLoading(false); | |
203 | } | |
204 | response.result = {}; | |
205 | try { | |
206 | response.result = Ext.decode(response.responseText); | |
207 | } catch(e) {} | |
208 | var msg = gettext('Connection error') + ' - server offline?'; | |
209 | if (response.aborted) { | |
210 | msg = gettext('Connection error') + ' - aborted.'; | |
211 | } else if (response.timedout) { | |
212 | msg = gettext('Connection error') + ' - Timeout.'; | |
213 | } else if (response.status && response.statusText) { | |
214 | msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText; | |
215 | } | |
216 | response.htmlStatus = msg; | |
217 | Ext.callback(callbackFn, options.scope, [options, false, response]); | |
218 | Ext.callback(failureFn, options.scope, [response, options]); | |
219 | } | |
220 | }); | |
221 | }; | |
222 | ||
223 | createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure); | |
224 | ||
225 | var target = newopts.waitMsgTarget; | |
226 | if (target) { | |
227 | // Note: ExtJS bug - this does not work when component is not rendered | |
228 | target.setLoading(newopts.waitMsg); | |
229 | } | |
230 | Ext.Ajax.request(newopts); | |
231 | }, | |
232 | ||
06694509 DM |
233 | assemble_field_data: function(values, data) { |
234 | if (Ext.isObject(data)) { | |
235 | Ext.Object.each(data, function(name, val) { | |
236 | if (values.hasOwnProperty(name)) { | |
237 | var bucket = values[name]; | |
238 | if (!Ext.isArray(bucket)) { | |
239 | bucket = values[name] = [bucket]; | |
240 | } | |
241 | if (Ext.isArray(val)) { | |
242 | values[name] = bucket.concat(val); | |
243 | } else { | |
244 | bucket.push(val); | |
245 | } | |
246 | } else { | |
247 | values[name] = val; | |
248 | } | |
249 | }); | |
250 | } | |
251 | }, | |
252 | ||
253 | dialog_title: function(subject, create, isAdd) { | |
254 | if (create) { | |
255 | if (isAdd) { | |
256 | return gettext('Add') + ': ' + subject; | |
257 | } else { | |
258 | return gettext('Create') + ': ' + subject; | |
259 | } | |
260 | } else { | |
261 | return gettext('Edit') + ': ' + subject; | |
262 | } | |
263 | }, | |
264 | ||
a58001dd DM |
265 | network_iface_types: { |
266 | eth: gettext("Network Device"), | |
267 | bridge: 'Linux Bridge', | |
268 | bond: 'Linux Bond', | |
269 | OVSBridge: 'OVS Bridge', | |
270 | OVSBond: 'OVS Bond', | |
271 | OVSPort: 'OVS Port', | |
272 | OVSIntPort: 'OVS IntPort' | |
273 | }, | |
274 | ||
275 | render_network_iface_type: function(value) { | |
276 | return Proxmox.Utils.network_iface_types[value] || | |
277 | Proxmox.Utils.unknownText; | |
278 | }, | |
279 | ||
53ac9bca DM |
280 | // you can override this to provide nicer task descriptions |
281 | format_task_description: function(type, id) { | |
282 | return type + ' ' + id; | |
283 | }, | |
284 | ||
285 | render_upid: function(value, metaData, record) { | |
286 | var type = record.data.type; | |
287 | var id = record.data.id; | |
288 | ||
289 | return Proxmox.Utils.format_task_description(type, id); | |
290 | }, | |
291 | ||
06694509 DM |
292 | parse_task_upid: function(upid) { |
293 | var task = {}; | |
294 | ||
295 | 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]+):$/); | |
296 | if (!res) { | |
297 | throw "unable to parse upid '" + upid + "'"; | |
298 | } | |
299 | task.node = res[1]; | |
300 | task.pid = parseInt(res[2], 16); | |
301 | task.pstart = parseInt(res[3], 16); | |
302 | task.starttime = parseInt(res[4], 16); | |
303 | task.type = res[5]; | |
304 | task.id = res[6]; | |
305 | task.user = res[7]; | |
306 | ||
53ac9bca DM |
307 | task.desc = Proxmox.Utils.format_task_description(task.type, task.id); |
308 | ||
06694509 DM |
309 | return task; |
310 | }, | |
311 | ||
312 | render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) { | |
313 | var servertime = new Date(value * 1000); | |
314 | return Ext.Date.format(servertime, 'Y-m-d H:i:s'); | |
315 | }, | |
316 | ||
0bb29d35 | 317 | }, |
5ffef550 | 318 | |
0bb29d35 DM |
319 | singleton: true, |
320 | constructor: function() { | |
321 | var me = this; | |
322 | Ext.apply(me, me.utilities); | |
323 | ||
324 | var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])"; | |
325 | var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")"; | |
326 | var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})"; | |
327 | var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")"; | |
328 | ||
329 | ||
330 | me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$"); | |
331 | me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$"); | |
332 | ||
333 | var IPV6_REGEXP = "(?:" + | |
334 | "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" + | |
335 | "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" + | |
336 | "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" + | |
337 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" + | |
338 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" + | |
339 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" + | |
340 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" + | |
341 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" + | |
342 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" + | |
343 | ")"; | |
344 | ||
345 | me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$"); | |
346 | me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$"); | |
347 | me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]"); | |
348 | ||
349 | me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$"); | |
350 | ||
351 | 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])?))"; | |
352 | me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$"); | |
353 | ||
354 | me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$"); | |
355 | me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$"); | |
356 | me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$"); | |
357 | } | |
358 | }); |