]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Utils.js
a8d7b6f70c81fc1ea2155c2012a93b2a4bc0729b
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' ),
68 eu
: 'Euskera (Basque)' ,
74 nb
: 'Norwegian (Bokmal)' ,
75 nn
: 'Norwegian (Nynorsk)' ,
76 fa
: 'Persian (Farsi)' ,
78 pt_BR
: 'Portuguese (Brazil)' ,
85 render_language : function ( value
) {
87 return Proxmox
. Utils
. defaultText
+ ' (English)' ;
89 var text
= Proxmox
. Utils
. language_map
[ value
];
91 return text
+ ' (' + value
+ ')' ;
96 language_array : function () {
97 var data
= [[ '__default__' , Proxmox
. Utils
. render_language ( '' )]];
98 Ext
. Object
. each ( Proxmox
. Utils
. language_map
, function ( key
, value
) {
99 data
. push ([ key
, Proxmox
. Utils
. render_language ( value
)]);
105 getNoSubKeyHtml : function ( url
) {
106 // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans
107 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' );
110 format_boolean_with_default : function ( value
) {
111 if ( Ext
. isDefined ( value
) && value
!== '__default__' ) {
112 return value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
114 return Proxmox
. Utils
. defaultText
;
117 format_boolean : function ( value
) {
118 return value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
121 format_neg_boolean : function ( value
) {
122 return ! value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
125 format_enabled_toggle : function ( value
) {
126 return value
? Proxmox
. Utils
. enabledText
: Proxmox
. Utils
. disabledText
;
129 format_expire : function ( date
) {
131 return Proxmox
. Utils
. neverText
;
133 return Ext
. Date
. format ( date
, "Y-m-d" );
136 format_duration_long : function ( ut
) {
138 var days
= Math
. floor ( ut
/ 86400 );
140 var hours
= Math
. floor ( ut
/ 3600 );
142 var mins
= Math
. floor ( ut
/ 60 );
145 var hours_str
= '00' + hours
. toString ();
146 hours_str
= hours_str
. substr ( hours_str
. length
- 2 );
147 var mins_str
= "00" + mins
. toString ();
148 mins_str
= mins_str
. substr ( mins_str
. length
- 2 );
149 var ut_str
= "00" + ut
. toString ();
150 ut_str
= ut_str
. substr ( ut_str
. length
- 2 );
153 var ds
= days
> 1 ? Proxmox
. Utils
. daysText
: Proxmox
. Utils
. dayText
;
154 return days
. toString () + ' ' + ds
+ ' ' +
155 hours_str
+ ':' + mins_str
+ ':' + ut_str
;
157 return hours_str
+ ':' + mins_str
+ ':' + ut_str
;
161 format_subscription_level : function ( level
) {
164 } else if ( level
=== 'b' ) {
166 } else if ( level
=== 's' ) {
168 } else if ( level
=== 'p' ) {
171 return Proxmox
. Utils
. noneText
;
175 compute_min_label_width : function ( text
, width
) {
177 if ( width
=== undefined ) { width
= 100 ; }
179 var tm
= new Ext
. util
. TextMetrics ();
180 var min
= tm
. getWidth ( text
+ ':' );
182 return min
< width
? width
: min
;
186 return ( Proxmox
. UserName
!== '' ) && Ext
. util
. Cookies
. get ( Proxmox
. Setup
. auth_cookie_name
);
189 authClear : function () {
190 Ext
. util
. Cookies
. clear ( Proxmox
. Setup
. auth_cookie_name
);
193 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
194 // use el.mask() instead
195 setErrorMask : function ( comp
, msg
) {
204 el
. mask ( gettext ( "Loading..." ));
211 monStoreErrors : function ( me
, store
, clearMaskBeforeLoad
) {
212 if ( clearMaskBeforeLoad
) {
213 me
. mon ( store
, 'beforeload' , function ( s
, operation
, eOpts
) {
214 Proxmox
. Utils
. setErrorMask ( me
, false );
217 me
. mon ( store
, 'beforeload' , function ( s
, operation
, eOpts
) {
219 me
. loadCount
= 0 ; // make sure it is numeric
220 Proxmox
. Utils
. setErrorMask ( me
, true );
225 // only works with 'proxmox' proxy
226 me
. mon ( store
. proxy
, 'afterload' , function ( proxy
, request
, success
) {
230 Proxmox
. Utils
. setErrorMask ( me
, false );
235 /*jslint nomen: true */
236 var operation
= request
. _operation
;
237 var error
= operation
. getError ();
238 if ( error
. statusText
) {
239 msg
= error
. statusText
+ ' (' + error
. status
+ ')' ;
241 msg
= gettext ( 'Connection error' );
243 Proxmox
. Utils
. setErrorMask ( me
, msg
);
247 extractRequestError : function ( result
, verbose
) {
248 var msg
= gettext ( 'Successful' );
250 if (! result
. success
) {
251 msg
= gettext ( "Unknown error" );
252 if ( result
. message
) {
253 msg
= result
. message
;
255 msg
+= ' (' + result
. status
+ ')' ;
258 if ( verbose
&& Ext
. isObject ( result
. errors
)) {
260 Ext
. Object
. each ( result
. errors
, function ( prop
, desc
) {
261 msg
+= "<br><b>" + Ext
. htmlEncode ( prop
) + "</b>: " +
262 Ext
. htmlEncode ( desc
);
271 API2Request : function ( reqOpts
) {
273 var newopts
= Ext
. apply ({
274 waitMsg
: gettext ( 'Please wait...' )
277 if (! newopts
. url
. match ( /^\/api2/ )) {
278 newopts
. url
= '/api2/extjs' + newopts
. url
;
280 delete newopts
. callback
;
282 var createWrapper = function ( successFn
, callbackFn
, failureFn
) {
284 success : function ( response
, options
) {
285 if ( options
. waitMsgTarget
) {
286 if ( Proxmox
. Utils
. toolkit
=== 'touch' ) {
287 options
. waitMsgTarget
. setMasked ( false );
289 options
. waitMsgTarget
. setLoading ( false );
292 var result
= Ext
. decode ( response
. responseText
);
293 response
. result
= result
;
294 if (! result
. success
) {
295 response
. htmlStatus
= Proxmox
. Utils
. extractRequestError ( result
, true );
296 Ext
. callback ( callbackFn
, options
. scope
, [ options
, false , response
]);
297 Ext
. callback ( failureFn
, options
. scope
, [ response
, options
]);
300 Ext
. callback ( callbackFn
, options
. scope
, [ options
, true , response
]);
301 Ext
. callback ( successFn
, options
. scope
, [ response
, options
]);
303 failure : function ( response
, options
) {
304 if ( options
. waitMsgTarget
) {
305 if ( Proxmox
. Utils
. toolkit
=== 'touch' ) {
306 options
. waitMsgTarget
. setMasked ( false );
308 options
. waitMsgTarget
. setLoading ( false );
311 response
. result
= {};
313 response
. result
= Ext
. decode ( response
. responseText
);
315 var msg
= gettext ( 'Connection error' ) + ' - server offline?' ;
316 if ( response
. aborted
) {
317 msg
= gettext ( 'Connection error' ) + ' - aborted.' ;
318 } else if ( response
. timedout
) {
319 msg
= gettext ( 'Connection error' ) + ' - Timeout.' ;
320 } else if ( response
. status
&& response
. statusText
) {
321 msg
= gettext ( 'Connection error' ) + ' ' + response
. status
+ ': ' + response
. statusText
;
323 response
. htmlStatus
= msg
;
324 Ext
. callback ( callbackFn
, options
. scope
, [ options
, false , response
]);
325 Ext
. callback ( failureFn
, options
. scope
, [ response
, options
]);
330 createWrapper ( reqOpts
. success
, reqOpts
. callback
, reqOpts
. failure
);
332 var target
= newopts
. waitMsgTarget
;
334 if ( Proxmox
. Utils
. toolkit
=== 'touch' ) {
335 target
. setMasked ({ xtype
: 'loadmask' , message
: newopts
. waitMsg
} );
337 // Note: ExtJS bug - this does not work when component is not rendered
338 target
. setLoading ( newopts
. waitMsg
);
341 Ext
. Ajax
. request ( newopts
);
344 checked_command : function ( orig_cmd
) {
345 Proxmox
. Utils
. API2Request ({
346 url
: '/nodes/localhost/subscription' ,
349 failure : function ( response
, opts
) {
350 Ext
. Msg
. alert ( gettext ( 'Error' ), response
. htmlStatus
);
352 success : function ( response
, opts
) {
353 var data
= response
. result
. data
;
355 if ( data
. status
!== 'Active' ) {
357 title
: gettext ( 'No valid subscription' ),
358 icon
: Ext
. Msg
. WARNING
,
359 msg
: Proxmox
. Utils
. getNoSubKeyHtml ( data
. url
),
361 callback : function ( btn
) {
375 assemble_field_data : function ( values
, data
) {
376 if ( Ext
. isObject ( data
)) {
377 Ext
. Object
. each ( data
, function ( name
, val
) {
378 if ( values
. hasOwnProperty ( name
)) {
379 var bucket
= values
[ name
];
380 if (! Ext
. isArray ( bucket
)) {
381 bucket
= values
[ name
] = [ bucket
];
383 if ( Ext
. isArray ( val
)) {
384 values
[ name
] = bucket
. concat ( val
);
395 dialog_title : function ( subject
, create
, isAdd
) {
398 return gettext ( 'Add' ) + ': ' + subject
;
400 return gettext ( 'Create' ) + ': ' + subject
;
403 return gettext ( 'Edit' ) + ': ' + subject
;
407 network_iface_types
: {
408 eth
: gettext ( "Network Device" ),
409 bridge
: 'Linux Bridge' ,
411 OVSBridge
: 'OVS Bridge' ,
414 OVSIntPort
: 'OVS IntPort'
417 render_network_iface_type : function ( value
) {
418 return Proxmox
. Utils
. network_iface_types
[ value
] ||
419 Proxmox
. Utils
. unknownText
;
423 diskinit
: [ 'Disk' , gettext ( 'Initialize Disk with GPT' ) ],
424 vncproxy
: [ 'VM/CT' , gettext ( 'Console' ) ],
425 spiceproxy
: [ 'VM/CT' , gettext ( 'Console' ) + ' (Spice)' ],
426 vncshell
: [ '' , gettext ( 'Shell' ) ],
427 spiceshell
: [ '' , gettext ( 'Shell' ) + ' (Spice)' ],
428 qmsnapshot
: [ 'VM' , gettext ( 'Snapshot' ) ],
429 qmrollback
: [ 'VM' , gettext ( 'Rollback' ) ],
430 qmdelsnapshot
: [ 'VM' , gettext ( 'Delete Snapshot' ) ],
431 qmcreate
: [ 'VM' , gettext ( 'Create' ) ],
432 qmrestore
: [ 'VM' , gettext ( 'Restore' ) ],
433 qmdestroy
: [ 'VM' , gettext ( 'Destroy' ) ],
434 qmigrate
: [ 'VM' , gettext ( 'Migrate' ) ],
435 qmclone
: [ 'VM' , gettext ( 'Clone' ) ],
436 qmmove
: [ 'VM' , gettext ( 'Move disk' ) ],
437 qmtemplate
: [ 'VM' , gettext ( 'Convert to template' ) ],
438 qmstart
: [ 'VM' , gettext ( 'Start' ) ],
439 qmstop
: [ 'VM' , gettext ( 'Stop' ) ],
440 qmreset
: [ 'VM' , gettext ( 'Reset' ) ],
441 qmshutdown
: [ 'VM' , gettext ( 'Shutdown' ) ],
442 qmsuspend
: [ 'VM' , gettext ( 'Suspend' ) ],
443 qmresume
: [ 'VM' , gettext ( 'Resume' ) ],
444 qmconfig
: [ 'VM' , gettext ( 'Configure' ) ],
445 vzsnapshot
: [ 'CT' , gettext ( 'Snapshot' ) ],
446 vzrollback
: [ 'CT' , gettext ( 'Rollback' ) ],
447 vzdelsnapshot
: [ 'CT' , gettext ( 'Delete Snapshot' ) ],
448 vzcreate
: [ 'CT' , gettext ( 'Create' ) ],
449 vzrestore
: [ 'CT' , gettext ( 'Restore' ) ],
450 vzdestroy
: [ 'CT' , gettext ( 'Destroy' ) ],
451 vzmigrate
: [ 'CT' , gettext ( 'Migrate' ) ],
452 vzclone
: [ 'CT' , gettext ( 'Clone' ) ],
453 vztemplate
: [ 'CT' , gettext ( 'Convert to template' ) ],
454 vzstart
: [ 'CT' , gettext ( 'Start' ) ],
455 vzstop
: [ 'CT' , gettext ( 'Stop' ) ],
456 vzmount
: [ 'CT' , gettext ( 'Mount' ) ],
457 vzumount
: [ 'CT' , gettext ( 'Unmount' ) ],
458 vzshutdown
: [ 'CT' , gettext ( 'Shutdown' ) ],
459 vzsuspend
: [ 'CT' , gettext ( 'Suspend' ) ],
460 vzresume
: [ 'CT' , gettext ( 'Resume' ) ],
461 hamigrate
: [ 'HA' , gettext ( 'Migrate' ) ],
462 hastart
: [ 'HA' , gettext ( 'Start' ) ],
463 hastop
: [ 'HA' , gettext ( 'Stop' ) ],
464 srvstart
: [ 'SRV' , gettext ( 'Start' ) ],
465 srvstop
: [ 'SRV' , gettext ( 'Stop' ) ],
466 srvrestart
: [ 'SRV' , gettext ( 'Restart' ) ],
467 srvreload
: [ 'SRV' , gettext ( 'Reload' ) ],
468 cephcreatemon
: [ 'Ceph Monitor' , gettext ( 'Create' ) ],
469 cephdestroymon
: [ 'Ceph Monitor' , gettext ( 'Destroy' ) ],
470 cephcreateosd
: [ 'Ceph OSD' , gettext ( 'Create' ) ],
471 cephdestroyosd
: [ 'Ceph OSD' , gettext ( 'Destroy' ) ],
472 cephcreatepool
: [ 'Ceph Pool' , gettext ( 'Create' ) ],
473 cephdestroypool
: [ 'Ceph Pool' , gettext ( 'Destroy' ) ],
474 imgcopy
: [ '' , gettext ( 'Copy data' ) ],
475 imgdel
: [ '' , gettext ( 'Erase data' ) ],
476 download
: [ '' , gettext ( 'Download' ) ],
477 vzdump
: [ '' , gettext ( 'Backup' ) ],
478 aptupdate
: [ '' , gettext ( 'Update package database' ) ],
479 startall
: [ '' , gettext ( 'Start all VMs and Containers' ) ],
480 stopall
: [ '' , gettext ( 'Stop all VMs and Containers' ) ],
481 migrateall
: [ '' , gettext ( 'Migrate all VMs and Containers' ) ]
484 format_task_description : function ( type
, id
) {
485 var farray
= Proxmox
. Utils
. task_desc_table
[ type
];
493 var prefix
= farray
[ 0 ];
494 var text
= farray
[ 1 ];
496 return prefix
+ ' ' + id
+ ' - ' + text
;
501 format_size : function ( size
) {
502 /*jslint confusion: true */
504 var units
= [ '' , 'Ki' , 'Mi' , 'Gi' , 'Ti' , 'Pi' , 'Ei' , 'Zi' , 'Yi' ];
507 while ( size
>= 1024 && (( num
++)+ 1 ) < units
. length
) {
511 return size
. toFixed (( num
> 0 )? 2 : 0 ) + " " + units
[ num
] + "B" ;
514 render_upid : function ( value
, metaData
, record
) {
515 var type
= record
. data
. type
;
516 var id
= record
. data
. id
;
518 return Proxmox
. Utils
. format_task_description ( type
, id
);
521 render_uptime : function ( value
) {
525 if ( uptime
=== undefined ) {
533 return Proxmox
. Utils
. format_duration_long ( uptime
);
536 parse_task_upid : function ( upid
) {
539 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]+):$/ );
541 throw "unable to parse upid '" + upid
+ "'" ;
544 task
. pid
= parseInt ( res
[ 2 ], 16 );
545 task
. pstart
= parseInt ( res
[ 3 ], 16 );
546 task
. starttime
= parseInt ( res
[ 4 ], 16 );
551 task
. desc
= Proxmox
. Utils
. format_task_description ( task
. type
, task
. id
);
556 render_timestamp : function ( value
, metaData
, record
, rowIndex
, colIndex
, store
) {
557 var servertime
= new Date ( value
* 1000 );
558 return Ext
. Date
. format ( servertime
, 'Y-m-d H:i:s' );
561 openXtermJsViewer : function ( vmtype
, vmid
, nodename
, vmname
) {
562 var url
= Ext
. urlEncode ({
563 console
: vmtype
, // kvm, lxc, upgrade or shell
569 var nw
= window
. open ( "?" + url
, '_blank' , 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420' );
576 constructor : function () {
578 Ext
. apply ( me
, me
. utilities
);
580 var IPV4_OCTET
= "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])" ;
581 var IPV4_REGEXP
= "(?:(?:" + IPV4_OCTET
+ " \\ .){3}" + IPV4_OCTET
+ ")" ;
582 var IPV6_H16
= "(?:[0-9a-fA-F]{1,4})" ;
583 var IPV6_LS32
= "(?:(?:" + IPV6_H16
+ ":" + IPV6_H16
+ ")|" + IPV4_REGEXP
+ ")" ;
586 me
. IP4_match
= new RegExp ( "^(?:" + IPV4_REGEXP
+ ")$" );
587 me
. IP4_cidr_match
= new RegExp ( "^(?:" + IPV4_REGEXP
+ ")\/([0-9]{1,2})$" );
589 var IPV6_REGEXP
= "(?:" +
590 "(?:(?:" + "(?:" + IPV6_H16
+ ":){6})" + IPV6_LS32
+ ")|" +
591 "(?:(?:" + "::" + "(?:" + IPV6_H16
+ ":){5})" + IPV6_LS32
+ ")|" +
592 "(?:(?:(?:" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){4})" + IPV6_LS32
+ ")|" +
593 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,1}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){3})" + IPV6_LS32
+ ")|" +
594 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,2}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){2})" + IPV6_LS32
+ ")|" +
595 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,3}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){1})" + IPV6_LS32
+ ")|" +
596 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,4}" + IPV6_H16
+ ")?::" + ")" + IPV6_LS32
+ ")|" +
597 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,5}" + IPV6_H16
+ ")?::" + ")" + IPV6_H16
+ ")|" +
598 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,7}" + IPV6_H16
+ ")?::" + ")" + ")" +
601 me
. IP6_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ ")$" );
602 me
. IP6_cidr_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ ")\/([0-9]{1,3})$" );
603 me
. IP6_bracket_match
= new RegExp ( "^ \\ [(" + IPV6_REGEXP
+ ") \\ ]" );
605 me
. IP64_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ ")$" );
607 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])?))" ;
608 me
. DnsName_match
= new RegExp ( "^" + DnsName_REGEXP
+ "$" );
610 me
. HostPort_match
= new RegExp ( "^(" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")(: \\ d+)?$" );
611 me
. HostPortBrackets_match
= new RegExp ( "^ \\ [(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ") \\ ](: \\ d+)?$" );
612 me
. IP6_dotnotation_match
= new RegExp ( "^" + IPV6_REGEXP
+ "( \\ . \\ d+)?$" );