]>
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 | ||
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 | ||
06694509 DM |
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 | ||
53ac9bca DM |
217 | // you can override this to provide nicer task descriptions |
218 | format_task_description: function(type, id) { | |
219 | return type + ' ' + id; | |
220 | }, | |
221 | ||
222 | render_upid: function(value, metaData, record) { | |
223 | var type = record.data.type; | |
224 | var id = record.data.id; | |
225 | ||
226 | return Proxmox.Utils.format_task_description(type, id); | |
227 | }, | |
228 | ||
06694509 DM |
229 | parse_task_upid: function(upid) { |
230 | var task = {}; | |
231 | ||
232 | 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]+):$/); | |
233 | if (!res) { | |
234 | throw "unable to parse upid '" + upid + "'"; | |
235 | } | |
236 | task.node = res[1]; | |
237 | task.pid = parseInt(res[2], 16); | |
238 | task.pstart = parseInt(res[3], 16); | |
239 | task.starttime = parseInt(res[4], 16); | |
240 | task.type = res[5]; | |
241 | task.id = res[6]; | |
242 | task.user = res[7]; | |
243 | ||
53ac9bca DM |
244 | task.desc = Proxmox.Utils.format_task_description(task.type, task.id); |
245 | ||
06694509 DM |
246 | return task; |
247 | }, | |
248 | ||
249 | render_timestamp: function(value, metaData, record, rowIndex, colIndex, store) { | |
250 | var servertime = new Date(value * 1000); | |
251 | return Ext.Date.format(servertime, 'Y-m-d H:i:s'); | |
252 | }, | |
253 | ||
0bb29d35 | 254 | }, |
5ffef550 | 255 | |
0bb29d35 DM |
256 | singleton: true, |
257 | constructor: function() { | |
258 | var me = this; | |
259 | Ext.apply(me, me.utilities); | |
260 | ||
261 | var IPV4_OCTET = "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])"; | |
262 | var IPV4_REGEXP = "(?:(?:" + IPV4_OCTET + "\\.){3}" + IPV4_OCTET + ")"; | |
263 | var IPV6_H16 = "(?:[0-9a-fA-F]{1,4})"; | |
264 | var IPV6_LS32 = "(?:(?:" + IPV6_H16 + ":" + IPV6_H16 + ")|" + IPV4_REGEXP + ")"; | |
265 | ||
266 | ||
267 | me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$"); | |
268 | me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$"); | |
269 | ||
270 | var IPV6_REGEXP = "(?:" + | |
271 | "(?:(?:" + "(?:" + IPV6_H16 + ":){6})" + IPV6_LS32 + ")|" + | |
272 | "(?:(?:" + "::" + "(?:" + IPV6_H16 + ":){5})" + IPV6_LS32 + ")|" + | |
273 | "(?:(?:(?:" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){4})" + IPV6_LS32 + ")|" + | |
274 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,1}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){3})" + IPV6_LS32 + ")|" + | |
275 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,2}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){2})" + IPV6_LS32 + ")|" + | |
276 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,3}" + IPV6_H16 + ")?::" + "(?:" + IPV6_H16 + ":){1})" + IPV6_LS32 + ")|" + | |
277 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,4}" + IPV6_H16 + ")?::" + ")" + IPV6_LS32 + ")|" + | |
278 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,5}" + IPV6_H16 + ")?::" + ")" + IPV6_H16 + ")|" + | |
279 | "(?:(?:(?:(?:" + IPV6_H16 + ":){0,7}" + IPV6_H16 + ")?::" + ")" + ")" + | |
280 | ")"; | |
281 | ||
282 | me.IP6_match = new RegExp("^(?:" + IPV6_REGEXP + ")$"); | |
283 | me.IP6_cidr_match = new RegExp("^(?:" + IPV6_REGEXP + ")\/([0-9]{1,3})$"); | |
284 | me.IP6_bracket_match = new RegExp("^\\[(" + IPV6_REGEXP + ")\\]"); | |
285 | ||
286 | me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$"); | |
287 | ||
288 | 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])?))"; | |
289 | me.DnsName_match = new RegExp("^" + DnsName_REGEXP + "$"); | |
290 | ||
291 | me.HostPort_match = new RegExp("^(" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")(:\\d+)?$"); | |
292 | me.HostPortBrackets_match = new RegExp("^\\[(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + "|" + DnsName_REGEXP + ")\\](:\\d+)?$"); | |
293 | me.IP6_dotnotation_match = new RegExp("^" + IPV6_REGEXP + "(\\.\\d+)?$"); | |
294 | } | |
295 | }); |