diff --git a/src/modules/pages/connection/connection.ts b/src/modules/pages/connection/connection.ts index 6b1094c..f06dcc5 100644 --- a/src/modules/pages/connection/connection.ts +++ b/src/modules/pages/connection/connection.ts @@ -24,24 +24,13 @@ export class Connection extends BI.Widget { title: HTapeLayout; watch = { - connectionSelected: (name: string) => { - if (name) { - const canEdit = connectionCanEdit(this.model.connectionSelectedOne); - const type = this.getSelectConnectionType(); - this.connectionTitleWidget.setText(`${name}(${getTextByDatabaseType(type)})`); - this.connectionEditWidget.setVisible(canEdit); - const hasRegistered = this.hasRegistered(); - this.title.setVisible(hasRegistered); - if (!hasRegistered) { - this.listView.populate(BI.createItems(this.renderNoRegistered())); - } else { - this.listView.populate(BI.createItems(this.renderItems())); - } - } else { - this.listView.populate(BI.createItems(this.renderEmpty())); - this.connectionTitleWidget.setText(''); - this.connectionEditWidget.setVisible(false); - } + connectionSelected: { + immediate: true, + handler: (name: string) => { + BI.nextTick(() => { + name ? this.renderConnectionListView(name) : this.renderEmptyListView(); + }); + }, }, }; @@ -50,8 +39,6 @@ export class Connection extends BI.Widget { } render() { - this.store.setConnectionSelected(''); - return { type: BI.HTapeLayout.xtype, hgap: 10, @@ -214,12 +201,6 @@ export class Connection extends BI.Widget { }]; } - private renderEmpty() { - return [{ - type: BI.Layout.xtype, - }]; - } - private hasRegistered() { const allDatabaseTypes = getAllDatabaseTypes(); switch (this.model.connectionSelectedOne.connectionType) { @@ -246,4 +227,32 @@ export class Connection extends BI.Widget { this.store.setPageIndex(PAGE_INDEX.MAINTAIN); this.store.setDatebaseTypeSelected(''); } + + private renderConnectionListView(name: string) { + const canEdit = connectionCanEdit(this.model.connectionSelectedOne), + type = this.getSelectConnectionType(), + hasRegistered = this.hasRegistered(); + + this.connectionTitleWidget.setText(`${name}(${getTextByDatabaseType(type)})`); + this.connectionEditWidget.setVisible(canEdit); + + this.title.setVisible(hasRegistered); + + hasRegistered + ? this.listView.populate(BI.createItems(this.renderItems())) + : this.listView.populate(BI.createItems(this.renderNoRegistered())); + } + + private renderEmptyListView() { + this.listView.populate( + BI.createItems([ + { + type: BI.Layout.xtype, + } + ]) + ); + + this.connectionTitleWidget.setText(''); + this.connectionEditWidget.setVisible(false); + } } diff --git a/src/modules/pages/connection/list/list.model.ts b/src/modules/pages/connection/list/list.model.ts index ebabce6..a4bf602 100644 --- a/src/modules/pages/connection/list/list.model.ts +++ b/src/modules/pages/connection/list/list.model.ts @@ -38,14 +38,19 @@ export class ConnectionListModel extends Model<{ } }); - this.model.connectionSelected = defaultDatabaseName ?? data.data[0].connectionName; - } else { - this.model.connectionSelected = ''; + // 仅首次进入时从url中读取参数,其他情况保留选中状态 + defaultDatabaseName ||= data.data[0].connectionName; + + this.setSelectedConnection(this.model.connectionSelected || defaultDatabaseName); } return new Promise(resolve => { resolve(); }); }), + + setSelectedConnection(name: string) { + this.model.connectionSelected = name; + } } } diff --git a/src/modules/pages/connection/list/list_item/list_item.model.ts b/src/modules/pages/connection/list/list_item/list_item.model.ts index f23de66..fa43a36 100644 --- a/src/modules/pages/connection/list/list_item/list_item.model.ts +++ b/src/modules/pages/connection/list/list_item/list_item.model.ts @@ -139,8 +139,9 @@ export class ListItemModel extends Model<{ // 后端传过来的是字符串,转为对象 item.connectionData = JSON.parse(item.connectionData as string); }); + if (name === this.model.connectionSelected) { - this.model.connectionSelected = BI.size(this.model.connections) > 0 ? this.model.connections[0].connectionName : ''; + this.setConnectionSelected(this.model.connections?.[0]?.connectionName || ''); } api.shutdownConnectionStatus(name); }); diff --git a/src/modules/pages/maintain/forms/form.model.ts b/src/modules/pages/maintain/forms/form.model.ts index 3bbc66e..7947b15 100644 --- a/src/modules/pages/maintain/forms/form.model.ts +++ b/src/modules/pages/maintain/forms/form.model.ts @@ -17,6 +17,7 @@ export class MaintainFormModel extends Model<{ pageIndex: AppModel['TYPE']['pageIndex']; connections: AppModel['TYPE']['connections']; isCopy: AppModel['TYPE']['isCopy']; + connectionSelected: AppModel['TYPE']['connectionSelected']; }, context: MaintainFormModel['context']; }> { @@ -31,6 +32,7 @@ export class MaintainFormModel extends Model<{ 'testEvent', 'connections', 'isCopy', + 'connectionSelected', ]; actions = { @@ -51,5 +53,8 @@ export class MaintainFormModel extends Model<{ goFirstPage() { this.model.pageIndex = PAGE_INDEX.CONNECTION; }, + setConnectionSelected(name: string) { + this.model.connectionSelected = name; + } } } diff --git a/src/modules/pages/maintain/forms/form.ts b/src/modules/pages/maintain/forms/form.ts index c989f1c..2e6248b 100644 --- a/src/modules/pages/maintain/forms/form.ts +++ b/src/modules/pages/maintain/forms/form.ts @@ -295,6 +295,8 @@ export class MaintainForm extends BI.Widget { return; } + // 新增之后connections待更新,connectionSelected先置空 + this.store.setConnectionSelected(''); this.store.goFirstPage(); this.store.setIsCopy(false); });