|
1 | | -import {Directive, OnInit, ViewContainerRef, Input, NgModule, AfterViewInit} from "@angular/core"; |
| 1 | +import {Directive, OnInit, ViewContainerRef, Input, NgModule, OnDestroy} from "@angular/core"; |
2 | 2 | import {CommonUtils} from "../core/utils/common-utils"; |
3 | | -import {PopupService} from "../service/popup.service"; |
4 | 3 |
|
5 | 4 | /** |
6 | 5 | * @internal |
@@ -31,8 +30,73 @@ export interface IJigsawComponent { |
31 | 30 | maxHeight: string; |
32 | 31 | } |
33 | 32 |
|
34 | | -export abstract class AbstractJigsawComponent implements IJigsawComponent, OnInit { |
| 33 | +export abstract class AbstractJigsawViewBase implements OnInit, OnDestroy { |
| 34 | + protected initialized: boolean = false; |
| 35 | + private _timerCache = []; |
| 36 | + |
| 37 | + /** |
| 38 | + * 延迟一会在执行一些逻辑,常常用于解决时间差的问题 |
| 39 | + * |
| 40 | + * @param {Function} handler 需要延迟执行的函数 |
| 41 | + */ |
| 42 | + protected callLater(handler: Function); |
| 43 | + /** |
| 44 | + * 延迟一会在执行一些逻辑,常常用于解决时间差的问题 |
| 45 | + * |
| 46 | + * @param {Function} handler 需要延迟执行的函数 |
| 47 | + * @param {number} timeout 延迟的毫秒数 |
| 48 | + */ |
| 49 | + protected callLater(handler: Function, timeout: number); |
| 50 | + /** |
| 51 | + * 延迟一会在执行一些逻辑,常常用于解决时间差的问题 |
| 52 | + * |
| 53 | + * @param {Function} handler 需要延迟执行的函数 |
| 54 | + * @param {any} context `handler`函数执行的上下文 |
| 55 | + */ |
| 56 | + protected callLater(handler: Function, context: any); |
| 57 | + /** |
| 58 | + * 延迟一会在执行一些逻辑,常常用于解决时间差的问题 |
| 59 | + * |
| 60 | + * @param {Function} handler 需要延迟执行的函数 |
| 61 | + * @param context `handler`函数执行的上下文 |
| 62 | + * @param {number} timeout 延迟的毫秒数 |
| 63 | + */ |
| 64 | + protected callLater(handler: Function, context: any, timeout: number); |
| 65 | + /** |
| 66 | + * @internal |
| 67 | + */ |
| 68 | + protected callLater(handler: Function, contextOrTimeout: any | number = undefined, timeout: number = 0): any { |
| 69 | + if (typeof contextOrTimeout === 'number') { |
| 70 | + timeout = +contextOrTimeout; |
| 71 | + contextOrTimeout = null; |
| 72 | + } |
| 73 | + |
| 74 | + const timer = setTimeout(() => { |
| 75 | + if (!this._timerCache) { |
| 76 | + // maybe this object has been destroyed! |
| 77 | + return; |
| 78 | + } |
| 79 | + const idx = this._timerCache.indexOf(timer); |
| 80 | + if (idx != -1) { |
| 81 | + this._timerCache.splice(idx, 1); |
| 82 | + } |
| 83 | + CommonUtils.safeInvokeCallback(contextOrTimeout, handler); |
| 84 | + }, timeout); |
| 85 | + this._timerCache.push(timer); |
| 86 | + return timer; |
| 87 | + } |
| 88 | + |
| 89 | + ngOnInit() { |
| 90 | + this.initialized = true; |
| 91 | + } |
35 | 92 |
|
| 93 | + ngOnDestroy() { |
| 94 | + this._timerCache.forEach(t => clearTimeout(t)); |
| 95 | + this._timerCache = null; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export abstract class AbstractJigsawComponent extends AbstractJigsawViewBase implements IJigsawComponent { |
36 | 100 | @Input() |
37 | 101 | public basicClass: string; |
38 | 102 |
|
@@ -66,13 +130,6 @@ export abstract class AbstractJigsawComponent implements IJigsawComponent, OnIni |
66 | 130 | public set maxHeight(value: string) { |
67 | 131 | this._maxHeight = CommonUtils.getCssValue(value); |
68 | 132 | } |
69 | | - |
70 | | - //TODO 所有组件都使用这个属性判断是否初始化好 |
71 | | - protected initialized: boolean = false; |
72 | | - |
73 | | - ngOnInit() { |
74 | | - this.initialized = true; |
75 | | - } |
76 | 133 | } |
77 | 134 |
|
78 | 135 | export interface IJigsawFormControl { |
|
0 commit comments