import { Subscription } from 'rxjs'; import { PickingResult } from '../lookup-grid-options'; import { LookupGridComponent } from '../lookup-grid.component'; export class LookupDialogManager { private lookupInit: Subscription = null; private _loadDataWhenOpen = true; private _navSelectedId = ''; private _selectFirstInNav = false; private keyDownHandler = null; constructor(private ins: LookupGridComponent) { } // 帮助窗口关闭后做一些清理工作 dialogClosed() { if (this.ins.displayText !== this.ins.originalText && !this.ins.nosearch) { this.ins.displayText = this.ins.originalText; this.ins.setModelValue(this.ins.displayText); } if (this.ins.componentRef) { this.ins.componentRef.destroy(); this.ins.componentRef = null; } if (this.ins.favoritesComponentRef) { this.ins.favoritesComponentRef.destroy(); this.ins.favoritesComponentRef = null; } if (this.ins.contentContainer) { this.ins.contentContainer.clear(); } if (this.ins.centerContainer) { this.ins.centerContainer.clear(); } if (this.ins.leftComponentRef) { this.ins.leftComponentRef.destroy(); this.ins.leftComponentRef = null; } if (this.ins.leftContainer) { this.ins.leftContainer.clear(); } this.ins.isShow = false; this.ins.isTextChange = false; if (this.ins.dialog) { this.ins.content = null; } this.ins.navigationFilter = null; this.ins.lookupUtils.pendingEnd(); if (this.ins.helpId) { this.ins.displayType = ''; } if (this.lookupInit) { this.lookupInit.unsubscribe(); this.lookupInit = null; } // this.ins.items = []; this.ins.favoriteItems = []; this.ins.isTabChanged = false; if (this.ins.uri) { this.ins.items = []; } this.ins._searchState = null; this.ins.pageIndex = 1; this.ins.loadDataWhenOpen = this._loadDataWhenOpen; this.ins.navSelectedIds = this._navSelectedId; this.ins.selectFirstInNav = this._selectFirstInNav; this.ins.isGetAllChidlNodes = false; this.ins.enableGetAllChildNodes = true; // 保存个性化数据 if (this.ins.usePersionalConf && this.ins.favoriteDataFrom !== 'locale') { this.ins.httpMgr.submitFavoriteData('dialog closed event.'); } if (this.keyDownHandler) { this.keyDownHandler(); this.keyDownHandler = null; } if (this.ins.inputGroup) { this.ins.inputGroup.focus(); } this.ins.userInitialConfig.reset(); this.ins.lookupUtils.destroy(); this.ins.dialogClosed.emit(); } onDialogCreated() { this._loadDataWhenOpen = this.ins.loadDataWhenOpen; this._navSelectedId = this.ins.navSelectedIds; this._selectFirstInNav = this.ins.selectFirstInNav; this.ins.dialogCreatedSubscription = this.ins.dialogCreated.subscribe(dlg => { if (dlg) { if (this.ins.dialogOpenedSubscription) { this.ins.dialogOpenedSubscription.unsubscribe(); } if (this.ins.dialogClosedSubscription) { this.ins.dialogClosedSubscription.unsubscribe(); } this.registerDialogEvent(); if (this.ins.isRecordSize) { const dlgSize = this.ins.personalConfigService.getDialogSize(); if (dlgSize) { const { width, height } = dlgSize; this.ins.dialogWidth = width ? width : this.ins.dialogWidth; this.ins.dialogHeight = height ? height : this.ins.dialogHeight; // 20200908 更新现窗口的尺寸 by Lucas dlg.width = this.ins.dialogWidth; dlg.height = this.ins.dialogHeight; } } dlg.show(); } }); } canOpenDialog(pr: PickingResult | any) { let o = true; if (pr === undefined || pr === null || pr === '') { o = true; } if (typeof pr === 'boolean') { o = pr; } if (typeof pr === 'object') { if (pr.showDialog === undefined) { o = true; } else { o = pr.showDialog; } if (pr.hasOwnProperty('data')) { /** 保存帮助前传递的数据 */ this.ins.customData = pr.data; } this.ins.selectedIds = pr.selectedIds || null; if (pr.message) { this.ins.notifyService.warning(pr.message); } } this.ins.isReady = false; this.ins.isShow = o; } getHeight() { return this.ins.dialog.size.contentHeight - this.ins.containerMargin.bottom - this.ins.containerMargin.top - (this.ins.useFavorite ? 40 : 0); } private getMainGridSize() { if (this.ins.isDoublleList()) { return { width: this.ins.dialog.size.width - this.ins.leftPanel.width - 27, height: this.getHeight() }; } return { width: this.ins.dialog.size.width - this.ins.containerMargin.left - this.ins.containerMargin.right, height: this.getHeight() }; } /** 注册弹出窗口的事件 */ private registerDialogEvent() { this.ins.dialogOpenedSubscription = this.ins.dialog.opened.subscribe(() => { this.ins.gridOptions = Object.assign(this.ins.gridOptions, this.getMainGridSize()); this.ins.dialog.el.nativeElement.querySelector('.ps-content').style.height = '100%'; if (this.ins.displayType && this.ins.customDisplayType) { this.ins.componentRef = this.ins.lookupCmpMgr.createContent(this.ins.gridOptions); this.ins.lookupCmpMgr.createFavoriteComponent(); } this.ins.initData(); // 修改帮助窗口的状态 this.ins.lookupUtils.pendingEnd(); this.ins.dialogOpened.emit(); }); this.ins.subscriptions.push(this.ins.dialogOpenedSubscription); this.lookupInit = this.ins.lookupinitializationSubject.subscribe(() => { this.ins.loadDataWhenOpen = true; // 注册快捷键 this.registerShortcutKey(); // 监听左侧尺寸变化事件 if (this.ins.leftPanel) { const leftPanelResize$ = this.ins.leftPanel.resizing.subscribe((s: any) => { this.ins.componentRef.instance.resize({ width: this.ins.dialog.size.width - s.size.width - 27, height: this.getHeight() }); this.ins.leftComponentRef.instance.resize(s.size); }); this.ins.subscriptions.push(leftPanelResize$); } }); this.ins.dialogClosedSubscription = this.ins.dialog.closed.subscribe(() => { // 输入框变化后,弹出窗口未选择数据关闭窗口时,还原原始值 this.ins.dialogMgr.dialogClosed(); }); this.ins.subscriptions.push(this.ins.dialogClosedSubscription); } private registerShortcutKey() { // 回车,与确定按钮处理逻辑一至。 const dlgContainerDom = this.ins.dialog.el.nativeElement.querySelector('.farris-modal'); if (dlgContainerDom && this.ins.shortcutKey && !this.keyDownHandler) { this.keyDownHandler = this.ins.eventManager.addEventListener(dlgContainerDom, 'keydown', (e: KeyboardEvent) => { e.stopPropagation(); const { moveUp, moveDown, searchFocus, confirm, nextPager, prevPager, expand, collapse } = this.ins.shortcutKey; const arrowKey = [moveUp, moveDown, expand, collapse]; if (arrowKey.includes(e.code)) { this.ins.componentRef.instance.onKeydownEvent(e); } else if (e.key === confirm) { if (e.target['nodeName'] === 'INPUT' && !e.ctrlKey) { return; } this.ins.okButton.nativeElement.click(); } else if (e.code === searchFocus) { // 帮助窗口查询输入框焦点 e.preventDefault(); this.ins.componentRef.instance.inputGroup.focus(); } else if (!this.ins.isTree() && (e.code === nextPager || e.code === prevPager )) { // 分页 const isNextPager = e.code === nextPager; this.paginationKeyDownHandler(isNextPager); } }); } } private paginationKeyDownHandler(next = true) { const datatableRef = this.ins.componentRef.instance; const {pageIndex, pageSize, total} = datatableRef; const pagerCount = Math.ceil(total / pageSize); let newPageNum = pageIndex; if (next) { newPageNum = newPageNum + 1; } else { newPageNum = newPageNum - 1; } if (newPageNum > pagerCount || newPageNum < 1) { newPageNum = pageIndex; } this.ins.componentRef.instance.onPageChange({ pageSize, pageIndex: newPageNum }); } }