UNPKG

5.94 kBJavaScriptView Raw
1import { Component, ChangeDetectionStrategy, Injectable, ComponentFactoryResolver, ApplicationRef, ɵɵdefineInjectable, ɵɵinject, NgModule } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { BehaviorSubject } from 'rxjs';
4import { filter } from 'rxjs/operators';
5
6/**
7 * @fileoverview added by tsickle
8 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
9 */
10class OverlayComponent {
11}
12OverlayComponent.decorators = [
13 { type: Component, args: [{
14 // tslint:disable-next-line:component-selector
15 selector: 'a4-overlay',
16 template: ``,
17 changeDetection: ChangeDetectionStrategy.OnPush,
18 styles: [`
19 :host {
20 position: fixed;
21 left: 0;
22 right: 0;
23 top: 0;
24 bottom: 0;
25 background-color: rgba(0,0,0,0.1);
26 z-index: 999;
27 }
28 `]
29 }] }
30];
31
32/**
33 * @fileoverview added by tsickle
34 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
35 */
36/** @enum {number} */
37const BusCommand = {
38 SHOW: 0, HIDE: 1,
39};
40BusCommand[BusCommand.SHOW] = 'SHOW';
41BusCommand[BusCommand.HIDE] = 'HIDE';
42class OverlayService {
43 /**
44 * @param {?} resolver
45 * @param {?} applicationRef
46 */
47 constructor(resolver, applicationRef) {
48 this.resolver = resolver;
49 this.applicationRef = applicationRef;
50 this.count = 0;
51 this.bus$ = new BehaviorSubject(null);
52 this.bus$.
53 pipe(filter((/**
54 * @param {?} p
55 * @return {?}
56 */
57 p => !!p)))
58 .subscribe((/**
59 * @param {?} p
60 * @return {?}
61 */
62 p => {
63 setTimeout((/**
64 * @return {?}
65 */
66 () => {
67 if (p.command === BusCommand.SHOW) {
68 this.doShow(p.dynamicComponent);
69 }
70 else {
71 this.doHide();
72 }
73 }), 0);
74 }));
75 }
76 /**
77 * @private
78 * @return {?}
79 */
80 get viewContainerRef() {
81 if (this.applicationRef.components.length === 0) {
82 return null;
83 }
84 /** @type {?} */
85 const appInstance = this.applicationRef.components[0].instance;
86 if (!appInstance.viewContainerRef) {
87 /** @type {?} */
88 const appName = this.applicationRef.componentTypes[0].name;
89 throw new Error(`Missing 'viewContainerRef' declaration in ${appName} constructor`);
90 }
91 return appInstance.viewContainerRef;
92 }
93 /**
94 * @private
95 * @param {?} dynamicComponent
96 * @return {?}
97 */
98 doShow(dynamicComponent) {
99 if (this.viewContainerRef) {
100 if (this.count === 0) {
101 /** @type {?} */
102 const factory = this.resolver.resolveComponentFactory(OverlayComponent);
103 this.componentRef = this.viewContainerRef.createComponent(factory);
104 if (dynamicComponent) {
105 /** @type {?} */
106 const dynamicFactory = this.resolver.resolveComponentFactory(dynamicComponent);
107 this.dynamicComponentRef = this.viewContainerRef.createComponent(dynamicFactory);
108 }
109 }
110 this.count++;
111 }
112 }
113 /**
114 * @return {?}
115 */
116 doHide() {
117 if (this.count > 0) {
118 this.count--;
119 if (this.count === 0) {
120 this.componentRef.destroy();
121 if (this.dynamicComponentRef) {
122 this.dynamicComponentRef.destroy();
123 this.dynamicComponentRef = null;
124 }
125 }
126 }
127 }
128 /**
129 * @param {?=} dynamicComponent
130 * @return {?}
131 */
132 show(dynamicComponent = null) {
133 this.bus$.next({ command: BusCommand.SHOW, dynamicComponent });
134 }
135 /**
136 * @return {?}
137 */
138 hide() {
139 this.bus$.next({ command: BusCommand.HIDE });
140 }
141 /**
142 * @return {?}
143 */
144 get attachedComponent() {
145 return this.dynamicComponentRef;
146 }
147}
148OverlayService.decorators = [
149 { type: Injectable, args: [{
150 providedIn: 'root'
151 },] }
152];
153/** @nocollapse */
154OverlayService.ctorParameters = () => [
155 { type: ComponentFactoryResolver },
156 { type: ApplicationRef }
157];
158/** @nocollapse */ OverlayService.ngInjectableDef = ɵɵdefineInjectable({ factory: function OverlayService_Factory() { return new OverlayService(ɵɵinject(ComponentFactoryResolver), ɵɵinject(ApplicationRef)); }, token: OverlayService, providedIn: "root" });
159
160/**
161 * @fileoverview added by tsickle
162 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
163 */
164class OverlayModule {
165}
166OverlayModule.decorators = [
167 { type: NgModule, args: [{
168 imports: [
169 CommonModule
170 ],
171 declarations: [
172 OverlayComponent
173 ],
174 providers: [
175 OverlayService
176 ],
177 entryComponents: [
178 OverlayComponent
179 ]
180 },] }
181];
182
183/**
184 * @fileoverview added by tsickle
185 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
186 */
187
188/**
189 * @fileoverview added by tsickle
190 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
191 */
192
193export { OverlayComponent, OverlayModule, OverlayService };
194//# sourceMappingURL=a4-overlay.js.map