UNPKG

@ohayojp.com/components

Version:

Common business components of ohayojp.

1,728 lines (1,720 loc) 62.2 kB
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Input, Output, ElementRef, Injectable, Directive, Injector, ɵɵdefineInjectable, ɵɵinject, INJECTOR, ChangeDetectorRef, Optional, Inject, ViewChild, NgModule } from '@angular/core'; import { OhayoLocaleService, ScrollService, MenuService, OHAYO_I18N_TOKEN, OhayoLocaleModule } from '@ohayojp.com/theme'; import { Subject, Subscription, BehaviorSubject } from 'rxjs'; import { ConnectionPositionPair, Overlay, OverlayModule } from '@angular/cdk/overlay'; import { ComponentPortal } from '@angular/cdk/portal'; import { __decorate, __metadata } from 'tslib'; import { DOCUMENT, CommonModule } from '@angular/common'; import { ActivatedRoute, Router, ROUTER_CONFIGURATION, NavigationStart, NavigationEnd, RouterModule } from '@angular/router'; import { InputBoolean, InputNumber } from '@ohayojp.com/util'; import { NzTabsModule } from 'ng-zorro-antd/tabs'; import { takeUntil, debounceTime, filter } from 'rxjs/operators'; import { NzIconModule } from 'ng-zorro-antd/icon'; import { NzMenuModule } from 'ng-zorro-antd/menu'; /** * @fileoverview added by tsickle * Generated from: reuse-tab-context-menu.component.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class ReuseTcomponentsontextMenuComponent { /** * @param {?} i18nSrv */ constructor(i18nSrv) { this.i18nSrv = i18nSrv; // tslint:disable-next-line:no-output-native this.close = new EventEmitter(); } /** * @param {?} value * @return {?} */ set i18n(value) { this._i18n = Object.assign(Object.assign({}, this.i18nSrv.getData('reuseTab')), value); } /** * @return {?} */ get i18n() { return this._i18n; } /** * @return {?} */ get includeNonCloseable() { return this.event.ctrlKey; } /** * @private * @param {?} type * @return {?} */ notify(type) { this.close.next({ type, item: this.item, includeNonCloseable: this.includeNonCloseable, }); } /** * @return {?} */ ngOnInit() { if (this.includeNonCloseable) this.item.closable = true; } /** * @param {?} e * @param {?} type * @param {?=} custom * @return {?} */ click(e, type, custom) { e.preventDefault(); e.stopPropagation(); if (type === 'close' && !this.item.closable) return; if (type === 'closeRight' && this.item.last) return; if (custom) { if (this.isDisabled(custom)) return; custom.fn(this.item, custom); } this.notify(type); } /** * @param {?} custom * @return {?} */ isDisabled(custom) { return custom.disabled ? custom.disabled(this.item) : false; } /** * @param {?} event * @return {?} */ closeMenu(event) { if (event.type === 'click' && event.button === 2) return; this.notify(null); } } ReuseTcomponentsontextMenuComponent.decorators = [ { type: Component, args: [{ selector: 'reuse-tab-context-menu', template: "<ul nz-menu>\n <li nz-menu-item (click)=\"click($event, 'refresh')\" data-type=\"refresh\" [innerHTML]=\"i18n.refresh\"></li>\n <li nz-menu-item (click)=\"click($event, 'close')\" data-type=\"close\" [nzDisabled]=\"!item.closable\" [innerHTML]=\"i18n.close\"></li>\n <li nz-menu-item (click)=\"click($event, 'closeOther')\" data-type=\"closeOther\" [innerHTML]=\"i18n.closeOther\"></li>\n <li nz-menu-item (click)=\"click($event, 'closeRight')\" data-type=\"closeRight\" [nzDisabled]=\"item.last\" [innerHTML]=\"i18n.closeRight\"></li>\n <ng-container *ngIf=\"customContextMenu!.length > 0\">\n <li nz-menu-divider></li>\n <li\n *ngFor=\"let i of customContextMenu\"\n nz-menu-item\n [attr.data-type]=\"i.id\"\n [nzDisabled]=\"isDisabled(i)\"\n (click)=\"click($event, 'custom', i)\"\n [innerHTML]=\"i.title\"\n ></li>\n </ng-container>\n</ul>\n", host: { '(document:click)': 'closeMenu($event)', '(document:contextmenu)': 'closeMenu($event)', }, preserveWhitespaces: false, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None }] } ]; /** @nocollapse */ ReuseTcomponentsontextMenuComponent.ctorParameters = () => [ { type: OhayoLocaleService } ]; ReuseTcomponentsontextMenuComponent.propDecorators = { i18n: [{ type: Input }], item: [{ type: Input }], event: [{ type: Input }], customContextMenu: [{ type: Input }], close: [{ type: Output }] }; if (false) { /** * @type {?} * @private */ ReuseTcomponentsontextMenuComponent.prototype._i18n; /** @type {?} */ ReuseTcomponentsontextMenuComponent.prototype.item; /** @type {?} */ ReuseTcomponentsontextMenuComponent.prototype.event; /** @type {?} */ ReuseTcomponentsontextMenuComponent.prototype.customContextMenu; /** @type {?} */ ReuseTcomponentsontextMenuComponent.prototype.close; /** * @type {?} * @private */ ReuseTcomponentsontextMenuComponent.prototype.i18nSrv; } /** * @fileoverview added by tsickle * Generated from: reuse-tab-context.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class ReuseTcomponentsontextService { /** * @param {?} overlay */ constructor(overlay) { this.overlay = overlay; this.show = new Subject(); this.close = new Subject(); } /** * @return {?} */ remove() { if (!this.ref) return; this.ref.detach(); this.ref.dispose(); this.ref = null; } /** * @param {?} context * @return {?} */ open(context) { this.remove(); const { event, item, customContextMenu } = context; /** @type {?} */ const fakeElement = new ElementRef({ getBoundingClientRect: (/** * @return {?} */ () => ({ bottom: event.clientY, height: 0, left: event.clientX, right: event.clientX, top: event.clientY, width: 0, })), }); /** @type {?} */ const positions = [ new ConnectionPositionPair({ originX: 'start', originY: 'bottom' }, { overlayX: 'start', overlayY: 'top' }), new ConnectionPositionPair({ originX: 'start', originY: 'top' }, { overlayX: 'start', overlayY: 'bottom' }), ]; /** @type {?} */ const positionStrategy = this.overlay.position().flexibleConnectedTo(fakeElement).withPositions(positions); this.ref = this.overlay.create({ positionStrategy, panelClass: 'reuse-tab__cm', scrollStrategy: this.overlay.scrollStrategies.close(), }); /** @type {?} */ const comp = this.ref.attach(new ComponentPortal(ReuseTcomponentsontextMenuComponent)); /** @type {?} */ const instance = comp.instance; instance.i18n = this.i18n; instance.item = Object.assign({}, item); instance.customContextMenu = (/** @type {?} */ (customContextMenu)); instance.event = event; /** @type {?} */ const sub$ = new Subscription(); sub$.add(instance.close.subscribe((/** * @param {?} res * @return {?} */ (res) => { this.close.next(res); this.remove(); }))); comp.onDestroy((/** * @return {?} */ () => sub$.unsubscribe())); } } ReuseTcomponentsontextService.decorators = [ { type: Injectable } ]; /** @nocollapse */ ReuseTcomponentsontextService.ctorParameters = () => [ { type: Overlay } ]; if (false) { /** * @type {?} * @private */ ReuseTcomponentsontextService.prototype.ref; /** @type {?} */ ReuseTcomponentsontextService.prototype.i18n; /** @type {?} */ ReuseTcomponentsontextService.prototype.show; /** @type {?} */ ReuseTcomponentsontextService.prototype.close; /** * @type {?} * @private */ ReuseTcomponentsontextService.prototype.overlay; } /** * @fileoverview added by tsickle * Generated from: reuse-tab-context.component.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class ReuseTcomponentsontextComponent { /** * @param {?} srv */ constructor(srv) { this.srv = srv; this.sub$ = new Subscription(); // tslint:disable-next-line:no-output-native this.change = new EventEmitter(); this.sub$.add(srv.show.subscribe((/** * @param {?} context * @return {?} */ context => this.srv.open(context)))); this.sub$.add(srv.close.subscribe((/** * @param {?} res * @return {?} */ res => this.change.emit(res)))); } /** * @param {?} value * @return {?} */ set i18n(value) { this.srv.i18n = value; } /** * @return {?} */ ngOnDestroy() { this.sub$.unsubscribe(); } } ReuseTcomponentsontextComponent.decorators = [ { type: Component, args: [{ selector: 'reuse-tab-context', template: `` }] } ]; /** @nocollapse */ ReuseTcomponentsontextComponent.ctorParameters = () => [ { type: ReuseTcomponentsontextService } ]; ReuseTcomponentsontextComponent.propDecorators = { i18n: [{ type: Input }], change: [{ type: Output }] }; if (false) { /** * @type {?} * @private */ ReuseTcomponentsontextComponent.prototype.sub$; /** @type {?} */ ReuseTcomponentsontextComponent.prototype.change; /** * @type {?} * @private */ ReuseTcomponentsontextComponent.prototype.srv; } /** * @fileoverview added by tsickle * Generated from: reuse-tab-context.directive.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class ReuseTcomponentsontextDirective { /** * @param {?} srv */ constructor(srv) { this.srv = srv; } /** * @param {?} event * @return {?} */ _onContextMenu(event) { this.srv.show.next({ event, item: this.item, customContextMenu: this.customContextMenu, }); event.preventDefault(); event.stopPropagation(); } } ReuseTcomponentsontextDirective.decorators = [ { type: Directive, args: [{ selector: '[reuse-tab-context-menu]', exportAs: 'reuseTcomponentsontextMenu', host: { '(contextmenu)': '_onContextMenu($event)', }, },] } ]; /** @nocollapse */ ReuseTcomponentsontextDirective.ctorParameters = () => [ { type: ReuseTcomponentsontextService } ]; ReuseTcomponentsontextDirective.propDecorators = { item: [{ type: Input, args: ['reuse-tab-context-menu',] }], customContextMenu: [{ type: Input }] }; if (false) { /** @type {?} */ ReuseTcomponentsontextDirective.prototype.item; /** @type {?} */ ReuseTcomponentsontextDirective.prototype.customContextMenu; /** * @type {?} * @private */ ReuseTcomponentsontextDirective.prototype.srv; } /** * @fileoverview added by tsickle * Generated from: reuse-tab.interfaces.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @enum {number} */ const ReuseTabMatchMode = { /** * (推荐)按菜单 `Menu` 配置 * * 可复用: * - `{ text:'Dashboard' }` * - `{ text:'Dashboard', reuse: true }` * * 不可复用: * - `{ text:'Dashboard', reuse: false }` */ Menu: 0, /** * 按菜单 `Menu` 强制配置 * * 可复用: * - `{ text:'Dashboard', reuse: true }` * * 不可复用: * - `{ text:'Dashboard' }` * - `{ text:'Dashboard', reuse: false }` */ MenuForce: 1, /** * 对所有路由有效,可以配合 `excludes` 过滤无须复用路由 */ URL: 2, }; ReuseTabMatchMode[ReuseTabMatchMode.Menu] = 'Menu'; ReuseTabMatchMode[ReuseTabMatchMode.MenuForce] = 'MenuForce'; ReuseTabMatchMode[ReuseTabMatchMode.URL] = 'URL'; /** * @record */ function ReuseTitle() { } if (false) { /** @type {?|undefined} */ ReuseTitle.prototype.text; /** @type {?|undefined} */ ReuseTitle.prototype.i18n; } /** * @record */ function ReuseTcomponentsached() { } if (false) { /** @type {?} */ ReuseTcomponentsached.prototype.title; /** @type {?} */ ReuseTcomponentsached.prototype.url; /** * 是否允许关闭,默认:`true` * @type {?|undefined} */ ReuseTcomponentsached.prototype.closable; /** * 当前滚动条位置 * @type {?|undefined} */ ReuseTcomponentsached.prototype.position; /** @type {?} */ ReuseTcomponentsached.prototype._snapshot; /** @type {?} */ ReuseTcomponentsached.prototype._handle; } /** * @record */ function ReuseTabNotify() { } if (false) { /** * 事件类型 * @type {?} */ ReuseTabNotify.prototype.active; /** @type {?|undefined} */ ReuseTabNotify.prototype.url; /** @type {?|undefined} */ ReuseTabNotify.prototype.title; /** @type {?|undefined} */ ReuseTabNotify.prototype.item; /** @type {?|undefined} */ ReuseTabNotify.prototype.list; /* Skipping unhandled member: [key: string]: any;*/ } /** * @record */ function ReuseItem() { } if (false) { /** @type {?} */ ReuseItem.prototype.url; /** @type {?} */ ReuseItem.prototype.title; /** @type {?} */ ReuseItem.prototype.closable; /** @type {?} */ ReuseItem.prototype.index; /** @type {?} */ ReuseItem.prototype.active; /** @type {?} */ ReuseItem.prototype.last; } /** * @record */ function ReuseContextEvent() { } if (false) { /** @type {?} */ ReuseContextEvent.prototype.event; /** @type {?} */ ReuseContextEvent.prototype.item; /** @type {?|undefined} */ ReuseContextEvent.prototype.comp; /** @type {?|undefined} */ ReuseContextEvent.prototype.customContextMenu; } /** * @record */ function ReuseContextCloseEvent() { } if (false) { /** @type {?} */ ReuseContextCloseEvent.prototype.type; /** @type {?} */ ReuseContextCloseEvent.prototype.item; /** @type {?} */ ReuseContextCloseEvent.prototype.includeNonCloseable; } /** * @record */ function ReuseContextI18n() { } if (false) { /** @type {?|undefined} */ ReuseContextI18n.prototype.close; /** @type {?|undefined} */ ReuseContextI18n.prototype.closeOther; /** @type {?|undefined} */ ReuseContextI18n.prototype.closeRight; /** @type {?|undefined} */ ReuseContextI18n.prototype.refresh; } /** * @record */ function ReuseCustomContextMenu() { } if (false) { /** @type {?} */ ReuseCustomContextMenu.prototype.id; /** @type {?} */ ReuseCustomContextMenu.prototype.title; /** @type {?} */ ReuseCustomContextMenu.prototype.fn; /** @type {?|undefined} */ ReuseCustomContextMenu.prototype.disabled; } /** * @record */ function ReuseComponentHandle() { } if (false) { /** @type {?} */ ReuseComponentHandle.prototype.componentRef; } /** * @record */ function ReuseComponentRef() { } if (false) { /** @type {?} */ ReuseComponentRef.prototype.instance; } /** * @record */ function ReuseComponentInstance() { } if (false) { /** @type {?} */ ReuseComponentInstance.prototype._onReuseInit; /** @type {?} */ ReuseComponentInstance.prototype._onReuseDestroy; /** @type {?} */ ReuseComponentInstance.prototype.destroy; } /** * @fileoverview added by tsickle * Generated from: reuse-tab.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * 路由复用类,提供复用所需要一些基本接口 * * **注:** 所有缓存数据来源于路由离开后才会产生 */ class ReuseTabService { // #endregion /** * @param {?} injector * @param {?} menuService */ constructor(injector, menuService) { this.injector = injector; this.menuService = menuService; this._inited = false; this._max = 10; this._keepingScroll = false; this._cachedChange = new BehaviorSubject(null); this._cached = []; this._titleCached = {}; this._closableCached = {}; this.positionBuffer = {}; this.debug = false; this.routeParamMatchMode = 'strict'; this.mode = ReuseTabMatchMode.Menu; /** * 排除规则,限 `mode=URL` */ this.excludes = []; } /** * @private * @return {?} */ get snapshot() { return this.injector.get(ActivatedRoute).snapshot; } // #region public /** * @return {?} */ get inited() { return this._inited; } /** * 当前路由地址 * @return {?} */ get curUrl() { return this.getUrl(this.snapshot); } /** * 允许最多复用多少个页面,取值范围 `2-100`,值发生变更时会强制关闭且忽略可关闭条件 * @param {?} value * @return {?} */ set max(value) { this._max = Math.min(Math.max(value, 2), 100); for (let i = this._cached.length; i > this._max; i--) { this._cached.pop(); } } /** * @param {?} value * @return {?} */ set keepingScroll(value) { this._keepingScroll = value; this.initScroll(); } /** * @return {?} */ get keepingScroll() { return this._keepingScroll; } /** * 获取已缓存的路由 * @return {?} */ get items() { return this._cached; } /** * 获取当前缓存的路由总数 * @return {?} */ get count() { return this._cached.length; } /** * 订阅缓存变更通知 * @return {?} */ get change() { return this._cachedChange.asObservable(); // .pipe(filter(w => w !== null)); } /** * 自定义当前标题 * @param {?} value * @return {?} */ set title(value) { /** @type {?} */ const url = this.curUrl; if (typeof value === 'string') value = { text: value }; this._titleCached[url] = value; this.di('update current tag title: ', value); this._cachedChange.next({ active: 'title', url, title: value, list: this._cached, }); } /** * 获取指定路径缓存所在位置,`-1` 表示无缓存 * @param {?} url * @return {?} */ index(url) { return this._cached.findIndex((/** * @param {?} w * @return {?} */ w => w.url === url)); } /** * 获取指定路径缓存是否存在 * @param {?} url * @return {?} */ exists(url) { return this.index(url) !== -1; } /** * 获取指定路径缓存 * @param {?=} url * @return {?} */ get(url) { return url ? this._cached.find((/** * @param {?} w * @return {?} */ w => w.url === url)) || null : null; } /** * @private * @param {?} url * @param {?} includeNonCloseable * @return {?} */ remove(url, includeNonCloseable) { /** @type {?} */ const idx = typeof url === 'string' ? this.index(url) : url; /** @type {?} */ const item = idx !== -1 ? this._cached[idx] : null; if (!item || (!includeNonCloseable && !item.closable)) return false; this.destroy(item._handle); this._cached.splice(idx, 1); delete this._titleCached[url]; return true; } /** * 根据URL移除标签 * * @param {?} url * @param {?=} includeNonCloseable * @return {?} */ close(url, includeNonCloseable = false) { this.removeUrlBuffer = url; this.remove(url, includeNonCloseable); this._cachedChange.next({ active: 'close', url, list: this._cached }); this.di('close tag', url); return true; } /** * 清除右边 * * @param {?} url * @param {?=} includeNonCloseable * @return {?} */ closeRight(url, includeNonCloseable = false) { /** @type {?} */ const start = this.index(url); for (let i = this.count - 1; i > start; i--) { this.remove(i, includeNonCloseable); } this.removeUrlBuffer = null; this._cachedChange.next({ active: 'closeRight', url, list: this._cached }); this.di('close right tages', url); return true; } /** * 清除所有缓存 * * @param {?=} includeNonCloseable * @return {?} */ clear(includeNonCloseable = false) { this._cached.forEach((/** * @param {?} w * @return {?} */ w => { if (!includeNonCloseable && w.closable) this.destroy(w._handle); })); this._cached = this._cached.filter((/** * @param {?} w * @return {?} */ w => !includeNonCloseable && !w.closable)); this.removeUrlBuffer = null; this._cachedChange.next({ active: 'clear', list: this._cached }); this.di('clear all catch'); } /** * 移动缓存数据 * \@example * ``` * // source * [ '/a/1', '/a/2', '/a/3', '/a/4', '/a/5' ] * move('/a/1', 2); * // output * [ '/a/2', '/a/3', '/a/1', '/a/4', '/a/5' ] * move('/a/1', -1); * // output * [ '/a/2', '/a/3', '/a/4', '/a/5', '/a/1' ] * ``` * @param {?} url 要移动的URL地址 * @param {?} position 新位置,下标从 `0` 开始 * * @return {?} */ move(url, position) { /** @type {?} */ const start = this._cached.findIndex((/** * @param {?} w * @return {?} */ w => w.url === url)); if (start === -1) return; /** @type {?} */ const data = this._cached.slice(); data.splice(position < 0 ? data.length + position : position, 0, data.splice(start, 1)[0]); this._cached = data; this._cachedChange.next({ active: 'move', url, position, list: this._cached, }); } /** * 强制关闭当前路由(包含不可关闭状态),并重新导航至 `newUrl` 路由 * @param {?} newUrl * @return {?} */ replace(newUrl) { /** @type {?} */ const url = this.curUrl; if (this.exists(url)) { this.close(url, true); } else { this.removeUrlBuffer = url; } this.injector.get(Router).navigateByUrl(newUrl); } /** * 获取标题,顺序如下: * * 1. 组件内使用 `ReuseTabService.title = 'new title'` 重新指定文本 * 2. 路由配置中 data 属性中包含 titleI18n > title * 3. 菜单数据中 text 属性 * * @param {?} url 指定URL * @param {?=} route 指定路由快照 * @return {?} */ getTitle(url, route) { if (this._titleCached[url]) { return this._titleCached[url]; } if (route && route.data && (route.data.titleI18n || route.data.title)) { return (/** @type {?} */ ({ text: route.data.title, i18n: route.data.titleI18n, })); } /** @type {?} */ const menu = this.getMenu(url); return menu ? { text: menu.text, i18n: menu.i18n } : { text: url }; } /** * 清除标题缓存 * @return {?} */ clearTitleCached() { this._titleCached = {}; } /** * 自定义当前 `closable` 状态 * @param {?} value * @return {?} */ set closable(value) { /** @type {?} */ const url = this.curUrl; this._closableCached[url] = value; this.di('update current tag closable: ', value); this._cachedChange.next({ active: 'closable', closable: value, list: this._cached, }); } /** * 获取 `closable` 状态,顺序如下: * * 1. 组件内使用 `ReuseTabService.closable = true` 重新指定 `closable` 状态 * 2. 路由配置中 data 属性中包含 `reuseClosable` * 3. 菜单数据中 `reuseClosable` 属性 * * @param {?} url 指定URL * @param {?=} route 指定路由快照 * @return {?} */ getClosable(url, route) { if (typeof this._closableCached[url] !== 'undefined') return this._closableCached[url]; if (route && route.data && typeof route.data.reuseClosable === 'boolean') return route.data.reuseClosable; /** @type {?} */ const menu = this.mode !== ReuseTabMatchMode.URL ? this.getMenu(url) : null; if (menu && typeof menu.reuseClosable === 'boolean') return menu.reuseClosable; return true; } /** * 清空 `closable` 缓存 * @return {?} */ clearClosableCached() { this._closableCached = {}; } /** * @param {?} route * @return {?} */ getTruthRoute(route) { /** @type {?} */ let next = route; while (next.firstChild) next = next.firstChild; return next; } /** * 根据快照获取URL地址 * @param {?} route * @return {?} */ getUrl(route) { /** @type {?} */ let next = this.getTruthRoute(route); /** @type {?} */ const segments = []; while (next) { segments.push(next.url.join('/')); next = (/** @type {?} */ (next.parent)); } /** @type {?} */ const url = '/' + segments .filter((/** * @param {?} i * @return {?} */ i => i)) .reverse() .join('/'); return url; } /** * 检查快照是否允许被复用 * @param {?} route * @return {?} */ can(route) { /** @type {?} */ const url = this.getUrl(route); if (url === this.removeUrlBuffer) return false; if (route.data && typeof route.data.reuse === 'boolean') return route.data.reuse; if (this.mode !== ReuseTabMatchMode.URL) { /** @type {?} */ const menu = this.getMenu(url); if (!menu) return false; if (this.mode === ReuseTabMatchMode.Menu) { if (menu.reuse === false) return false; } else { if (!menu.reuse || menu.reuse !== true) return false; } return true; } return !this.isExclude(url); } /** * @param {?} url * @return {?} */ isExclude(url) { return this.excludes.findIndex((/** * @param {?} r * @return {?} */ r => r.test(url))) !== -1; } /** * 刷新,触发一个 refresh 类型事件 * @param {?=} data * @return {?} */ refresh(data) { this._cachedChange.next({ active: 'refresh', data }); } // #endregion // #region privates /** * @private * @param {?} _handle * @return {?} */ destroy(_handle) { if (_handle && _handle.componentRef && _handle.componentRef.destroy) _handle.componentRef.destroy(); } /** * @private * @param {...?} args * @return {?} */ di(...args) { if (!this.debug) return; // tslint:disable-next-line:no-console console.warn(...args); } /** * @return {?} */ init() { this.initScroll(); this._inited = true; } /** * @private * @param {?} url * @return {?} */ getMenu(url) { /** @type {?} */ const menus = this.menuService.getPathByUrl(url); if (!menus || menus.length === 0) return null; return menus.pop(); } /** * @param {?} method * @param {?} comp * @param {?=} type * @return {?} */ runHook(method, comp, type = 'init') { if (typeof comp === 'number') { /** @type {?} */ const item = this._cached[comp]; comp = item._handle.componentRef; } /** @type {?} */ const compThis = comp.instance; if (comp == null || !compThis) { return; } /** @type {?} */ const fn = compThis[method]; if (typeof fn !== 'function') { return; } if (method === '_onReuseInit') { fn.call(compThis, type); } else { ((/** @type {?} */ (fn))).call(compThis); } } /** * @private * @param {?} route * @return {?} */ hasInValidRoute(route) { return !route.routeConfig || !!route.routeConfig.loadChildren || !!route.routeConfig.children; } /** * 决定是否允许路由复用,若 `true` 会触发 `store` * @param {?} route * @return {?} */ shouldDetach(route) { if (this.hasInValidRoute(route)) return false; this.di('#shouldDetach', this.can(route), this.getUrl(route)); return this.can(route); } /** * 存储 * @param {?} _snapshot * @param {?} _handle * @return {?} */ store(_snapshot, _handle) { /** @type {?} */ const url = this.getUrl(_snapshot); /** @type {?} */ const idx = this.index(url); /** @type {?} */ const isAdd = idx === -1; /** @type {?} */ const item = { title: this.getTitle(url, _snapshot), closable: this.getClosable(url, _snapshot), position: this.getKeepingScroll(url, _snapshot) ? this.positionBuffer[url] : null, url, _snapshot, _handle, }; if (isAdd) { if (this.count >= this._max) { // Get the oldest closable location /** @type {?} */ const closeIdx = this._cached.findIndex((/** * @param {?} w * @return {?} */ w => (/** @type {?} */ (w.closable)))); if (closeIdx !== -1) this.remove(closeIdx, false); } this._cached.push(item); } else { this._cached[idx] = item; } this.removeUrlBuffer = null; this.di('#store', isAdd ? '[new]' : '[override]', url); if (_handle && _handle.componentRef) { this.runHook('_onReuseDestroy', _handle.componentRef); } if (!isAdd) { this._cachedChange.next({ active: 'override', item, list: this._cached }); } } /** * 决定是否允许应用缓存数据 * @param {?} route * @return {?} */ shouldAttach(route) { if (this.hasInValidRoute(route)) return false; /** @type {?} */ const url = this.getUrl(route); /** @type {?} */ const data = this.get(url); /** @type {?} */ const ret = !!(data && data._handle); this.di('#shouldAttach', ret, url); if (ret) { /** @type {?} */ const compRef = (/** @type {?} */ (data))._handle.componentRef; if (compRef) { this.componentRef = compRef; this.runHook('_onReuseInit', compRef); } } else { this._cachedChange.next({ active: 'add', url, list: this._cached }); } return ret; } /** * 提取复用数据 * @param {?} route * @return {?} */ retrieve(route) { if (this.hasInValidRoute(route)) return null; /** @type {?} */ const url = this.getUrl(route); /** @type {?} */ const data = this.get(url); /** @type {?} */ const ret = (data && data._handle) || null; this.di('#retrieve', url, ret); return ret; } /** * 决定是否应该进行复用路由处理 * @param {?} future * @param {?} curr * @return {?} */ shouldReuseRoute(future, curr) { /** @type {?} */ let ret = future.routeConfig === curr.routeConfig; if (!ret) return false; /** @type {?} */ const path = (/** @type {?} */ (((future.routeConfig && future.routeConfig.path) || ''))); if (path.length > 0 && ~path.indexOf(':')) { if (this.routeParamMatchMode === 'strict') { ret = this.getUrl(future) === this.getUrl(curr); } else { ret = path === ((curr.routeConfig && curr.routeConfig.path) || ''); } } this.di('====================='); this.di('#shouldReuseRoute', ret, `${this.getUrl(curr)}=>${this.getUrl(future)}`, future, curr); return ret; } // #region scroll /** * 获取 `keepingScroll` 状态,顺序如下: * * 1. 路由配置中 data 属性中包含 `keepingScroll` * 2. 菜单数据中 `keepingScroll` 属性 * 3. 组件 `keepingScroll` 值 * @param {?} url * @param {?=} route * @return {?} */ getKeepingScroll(url, route) { if (route && route.data && typeof route.data.keepingScroll === 'boolean') return route.data.keepingScroll; /** @type {?} */ const menu = this.mode !== ReuseTabMatchMode.URL ? this.getMenu(url) : null; if (menu && typeof menu.keepingScroll === 'boolean') return menu.keepingScroll; return this.keepingScroll; } /** * @private * @return {?} */ get isDisabledInRouter() { /** @type {?} */ const routerConfig = this.injector.get(ROUTER_CONFIGURATION, (/** @type {?} */ ({}))); return routerConfig.scrollPositionRestoration === 'disabled'; } /** * @private * @return {?} */ get ss() { return this.injector.get(ScrollService); } /** * @private * @return {?} */ initScroll() { if (this._router$) { this._router$.unsubscribe(); } this._router$ = this.injector.get(Router).events.subscribe((/** * @param {?} e * @return {?} */ e => { if (e instanceof NavigationStart) { /** @type {?} */ const url = this.curUrl; if (this.getKeepingScroll(url, this.getTruthRoute(this.snapshot))) { this.positionBuffer[url] = this.ss.getScrollPosition(this.keepingScrollContainer); } else { delete this.positionBuffer[url]; } } else if (e instanceof NavigationEnd) { /** @type {?} */ const url = this.curUrl; /** @type {?} */ const item = this.get(url); if (item && item.position && this.getKeepingScroll(url, this.getTruthRoute(this.snapshot))) { if (this.isDisabledInRouter) { this.ss.scrollToPosition(this.keepingScrollContainer, item.position); } else { setTimeout((/** * @return {?} */ () => this.ss.scrollToPosition(this.keepingScrollContainer, (/** @type {?} */ (item.position)))), 1); } } } })); } // #endregion /** * @return {?} */ ngOnDestroy() { const { _cachedChange, _router$ } = this; this.clear(); this._cached = []; _cachedChange.complete(); if (_router$) { _router$.unsubscribe(); } } } ReuseTabService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ ReuseTabService.ctorParameters = () => [ { type: Injector }, { type: MenuService } ]; /** @nocollapse */ ReuseTabService.ɵprov = ɵɵdefineInjectable({ factory: function ReuseTabService_Factory() { return new ReuseTabService(ɵɵinject(INJECTOR), ɵɵinject(MenuService)); }, token: ReuseTabService, providedIn: "root" }); if (false) { /** * @type {?} * @private */ ReuseTabService.prototype._inited; /** * @type {?} * @private */ ReuseTabService.prototype._max; /** * @type {?} * @private */ ReuseTabService.prototype._keepingScroll; /** * @type {?} * @private */ ReuseTabService.prototype._cachedChange; /** * @type {?} * @private */ ReuseTabService.prototype._cached; /** * @type {?} * @private */ ReuseTabService.prototype._titleCached; /** * @type {?} * @private */ ReuseTabService.prototype._closableCached; /** * @type {?} * @private */ ReuseTabService.prototype._router$; /** * @type {?} * @private */ ReuseTabService.prototype.removeUrlBuffer; /** * @type {?} * @private */ ReuseTabService.prototype.positionBuffer; /** @type {?} */ ReuseTabService.prototype.componentRef; /** @type {?} */ ReuseTabService.prototype.debug; /** @type {?} */ ReuseTabService.prototype.routeParamMatchMode; /** @type {?} */ ReuseTabService.prototype.mode; /** * 排除规则,限 `mode=URL` * @type {?} */ ReuseTabService.prototype.excludes; /** @type {?} */ ReuseTabService.prototype.keepingScrollContainer; /** * @type {?} * @private */ ReuseTabService.prototype.injector; /** * @type {?} * @private */ ReuseTabService.prototype.menuService; } /** * @fileoverview added by tsickle * Generated from: reuse-tab.component.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ class ReuseTcomponentsomponent { // #endregion /** * @param {?} srv * @param {?} cdr * @param {?} router * @param {?} route * @param {?} i18nSrv * @param {?} doc */ constructor(srv, cdr, router, route, i18nSrv, doc) { this.srv = srv; this.cdr = cdr; this.router = router; this.route = route; this.i18nSrv = i18nSrv; this.doc = doc; this.unsubscribe$ = new Subject(); this.updatePos$ = new Subject(); this.list = []; this.pos = 0; // #region fields this.mode = ReuseTabMatchMode.Menu; this.debug = false; this.allowClose = true; this.keepingScroll = false; this.customContextMenu = []; this.tabType = 'line'; this.routeParamMatchMode = 'strict'; // tslint:disable-next-line:no-output-native this.change = new EventEmitter(); // tslint:disable-next-line:no-output-native this.close = new EventEmitter(); } /** * @param {?} value * @return {?} */ set keepingScrollContainer(value) { this._keepingScrollContainer = typeof value === 'string' ? this.doc.querySelector(value) : value; } /** * @private * @param {?} title * @return {?} */ genTit(title) { return title.i18n && this.i18nSrv ? this.i18nSrv.fanyi(title.i18n) : (/** @type {?} */ (title.text)); } /** * @private * @return {?} */ get curUrl() { return this.srv.getUrl(this.route.snapshot); } /** * @private * @return {?} */ genCurItem() { /** @type {?} */ const url = this.curUrl; /** @type {?} */ const snapshotTrue = this.srv.getTruthRoute(this.route.snapshot); return { url, title: this.genTit(this.srv.getTitle(url, snapshotTrue)), closable: this.allowClose && this.srv.count > 0 && this.srv.getClosable(url, snapshotTrue), active: false, last: false, index: 0, }; } /** * @private * @param {?} notify * @return {?} */ genList(notify) { /** @type {?} */ const ls = this.srv.items.map((/** * @param {?} item * @param {?} index * @return {?} */ (item, index) => ((/** @type {?} */ ({ url: item.url, title: this.genTit(item.title), closable: this.allowClose && item.closable && this.srv.count > 0, index, active: false, last: false, }))))); /** @type {?} */ const url = this.curUrl; /** @type {?} */ let addCurrent = ls.findIndex((/** * @param {?} w * @return {?} */ w => w.url === url)) === -1; if (notify && notify.active === 'close' && notify.url === url) { addCurrent = false; /** @type {?} */ let toPos = 0; /** @type {?} */ const curItem = (/** @type {?} */ (this.list.find((/** * @param {?} w * @return {?} */ w => w.url === url)))); if (curItem.index === ls.length) { // When closed is last toPos = ls.length - 1; } else if (curItem.index < ls.length) { // Should be actived next tab when closed is middle toPos = Math.max(0, curItem.index); } this.router.navigateByUrl(ls[toPos].url); } if (addCurrent) { ls.push(this.genCurItem()); } ls.forEach((/** * @param {?} item * @param {?} index * @return {?} */ (item, index) => (item.index = index))); if (ls.length === 1) { ls[0].closable = false; } this.list = ls; this.cdr.detectChanges(); this.updatePos$.next(); } /** * @private * @param {?} res * @return {?} */ updateTitle(res) { /** @type {?} */ const item = this.list.find((/** * @param {?} w * @return {?} */ w => w.url === (/** @type {?} */ (res)).url)); if (!item) return; item.title = this.genTit((/** @type {?} */ ((/** @type {?} */ (res)).title))); this.cdr.detectChanges(); } /** * @private * @param {?} item * @return {?} */ refresh(item) { this.srv.runHook('_onReuseInit', this.pos === item.index ? this.srv.componentRef : item.index, 'refresh'); } // #region UI /** * @param {?} res * @return {?} */ contextMenuChange(res) { /** @type {?} */ let fn = null; switch (res.type) { case 'refresh': this.refresh(res.item); break; case 'close': this._close(null, res.item.index, res.includeNonCloseable); break; case 'closeRight': fn = (/** * @return {?} */ () => { this.srv.closeRight(res.item.url, res.includeNonCloseable); this.close.emit(null); }); break; case 'closeOther': fn = (/** * @return {?} */ () => { this.srv.clear(res.includeNonCloseable); this.close.emit(null); }); break; } if (!fn) { return; } if (!res.item.active && res.item.index <= (/** @type {?} */ (this.list.find((/** * @param {?} w * @return {?} */ w => w.active)))).index) { this._to(res.item.index, fn); } else { fn(); } } /** * @param {?} index * @param {?=} cb * @return {?} */ _to(index, cb) { index = Math.max(0, Math.min(index, this.list.length - 1)); /** @type {?} */ const item = this.list[index]; this.router.navigateByUrl(item.url).then((/** * @param {?} res * @return {?} */ res => { if (!res) return; this.item = item; this.change.emit(item); if (cb) { cb(); } })); } /** * @param {?} e * @param {?} idx * @param {?} includeNonCloseable * @return {?} */ _close(e, idx, includeNonCloseable) { if (e != null) { e.preventDefault(); e.stopPropagation(); } /** @type {?} */ const item = this.list[idx]; this.srv.close(item.url, includeNonCloseable); this.close.emit(item); this.cdr.detectChanges(); return false; } /** * @param {?} instance * @return {?} */ activate(instance) { this.srv.componentRef = { instance }; } // #endregion /** * @return {?} */ ngOnInit() { this.updatePos$.pipe(takeUntil(this.unsubscribe$), debounceTime(50)).subscribe((/** * @return {?} */ () => { /** @type {?} */ const url = this.srv.getUrl(this.route.snapshot); /** @type {?} */ const ls = this.list.filter((/** * @param {?} w * @return {?} */ w => w.url === url || !this.srv.isExclude(w.url))); if (ls.length === 0) { return; } /** @type {?} */ const last = ls[ls.length - 1]; /** @type {?} */ const item = ls.find((/** * @param {?} w * @return {?} */ w => w.url === url)); last.last = true; /** @type {?} */ const pos = item == null ? last.index : item.index; ls.forEach((/** * @param {?} i * @param {?} idx * @return {?} */ (i, idx) => (i.active = pos === idx))); this.pos = pos; // TODO: 目前无法知道为什么 `pos` 无法通过 `nzSelectedIndex` 生效,因此强制使用组件实例的方式来修改,这种方式是安全的 // https://github.com/ohayojp/ohayojp/issues/1736 this.tabset.nzSelectedIndex = pos; this.list = ls; this.cdr.detectChanges(); })); this.srv.change.pipe(takeUntil(this.unsubscribe$)).subscribe((/** * @param {?} res * @return {?} */ res => { var _a; switch (res === null || res === void 0 ? void 0 : res.active) { case 'title': this.updateTitle(res); return; case 'override': if (((_a = res === null || res === void 0 ? void 0 : res.list) === null || _a === void 0 ? void 0 : _a.length) === this.list.length) { this.updatePos$.next(); return;