]>
git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
3 // TODO: implement gettext
4 function gettext(buf
) { return buf
; }
6 // avoid errors related to Accessible Rich Internet Applications
7 // (access for people with disabilities)
8 // TODO reenable after all components are upgraded
9 Ext
.enableAria
= false;
10 Ext
.enableAriaButtons
= false;
11 Ext
.enableAriaPanels
= false;
13 // avoid errors when running without development tools
14 if (!Ext
.isDefined(Ext
.global
.console
)) {
20 console
.log("Starting PMG Manager");
22 Ext
.Ajax
.defaultHeaders
= {
23 'Accept': 'application/json'
26 Ext
.Ajax
.on('beforerequest', function(conn
, options
) {
27 if (PMG
.CSRFPreventionToken
) {
28 if (!options
.headers
) {
31 options
.headers
.CSRFPreventionToken
= PMG
.CSRFPreventionToken
;
35 Ext
.define('PMG.Utils', { utilities
: {
37 // this singleton contains miscellaneous utilities
40 return (PMG
.UserName
!== '') && Ext
.util
.Cookies
.get('PMGAuthCookie');
43 authClear: function() {
44 Ext
.util
.Cookies
.clear("PMGAuthCookie");
47 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
48 // use el.mask() instead
49 setErrorMask: function(comp
, msg
) {
58 el
.mask(gettext("Loading..."));
65 extractRequestError: function(result
, verbose
) {
66 var msg
= gettext('Successful');
68 if (!result
.success
) {
69 msg
= gettext("Unknown error");
73 msg
+= ' (' + result
.status
+ ')';
76 if (verbose
&& Ext
.isObject(result
.errors
)) {
78 Ext
.Object
.each(result
.errors
, function(prop
, desc
) {
79 msg
+= "<br><b>" + Ext
.htmlEncode(prop
) + "</b>: " +
89 API2Request: function(reqOpts
) {
91 var newopts
= Ext
.apply({
92 waitMsg
: gettext('Please wait...')
95 if (!newopts
.url
.match(/^\/api2/)) {
96 newopts
.url
= '/api2/extjs' + newopts
.url
;
98 delete newopts
.callback
;
100 var createWrapper = function(successFn
, callbackFn
, failureFn
) {
102 success: function(response
, options
) {
103 if (options
.waitMsgTarget
) {
104 options
.waitMsgTarget
.setLoading(false);
106 var result
= Ext
.decode(response
.responseText
);
107 response
.result
= result
;
108 if (!result
.success
) {
109 response
.htmlStatus
= PMG
.Utils
.extractRequestError(result
, true);
110 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
111 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
114 Ext
.callback(callbackFn
, options
.scope
, [options
, true, response
]);
115 Ext
.callback(successFn
, options
.scope
, [response
, options
]);
117 failure: function(response
, options
) {
118 if (options
.waitMsgTarget
) {
119 options
.waitMsgTarget
.setLoading(false);
121 response
.result
= {};
123 response
.result
= Ext
.decode(response
.responseText
);
125 var msg
= gettext('Connection error') + ' - server offline?';
126 if (response
.aborted
) {
127 msg
= gettext('Connection error') + ' - aborted.';
128 } else if (response
.timedout
) {
129 msg
= gettext('Connection error') + ' - Timeout.';
130 } else if (response
.status
&& response
.statusText
) {
131 msg
= gettext('Connection error') + ' ' + response
.status
+ ': ' + response
.statusText
;
133 response
.htmlStatus
= msg
;
134 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
135 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
140 createWrapper(reqOpts
.success
, reqOpts
.callback
, reqOpts
.failure
);
142 var target
= newopts
.waitMsgTarget
;
144 // Note: ExtJS bug - this does not work when component is not rendered
145 target
.setLoading(newopts
.waitMsg
);
147 Ext
.Ajax
.request(newopts
);
150 yesText
: gettext('Yes'),
151 noText
: gettext('No')
156 constructor: function() {
158 Ext
.apply(me
, me
.utilities
);
160 var IPV4_OCTET
= "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
161 var IPV4_REGEXP
= "(?:(?:" + IPV4_OCTET
+ "\\.){3}" + IPV4_OCTET
+ ")";
162 var IPV6_H16
= "(?:[0-9a-fA-F]{1,4})";
163 var IPV6_LS32
= "(?:(?:" + IPV6_H16
+ ":" + IPV6_H16
+ ")|" + IPV4_REGEXP
+ ")";
166 me
.IP4_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")$");
167 me
.IP4_cidr_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")\/([0-9]{1,2})$");
169 var IPV6_REGEXP
= "(?:" +
170 "(?:(?:" + "(?:" + IPV6_H16
+ ":){6})" + IPV6_LS32
+ ")|" +
171 "(?:(?:" + "::" + "(?:" + IPV6_H16
+ ":){5})" + IPV6_LS32
+ ")|" +
172 "(?:(?:(?:" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){4})" + IPV6_LS32
+ ")|" +
173 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,1}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){3})" + IPV6_LS32
+ ")|" +
174 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,2}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){2})" + IPV6_LS32
+ ")|" +
175 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,3}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){1})" + IPV6_LS32
+ ")|" +
176 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,4}" + IPV6_H16
+ ")?::" + ")" + IPV6_LS32
+ ")|" +
177 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,5}" + IPV6_H16
+ ")?::" + ")" + IPV6_H16
+ ")|" +
178 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,7}" + IPV6_H16
+ ")?::" + ")" + ")" +
181 me
.IP6_match
= new RegExp("^(?:" + IPV6_REGEXP
+ ")$");
182 me
.IP6_cidr_match
= new RegExp("^(?:" + IPV6_REGEXP
+ ")\/([0-9]{1,3})$");
183 me
.IP6_bracket_match
= new RegExp("^\\[(" + IPV6_REGEXP
+ ")\\]");
185 me
.IP64_match
= new RegExp("^(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ ")$");
187 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])?))";
188 me
.DnsName_match
= new RegExp("^" + DnsName_REGEXP
+ "$");
190 me
.HostPort_match
= new RegExp("^(" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")(:\\d+)?$");
191 me
.HostPortBrackets_match
= new RegExp("^\\[(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")\\](:\\d+)?$");
192 me
.IP6_dotnotation_match
= new RegExp("^" + IPV6_REGEXP
+ "(\\.\\d+)?$");