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