@rdkmaster/jigsaw-labs
Version:
Jigsaw, the next generation component set for RDK
171 lines (139 loc) • 4.44 kB
text/typescript
import {
Component, Input, ViewContainerRef, TemplateRef, ViewChild, ElementRef,
AfterViewInit, EmbeddedViewRef, ChangeDetectorRef, Type, ComponentFactoryResolver,
ComponentRef, OnDestroy, Output, EventEmitter
} from '@angular/core';
import {AbstractJigsawComponent, IDynamicInstantiatable} from "../common";
/**
* 改变tab标题时发送事件的携带数据类型。
*/
export class TabTitleInfo {
key: number;
title: string
}
export abstract class JigsawTabBase extends AbstractJigsawComponent implements OnDestroy {
constructor(protected _changeDetector: ChangeDetectorRef, protected _componentFactory: ComponentFactoryResolver) {
super()
}
public key: number;
public tabItem: TemplateRef<any> | Type<IDynamicInstantiatable>;
public initData: Object;
protected _body: ViewContainerRef;
/**
* @internal
*/
public _tabItemRef: EmbeddedViewRef<any> | ComponentRef<IDynamicInstantiatable>;
protected _insert(): void {
if (!this._tabItemRef) {
this._tabItemRef = this._createTab(this.tabItem, this.initData);
this._changeDetector.detectChanges()
}
}
protected _destroy(): void {
if (this._tabItemRef) {
this._tabItemRef.destroy();
this._tabItemRef = null
}
}
protected _createTab(what: Type<IDynamicInstantiatable> | TemplateRef<any>,
initData: Object): EmbeddedViewRef<any> | ComponentRef<IDynamicInstantiatable> {
if (what instanceof TemplateRef) {
return this._body.createEmbeddedView(what, initData);
} else if (what) {
const factory = this._componentFactory.resolveComponentFactory(what);
const componentRef = this._body.createComponent(factory);
componentRef.instance.initData = initData;
return componentRef;
}
}
ngOnDestroy() {
this._destroy()
}
}
/**
* @internal
*/
export class JigsawTabLabel extends JigsawTabBase implements AfterViewInit {
constructor(public elementRef: ElementRef,
protected _changeDetector: ChangeDetectorRef,
protected _componentFactory: ComponentFactoryResolver) {
super(_changeDetector, _componentFactory)
}
public editable: boolean;
public remove = new EventEmitter<number>();
public change = new EventEmitter<TabTitleInfo>();
// label 左侧的距离
public getOffsetLeft(): number {
return this.elementRef.nativeElement.offsetLeft;
}
public getOffsetTop(): number {
return this.elementRef.nativeElement.offsetTop;
}
// 组件的宽度
public getOffsetWidth(): number {
return this.elementRef.nativeElement.offsetWidth;
}
/**
* @internal
*/
public _$handleRemove(e) {
e.preventDefault();
e.stopPropagation();
this.remove.emit(this.key);
}
ngAfterViewInit() {
this._insert()
}
}
/**
* @internal
*/
export class JigsawTabContent extends JigsawTabBase implements AfterViewInit {
constructor(protected _changeDetector: ChangeDetectorRef, protected _componentFactory: ComponentFactoryResolver) {
super(_changeDetector, _componentFactory)
}
public lazy: boolean;
private _isActive: boolean;
public get isActive(): boolean {
return this._isActive;
}
public set isActive(active: boolean) {
this._isActive = active;
if (this.initialized && active) {
this._insert();
}
}
ngAfterViewInit() {
if (!this.lazy || this._isActive) {
// 同步加载,或者处于激活状态
this._insert();
}
}
}