]> git.proxmox.com Git - pmg-gui.git/blob - js/Utils.js
Add basic GUI with login window
[pmg-gui.git] / js / Utils.js
1 Ext.ns('PMG');
2
3 // TODO: implement gettext
4 function gettext(buf) { return buf; }
5
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;
12
13 // avoid errors when running without development tools
14 if (!Ext.isDefined(Ext.global.console)) {
15 var console = {
16 dir: function() {},
17 log: function() {}
18 };
19 }
20 console.log("Starting PMG Manager");
21
22 Ext.Ajax.defaultHeaders = {
23 'Accept': 'application/json'
24 };
25
26 Ext.Ajax.on('beforerequest', function(conn, options) {
27 if (PMG.CSRFPreventionToken) {
28 if (!options.headers) {
29 options.headers = {};
30 }
31 options.headers.CSRFPreventionToken = PMG.CSRFPreventionToken;
32 }
33 });
34
35 Ext.define('PMG.Utils', { utilities: {
36
37 // this singleton contains miscellaneous utilities
38
39 authOK: function() {
40 return Ext.util.Cookies.get('PMGAuthCookie');
41 },
42
43 authClear: function() {
44 Ext.util.Cookies.clear("PMGAuthCookie");
45 },
46
47 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
48 // use el.mask() instead
49 setErrorMask: function(comp, msg) {
50 var el = comp.el;
51 if (!el) {
52 return;
53 }
54 if (!msg) {
55 el.unmask();
56 } else {
57 if (msg === true) {
58 el.mask(gettext("Loading..."));
59 } else {
60 el.mask(msg);
61 }
62 }
63 },
64
65 extractRequestError: function(result, verbose) {
66 var msg = gettext('Successful');
67
68 if (!result.success) {
69 msg = gettext("Unknown error");
70 if (result.message) {
71 msg = result.message;
72 if (result.status) {
73 msg += ' (' + result.status + ')';
74 }
75 }
76 if (verbose && Ext.isObject(result.errors)) {
77 msg += "<br>";
78 Ext.Object.each(result.errors, function(prop, desc) {
79 msg += "<br><b>" + Ext.htmlEncode(prop) + "</b>: " +
80 Ext.htmlEncode(desc);
81 });
82 }
83 }
84
85 return msg;
86 },
87
88 // Ext.Ajax.request
89 API2Request: function(reqOpts) {
90
91 var newopts = Ext.apply({
92 waitMsg: gettext('Please wait...')
93 }, reqOpts);
94
95 if (!newopts.url.match(/^\/api2/)) {
96 newopts.url = '/api2/extjs' + newopts.url;
97 }
98 delete newopts.callback;
99
100 var createWrapper = function(successFn, callbackFn, failureFn) {
101 Ext.apply(newopts, {
102 success: function(response, options) {
103 if (options.waitMsgTarget) {
104 options.waitMsgTarget.setLoading(false);
105 }
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]);
112 return;
113 }
114 Ext.callback(callbackFn, options.scope, [options, true, response]);
115 Ext.callback(successFn, options.scope, [response, options]);
116 },
117 failure: function(response, options) {
118 if (options.waitMsgTarget) {
119 options.waitMsgTarget.setLoading(false);
120 }
121 response.result = {};
122 try {
123 response.result = Ext.decode(response.responseText);
124 } catch(e) {}
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;
132 }
133 response.htmlStatus = msg;
134 Ext.callback(callbackFn, options.scope, [options, false, response]);
135 Ext.callback(failureFn, options.scope, [response, options]);
136 }
137 });
138 };
139
140 createWrapper(reqOpts.success, reqOpts.callback, reqOpts.failure);
141
142 var target = newopts.waitMsgTarget;
143 if (target) {
144 // Note: ExtJS bug - this does not work when component is not rendered
145 target.setLoading(newopts.waitMsg);
146 }
147 Ext.Ajax.request(newopts);
148 },
149
150 yesText: gettext('Yes'),
151 noText: gettext('No')
152
153 },
154
155 singleton: true,
156 constructor: function() {
157 var me = this;
158 Ext.apply(me, me.utilities);
159
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 + ")";
164
165
166 me.IP4_match = new RegExp("^(?:" + IPV4_REGEXP + ")$");
167 me.IP4_cidr_match = new RegExp("^(?:" + IPV4_REGEXP + ")\/([0-9]{1,2})$");
168
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 + ")?::" + ")" + ")" +
179 ")";
180
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 + ")\\]");
184
185 me.IP64_match = new RegExp("^(?:" + IPV6_REGEXP + "|" + IPV4_REGEXP + ")$");
186
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 + "$");
189
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+)?$");
193 }
194 });