|
1 | | -import { |
2 | | - ComponentFactoryResolver, |
3 | | - ComponentRef, |
4 | | - Injector |
5 | | -} from "@angular/core"; |
6 | | -import { Modal } from "./modal.component"; |
7 | | -import { ReplaySubject } from "rxjs"; |
| 1 | +import { ComponentFactoryResolver } from "@angular/core"; |
8 | 2 | import { Injectable } from "@angular/core"; |
9 | 3 | import { AlertModal } from "./alert-modal.component"; |
10 | 4 | import { AlertModalData } from "./alert-modal.interface"; |
11 | 5 | import { PlaceholderService } from "carbon-components-angular/placeholder"; |
12 | | -import { tap, delay } from "rxjs/operators"; |
13 | | - |
| 6 | +import { BaseModalService } from "./base-modal.service"; |
14 | 7 |
|
15 | 8 | /** |
| 9 | + * Extends Base Modal Service to create Alert Modal with a function call. Placed in a seperate service |
| 10 | + * to prevent remote scoping (NG3003) which has side effects. Hence, import cycles are not allowed when |
| 11 | + * compilationMode is set to `partial`. |
| 12 | + * |
| 13 | + * |
16 | 14 | * Modal service handles instantiating and destroying modal instances. |
17 | 15 | * Uses PlaceholderService to track open instances, and for it's placeholder view reference. |
18 | 16 | */ |
19 | 17 | @Injectable() |
20 | | -export class ModalService { |
21 | | - // track all our open modals |
22 | | - protected static modalList: Array<ComponentRef<any>> = []; |
23 | | - |
| 18 | +export class ModalService extends BaseModalService { |
24 | 19 | /** |
25 | 20 | * Creates an instance of `ModalService`. |
26 | 21 | */ |
27 | | - constructor(public resolver: ComponentFactoryResolver, public placeholderService: PlaceholderService) {} |
28 | | - |
29 | | - /** |
30 | | - * Creates and renders the modal component that is passed in. |
31 | | - * `inputs` is an optional parameter of `data` that can be passed to the `Modal` component. |
32 | | - */ |
33 | | - create<T>(data: {component: any, inputs?: any}): ComponentRef<any> { |
34 | | - let defaults = {inputs: {}}; |
35 | | - data = Object.assign({}, defaults, data); |
36 | | - |
37 | | - const inputProviders = Object.keys(data.inputs).map(inputName => ({ |
38 | | - provide: inputName, |
39 | | - useValue: data.inputs[inputName] |
40 | | - })); |
41 | | - const injector = Injector.create(inputProviders); |
42 | | - const factory = this.resolver.resolveComponentFactory(data.component); |
43 | | - let focusedElement = document.activeElement as HTMLElement; |
44 | | - |
45 | | - let component = this.placeholderService.createComponent(factory, injector); |
46 | | - |
47 | | - setTimeout(() => { |
48 | | - component.instance.open = true; |
49 | | - }); |
50 | | - |
51 | | - component["previouslyFocusedElement"] = focusedElement; // used to return focus to previously focused element |
52 | | - |
53 | | - component.instance.close.pipe( |
54 | | - // trigger the close animation |
55 | | - tap(() => { |
56 | | - component.instance.open = false; |
57 | | - }), |
58 | | - // delay closing by an arbitrary amount to allow the animation to finish |
59 | | - delay(150) |
60 | | - ).subscribe(() => { |
61 | | - this.placeholderService.destroyComponent(component); |
62 | | - // filter out our component |
63 | | - ModalService.modalList = ModalService.modalList.filter(c => c !== component); |
64 | | - }); |
65 | | - |
66 | | - component.onDestroy(() => { |
67 | | - focusedElement.focus(); |
68 | | - }); |
69 | | - |
70 | | - ModalService.modalList.push(component); |
71 | | - |
72 | | - return component; |
| 22 | + constructor(public resolver: ComponentFactoryResolver, public placeholderService: PlaceholderService) { |
| 23 | + super(resolver, placeholderService); |
73 | 24 | } |
74 | 25 |
|
| 26 | + |
75 | 27 | /** |
76 | 28 | * Creates and renders a new alert modal component. |
77 | 29 | * @param data You can pass in: |
@@ -100,28 +52,10 @@ export class ModalService { |
100 | 52 | hasScrollingContent: data.hasScrollingContent || null, |
101 | 53 | size: data.size, |
102 | 54 | buttons: data.buttons || [], |
103 | | - close: data.close || (() => {}), |
| 55 | + close: data.close || (() => { }), |
104 | 56 | showCloseButton: data.showCloseButton |
105 | 57 | } |
106 | 58 | }); |
107 | 59 | } |
108 | | - |
109 | | - /** |
110 | | - * Destroys the modal on the supplied index. |
111 | | - * When called without parameters it destroys the most recently created/top most modal. |
112 | | - */ |
113 | | - destroy(index = -1) { |
114 | | - // return if nothing to destroy because it's already destroyed |
115 | | - if (index >= ModalService.modalList.length || ModalService.modalList.length === 0) { |
116 | | - return; |
117 | | - } |
118 | | - // on negative index destroy the last on the list (top modal) |
119 | | - if (index < 0) { |
120 | | - index = ModalService.modalList.length - 1; |
121 | | - } |
122 | | - |
123 | | - this.placeholderService.destroyComponent(ModalService.modalList[index]); |
124 | | - ModalService.modalList.splice(index, 1); |
125 | | - } |
126 | 60 | } |
127 | 61 |
|
0 commit comments