]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Utils.js
fa27fe063cbbab411b2488cbcbe3b16514ef98bb
2 Ext
.ns('Proxmox.Setup');
4 // TODO: implement gettext
5 function gettext(buf
) { return buf
; }
8 if (!Ext
.isDefined(Proxmox
.Setup
.auth_cookie_name
)) {
9 throw "Proxmox library not initialized";
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;
19 // avoid errors when running without development tools
20 if (!Ext
.isDefined(Ext
.global
.console
)) {
27 Ext
.Ajax
.defaultHeaders
= {
28 'Accept': 'application/json'
31 Ext
.Ajax
.on('beforerequest', function(conn
, options
) {
32 if (Proxmox
.CSRFPreventionToken
) {
33 if (!options
.headers
) {
36 options
.headers
.CSRFPreventionToken
= Proxmox
.CSRFPreventionToken
;
40 Ext
.define('Proxmox.Utils', { utilities
: {
42 // this singleton contains miscellaneous utilities
45 return (Proxmox
.UserName
!== '') && Ext
.util
.Cookies
.get(Proxmox
.Setup
.auth_cookie_name
);
48 authClear: function() {
49 Ext
.util
.Cookies
.clear(Proxmox
.Setup
.auth_cookie_name
);
52 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
53 // use el.mask() instead
54 setErrorMask: function(comp
, msg
) {
63 el
.mask(gettext("Loading..."));
70 monStoreErrors: function(me
, store
) {
71 me
.mon(store
, 'beforeload', function(s
, operation
, eOpts
) {
73 me
.loadCount
= 0; // make sure it is numeric
74 Proxmox
.Utils
.setErrorMask(me
, true);
78 // only works with 'proxmox' proxy
79 me
.mon(store
.proxy
, 'afterload', function(proxy
, request
, success
) {
83 Proxmox
.Utils
.setErrorMask(me
, false);
88 /*jslint nomen: true */
89 var operation
= request
._operation
;
90 var error
= operation
.getError();
91 if (error
.statusText
) {
92 msg
= error
.statusText
+ ' (' + error
.status
+ ')';
94 msg
= gettext('Connection error');
96 Proxmox
.Utils
.setErrorMask(me
, msg
);
100 extractRequestError: function(result
, verbose
) {
101 var msg
= gettext('Successful');
103 if (!result
.success
) {
104 msg
= gettext("Unknown error");
105 if (result
.message
) {
106 msg
= result
.message
;
108 msg
+= ' (' + result
.status
+ ')';
111 if (verbose
&& Ext
.isObject(result
.errors
)) {
113 Ext
.Object
.each(result
.errors
, function(prop
, desc
) {
114 msg
+= "<br><b>" + Ext
.htmlEncode(prop
) + "</b>: " +
115 Ext
.htmlEncode(desc
);
124 API2Request: function(reqOpts
) {
126 var newopts
= Ext
.apply({
127 waitMsg
: gettext('Please wait...')
130 if (!newopts
.url
.match(/^\/api2/)) {
131 newopts
.url
= '/api2/extjs' + newopts
.url
;
133 delete newopts
.callback
;
135 var createWrapper = function(successFn
, callbackFn
, failureFn
) {
137 success: function(response
, options
) {
138 if (options
.waitMsgTarget
) {
139 options
.waitMsgTarget
.setLoading(false);
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
]);
149 Ext
.callback(callbackFn
, options
.scope
, [options
, true, response
]);
150 Ext
.callback(successFn
, options
.scope
, [response
, options
]);
152 failure: function(response
, options
) {
153 if (options
.waitMsgTarget
) {
154 options
.waitMsgTarget
.setLoading(false);
156 response
.result
= {};
158 response
.result
= Ext
.decode(response
.responseText
);
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
;
168 response
.htmlStatus
= msg
;
169 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
170 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
175 createWrapper(reqOpts
.success
, reqOpts
.callback
, reqOpts
.failure
);
177 var target
= newopts
.waitMsgTarget
;
179 // Note: ExtJS bug - this does not work when component is not rendered
180 target
.setLoading(newopts
.waitMsg
);
182 Ext
.Ajax
.request(newopts
);
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
];
193 if (Ext
.isArray(val
)) {
194 values
[name
] = bucket
.concat(val
);
205 dialog_title: function(subject
, create
, isAdd
) {
208 return gettext('Add') + ': ' + subject
;
210 return gettext('Create') + ': ' + subject
;
213 return gettext('Edit') + ': ' + subject
;
217 parse_task_upid: function(upid
) {
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]+):$/);
222 throw "unable to parse upid '" + upid
+ "'";
225 task
.pid
= parseInt(res
[2], 16);
226 task
.pstart
= parseInt(res
[3], 16);
227 task
.starttime
= parseInt(res
[4], 16);
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');
243 constructor: function() {
245 Ext
.apply(me
, me
.utilities
);
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
+ ")";
253 me
.IP4_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")$");
254 me
.IP4_cidr_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")\/([0-9]{1,2})$");
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
+ ")?::" + ")" + ")" +
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
+ ")\\]");
272 me
.IP64_match
= new RegExp("^(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ ")$");
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
+ "$");
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+)?$");