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.
56 lines
2.2 KiB
56 lines
2.2 KiB
import { model, Model } from '@core/core'; |
|
import { ApiFactory } from '../../..//crud/apiFactory'; |
|
import { AppModel } from '../../../app.model'; |
|
import type { ConnectionJDBC } from '../../../crud/crud.typings'; |
|
const api = new ApiFactory().create(); |
|
@model() |
|
export class ConnectionListModel extends Model<{ |
|
types: { |
|
connections: AppModel['TYPE']['connections']; |
|
connectionSelected: AppModel['TYPE']['connectionSelected']; |
|
}, |
|
context: ConnectionListModel['context']; |
|
}> { |
|
static xtype = 'dec.dcm.model.connection.list'; |
|
|
|
context = <const>['connections', 'connectionSelected']; |
|
|
|
computed = { |
|
shwoType: () => BI.size(this.model.connections) > 0 ? 'list' : 'none', |
|
} |
|
|
|
actions = { |
|
setConnections: (): Promise<void> => api.getConnectionList().then(data => { |
|
data.data.push(...BI.Constants.getConstant('dec.constant.connection.list')); |
|
if (BI.size(data.data) > 0) { |
|
this.model.connections = data.data; |
|
let defaultDatabaseName, |
|
defaultDatabaseId = BI.Services.getService("dec.service.global") |
|
.getHashSearchParams("databaseId"); |
|
|
|
this.model.connections.forEach(item => { |
|
// 后端传过来的是字符串,转为对象 |
|
BI.isString(item.connectionData) && (item.connectionData = JSON.parse(item.connectionData as string)); |
|
|
|
// 目前只有jdbc存在identity,后期拓展 |
|
if ((item.connectionData as ConnectionJDBC).identity === defaultDatabaseId) { |
|
defaultDatabaseName = item.connectionName; |
|
} |
|
}); |
|
|
|
// 仅首次进入时从url中读取参数,其他情况保留选中状态 |
|
defaultDatabaseName ||= data.data[0].connectionName; |
|
|
|
this.setSelectedConnection(this.model.connectionSelected || defaultDatabaseName); |
|
} |
|
|
|
return new Promise(resolve => { |
|
resolve(); |
|
}); |
|
}), |
|
|
|
setSelectedConnection(name: string) { |
|
this.model.connectionSelected = name; |
|
} |
|
} |
|
}
|
|
|