2015-07-03 11:29:11 +02:00
Ext . define ( 'PVE.form.ControllerSelector' , {
extend : 'Ext.form.FieldContainer' ,
2016-07-21 11:10:43 +02:00
alias : 'widget.pveControllerSelector' ,
2015-07-03 11:29:11 +02:00
statics : {
maxIds : {
ide : 3 ,
sata : 5 ,
virtio : 15 ,
scsi : 13
}
} ,
noVirtIO : false ,
vmconfig : { } , // used to check for existing devices
2016-10-06 11:01:26 +02:00
sortByPreviousUsage : function ( vmconfig , controllerList ) {
var usedControllers = Ext . clone ( PVE . form . ControllerSelector . maxIds ) ;
var type ;
for ( type in usedControllers ) {
if ( usedControllers . hasOwnProperty ( type ) ) {
usedControllers [ type ] = 0 ;
}
}
var property ;
for ( property in vmconfig ) {
if ( vmconfig . hasOwnProperty ( property ) ) {
if ( property . match ( PVE . Utils . bus _match ) && ! vmconfig [ property ] . match ( /media=cdrom/ ) ) {
var foundController = property . match ( PVE . Utils . bus _match ) [ 1 ] ;
usedControllers [ foundController ] ++ ;
}
2016-10-07 08:12:52 +02:00
}
2016-10-06 11:01:26 +02:00
}
2016-10-10 14:32:50 +02:00
var vmDefaults = PVE . qemu . OSDefaults [ vmconfig . ostype ] ;
var sortPriority = vmDefaults && vmDefaults . busPriority
? vmDefaults . busPriority : PVE . qemu . OSDefaults . generic ;
2016-10-06 11:01:26 +02:00
2016-10-07 08:12:52 +02:00
var sortedList = Ext . clone ( controllerList ) ;
sortedList . sort ( function ( a , b ) {
if ( usedControllers [ b ] == usedControllers [ a ] ) {
return sortPriority [ b ] - sortPriority [ a ] ;
}
return usedControllers [ b ] - usedControllers [ a ] ;
} ) ;
return sortedList ;
2016-10-06 11:01:26 +02:00
} ,
2015-07-03 11:29:11 +02:00
setVMConfig : function ( vmconfig , autoSelect ) {
var me = this ;
me . vmconfig = Ext . apply ( { } , vmconfig ) ;
2017-10-02 10:37:30 +02:00
var clist = [ 'ide' , 'virtio' , 'scsi' , 'sata' ] ;
2017-10-02 10:37:32 +02:00
var bussel = me . down ( 'field[name=controller]' ) ;
var deviceid = me . down ( 'field[name=deviceid]' ) ;
2017-10-02 10:37:30 +02:00
if ( autoSelect === 'cdrom' ) {
clist = [ 'ide' , 'scsi' , 'sata' ] ;
if ( ! Ext . isDefined ( me . vmconfig . ide2 ) ) {
2017-10-02 10:37:32 +02:00
bussel . setValue ( 'ide' ) ;
deviceid . setValue ( 2 ) ;
2017-10-02 10:37:30 +02:00
return ;
2015-07-03 11:29:11 +02:00
}
2017-10-02 10:37:30 +02:00
} else {
// in most cases we want to add a disk to the same controller
// we previously used
clist = me . sortByPreviousUsage ( me . vmconfig , clist ) ;
}
2015-07-03 11:29:11 +02:00
2017-10-02 10:37:30 +02:00
Ext . Array . each ( clist , function ( controller ) {
var confid , i ;
2017-10-02 10:37:32 +02:00
bussel . setValue ( controller ) ;
2017-10-02 10:37:30 +02:00
for ( i = 0 ; i <= PVE . form . ControllerSelector . maxIds [ controller ] ; i ++ ) {
confid = controller + i . toString ( ) ;
if ( ! Ext . isDefined ( me . vmconfig [ confid ] ) ) {
2017-10-02 10:37:32 +02:00
deviceid . setValue ( i ) ;
2017-10-02 10:37:30 +02:00
return false ; // break
2015-07-03 11:29:11 +02:00
}
2017-10-02 10:37:30 +02:00
}
} ) ;
2017-10-02 10:37:32 +02:00
deviceid . validate ( ) ;
2015-07-03 11:29:11 +02:00
} ,
initComponent : function ( ) {
var me = this ;
Ext . apply ( me , {
fieldLabel : gettext ( 'Bus/Device' ) ,
layout : 'hbox' ,
defaults : {
hideLabel : true
} ,
items : [
{
2016-07-21 11:10:43 +02:00
xtype : 'pveBusSelector' ,
2015-07-03 11:29:11 +02:00
name : 'controller' ,
2016-07-21 11:10:45 +02:00
value : PVE . qemu . OSDefaults . generic . busType ,
2015-07-03 11:29:11 +02:00
noVirtIO : me . noVirtIO ,
allowBlank : false ,
2017-08-29 12:08:25 +02:00
flex : 2 ,
2015-07-03 11:29:11 +02:00
listeners : {
change : function ( t , value ) {
2018-01-30 10:34:14 +01:00
if ( ! value ) {
2015-07-03 11:29:11 +02:00
return ;
}
var field = me . down ( 'field[name=deviceid]' ) ;
field . setMaxValue ( PVE . form . ControllerSelector . maxIds [ value ] ) ;
field . validate ( ) ;
}
}
} ,
{
2018-01-15 15:17:52 +01:00
xtype : 'proxmoxintegerfield' ,
2015-07-03 11:29:11 +02:00
name : 'deviceid' ,
minValue : 0 ,
maxValue : PVE . form . ControllerSelector . maxIds . ide ,
value : '0' ,
2017-08-29 12:08:25 +02:00
flex : 1 ,
2018-01-15 15:17:52 +01:00
allowBlank : false ,
2015-07-03 11:29:11 +02:00
validator : function ( value ) {
/*jslint confusion: true */
if ( ! me . rendered ) {
return ;
}
var field = me . down ( 'field[name=controller]' ) ;
var controller = field . getValue ( ) ;
var confid = controller + value ;
if ( Ext . isDefined ( me . vmconfig [ confid ] ) ) {
return "This device is already in use." ;
}
return true ;
}
}
]
} ) ;
me . callParent ( ) ;
}
} ) ;