]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Utils.js
2 Ext
. ns ( 'Proxmox.Setup' );
4 if (! Ext
. isFunction ( 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 yesText
: gettext ( 'Yes' ),
45 noText
: gettext ( 'No' ),
46 enabledText
: gettext ( 'Enabled' ),
47 disabledText
: gettext ( 'Disabled' ),
48 noneText
: gettext ( 'none' ),
49 errorText
: gettext ( 'Error' ),
50 unknownText
: gettext ( 'Unknown' ),
51 defaultText
: gettext ( 'Default' ),
52 daysText
: gettext ( 'days' ),
53 dayText
: gettext ( 'day' ),
54 runningText
: gettext ( 'running' ),
55 stoppedText
: gettext ( 'stopped' ),
56 neverText
: gettext ( 'never' ),
57 totalText
: gettext ( 'Total' ),
58 usedText
: gettext ( 'Used' ),
59 directoryText
: gettext ( 'Directory' ),
60 stateText
: gettext ( 'State' ),
61 groupText
: gettext ( 'Group' ),
71 render_language : function ( value
) {
73 return Proxmox
. Utils
. defaultText
+ ' (English)' ;
75 var text
= Proxmox
. Utils
. language_map
[ value
];
77 return text
+ ' (' + value
+ ')' ;
82 language_array : function () {
83 var data
= [[ '__default__' , Proxmox
. Utils
. render_language ( '' )]];
84 Ext
. Object
. each ( Proxmox
. Utils
. language_map
, function ( key
, value
) {
85 data
. push ([ key
, Proxmox
. Utils
. render_language ( value
)]);
91 getNoSubKeyHtml : function ( url
) {
92 // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans
93 return Ext
. String
. format ( 'You do not have a valid subscription for this server. Please visit <a target="_blank" href="{0}">www.proxmox.com</a> to get a list of available options.' , url
|| 'http://www.proxmox.com' );
96 format_boolean_with_default : function ( value
) {
97 if ( Ext
. isDefined ( value
) && value
!== '__default__' ) {
98 return value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
100 return Proxmox
. Utils
. defaultText
;
103 format_boolean : function ( value
) {
104 return value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
107 format_neg_boolean : function ( value
) {
108 return ! value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
111 format_enabled_toggle : function ( value
) {
112 return value
? Proxmox
. Utils
. enabledText
: Proxmox
. Utils
. disabledText
;
115 format_expire : function ( date
) {
117 return Proxmox
. Utils
. neverText
;
119 return Ext
. Date
. format ( date
, "Y-m-d" );
122 format_duration_long : function ( ut
) {
124 var days
= Math
. floor ( ut
/ 86400 );
126 var hours
= Math
. floor ( ut
/ 3600 );
128 var mins
= Math
. floor ( ut
/ 60 );
131 var hours_str
= '00' + hours
. toString ();
132 hours_str
= hours_str
. substr ( hours_str
. length
- 2 );
133 var mins_str
= "00" + mins
. toString ();
134 mins_str
= mins_str
. substr ( mins_str
. length
- 2 );
135 var ut_str
= "00" + ut
. toString ();
136 ut_str
= ut_str
. substr ( ut_str
. length
- 2 );
139 var ds
= days
> 1 ? Proxmox
. Utils
. daysText
: Proxmox
. Utils
. dayText
;
140 return days
. toString () + ' ' + ds
+ ' ' +
141 hours_str
+ ':' + mins_str
+ ':' + ut_str
;
143 return hours_str
+ ':' + mins_str
+ ':' + ut_str
;
147 format_duration_short : function ( ut
) {
150 return ut
. toString () + 's' ;
155 return mins
. toFixed ( 0 ) + 'm' ;
159 var hours
= ut
/ 3600 ;
160 return hours
. toFixed ( 0 ) + 'h' ;
163 var days
= ut
/ 86400 ;
164 return days
. toFixed ( 0 ) + 'd' ;
167 format_subscription_level : function ( level
) {
170 } else if ( level
=== 'b' ) {
172 } else if ( level
=== 's' ) {
174 } else if ( level
=== 'p' ) {
177 return Proxmox
. Utils
. noneText
;
181 compute_min_label_width : function ( text
, width
) {
183 if ( width
=== undefined ) { width
= 100 ; }
185 var tm
= new Ext
. util
. TextMetrics ();
186 var min
= tm
. getWidth ( text
+ ':' );
188 return min
< width
? width
: min
;
192 return ( Proxmox
. UserName
!== '' ) && Ext
. util
. Cookies
. get ( Proxmox
. Setup
. auth_cookie_name
);
195 authClear : function () {
196 Ext
. util
. Cookies
. clear ( Proxmox
. Setup
. auth_cookie_name
);
199 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
200 // use el.mask() instead
201 setErrorMask : function ( comp
, msg
) {
210 el
. mask ( gettext ( "Loading..." ));
217 monStoreErrors : function ( me
, store
, clearMaskBeforeLoad
) {
218 if ( clearMaskBeforeLoad
) {
219 me
. mon ( store
, 'beforeload' , function ( s
, operation
, eOpts
) {
220 Proxmox
. Utils
. setErrorMask ( me
, false );
223 me
. mon ( store
, 'beforeload' , function ( s
, operation
, eOpts
) {
225 me
. loadCount
= 0 ; // make sure it is numeric
226 Proxmox
. Utils
. setErrorMask ( me
, true );
231 // only works with 'proxmox' proxy
232 me
. mon ( store
. proxy
, 'afterload' , function ( proxy
, request
, success
) {
236 Proxmox
. Utils
. setErrorMask ( me
, false );
241 /*jslint nomen: true */
242 var operation
= request
. _operation
;
243 var error
= operation
. getError ();
244 if ( error
. statusText
) {
245 msg
= error
. statusText
+ ' (' + error
. status
+ ')' ;
247 msg
= gettext ( 'Connection error' );
249 Proxmox
. Utils
. setErrorMask ( me
, msg
);
253 extractRequestError : function ( result
, verbose
) {
254 var msg
= gettext ( 'Successful' );
256 if (! result
. success
) {
257 msg
= gettext ( "Unknown error" );
258 if ( result
. message
) {
259 msg
= result
. message
;
261 msg
+= ' (' + result
. status
+ ')' ;
264 if ( verbose
&& Ext
. isObject ( result
. errors
)) {
266 Ext
. Object
. each ( result
. errors
, function ( prop
, desc
) {
267 msg
+= "<br><b>" + Ext
. htmlEncode ( prop
) + "</b>: " +
268 Ext
. htmlEncode ( desc
);
277 API2Request : function ( reqOpts
) {
279 var newopts
= Ext
. apply ({
280 waitMsg
: gettext ( 'Please wait...' )
283 if (! newopts
. url
. match ( /^\/api2/ )) {
284 newopts
. url
= '/api2/extjs' + newopts
. url
;
286 delete newopts
. callback
;
288 var createWrapper = function ( successFn
, callbackFn
, failureFn
) {
290 success : function ( response
, options
) {
291 if ( options
. waitMsgTarget
) {
292 options
. waitMsgTarget
. setLoading ( false );
294 var result
= Ext
. decode ( response
. responseText
);
295 response
. result
= result
;
296 if (! result
. success
) {
297 response
. htmlStatus
= Proxmox
. Utils
. extractRequestError ( result
, true );
298 Ext
. callback ( callbackFn
, options
. scope
, [ options
, false , response
]);
299 Ext
. callback ( failureFn
, options
. scope
, [ response
, options
]);
302 Ext
. callback ( callbackFn
, options
. scope
, [ options
, true , response
]);
303 Ext
. callback ( successFn
, options
. scope
, [ response
, options
]);
305 failure : function ( response
, options
) {
306 if ( options
. waitMsgTarget
) {
307 options
. waitMsgTarget
. setLoading ( false );
309 response
. result
= {};
311 response
. result
= Ext
. decode ( response
. responseText
);
313 var msg
= gettext ( 'Connection error' ) + ' - server offline?' ;
314 if ( response
. aborted
) {
315 msg
= gettext ( 'Connection error' ) + ' - aborted.' ;
316 } else if ( response
. timedout
) {
317 msg
= gettext ( 'Connection error' ) + ' - Timeout.' ;
318 } else if ( response
. status
&& response
. statusText
) {
319 msg
= gettext ( 'Connection error' ) + ' ' + response
. status
+ ': ' + response
. statusText
;
321 response
. htmlStatus
= msg
;
322 Ext
. callback ( callbackFn
, options
. scope
, [ options
, false , response
]);
323 Ext
. callback ( failureFn
, options
. scope
, [ response
, options
]);
328 createWrapper ( reqOpts
. success
, reqOpts
. callback
, reqOpts
. failure
);
330 var target
= newopts
. waitMsgTarget
;
332 // Note: ExtJS bug - this does not work when component is not rendered
333 target
. setLoading ( newopts
. waitMsg
);
335 Ext
. Ajax
. request ( newopts
);
338 checked_command : function ( orig_cmd
) {
339 Proxmox
. Utils
. API2Request ({
340 url
: '/nodes/localhost/subscription' ,
343 failure : function ( response
, opts
) {
344 Ext
. Msg
. alert ( gettext ( 'Error' ), response
. htmlStatus
);
346 success : function ( response
, opts
) {
347 var data
= response
. result
. data
;
349 if ( data
. status
!== 'Active' ) {
351 title
: gettext ( 'No valid subscription' ),
352 icon
: Ext
. Msg
. WARNING
,
353 msg
: Proxmox
. Utils
. getNoSubKeyHtml ( data
. url
),
355 callback : function ( btn
) {
369 assemble_field_data : function ( values
, data
) {
370 if ( Ext
. isObject ( data
)) {
371 Ext
. Object
. each ( data
, function ( name
, val
) {
372 if ( values
. hasOwnProperty ( name
)) {
373 var bucket
= values
[ name
];
374 if (! Ext
. isArray ( bucket
)) {
375 bucket
= values
[ name
] = [ bucket
];
377 if ( Ext
. isArray ( val
)) {
378 values
[ name
] = bucket
. concat ( val
);
389 dialog_title : function ( subject
, create
, isAdd
) {
392 return gettext ( 'Add' ) + ': ' + subject
;
394 return gettext ( 'Create' ) + ': ' + subject
;
397 return gettext ( 'Edit' ) + ': ' + subject
;
401 network_iface_types
: {
402 eth
: gettext ( "Network Device" ),
403 bridge
: 'Linux Bridge' ,
405 OVSBridge
: 'OVS Bridge' ,
408 OVSIntPort
: 'OVS IntPort'
411 render_network_iface_type : function ( value
) {
412 return Proxmox
. Utils
. network_iface_types
[ value
] ||
413 Proxmox
. Utils
. unknownText
;
416 // you can override this to provide nicer task descriptions
417 format_task_description : function ( type
, id
) {
418 return type
+ ' ' + id
;
421 format_size : function ( size
) {
422 /*jslint confusion: true */
424 var units
= [ '' , 'Ki' , 'Mi' , 'Gi' , 'Ti' , 'Pi' , 'Ei' , 'Zi' , 'Yi' ];
427 while ( size
>= 1024 && (( num
++)+ 1 ) < units
. length
) {
431 return size
. toFixed (( num
> 0 )? 2 : 0 ) + " " + units
[ num
] + "B" ;
434 render_upid : function ( value
, metaData
, record
) {
435 var type
= record
. data
. type
;
436 var id
= record
. data
. id
;
438 return Proxmox
. Utils
. format_task_description ( type
, id
);
441 render_uptime : function ( value
) {
445 if ( uptime
=== undefined ) {
453 return Proxmox
. Utils
. format_duration_long ( uptime
);
456 parse_task_upid : function ( upid
) {
459 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]+):$/ );
461 throw "unable to parse upid '" + upid
+ "'" ;
464 task
. pid
= parseInt ( res
[ 2 ], 16 );
465 task
. pstart
= parseInt ( res
[ 3 ], 16 );
466 task
. starttime
= parseInt ( res
[ 4 ], 16 );
471 task
. desc
= Proxmox
. Utils
. format_task_description ( task
. type
, task
. id
);
476 render_timestamp : function ( value
, metaData
, record
, rowIndex
, colIndex
, store
) {
477 var servertime
= new Date ( value
* 1000 );
478 return Ext
. Date
. format ( servertime
, 'Y-m-d H:i:s' );
481 openXtermJsViewer : function ( vmtype
, vmid
, nodename
, vmname
) {
482 var url
= Ext
. urlEncode ({
483 console
: vmtype
, // kvm, lxc, upgrade or shell
489 var nw
= window
. open ( "?" + url
, '_blank' , 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420' );
496 constructor : function () {
498 Ext
. apply ( me
, me
. utilities
);
500 var IPV4_OCTET
= "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])" ;
501 var IPV4_REGEXP
= "(?:(?:" + IPV4_OCTET
+ " \\ .){3}" + IPV4_OCTET
+ ")" ;
502 var IPV6_H16
= "(?:[0-9a-fA-F]{1,4})" ;
503 var IPV6_LS32
= "(?:(?:" + IPV6_H16
+ ":" + IPV6_H16
+ ")|" + IPV4_REGEXP
+ ")" ;
506 me
. IP4_match
= new RegExp ( "^(?:" + IPV4_REGEXP
+ ")$" );
507 me
. IP4_cidr_match
= new RegExp ( "^(?:" + IPV4_REGEXP
+ ")\/([0-9]{1,2})$" );
509 var IPV6_REGEXP
= "(?:" +
510 "(?:(?:" + "(?:" + IPV6_H16
+ ":){6})" + IPV6_LS32
+ ")|" +
511 "(?:(?:" + "::" + "(?:" + IPV6_H16
+ ":){5})" + IPV6_LS32
+ ")|" +
512 "(?:(?:(?:" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){4})" + IPV6_LS32
+ ")|" +
513 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,1}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){3})" + IPV6_LS32
+ ")|" +
514 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,2}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){2})" + IPV6_LS32
+ ")|" +
515 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,3}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){1})" + IPV6_LS32
+ ")|" +
516 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,4}" + IPV6_H16
+ ")?::" + ")" + IPV6_LS32
+ ")|" +
517 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,5}" + IPV6_H16
+ ")?::" + ")" + IPV6_H16
+ ")|" +
518 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,7}" + IPV6_H16
+ ")?::" + ")" + ")" +
521 me
. IP6_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ ")$" );
522 me
. IP6_cidr_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ ")\/([0-9]{1,3})$" );
523 me
. IP6_bracket_match
= new RegExp ( "^ \\ [(" + IPV6_REGEXP
+ ") \\ ]" );
525 me
. IP64_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ ")$" );
527 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])?))" ;
528 me
. DnsName_match
= new RegExp ( "^" + DnsName_REGEXP
+ "$" );
530 me
. HostPort_match
= new RegExp ( "^(" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")(: \\ d+)?$" );
531 me
. HostPortBrackets_match
= new RegExp ( "^ \\ [(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ") \\ ](: \\ d+)?$" );
532 me
. IP6_dotnotation_match
= new RegExp ( "^" + IPV6_REGEXP
+ "( \\ . \\ d+)?$" );