]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Utils.js
add TimeView, TimeEdit and TaskViewer
[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 authOK: function() {
45 return (Proxmox.UserName !== '') && Ext.util.Cookies.get(Proxmox.Setup.auth_cookie_name);
46 },
47
48 authClear: function() {
49 Ext.util.Cookies.clear(Proxmox.Setup.auth_cookie_name);
50 },
51
52 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
53 // use el.mask() instead
54 setErrorMask: function(comp, msg) {
55 var el = comp.el;
56 if (!el) {
57 return;
58 }
59 if (!msg) {
60 el.unmask();
61 } else {
62 if (msg === true) {
63 el.mask(gettext("Loading..."));
64 } else {
65 el.mask(msg);
66 }
67 }
68 },
69
70 monStoreErrors: function(me, store) {
71 me.mon(store, 'beforeload', function(s, operation, eOpts) {
72 if (!me.loadCount) {
73 me.loadCount = 0; // make sure it is numeric
74 Proxmox.Utils.setErrorMask(me, true);
75 }
76 });
77
78 // only works with 'proxmox' proxy
79 me.mon(store.proxy, 'afterload', function(proxy, request, success) {
80 me.loadCount++;
81
82 if (success) {
83 Proxmox.Utils.setErrorMask(me, false);
84 return;
85 }
86
87 var msg;
88 /*jslint nomen: true */
89 var operation = request._operation;
90 var error = operation.getError();
91 if (error.statusText) {
92 msg = error.statusText + ' (' + error.status + ')';
93 } else {
94 msg = gettext('Connection error');
95 }
96 Proxmox.Utils.setErrorMask(me, msg);
97 });
98 },
99
100 extractRequestError: function(result, verbose) {
101 var msg = gettext('Successful');
102
103 if (!result.success) {
104 msg = gettext("Unknown error");
105 if (result.message) {
106 msg = result.message;
107 if (result.status) {
108 msg += ' (' + result.status + ')';
109 }
110 }
111 if (verbose && Ext.isObject(result.errors)) {
112 msg += "<br>";
113 Ext.Object.each(result.errors, function(prop, desc) {
114 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
115 Ext.htmlEncode(desc);
116 });
117 }
118 }
119
120 return msg;
121 },
122
123 // Ext.Ajax.request
124 API2Request: function(reqOpts) {
125
126 var newopts = Ext.apply({
127 waitMsg: gettext('Please wait...')
128 }, reqOpts);
129
130 if (!newopts.url.match(/^\/api2/)) {
131 newopts.url = '/api2/extjs' + newopts.url;
132 }
133 delete newopts.callback;
134
135 var createWrapper = function(successFn, callbackFn, failureFn) {
136 Ext.apply(newopts, {
137 success: function(response, options) {
138 if (options.waitMsgTarget) {
139 options.waitMsgTarget.setLoading(false);
140 }
141 var result = Ext.decode(response.responseText);
142 response.result = result;
143 if (!result.success) {
144 response.htmlStatus = Proxmox.Utils.extractRequestError(result, true);
145 Ext.callback(callbackFn, options.scope, [options, false, response]);
146 Ext.callback(failureFn, options.scope, [response, options]);
147 return;
148 }
149 Ext.callback(callbackFn, options.scope, [options, true, response]);
150 Ext.callback(successFn, options.scope, [response, options]);
151 },
152 failure: function(response, options) {
153 if (options.waitMsgTarget) {
154 options.waitMsgTarget.setLoading(false);
155 }
156 response.result = {};
157 try {
158 response.result = Ext.decode(response.responseText);
159 } catch(e) {}
160 var msg = gettext('Connection error') + ' - server offline?';
161 if (response.aborted) {
162 msg = gettext('Connection error') + ' - aborted.';
163 } else if (response.timedout) {
164 msg = gettext('Connection error') + ' - Timeout.';
165 } else if (response.status && response.statusText) {
166 msg = gettext('Connection error') + ' ' + response.status + ': ' + response.statusText;
167 }
168 response.htmlStatus = msg;
169 Ext.callback(callbackFn, options.scope, [options, false, response]);
170 Ext.callback(failureFn, options.scope, [response, options]);
171 }
172 });
173 };
174
175 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
176
177 var target = newopts.waitMsgTarget;
178 if (target) {
179 // Note: ExtJS bug - this does not work when component is not rendered
180 target.setLoading(newopts.waitMsg);
181 }
182 Ext.Ajax.request(newopts);
183 },
184
185 assemble_field_data: function(values, data) {
186 if (Ext.isObject(data)) {
187 Ext.Object.each(data, function(name, val) {
188 if (values.hasOwnProperty(name)) {
189 var bucket = values[name];
190 if (!Ext.isArray(bucket)) {
191 bucket = values[name] = [bucket];
192 }
193 if (Ext.isArray(val)) {
194 values[name] = bucket.concat(val);
195 } else {
196 bucket.push(val);
197 }
198 } else {
199 values[name] = val;
200 }
201 });
202 }
203 },
204
205 dialog_title: function(subject, create, isAdd) {
206 if (create) {
207 if (isAdd) {
208 return gettext('Add') + ': ' + subject;
209 } else {
210 return gettext('Create') + ': ' + subject;
211 }
212 } else {
213 return gettext('Edit') + ': ' + subject;
214 }
215 },
216
217 parse_task_upid: function(upid) {
218 var task = {};
219
220 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]+):$/);
221 if (!res) {
222 throw "unable to parse upid '" + upid + "'";
223 }
224 task.node = res[1];
225 task.pid = parseInt(res[2], 16);
226 task.pstart = parseInt(res[3], 16);
227 task.starttime = parseInt(res[4], 16);
228 task.type = res[5];
229 task.id = res[6];
230 task.user = res[7];
231
232 return task;
233 },
234
235 render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) {
236 var servertime = new Date(value * 1000);
237 return Ext.Date.format(servertime, 'Y-m-d H:i:s');
238 },
239
240 },
241
242 singleton: true,
243 constructor: function() {
244 var me = this;
245 Ext.apply(me, me.utilities);
246
247 var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
248 var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")";
249 var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})";
250 var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")";
251
252
253 me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
254 me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$");
255
256 var IPV6_REGEXP = "(?:" +
257 "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" +
258 "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" +
259 "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" +
260 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" +
261 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" +
262 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" +
263 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" +
264 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" +
265 "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" +
266 ")";
267
268 me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$");
269 me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$");
270 me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]");
271
272 me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
273
274 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])?))";
275 me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$");
276
277 me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$");
278 me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$");
279 me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$");
280 }
281 });