]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Utils.js
7447f05b99d4c6acbea309dae61f03005a328294
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
);
188 constructor: function() {
190 Ext
.apply(me
, me
.utilities
);
192 var IPV4_OCTET
= "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
193 var IPV4_REGEXP
= "(?:(?:" + IPV4_OCTET
+ "\\.){3}" + IPV4_OCTET
+ ")";
194 var IPV6_H16
= "(?:[0-9a-fA-F]{1,4})";
195 var IPV6_LS32
= "(?:(?:" + IPV6_H16
+ ":" + IPV6_H16
+ ")|" + IPV4_REGEXP
+ ")";
198 me
.IP4_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")$");
199 me
.IP4_cidr_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")\/([0-9]{1,2})$");
201 var IPV6_REGEXP
= "(?:" +
202 "(?:(?:" + "(?:" + IPV6_H16
+ ":){6})" + IPV6_LS32
+ ")|" +
203 "(?:(?:" + "::" + "(?:" + IPV6_H16
+ ":){5})" + IPV6_LS32
+ ")|" +
204 "(?:(?:(?:" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){4})" + IPV6_LS32
+ ")|" +
205 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,1}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){3})" + IPV6_LS32
+ ")|" +
206 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,2}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){2})" + IPV6_LS32
+ ")|" +
207 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,3}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){1})" + IPV6_LS32
+ ")|" +
208 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,4}" + IPV6_H16
+ ")?::" + ")" + IPV6_LS32
+ ")|" +
209 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,5}" + IPV6_H16
+ ")?::" + ")" + IPV6_H16
+ ")|" +
210 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,7}" + IPV6_H16
+ ")?::" + ")" + ")" +
213 me
.IP6_match
= new RegExp("^(?:" + IPV6_REGEXP
+ ")$");
214 me
.IP6_cidr_match
= new RegExp("^(?:" + IPV6_REGEXP
+ ")\/([0-9]{1,3})$");
215 me
.IP6_bracket_match
= new RegExp("^\\[(" + IPV6_REGEXP
+ ")\\]");
217 me
.IP64_match
= new RegExp("^(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ ")$");
219 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])?))";
220 me
.DnsName_match
= new RegExp("^" + DnsName_REGEXP
+ "$");
222 me
.HostPort_match
= new RegExp("^(" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")(:\\d+)?$");
223 me
.HostPortBrackets_match
= new RegExp("^\\[(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")\\](:\\d+)?$");
224 me
.IP6_dotnotation_match
= new RegExp("^" + IPV6_REGEXP
+ "(\\.\\d+)?$");