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