|
| 1 | +// todo: add delay support |
| 2 | +// todo: merge events onShow, onShown, etc... |
| 3 | +// todo: add global positioning configuration? |
| 4 | +import { |
| 5 | + NgZone, ViewContainerRef, ComponentFactoryResolver, Injector, Renderer, |
| 6 | + ElementRef, ComponentRef, ComponentFactory, Type, TemplateRef, EventEmitter |
| 7 | +} from '@angular/core'; |
| 8 | +import { ContentRef } from './content-ref.class'; |
| 9 | +import { PositioningService, PositioningOptions } from '../positioning'; |
| 10 | +import { listenToTriggers } from '../utils/triggers'; |
| 11 | + |
| 12 | +export interface ListenOptions { |
| 13 | + target?: ElementRef; |
| 14 | + triggers?: string; |
| 15 | + show?: Function; |
| 16 | + hide?: Function; |
| 17 | + toggle?: Function; |
| 18 | +} |
| 19 | + |
| 20 | +export class ComponentLoader<T> { |
| 21 | + public onBeforeShow: EventEmitter<any> = new EventEmitter(); |
| 22 | + public onShown: EventEmitter<any> = new EventEmitter(); |
| 23 | + public onBeforeHide: EventEmitter<any> = new EventEmitter(); |
| 24 | + public onHidden: EventEmitter<any> = new EventEmitter(); |
| 25 | + |
| 26 | + public instance: T; |
| 27 | + |
| 28 | + private _componentFactory: ComponentFactory<T>; |
| 29 | + private _elementRef: ElementRef; |
| 30 | + private _componentRef: ComponentRef<T>; |
| 31 | + private _zoneSubscription: any; |
| 32 | + private _contentRef: ContentRef; |
| 33 | + private _viewContainerRef: ViewContainerRef; |
| 34 | + private _injector: Injector; |
| 35 | + private _renderer: Renderer; |
| 36 | + private _ngZone: NgZone; |
| 37 | + private _componentFactoryResolver: ComponentFactoryResolver; |
| 38 | + private _posService: PositioningService; |
| 39 | + |
| 40 | + private _unregisterListenersFn: Function; |
| 41 | + |
| 42 | + public get isShown(): boolean { |
| 43 | + return !!this._componentRef; |
| 44 | + }; |
| 45 | + |
| 46 | + /** |
| 47 | + * Placement of a component. Accepts: "top", "bottom", "left", "right" |
| 48 | + */ |
| 49 | + private attachment: string; |
| 50 | + |
| 51 | + /** |
| 52 | + * A selector specifying the element the popover should be appended to. |
| 53 | + * Currently only supports "body". |
| 54 | + */ |
| 55 | + private container: string | ElementRef | any; |
| 56 | + |
| 57 | + /** |
| 58 | + * Specifies events that should trigger. Supports a space separated list of |
| 59 | + * event names. |
| 60 | + */ |
| 61 | + private triggers: string; |
| 62 | + |
| 63 | + /** |
| 64 | + * Do not use this directly, it should be instanced via |
| 65 | + * `ComponentLoadFactory.attach` |
| 66 | + * @internal |
| 67 | + * @param _viewContainerRef |
| 68 | + * @param _elementRef |
| 69 | + * @param _injector |
| 70 | + * @param _renderer |
| 71 | + * @param _componentFactoryResolver |
| 72 | + * @param _ngZone |
| 73 | + * @param _posService |
| 74 | + */ |
| 75 | + // tslint:disable-next-line |
| 76 | + public constructor(_viewContainerRef: ViewContainerRef, _renderer: Renderer, |
| 77 | + _elementRef: ElementRef, |
| 78 | + _injector: Injector, _componentFactoryResolver: ComponentFactoryResolver, |
| 79 | + _ngZone: NgZone, _posService: PositioningService) { |
| 80 | + this._ngZone = _ngZone; |
| 81 | + this._injector = _injector; |
| 82 | + this._renderer = _renderer; |
| 83 | + this._elementRef = _elementRef; |
| 84 | + this._posService = _posService; |
| 85 | + this._viewContainerRef = _viewContainerRef; |
| 86 | + this._componentFactoryResolver = _componentFactoryResolver; |
| 87 | + } |
| 88 | + |
| 89 | + public attach(compType: Type<T>): ComponentLoader<T> { |
| 90 | + this._componentFactory = this._componentFactoryResolver |
| 91 | + .resolveComponentFactory<T>(compType); |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + // todo: add behaviour: to target element, `body`, custom element |
| 96 | + public to(container?: string): ComponentLoader<T> { |
| 97 | + this.container = container || this.container; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public position(opts?: PositioningOptions): ComponentLoader<T> { |
| 102 | + this.attachment = opts.attachment || this.attachment; |
| 103 | + this._elementRef = opts.target as ElementRef || this._elementRef; |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + public show(content?: string | TemplateRef<any>, mixin?: any): ComponentRef<T> { |
| 108 | + this._subscribePositioning(); |
| 109 | + |
| 110 | + if (!this._componentRef) { |
| 111 | + this.onBeforeShow.emit(); |
| 112 | + this._contentRef = this._getContentRef(content); |
| 113 | + this._componentRef = this._viewContainerRef |
| 114 | + .createComponent(this._componentFactory, 0, this._injector, this._contentRef.nodes); |
| 115 | + this.instance = this._componentRef.instance; |
| 116 | + |
| 117 | + Object.assign(this._componentRef.instance, mixin || {}); |
| 118 | + |
| 119 | + if (this.container === 'body' && typeof document !== 'undefined') { |
| 120 | + document.querySelector(this.container as string) |
| 121 | + .appendChild(this._componentRef.location.nativeElement); |
| 122 | + } |
| 123 | + |
| 124 | + // we need to manually invoke change detection since events registered |
| 125 | + // via |
| 126 | + // Renderer::listen() are not picked up by change detection with the |
| 127 | + // OnPush strategy |
| 128 | + this._componentRef.changeDetectorRef.markForCheck(); |
| 129 | + this.onShown.emit(this._componentRef.instance); |
| 130 | + } |
| 131 | + return this._componentRef; |
| 132 | + } |
| 133 | + |
| 134 | + public hide(): ComponentLoader<T> { |
| 135 | + if (this._componentRef) { |
| 136 | + this.onBeforeHide.emit(this._componentRef.instance); |
| 137 | + this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._componentRef.hostView)); |
| 138 | + this._componentRef = null; |
| 139 | + |
| 140 | + if (this._contentRef.viewRef) { |
| 141 | + this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._contentRef.viewRef)); |
| 142 | + this._contentRef = null; |
| 143 | + } |
| 144 | + |
| 145 | + this._componentRef = null; |
| 146 | + this.onHidden.emit(); |
| 147 | + } |
| 148 | + return this; |
| 149 | + } |
| 150 | + |
| 151 | + public toggle(): void { |
| 152 | + if (this.isShown) { |
| 153 | + this.hide(); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + this.show(); |
| 158 | + } |
| 159 | + |
| 160 | + public dispose(): void { |
| 161 | + if (this.isShown) { |
| 162 | + this.hide(); |
| 163 | + } |
| 164 | + |
| 165 | + this._unsubscribePositioning(); |
| 166 | + |
| 167 | + if (this._unregisterListenersFn) { |
| 168 | + this._unregisterListenersFn(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public listen(listenOpts: ListenOptions): ComponentLoader<T> { |
| 173 | + if (this._unregisterListenersFn) { |
| 174 | + this._unregisterListenersFn(); |
| 175 | + } |
| 176 | + |
| 177 | + this.triggers = listenOpts.triggers || this.triggers; |
| 178 | + |
| 179 | + listenOpts.target = listenOpts.target || this._elementRef; |
| 180 | + listenOpts.show = listenOpts.show || (() => this.show()); |
| 181 | + listenOpts.hide = listenOpts.hide || (() => this.hide()); |
| 182 | + listenOpts.toggle = listenOpts.toggle || (() => this.isShown |
| 183 | + ? listenOpts.hide() |
| 184 | + : listenOpts.show()); |
| 185 | + |
| 186 | + this._unregisterListenersFn = listenToTriggers( |
| 187 | + this._renderer, |
| 188 | + listenOpts.target.nativeElement, |
| 189 | + this.triggers, |
| 190 | + listenOpts.show, |
| 191 | + listenOpts.hide, |
| 192 | + listenOpts.toggle); |
| 193 | + |
| 194 | + return this; |
| 195 | + } |
| 196 | + |
| 197 | + private _subscribePositioning(): void { |
| 198 | + if (this._zoneSubscription) { |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + this._zoneSubscription = this._ngZone |
| 203 | + .onStable.subscribe(() => { |
| 204 | + if (!this._componentRef) { |
| 205 | + return; |
| 206 | + } |
| 207 | + this._posService.position({ |
| 208 | + element: this._componentRef.location, |
| 209 | + target: this._elementRef, |
| 210 | + attachment: this.attachment, |
| 211 | + appendToBody: this.container === 'body' |
| 212 | + }); |
| 213 | + }); |
| 214 | + } |
| 215 | + |
| 216 | + private _unsubscribePositioning(): void { |
| 217 | + if (!this._zoneSubscription) { |
| 218 | + return; |
| 219 | + } |
| 220 | + this._zoneSubscription.unsubscribe(); |
| 221 | + this._zoneSubscription = null; |
| 222 | + } |
| 223 | + |
| 224 | + private _getContentRef(content: string | TemplateRef<any>): ContentRef { |
| 225 | + if (!content) { |
| 226 | + return new ContentRef([]); |
| 227 | + } |
| 228 | + |
| 229 | + if (content instanceof TemplateRef) { |
| 230 | + const viewRef = this._viewContainerRef |
| 231 | + .createEmbeddedView<TemplateRef<T>>(content); |
| 232 | + return new ContentRef([viewRef.rootNodes], viewRef); |
| 233 | + } |
| 234 | + |
| 235 | + return new ContentRef([[this._renderer.createText(null, `${content}`)]]); |
| 236 | + } |
| 237 | +} |
0 commit comments