]>
git.proxmox.com Git - proxmox-widget-toolkit.git/blob - Utils.js
2 Ext
. ns ( 'Proxmox.Setup' );
4 if (! Ext
. isDefined ( Proxmox
. Setup
. auth_cookie_name
)) {
5 throw "Proxmox library not initialized" ;
8 // avoid errors related to Accessible Rich Internet Applications
9 // (access for people with disabilities)
10 // TODO reenable after all components are upgraded
11 Ext
. enableAria
= false ;
12 Ext
. enableAriaButtons
= false ;
13 Ext
. enableAriaPanels
= false ;
15 // avoid errors when running without development tools
16 if (! Ext
. isDefined ( Ext
. global
. console
)) {
23 Ext
. Ajax
. defaultHeaders
= {
24 'Accept' : 'application/json'
27 Ext
. Ajax
. on ( 'beforerequest' , function ( conn
, options
) {
28 if ( Proxmox
. CSRFPreventionToken
) {
29 if (! options
. headers
) {
32 options
. headers
. CSRFPreventionToken
= Proxmox
. CSRFPreventionToken
;
36 Ext
. define ( 'Proxmox.Utils' , { utilities
: {
38 // this singleton contains miscellaneous utilities
40 yesText
: gettext ( 'Yes' ),
41 noText
: gettext ( 'No' ),
42 enabledText
: gettext ( 'Enabled' ),
43 disabledText
: gettext ( 'Disabled' ),
44 noneText
: gettext ( 'none' ),
45 errorText
: gettext ( 'Error' ),
46 unknownText
: gettext ( 'Unknown' ),
47 defaultText
: gettext ( 'Default' ),
48 daysText
: gettext ( 'days' ),
49 dayText
: gettext ( 'day' ),
50 runningText
: gettext ( 'running' ),
51 stoppedText
: gettext ( 'stopped' ),
52 neverText
: gettext ( 'never' ),
53 totalText
: gettext ( 'Total' ),
54 usedText
: gettext ( 'Used' ),
55 directoryText
: gettext ( 'Directory' ),
56 stateText
: gettext ( 'State' ),
57 groupText
: gettext ( 'Group' ),
64 eu
: 'Euskera (Basque)' ,
70 nb
: 'Norwegian (Bokmal)' ,
71 nn
: 'Norwegian (Nynorsk)' ,
72 fa
: 'Persian (Farsi)' ,
74 pt_BR
: 'Portuguese (Brazil)' ,
81 render_language : function ( value
) {
83 return Proxmox
. Utils
. defaultText
+ ' (English)' ;
85 var text
= Proxmox
. Utils
. language_map
[ value
];
87 return text
+ ' (' + value
+ ')' ;
92 language_array : function () {
93 var data
= [[ '__default__' , Proxmox
. Utils
. render_language ( '' )]];
94 Ext
. Object
. each ( Proxmox
. Utils
. language_map
, function ( key
, value
) {
95 data
. push ([ key
, Proxmox
. Utils
. render_language ( value
)]);
101 getNoSubKeyHtml : function ( url
) {
102 // url http://www.proxmox.com/products/proxmox-ve/subscription-service-plans
103 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' );
106 format_boolean_with_default : function ( value
) {
107 if ( Ext
. isDefined ( value
) && value
!== '__default__' ) {
108 return value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
110 return Proxmox
. Utils
. defaultText
;
113 format_boolean : function ( value
) {
114 return value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
117 format_neg_boolean : function ( value
) {
118 return ! value
? Proxmox
. Utils
. yesText
: Proxmox
. Utils
. noText
;
121 format_enabled_toggle : function ( value
) {
122 return value
? Proxmox
. Utils
. enabledText
: Proxmox
. Utils
. disabledText
;
125 format_expire : function ( date
) {
127 return Proxmox
. Utils
. neverText
;
129 return Ext
. Date
. format ( date
, "Y-m-d" );
132 format_duration_long : function ( ut
) {
134 var days
= Math
. floor ( ut
/ 86400 );
136 var hours
= Math
. floor ( ut
/ 3600 );
138 var mins
= Math
. floor ( ut
/ 60 );
141 var hours_str
= '00' + hours
. toString ();
142 hours_str
= hours_str
. substr ( hours_str
. length
- 2 );
143 var mins_str
= "00" + mins
. toString ();
144 mins_str
= mins_str
. substr ( mins_str
. length
- 2 );
145 var ut_str
= "00" + ut
. toString ();
146 ut_str
= ut_str
. substr ( ut_str
. length
- 2 );
149 var ds
= days
> 1 ? Proxmox
. Utils
. daysText
: Proxmox
. Utils
. dayText
;
150 return days
. toString () + ' ' + ds
+ ' ' +
151 hours_str
+ ':' + mins_str
+ ':' + ut_str
;
153 return hours_str
+ ':' + mins_str
+ ':' + ut_str
;
157 format_subscription_level : function ( level
) {
160 } else if ( level
=== 'b' ) {
162 } else if ( level
=== 's' ) {
164 } else if ( level
=== 'p' ) {
167 return Proxmox
. Utils
. noneText
;
171 compute_min_label_width : function ( text
, width
) {
173 if ( width
=== undefined ) { width
= 100 ; }
175 var tm
= new Ext
. util
. TextMetrics ();
176 var min
= tm
. getWidth ( text
+ ':' );
178 return min
< width
? width
: min
;
182 return ( Proxmox
. UserName
!== '' ) && Ext
. util
. Cookies
. get ( Proxmox
. Setup
. auth_cookie_name
);
185 authClear : function () {
186 Ext
. util
. Cookies
. clear ( Proxmox
. Setup
. auth_cookie_name
);
189 // comp.setLoading() is buggy in ExtJS 4.0.7, so we
190 // use el.mask() instead
191 setErrorMask : function ( comp
, msg
) {
200 el
. mask ( gettext ( "Loading..." ));
207 monStoreErrors : function ( me
, store
, clearMaskBeforeLoad
) {
208 if ( clearMaskBeforeLoad
) {
209 me
. mon ( store
, 'beforeload' , function ( s
, operation
, eOpts
) {
210 Proxmox
. Utils
. setErrorMask ( me
, false );
213 me
. mon ( store
, 'beforeload' , function ( s
, operation
, eOpts
) {
215 me
. loadCount
= 0 ; // make sure it is numeric
216 Proxmox
. Utils
. setErrorMask ( me
, true );
221 // only works with 'proxmox' proxy
222 me
. mon ( store
. proxy
, 'afterload' , function ( proxy
, request
, success
) {
226 Proxmox
. Utils
. setErrorMask ( me
, false );
231 /*jslint nomen: true */
232 var operation
= request
. _operation
;
233 var error
= operation
. getError ();
234 if ( error
. statusText
) {
235 msg
= error
. statusText
+ ' (' + error
. status
+ ')' ;
237 msg
= gettext ( 'Connection error' );
239 Proxmox
. Utils
. setErrorMask ( me
, msg
);
243 extractRequestError : function ( result
, verbose
) {
244 var msg
= gettext ( 'Successful' );
246 if (! result
. success
) {
247 msg
= gettext ( "Unknown error" );
248 if ( result
. message
) {
249 msg
= result
. message
;
251 msg
+= ' (' + result
. status
+ ')' ;
254 if ( verbose
&& Ext
. isObject ( result
. errors
)) {
256 Ext
. Object
. each ( result
. errors
, function ( prop
, desc
) {
257 msg
+= "<br><b>" + Ext
. htmlEncode ( prop
) + "</b>: " +
258 Ext
. htmlEncode ( desc
);
267 API2Request : function ( reqOpts
) {
269 var newopts
= Ext
. apply ({
270 waitMsg
: gettext ( 'Please wait...' )
273 if (! newopts
. url
. match ( /^\/api2/ )) {
274 newopts
. url
= '/api2/extjs' + newopts
. url
;
276 delete newopts
. callback
;
278 var createWrapper = function ( successFn
, callbackFn
, failureFn
) {
280 success : function ( response
, options
) {
281 if ( options
. waitMsgTarget
) {
282 if ( Proxmox
. Utils
. toolkit
=== 'touch' ) {
283 options
. waitMsgTarget
. setMasked ( false );
285 options
. waitMsgTarget
. setLoading ( false );
288 var result
= Ext
. decode ( response
. responseText
);
289 response
. result
= result
;
290 if (! result
. success
) {
291 response
. htmlStatus
= Proxmox
. Utils
. extractRequestError ( result
, true );
292 Ext
. callback ( callbackFn
, options
. scope
, [ options
, false , response
]);
293 Ext
. callback ( failureFn
, options
. scope
, [ response
, options
]);
296 Ext
. callback ( callbackFn
, options
. scope
, [ options
, true , response
]);
297 Ext
. callback ( successFn
, options
. scope
, [ response
, options
]);
299 failure : function ( response
, options
) {
300 if ( options
. waitMsgTarget
) {
301 if ( Proxmox
. Utils
. toolkit
=== 'touch' ) {
302 options
. waitMsgTarget
. setMasked ( false );
304 options
. waitMsgTarget
. setLoading ( false );
307 response
. result
= {};
309 response
. result
= Ext
. decode ( response
. responseText
);
311 var msg
= gettext ( 'Connection error' ) + ' - server offline?' ;
312 if ( response
. aborted
) {
313 msg
= gettext ( 'Connection error' ) + ' - aborted.' ;
314 } else if ( response
. timedout
) {
315 msg
= gettext ( 'Connection error' ) + ' - Timeout.' ;
316 } else if ( response
. status
&& response
. statusText
) {
317 msg
= gettext ( 'Connection error' ) + ' ' + response
. status
+ ': ' + response
. statusText
;
319 response
. htmlStatus
= msg
;
320 Ext
. callback ( callbackFn
, options
. scope
, [ options
, false , response
]);
321 Ext
. callback ( failureFn
, options
. scope
, [ response
, options
]);
326 createWrapper ( reqOpts
. success
, reqOpts
. callback
, reqOpts
. failure
);
328 var target
= newopts
. waitMsgTarget
;
330 if ( Proxmox
. Utils
. toolkit
=== 'touch' ) {
331 target
. setMasked ({ xtype
: 'loadmask' , message
: newopts
. waitMsg
} );
333 // Note: ExtJS bug - this does not work when component is not rendered
334 target
. setLoading ( newopts
. waitMsg
);
337 Ext
. Ajax
. request ( newopts
);
340 checked_command : function ( orig_cmd
) {
341 Proxmox
. Utils
. API2Request ({
342 url
: '/nodes/localhost/subscription' ,
345 failure : function ( response
, opts
) {
346 Ext
. Msg
. alert ( gettext ( 'Error' ), response
. htmlStatus
);
348 success : function ( response
, opts
) {
349 var data
= response
. result
. data
;
351 if ( data
. status
!== 'Active' ) {
353 title
: gettext ( 'No valid subscription' ),
354 icon
: Ext
. Msg
. WARNING
,
355 msg
: Proxmox
. Utils
. getNoSubKeyHtml ( data
. url
),
357 callback : function ( btn
) {
371 assemble_field_data : function ( values
, data
) {
372 if ( Ext
. isObject ( data
)) {
373 Ext
. Object
. each ( data
, function ( name
, val
) {
374 if ( values
. hasOwnProperty ( name
)) {
375 var bucket
= values
[ name
];
376 if (! Ext
. isArray ( bucket
)) {
377 bucket
= values
[ name
] = [ bucket
];
379 if ( Ext
. isArray ( val
)) {
380 values
[ name
] = bucket
. concat ( val
);
391 dialog_title : function ( subject
, create
, isAdd
) {
394 return gettext ( 'Add' ) + ': ' + subject
;
396 return gettext ( 'Create' ) + ': ' + subject
;
399 return gettext ( 'Edit' ) + ': ' + subject
;
403 network_iface_types
: {
404 eth
: gettext ( "Network Device" ),
405 bridge
: 'Linux Bridge' ,
407 OVSBridge
: 'OVS Bridge' ,
410 OVSIntPort
: 'OVS IntPort'
413 render_network_iface_type : function ( value
) {
414 return Proxmox
. Utils
. network_iface_types
[ value
] ||
415 Proxmox
. Utils
. unknownText
;
419 diskinit
: [ 'Disk' , gettext ( 'Initialize Disk with GPT' ) ],
420 vncproxy
: [ 'VM/CT' , gettext ( 'Console' ) ],
421 spiceproxy
: [ 'VM/CT' , gettext ( 'Console' ) + ' (Spice)' ],
422 vncshell
: [ '' , gettext ( 'Shell' ) ],
423 spiceshell
: [ '' , gettext ( 'Shell' ) + ' (Spice)' ],
424 qmsnapshot
: [ 'VM' , gettext ( 'Snapshot' ) ],
425 qmrollback
: [ 'VM' , gettext ( 'Rollback' ) ],
426 qmdelsnapshot
: [ 'VM' , gettext ( 'Delete Snapshot' ) ],
427 qmcreate
: [ 'VM' , gettext ( 'Create' ) ],
428 qmrestore
: [ 'VM' , gettext ( 'Restore' ) ],
429 qmdestroy
: [ 'VM' , gettext ( 'Destroy' ) ],
430 qmigrate
: [ 'VM' , gettext ( 'Migrate' ) ],
431 qmclone
: [ 'VM' , gettext ( 'Clone' ) ],
432 qmmove
: [ 'VM' , gettext ( 'Move disk' ) ],
433 qmtemplate
: [ 'VM' , gettext ( 'Convert to template' ) ],
434 qmstart
: [ 'VM' , gettext ( 'Start' ) ],
435 qmstop
: [ 'VM' , gettext ( 'Stop' ) ],
436 qmreset
: [ 'VM' , gettext ( 'Reset' ) ],
437 qmshutdown
: [ 'VM' , gettext ( 'Shutdown' ) ],
438 qmsuspend
: [ 'VM' , gettext ( 'Suspend' ) ],
439 qmresume
: [ 'VM' , gettext ( 'Resume' ) ],
440 qmconfig
: [ 'VM' , gettext ( 'Configure' ) ],
441 vzsnapshot
: [ 'CT' , gettext ( 'Snapshot' ) ],
442 vzrollback
: [ 'CT' , gettext ( 'Rollback' ) ],
443 vzdelsnapshot
: [ 'CT' , gettext ( 'Delete Snapshot' ) ],
444 vzcreate
: [ 'CT' , gettext ( 'Create' ) ],
445 vzrestore
: [ 'CT' , gettext ( 'Restore' ) ],
446 vzdestroy
: [ 'CT' , gettext ( 'Destroy' ) ],
447 vzmigrate
: [ 'CT' , gettext ( 'Migrate' ) ],
448 vzclone
: [ 'CT' , gettext ( 'Clone' ) ],
449 vztemplate
: [ 'CT' , gettext ( 'Convert to template' ) ],
450 vzstart
: [ 'CT' , gettext ( 'Start' ) ],
451 vzstop
: [ 'CT' , gettext ( 'Stop' ) ],
452 vzmount
: [ 'CT' , gettext ( 'Mount' ) ],
453 vzumount
: [ 'CT' , gettext ( 'Unmount' ) ],
454 vzshutdown
: [ 'CT' , gettext ( 'Shutdown' ) ],
455 vzsuspend
: [ 'CT' , gettext ( 'Suspend' ) ],
456 vzresume
: [ 'CT' , gettext ( 'Resume' ) ],
457 hamigrate
: [ 'HA' , gettext ( 'Migrate' ) ],
458 hastart
: [ 'HA' , gettext ( 'Start' ) ],
459 hastop
: [ 'HA' , gettext ( 'Stop' ) ],
460 srvstart
: [ 'SRV' , gettext ( 'Start' ) ],
461 srvstop
: [ 'SRV' , gettext ( 'Stop' ) ],
462 srvrestart
: [ 'SRV' , gettext ( 'Restart' ) ],
463 srvreload
: [ 'SRV' , gettext ( 'Reload' ) ],
464 cephcreatemon
: [ 'Ceph Monitor' , gettext ( 'Create' ) ],
465 cephdestroymon
: [ 'Ceph Monitor' , gettext ( 'Destroy' ) ],
466 cephcreateosd
: [ 'Ceph OSD' , gettext ( 'Create' ) ],
467 cephdestroyosd
: [ 'Ceph OSD' , gettext ( 'Destroy' ) ],
468 cephcreatepool
: [ 'Ceph Pool' , gettext ( 'Create' ) ],
469 cephdestroypool
: [ 'Ceph Pool' , gettext ( 'Destroy' ) ],
470 imgcopy
: [ '' , gettext ( 'Copy data' ) ],
471 imgdel
: [ '' , gettext ( 'Erase data' ) ],
472 download
: [ '' , gettext ( 'Download' ) ],
473 vzdump
: [ '' , gettext ( 'Backup' ) ],
474 aptupdate
: [ '' , gettext ( 'Update package database' ) ],
475 startall
: [ '' , gettext ( 'Start all VMs and Containers' ) ],
476 stopall
: [ '' , gettext ( 'Stop all VMs and Containers' ) ],
477 migrateall
: [ '' , gettext ( 'Migrate all VMs and Containers' ) ]
480 format_task_description : function ( type
, id
) {
481 var farray
= Proxmox
. Utils
. task_desc_table
[ type
];
489 var prefix
= farray
[ 0 ];
490 var text
= farray
[ 1 ];
492 return prefix
+ ' ' + id
+ ' - ' + text
;
497 format_size : function ( size
) {
498 /*jslint confusion: true */
500 var units
= [ '' , 'Ki' , 'Mi' , 'Gi' , 'Ti' , 'Pi' , 'Ei' , 'Zi' , 'Yi' ];
503 while ( size
>= 1024 && (( num
++)+ 1 ) < units
. length
) {
507 return size
. toFixed (( num
> 0 )? 2 : 0 ) + " " + units
[ num
] + "B" ;
510 render_upid : function ( value
, metaData
, record
) {
511 var type
= record
. data
. type
;
512 var id
= record
. data
. id
;
514 return Proxmox
. Utils
. format_task_description ( type
, id
);
517 render_uptime : function ( value
) {
521 if ( uptime
=== undefined ) {
529 return Proxmox
. Utils
. format_duration_long ( uptime
);
532 parse_task_upid : function ( upid
) {
535 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]+):$/ );
537 throw "unable to parse upid '" + upid
+ "'" ;
540 task
. pid
= parseInt ( res
[ 2 ], 16 );
541 task
. pstart
= parseInt ( res
[ 3 ], 16 );
542 task
. starttime
= parseInt ( res
[ 4 ], 16 );
547 task
. desc
= Proxmox
. Utils
. format_task_description ( task
. type
, task
. id
);
552 render_timestamp : function ( value
, metaData
, record
, rowIndex
, colIndex
, store
) {
553 var servertime
= new Date ( value
* 1000 );
554 return Ext
. Date
. format ( servertime
, 'Y-m-d H:i:s' );
557 openXtermJsViewer : function ( vmtype
, vmid
, nodename
, vmname
) {
558 var url
= Ext
. urlEncode ({
559 console
: vmtype
, // kvm, lxc, upgrade or shell
565 var nw
= window
. open ( "?" + url
, '_blank' , 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width=800,height=420' );
572 constructor : function () {
574 Ext
. apply ( me
, me
. utilities
);
576 var IPV4_OCTET
= "(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])" ;
577 var IPV4_REGEXP
= "(?:(?:" + IPV4_OCTET
+ " \\ .){3}" + IPV4_OCTET
+ ")" ;
578 var IPV6_H16
= "(?:[0-9a-fA-F]{1,4})" ;
579 var IPV6_LS32
= "(?:(?:" + IPV6_H16
+ ":" + IPV6_H16
+ ")|" + IPV4_REGEXP
+ ")" ;
582 me
. IP4_match
= new RegExp ( "^(?:" + IPV4_REGEXP
+ ")$" );
583 me
. IP4_cidr_match
= new RegExp ( "^(?:" + IPV4_REGEXP
+ ")\/([0-9]{1,2})$" );
585 var IPV6_REGEXP
= "(?:" +
586 "(?:(?:" + "(?:" + IPV6_H16
+ ":){6})" + IPV6_LS32
+ ")|" +
587 "(?:(?:" + "::" + "(?:" + IPV6_H16
+ ":){5})" + IPV6_LS32
+ ")|" +
588 "(?:(?:(?:" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){4})" + IPV6_LS32
+ ")|" +
589 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,1}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){3})" + IPV6_LS32
+ ")|" +
590 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,2}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){2})" + IPV6_LS32
+ ")|" +
591 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,3}" + IPV6_H16
+ ")?::" + "(?:" + IPV6_H16
+ ":){1})" + IPV6_LS32
+ ")|" +
592 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,4}" + IPV6_H16
+ ")?::" + ")" + IPV6_LS32
+ ")|" +
593 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,5}" + IPV6_H16
+ ")?::" + ")" + IPV6_H16
+ ")|" +
594 "(?:(?:(?:(?:" + IPV6_H16
+ ":){0,7}" + IPV6_H16
+ ")?::" + ")" + ")" +
597 me
. IP6_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ ")$" );
598 me
. IP6_cidr_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ ")\/([0-9]{1,3})$" );
599 me
. IP6_bracket_match
= new RegExp ( "^ \\ [(" + IPV6_REGEXP
+ ") \\ ]" );
601 me
. IP64_match
= new RegExp ( "^(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ ")$" );
603 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])?))" ;
604 me
. DnsName_match
= new RegExp ( "^" + DnsName_REGEXP
+ "$" );
606 me
. HostPort_match
= new RegExp ( "^(" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ")(: \\ d+)?$" );
607 me
. HostPortBrackets_match
= new RegExp ( "^ \\ [(?:" + IPV6_REGEXP
+ "|" + IPV4_REGEXP
+ "|" + DnsName_REGEXP
+ ") \\ ](: \\ d+)?$" );
608 me
. IP6_dotnotation_match
= new RegExp ( "^" + IPV6_REGEXP
+ "( \\ . \\ d+)?$" );