diff --git a/packages/command-services/lib/navigation-event.service.ts b/packages/command-services/lib/navigation-event.service.ts index 08c2bd7cb9022e396f2d8758925dcab18dad07ed..7aca5ed3d83fea9958d14e39060d8736b874a175 100644 --- a/packages/command-services/lib/navigation-event.service.ts +++ b/packages/command-services/lib/navigation-event.service.ts @@ -1,6 +1,6 @@ import { RuntimeFrameworkService } from './rtf.service'; import { QuerystringService } from './querystring.service'; -import { TAB_EVENT } from './types'; +import { TAB_EVENT, TabClosingConfirmResult } from './types'; import { CommandContext, Token } from '@farris/devkit-vue'; import { NavigationHistoryService } from './navigation-history.service'; @@ -10,16 +10,14 @@ export class NavigationEventService { * 命令上下文 */ public commandContext: CommandContext; - /** * 关闭后事件处理器 */ private onClosedListeners: Map void>; - /** * 关闭前处理器 */ - private onClosingListeners: Map Promise>; + private onClosingListeners: Map Promise>; /** * 切换事件处理器 */ @@ -51,7 +49,7 @@ export class NavigationEventService { private navigationHistoryService: NavigationHistoryService ) { this.onClosedListeners = new Map void>(); - this.onClosingListeners = new Map Promise>(); + this.onClosingListeners = new Map Promise>(); this.onTabSwitchListeners = new Map void>(); this.onTabRefreshListeners = new Map void>(); this.registerEvent(); @@ -138,21 +136,23 @@ export class NavigationEventService { } const listeners = Array.from(this.onClosingListeners.values()); - // 用户拒绝 - let userRejected = false; + // 用户已确认(点击了确定或取消) + let userConfirmed = false; + let userConfirmResult: boolean | undefined; const resultPromise = listeners.reduce((promiseChain: Promise, handle) => { - return promiseChain.then(chainResults => { + return promiseChain.then((chainResults: TabClosingConfirmResult[]) => { - // 如果用户已经拒绝,则跳过后续的处理 - if (userRejected) { - return chainResults; - } + const extraOptions = {userConfirmed, userConfirmResult}; // 处理当前的 handle,只取第一个结果 - return handle(e).then(result => { + return handle(e, extraOptions).then((result: TabClosingConfirmResult) => { - // 如果用户拒绝,则设置标志 - userRejected = !result; + // 如果用户点击了确定或取消,则终止 + userConfirmed = result.confirmType === 'User'; + if (result.confirmType === 'User') { + userConfirmed = true; + userConfirmResult = result.confirmResult; + } // 将结果添加到结果链中 return [...chainResults, result]; @@ -162,10 +162,12 @@ export class NavigationEventService { }, Promise.resolve([])); // 如果所有用户返回true,则关闭标签页 - return resultPromise.then(chainResults => { + return resultPromise.then((chainResults: TabClosingConfirmResult[]) => { // 检查是否所有的结果都是 true - return chainResults.every((result: any) => result); + return chainResults.every((result: TabClosingConfirmResult) => { + return result.confirmResult === true; + }); }); } diff --git a/packages/command-services/lib/navigation-middleware.service.ts b/packages/command-services/lib/navigation-middleware.service.ts index 5653a232bf395e2e7404031983e2b8586bd24cf0..9dce5cc79c55a110764c55cd8cf94c48263bd5c4 100644 --- a/packages/command-services/lib/navigation-middleware.service.ts +++ b/packages/command-services/lib/navigation-middleware.service.ts @@ -34,12 +34,27 @@ export class NavigationMiddlewareService implements IDisposable { * 菜单关闭前 */ public onClosing() { - const listenerToken = this.navigationService.addEventListener(TAB_EVENT.onTabClosing, (options) => { + const listenerToken = this.navigationService.addEventListener(TAB_EVENT.onTabClosing, (options: any, extraOptions?: any) => { + + // 如果用户已经手工确认了是否关闭菜单,以用户选择为准,不再重复确认 + if (extraOptions && extraOptions.userConfirmed) { + + // 用户已经确定不关闭菜单 + if (extraOptions.userConfirmResult === false) { + return Promise.resolve({confirmResult:false, confirmType: 'User'}); + } else { + + // 用户已经确定关闭菜单 + return this.cancelDataService.cancel(false).then(() => { + return {confirmResult: true, confirmType: 'User'}; + }); + } + } // 判断是否已经有关闭前的确认框 const isConfirming = this.viewModel.getModule().getContext().getParam('ON_CLOSING_CONFIRM') || false; if (isConfirming) { - return Promise.resolve(false); + return Promise.resolve({confirmResult:false, confirmType: 'User'}); } // 检测变更前先结束编辑 @@ -59,20 +74,26 @@ export class NavigationMiddlewareService implements IDisposable { // 如果用户选择了确定,执行取消并关闭Tab关闭 if (result) { - return this.cancelDataService.cancel(false).then(() => true); + return this.cancelDataService.cancel(false).then(() => { + return {confirmResult:true, confirmType: 'User'}; + }); } // 否则返回false,阻止Tab页关闭 - return Promise.resolve(false); + return Promise.resolve({confirmResult:false, confirmType: 'User'}); }); } else { // 未检测到变更,关闭Tab页 - return Promise.resolve(true); + return Promise.resolve({confirmResult:true, confirmType: 'NoChange'}); } }).catch((error) => { return this.formMessageService.confirm(this.languageService.language.hasChangeCheckFaild).then((result: boolean) => { - return Promise.resolve(result); + if (result) { + return Promise.resolve({confirmResult:true, confirmType: 'User'}); + } else { + return Promise.resolve( {confirmResult:false, confirmType: 'User'}); + } }); }); }); diff --git a/packages/command-services/lib/navigation.service.ts b/packages/command-services/lib/navigation.service.ts index 6938793ee81054fd3b23797c8dc5c28aaddd4aa3..169d176f71fd0c496930a53e014fe19f21b31d06 100644 --- a/packages/command-services/lib/navigation.service.ts +++ b/packages/command-services/lib/navigation.service.ts @@ -251,7 +251,7 @@ export class NavigationService { * @param handler * @returns */ - public addEventListener(eventType: string, handler: (options: any) => any): string | null { + public addEventListener(eventType: string, handler: (options: any, extraOptions?: any) => any): string | null { return this.navigationEventService.addEventListener(eventType, handler); } diff --git a/packages/command-services/lib/types.ts b/packages/command-services/lib/types.ts index 4227e18c14f715e1513693bdf9ddf9af59c89f67..0c1d787b0de96e57e88acef8331f528477183b51 100644 --- a/packages/command-services/lib/types.ts +++ b/packages/command-services/lib/types.ts @@ -7,21 +7,36 @@ export const AppType = { }; export const TAB_EVENT = { + /** * Tab关闭后 */ onTabClosed: 'FuncClosed', + /** * Tab关闭前 */ onTabClosing: 'beforeFuncCloseEvent', + /** * Tab切换 */ onTabSwitched: 'funcSwitchEvent', + + /** + * Tab刷新 + */ onTabRefresh: 'tabRefresh' }; +/** + * 标签页关闭前确认结果 + */ +export interface TabClosingConfirmResult { + confirmResult: boolean; + confirmType: 'User' | 'NoChange' +} + export const TAB_QUERY_STRING = { TabId: 'tabId', AppType: 'appType',