1 | import { Component, ChangeDetectionStrategy, Injectable, ComponentFactoryResolver, ApplicationRef, ɵɵdefineInjectable, ɵɵinject, NgModule } from '@angular/core';
|
2 | import { CommonModule } from '@angular/common';
|
3 | import { BehaviorSubject } from 'rxjs';
|
4 | import { filter } from 'rxjs/operators';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | class OverlayComponent {
|
11 | }
|
12 | OverlayComponent.decorators = [
|
13 | { type: Component, args: [{
|
14 |
|
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 |
|
34 |
|
35 |
|
36 |
|
37 | const BusCommand = {
|
38 | SHOW: 0, HIDE: 1,
|
39 | };
|
40 | BusCommand[BusCommand.SHOW] = 'SHOW';
|
41 | BusCommand[BusCommand.HIDE] = 'HIDE';
|
42 | class OverlayService {
|
43 | |
44 |
|
45 |
|
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 |
|
55 |
|
56 |
|
57 | p => !!p)))
|
58 | .subscribe(( |
59 |
|
60 |
|
61 |
|
62 | p => {
|
63 | setTimeout(( |
64 |
|
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 |
|
78 |
|
79 |
|
80 | get viewContainerRef() {
|
81 | if (this.applicationRef.components.length === 0) {
|
82 | return null;
|
83 | }
|
84 |
|
85 | const appInstance = this.applicationRef.components[0].instance;
|
86 | if (!appInstance.viewContainerRef) {
|
87 |
|
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 |
|
95 |
|
96 |
|
97 |
|
98 | doShow(dynamicComponent) {
|
99 | if (this.viewContainerRef) {
|
100 | if (this.count === 0) {
|
101 |
|
102 | const factory = this.resolver.resolveComponentFactory(OverlayComponent);
|
103 | this.componentRef = this.viewContainerRef.createComponent(factory);
|
104 | if (dynamicComponent) {
|
105 |
|
106 | const dynamicFactory = this.resolver.resolveComponentFactory(dynamicComponent);
|
107 | this.dynamicComponentRef = this.viewContainerRef.createComponent(dynamicFactory);
|
108 | }
|
109 | }
|
110 | this.count++;
|
111 | }
|
112 | }
|
113 | |
114 |
|
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 |
|
130 |
|
131 |
|
132 | show(dynamicComponent = null) {
|
133 | this.bus$.next({ command: BusCommand.SHOW, dynamicComponent });
|
134 | }
|
135 | |
136 |
|
137 |
|
138 | hide() {
|
139 | this.bus$.next({ command: BusCommand.HIDE });
|
140 | }
|
141 | |
142 |
|
143 |
|
144 | get attachedComponent() {
|
145 | return this.dynamicComponentRef;
|
146 | }
|
147 | }
|
148 | OverlayService.decorators = [
|
149 | { type: Injectable, args: [{
|
150 | providedIn: 'root'
|
151 | },] }
|
152 | ];
|
153 |
|
154 | OverlayService.ctorParameters = () => [
|
155 | { type: ComponentFactoryResolver },
|
156 | { type: ApplicationRef }
|
157 | ];
|
158 | OverlayService.ngInjectableDef = ɵɵdefineInjectable({ factory: function OverlayService_Factory() { return new OverlayService(ɵɵinject(ComponentFactoryResolver), ɵɵinject(ApplicationRef)); }, token: OverlayService, providedIn: "root" });
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | class OverlayModule {
|
165 | }
|
166 | OverlayModule.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 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | export { OverlayComponent, OverlayModule, OverlayService };
|
194 |
|