]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Utils.js
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
44 unknownText
: gettext('Unknown'),
47 return (Proxmox
.UserName
!== '') && Ext
.util
.Cookies
.get(Proxmox
.Setup
.auth_cookie_name
);
50 authClear: function() {
51 Ext
.util
.Cookies
.clear(Proxmox
.Setup
.auth_cookie_name
);
54 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
55 // use el.mask() instead
56 setErrorMask: function(comp
, msg
) {
65 el
.mask(gettext("Loading..."));
72 monStoreErrors: function(me
, store
) {
73 me
.mon(store
, 'beforeload', function(s
, operation
, eOpts
) {
75 me
.loadCount
= 0; // make sure it is numeric
76 Proxmox
.Utils
.setErrorMask(me
, true);
80 // only works with 'proxmox' proxy
81 me
.mon(store
.proxy
, 'afterload', function(proxy
, request
, success
) {
85 Proxmox
.Utils
.setErrorMask(me
, false);
90 /*jslint nomen: true */
91 var operation
= request
._operation
;
92 var error
= operation
.getError();
93 if (error
.statusText
) {
94 msg
= error
.statusText
+ ' (' + error
.status
+ ')';
96 msg
= gettext('Connection error');
98 Proxmox
.Utils
.setErrorMask(me
, msg
);
102 extractRequestError: function(result
, verbose
) {
103 var msg
= gettext('Successful');
105 if (!result
.success
) {
106 msg
= gettext("Unknown error");
107 if (result
.message
) {
108 msg
= result
.message
;
110 msg
+= ' (' + result
.status
+ ')';
113 if (verbose
&& Ext
.isObject(result
.errors
)) {
115 Ext
.Object
.each(result
.errors
, function(prop
, desc
) {
116 msg
+= "<br><b>" + Ext
.htmlEncode(prop
) + "</b>: " +
117 Ext
.htmlEncode(desc
);
126 API2Request: function(reqOpts
) {
128 var newopts
= Ext
.apply({
129 waitMsg
: gettext('Please wait...')
132 if (!newopts
.url
.match(/^\/api2/)) {
133 newopts
.url
= '/api2/extjs' + newopts
.url
;
135 delete newopts
.callback
;
137 var createWrapper = function(successFn
, callbackFn
, failureFn
) {
139 success: function(response
, options
) {
140 if (options
.waitMsgTarget
) {
141 options
.waitMsgTarget
.setLoading(false);
143 var result
= Ext
.decode(response
.responseText
);
144 response
.result
= result
;
145 if (!result
.success
) {
146 response
.htmlStatus
= Proxmox
.Utils
.extractRequestError(result
, true);
147 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
148 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
151 Ext
.callback(callbackFn
, options
.scope
, [options
, true, response
]);
152 Ext
.callback(successFn
, options
.scope
, [response
, options
]);
154 failure: function(response
, options
) {
155 if (options
.waitMsgTarget
) {
156 options
.waitMsgTarget
.setLoading(false);
158 response
.result
= {};
160 response
.result
= Ext
.decode(response
.responseText
);
162 var msg
= gettext('Connection error') + ' - server offline?';
163 if (response
.aborted
) {
164 msg
= gettext('Connection error') + ' - aborted.';
165 } else if (response
.timedout
) {
166 msg
= gettext('Connection error') + ' - Timeout.';
167 } else if (response
.status
&& response
.statusText
) {
168 msg
= gettext('Connection error') + ' ' + response
.status
+ ': ' + response
.statusText
;
170 response
.htmlStatus
= msg
;
171 Ext
.callback(callbackFn
, options
.scope
, [options
, false, response
]);
172 Ext
.callback(failureFn
, options
.scope
, [response
, options
]);
177 createWrapper(reqOpts
.success
, reqOpts
.callback
, reqOpts
.failure
);
179 var target
= newopts
.waitMsgTarget
;
181 // Note: ExtJS bug - this does not work when component is not rendered
182 target
.setLoading(newopts
.waitMsg
);
184 Ext
.Ajax
.request(newopts
);
187 assemble_field_data: function(values
, data
) {
188 if (Ext
.isObject(data
)) {
189 Ext
.Object
.each(data
, function(name
, val
) {
190 if (values
.hasOwnProperty(name
)) {
191 var bucket
= values
[name
];
192 if (!Ext
.isArray(bucket
)) {
193 bucket
= values
[name
] = [bucket
];
195 if (Ext
.isArray(val
)) {
196 values
[name
] = bucket
.concat(val
);
207 dialog_title: function(subject
, create
, isAdd
) {
210 return gettext('Add') + ': ' + subject
;
212 return gettext('Create') + ': ' + subject
;
215 return gettext('Edit') + ': ' + subject
;
219 network_iface_types
: {
220 eth
: gettext("Network Device"),
221 bridge
: 'Linux Bridge',
223 OVSBridge
: 'OVS Bridge',
226 OVSIntPort
: 'OVS IntPort'
229 render_network_iface_type: function(value
) {
230 return Proxmox
.Utils
.network_iface_types
[value
] ||
231 Proxmox
.Utils
.unknownText
;
234 // you can override this to provide nicer task descriptions
235 format_task_description: function(type
, id
) {
236 return type
+ ' ' + id
;
239 render_upid: function(value
, metaData
, record
) {
240 var type
= record
.data
.type
;
241 var id
= record
.data
.id
;
243 return Proxmox
.Utils
.format_task_description(type
, id
);
246 parse_task_upid: function(upid
) {
249 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]+):$/);
251 throw "unable to parse upid '" + upid
+ "'";
254 task
.pid
= parseInt(res
[2], 16);
255 task
.pstart
= parseInt(res
[3], 16);
256 task
.starttime
= parseInt(res
[4], 16);
261 task
.desc
= Proxmox
.Utils
.format_task_description(task
.type
, task
.id
);
266 render_timestamp: function(value
, metaData
, record
, rowIndex
, colIndex
, store
) {
267 var servertime
= new Date(value
* 1000);
268 return Ext
.Date
.format(servertime
, 'Y-m-d H:i:s');
274 constructor: function() {
276 Ext
.apply(me
, me
.utilities
);
278 var IPV4_OCTET
= "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])";
279 var IPV4_REGEXP
= "(?:(?:" + IPV4_OCTET
+ "\\.){3}" + IPV4_OCTET
+ ")";
280 var IPV6_H16
= "(?:[0-9a-fA-F]{1,4})";
281 var IPV6_LS32
= "(?:(?:" + IPV6_H16
+ ":" + IPV6_H16
+ ")|" + IPV4_REGEXP
+ ")";
284 me
.IP4_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")$");
285 me
.IP4_cidr_match
= new RegExp("^(?:" + IPV4_REGEXP
+ ")\/([0-9]{1,2})$");
287 var IPV6_REGEXP
= "(?:" +
288 "(?:(?:" + "(?:" + IPV6_H16
+ ":){6})" + IPV6_LS32
+ ")|" +
289 "(?:(?:" + "::" + "(?:" + IPV6_H16
+ ":){5})" + IPV6_LS32
+ ")|" +
290 "(?:(?:(?:" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){4})" + IPV6_LS32
+ ")|" +
291 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,1}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){3})" + IPV6_LS32
+ ")|" +
292 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,2}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){2})" + IPV6_LS32
+ ")|" +
293 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,3}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){1})" + IPV6_LS32
+ ")|" +
294 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,4}" + IPV6_H16
+ ")?::" + ")" + IPV6_LS32
+ ")|" +
295 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,5}" + IPV6_H16
+ ")?::" + ")" + IPV6_H16
+ ")|" +
296 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,7}" + IPV6_H16
+ ")?::" + ")" + ")" +
299 me
.IP6_match
= new RegExp("^(?:" + IPV6_REGEXP
+ ")$");
300 me
.IP6_cidr_match
= new RegExp("^(?:" + IPV6_REGEXP
+ ")\/([0-9]{1,3})$");
301 me
.IP6_bracket_match
= new RegExp("^\\[(" + IPV6_REGEXP
+ ")\\]");
303 me
.IP64_match
= new RegExp("^(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ ")$");
305 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])?))";
306 me
.DnsName_match
= new RegExp("^" + DnsName_REGEXP
+ "$");
308 me
.HostPort_match
= new RegExp("^(" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")(:\\d+)?$");
309 me
.HostPortBrackets_match
= new RegExp("^\\[(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")\\](:\\d+)?$");
310 me
.IP6_dotnotation_match
= new RegExp("^" + IPV6_REGEXP
+ "(\\.\\d+)?$");