You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.7 KiB
133 lines
4.7 KiB
import { model, Model } from '@core/core'; |
|
import { ConnectionJDBC } from '../../../../crud/crud.typings'; |
|
import { getJdbcDatabaseType } from '../../../../app.service'; |
|
import { ApiFactory } from '../../../../crud/apiFactory'; |
|
|
|
const api = new ApiFactory().create(); |
|
|
|
@model() |
|
export class DriverSelectorModel extends Model { |
|
static xtype = 'dec.dcm.model.maintain.form.jdbc.driver_selector'; |
|
|
|
state = () => { |
|
const defaultDrivers = this.getDrivers(); |
|
const [driverSource, selectedDriver] = this.resolveSelectedDriverType(); |
|
|
|
return { |
|
defaultDrivers, |
|
driverSource, |
|
selectedDriverType: driverSource === '' ? 'default' : 'custom', |
|
customDrivers: [], |
|
defaultDriver: { |
|
driver: driverSource === '' ? selectedDriver : '', |
|
}, |
|
customDriver: { |
|
driver: driverSource !== '' ? selectedDriver : '', |
|
value: driverSource !== '' ? `${this.options.driver} (${driverSource})` : '', |
|
}, |
|
}; |
|
}; |
|
|
|
computed = { |
|
driverClassItems: () => this.model.customDrivers.map(driver => { |
|
return { |
|
text: `${driver.driverClass} (${driver.name})`, |
|
value: `${driver.driverClass} (${driver.name})`, |
|
driverClass: driver.driverClass, |
|
}; |
|
}), |
|
|
|
driverTypeComboValue: () => this.model.driverSource === '' ? 'default' : 'custom', |
|
|
|
driverManageEntryVisible: () => this.model.selectedDriverType === 'custom' && BI.Services.getService('dec.service.global').isAdmin(), |
|
}; |
|
|
|
actions = { |
|
initDriverClassList: cb => { |
|
|
|
api.getSimpleDriverList().then(res => { |
|
this.model.customDrivers = res.data.filter(driver => { |
|
return BI.isKey(driver.driverClass); |
|
}); |
|
cb(); |
|
}); |
|
}, |
|
|
|
changeDefaultDriver: driver => { |
|
this.model.defaultDriver.driver = driver; |
|
this.model.driverSource = ''; |
|
}, |
|
|
|
changeCustomDriver: value => { |
|
|
|
const item = this.model.driverClassItems.find(item => { |
|
return item.value === value; |
|
}); |
|
const driver = item.driverClass; |
|
this.model.customDriver.driver = driver; |
|
|
|
this.model.customDrivers.some(customDriver => { |
|
// DEC-21469 存在driver值相同但driver名不同的场景,因此要用拼接名判断 |
|
if (`${customDriver.driverClass} (${customDriver.name})` === value) { |
|
this.model.driverSource = customDriver.name; |
|
this.model.customDriver.value = `${driver} (${customDriver.name})`; |
|
|
|
return true; |
|
} |
|
|
|
return false; |
|
}); |
|
}, |
|
|
|
changeSelectedDriverType: driverTypeComboValue => { |
|
this.model.selectedDriverType = driverTypeComboValue; |
|
this.model.driverSource = driverTypeComboValue === 'default' ? '' : this.model.driverSource; |
|
}, |
|
|
|
changeDriverSource: driverTypeComboValue => { |
|
this.model.driverSource = driverTypeComboValue === 'default' ? '' : this.model.driverSource; |
|
}, |
|
|
|
setDefaultDrivers: version => { |
|
const defaultDrivers = this.getDrivers(version); |
|
this.model.defaultDrivers = defaultDrivers; |
|
this.changeDefaultDriver(defaultDrivers[0]?.value); |
|
} |
|
}; |
|
|
|
private resolveSelectedDriverType = () => { |
|
if (BI.isNotEmptyString(this.options.driverSource)) { |
|
return [this.options.driverSource, this.options.driver]; |
|
} |
|
|
|
return [this.options.driverSource, this.options.driver]; |
|
}; |
|
|
|
private getDrivers = (version?: string) => { |
|
const connectionData = this.options.connectionData as ConnectionJDBC; |
|
const connectionType = getJdbcDatabaseType(connectionData.database, connectionData.driver); |
|
const selectedVersion = version ?? this.options.version; |
|
const drivers = connectionType.drivers ? |
|
(BI.isUndefined(connectionType.versions) ? connectionType.drivers : connectionType.drivers[selectedVersion]).map(item => { |
|
return { |
|
text: item, |
|
value: item, |
|
}; |
|
}) : |
|
[{ |
|
text: connectionType.driver, |
|
value: connectionType.driver, |
|
}]; |
|
if (BI.isUndefined(connectionType.versions) && !drivers.some(item => item.text === connectionData.driver)) { |
|
return [ |
|
{ |
|
text: connectionData.driver, |
|
value: connectionData.driver, |
|
}, |
|
...drivers, |
|
]; |
|
} |
|
|
|
return drivers; |
|
}; |
|
}
|
|
|