diff options
author | YuanHu <yuan.hu1@zte.com.cn> | 2018-03-27 15:50:00 +0800 |
---|---|---|
committer | YuanHu <yuan.hu1@zte.com.cn> | 2018-03-27 15:50:00 +0800 |
commit | 309680feb63ec6a49163387cb1e116b721254847 (patch) | |
tree | 0d58a1e12acad73b1421236412f6e41ca08196d3 /sdc-workflow-designer-ui/src/app/paletx/util/popup.ts | |
parent | 5b3cc39ad139793fe689683a4a7344ddb3c8361f (diff) |
Add paletx common & util
Add paletx common & util
Issue-ID: SDC-1130,SDC-1131
Change-Id: Ibbdd8d4fabf45637be7d44400c18d34efc15d46e
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
Diffstat (limited to 'sdc-workflow-designer-ui/src/app/paletx/util/popup.ts')
-rw-r--r-- | sdc-workflow-designer-ui/src/app/paletx/util/popup.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/sdc-workflow-designer-ui/src/app/paletx/util/popup.ts b/sdc-workflow-designer-ui/src/app/paletx/util/popup.ts new file mode 100644 index 00000000..dacbc0b9 --- /dev/null +++ b/sdc-workflow-designer-ui/src/app/paletx/util/popup.ts @@ -0,0 +1,59 @@ +import { + Injector, + TemplateRef, + ViewRef, + ViewContainerRef, + Renderer, + ComponentRef, + ComponentFactory, + ComponentFactoryResolver +} from '@angular/core'; + +export class ContentRef { + constructor(public nodes: any[], public viewRef?: ViewRef, public componentRef?: ComponentRef<any>) { + } +} + +export class PopupService<T> { + private _windowFactory: ComponentFactory<T>; + private _windowRef: ComponentRef<T>; + private _contentRef: ContentRef; + + constructor(type: any, private _injector: Injector, private _viewContainerRef: ViewContainerRef, private _renderer: Renderer, + componentFactoryResolver: ComponentFactoryResolver) { + this._windowFactory = componentFactoryResolver.resolveComponentFactory<T>(type); + } + + public open(content?: string | TemplateRef<any>, context?: any): ComponentRef<T> { + if (!this._windowRef) { + this._contentRef = this._getContentRef(content, context); + this._windowRef = + this._viewContainerRef.createComponent(this._windowFactory, 0, this._injector, this._contentRef.nodes); + } + + return this._windowRef; + } + + public close() { + if (this._windowRef) { + this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._windowRef.hostView)); + this._windowRef = null; + + if (this._contentRef.viewRef) { + this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._contentRef.viewRef)); + this._contentRef = null; + } + } + } + + private _getContentRef(content: string | TemplateRef<any>, context?: any): ContentRef { + if (!content) { + return new ContentRef([]); + } else if (content instanceof TemplateRef) { + const viewRef = this._viewContainerRef.createEmbeddedView(<TemplateRef<T>>content, context); + return new ContentRef([viewRef.rootNodes], viewRef); + } else { + return new ContentRef([[this._renderer.createText(null, `${content}`)]]); + } + } +} |