-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathdialogs.ts
164 lines (140 loc) · 5.71 KB
/
dialogs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {
ComponentFactoryResolver,
ComponentRef,
Directive,
Injectable,
NgModuleRef,
ReflectiveInjector,
Type,
ViewContainerRef
} from "@angular/core";
import { NSLocationStrategy } from "../router/ns-location-strategy";
import { View, ViewBase } from "tns-core-modules/ui/core/view";
import { ProxyViewContainer } from "tns-core-modules/ui/proxy-view-container/proxy-view-container";
import { AppHostView } from "../app-host-view";
import { DetachedLoader } from "../common/detached-loader";
import { PageFactory, PAGE_FACTORY } from "../platform-providers";
import { once } from "../common/utils";
import { topmost, Frame } from "tns-core-modules/ui/frame";
import { ShowModalOptions } from "tns-core-modules/ui/core/view";
export type BaseShowModalOptions = Pick<ShowModalOptions, Exclude<keyof ShowModalOptions, "closeCallback" | "context">>;
export interface ModalDialogOptions extends BaseShowModalOptions {
context?: any;
viewContainerRef?: ViewContainerRef;
moduleRef?: NgModuleRef<any>;
target?: View;
}
export class ModalDialogParams {
constructor(
public context: any = {},
public closeCallback: (...args) => any) {
}
}
interface ShowDialogOptions extends BaseShowModalOptions {
containerRef: ViewContainerRef;
context: any;
doneCallback;
pageFactory: PageFactory;
parentView: ViewBase;
resolver: ComponentFactoryResolver;
type: Type<any>;
}
@Injectable()
export class ModalDialogService {
constructor(private location: NSLocationStrategy) {
}
public showModal(type: Type<any>,
options: ModalDialogOptions
): Promise<any> {
if (!options.viewContainerRef) {
throw new Error(
"No viewContainerRef: " +
"Make sure you pass viewContainerRef in ModalDialogOptions."
);
}
let parentView = options.viewContainerRef.element.nativeElement;
if (options.target) {
parentView = options.target;
}
if (parentView instanceof AppHostView && parentView.ngAppRoot) {
parentView = parentView.ngAppRoot;
}
// _ngDialogRoot is the first child of the previously detached proxy.
// It should have 'viewController' (iOS) or '_dialogFragment' (Android) available for
// presenting future modal views.
if (parentView._ngDialogRoot) {
parentView = parentView._ngDialogRoot;
}
const pageFactory: PageFactory = options.viewContainerRef.injector.get(PAGE_FACTORY);
// resolve from particular module (moduleRef)
// or from same module as parentView (viewContainerRef)
const componentContainer = options.moduleRef || options.viewContainerRef;
const resolver = componentContainer.injector.get(ComponentFactoryResolver);
let frame = parentView;
if (!(parentView instanceof Frame)) {
frame = (parentView.page && parentView.page.frame) || topmost();
}
this.location._beginModalNavigation(frame);
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
this._showDialog({
...options,
containerRef: options.viewContainerRef,
context: options.context,
doneCallback: resolve,
pageFactory,
parentView,
resolver,
type
});
} catch (err) {
reject(err);
}
}, 10);
});
}
private _showDialog(options: ShowDialogOptions): void {
let componentView: View;
let detachedLoaderRef: ComponentRef<DetachedLoader>;
const closeCallback = once((...args) => {
options.doneCallback.apply(undefined, args);
if (componentView) {
componentView.closeModal();
this.location._closeModalNavigation();
detachedLoaderRef.instance.detectChanges();
detachedLoaderRef.destroy();
}
});
const modalParams = new ModalDialogParams(options.context, closeCallback);
const providers = ReflectiveInjector.resolve([
{ provide: ModalDialogParams, useValue: modalParams },
]);
const childInjector = ReflectiveInjector.fromResolvedProviders(providers, options.containerRef.parentInjector);
const detachedFactory = options.resolver.resolveComponentFactory(DetachedLoader);
detachedLoaderRef = options.containerRef.createComponent(detachedFactory, -1, childInjector, null);
detachedLoaderRef.instance.loadComponent(options.type).then((compRef) => {
const detachedProxy = <ProxyViewContainer>compRef.location.nativeElement;
if (detachedProxy.getChildrenCount() > 1) {
throw new Error("Modal content has more than one root view.");
}
componentView = detachedProxy.getChildAt(0);
if (componentView.parent) {
(<any>componentView.parent)._ngDialogRoot = componentView;
(<any>componentView.parent).removeChild(componentView);
}
options.parentView.showModal(componentView, { ...options, closeCallback });
});
}
}
@Directive({
selector: "[modal-dialog-host]" // tslint:disable-line:directive-selector
})
export class ModalDialogHost { // tslint:disable-line:directive-class-suffix
constructor() {
throw new Error("ModalDialogHost is deprecated. " +
"Call ModalDialogService.showModal() " +
"by passing ViewContainerRef in the options instead."
);
}
}