forked from angular/components
/
bottom-sheet.ts
216 lines (187 loc) · 7.77 KB
/
bottom-sheet.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Directionality} from '@angular/cdk/bidi';
import {Overlay, OverlayConfig, OverlayRef} from '@angular/cdk/overlay';
import {ComponentPortal, ComponentType, TemplatePortal} from '@angular/cdk/portal';
import {
ComponentRef,
Injectable,
Injector,
Optional,
SkipSelf,
TemplateRef,
InjectionToken,
Inject,
OnDestroy,
StaticProvider,
InjectFlags,
} from '@angular/core';
import {of as observableOf} from 'rxjs';
import {MAT_BOTTOM_SHEET_DATA, MatBottomSheetConfig} from './bottom-sheet-config';
import {MatBottomSheetContainer} from './bottom-sheet-container';
import {MatBottomSheetModule} from './bottom-sheet-module';
import {MatBottomSheetRef} from './bottom-sheet-ref';
/** Injection token that can be used to specify default bottom sheet options. */
export const MAT_BOTTOM_SHEET_DEFAULT_OPTIONS =
new InjectionToken<MatBottomSheetConfig>('mat-bottom-sheet-default-options');
/**
* Service to trigger Material Design bottom sheets.
*/
@Injectable({providedIn: MatBottomSheetModule})
export class MatBottomSheet implements OnDestroy {
private _bottomSheetRefAtThisLevel: MatBottomSheetRef<any> | null = null;
/** Reference to the currently opened bottom sheet. */
get _openedBottomSheetRef(): MatBottomSheetRef<any> | null {
const parent = this._parentBottomSheet;
return parent ? parent._openedBottomSheetRef : this._bottomSheetRefAtThisLevel;
}
set _openedBottomSheetRef(value: MatBottomSheetRef<any> | null) {
if (this._parentBottomSheet) {
this._parentBottomSheet._openedBottomSheetRef = value;
} else {
this._bottomSheetRefAtThisLevel = value;
}
}
constructor(
private _overlay: Overlay,
private _injector: Injector,
@Optional() @SkipSelf() private _parentBottomSheet: MatBottomSheet,
@Optional() @Inject(MAT_BOTTOM_SHEET_DEFAULT_OPTIONS)
private _defaultOptions?: MatBottomSheetConfig) {}
/**
* Opens a bottom sheet containing the given component.
* @param component Type of the component to load into the bottom sheet.
* @param config Extra configuration options.
* @returns Reference to the newly-opened bottom sheet.
*/
open<T, D = any, R = any>(component: ComponentType<T>,
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T, R>;
/**
* Opens a bottom sheet containing the given template.
* @param template TemplateRef to instantiate as the bottom sheet content.
* @param config Extra configuration options.
* @returns Reference to the newly-opened bottom sheet.
*/
open<T, D = any, R = any>(template: TemplateRef<T>,
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T, R>;
open<T, D = any, R = any>(componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
config?: MatBottomSheetConfig<D>): MatBottomSheetRef<T, R> {
const _config =
_applyConfigDefaults(this._defaultOptions || new MatBottomSheetConfig(), config);
const overlayRef = this._createOverlay(_config);
const container = this._attachContainer(overlayRef, _config);
const ref = new MatBottomSheetRef<T, R>(container, overlayRef);
if (componentOrTemplateRef instanceof TemplateRef) {
container.attachTemplatePortal(new TemplatePortal<T>(componentOrTemplateRef, null!, {
$implicit: _config.data,
bottomSheetRef: ref
} as any));
} else {
const portal = new ComponentPortal(componentOrTemplateRef, undefined,
this._createInjector(_config, ref));
const contentRef = container.attachComponentPortal(portal);
ref.instance = contentRef.instance;
}
// When the bottom sheet is dismissed, clear the reference to it.
ref.afterDismissed().subscribe(() => {
// Clear the bottom sheet ref if it hasn't already been replaced by a newer one.
if (this._openedBottomSheetRef == ref) {
this._openedBottomSheetRef = null;
}
});
if (this._openedBottomSheetRef) {
// If a bottom sheet is already in view, dismiss it and enter the
// new bottom sheet after exit animation is complete.
this._openedBottomSheetRef.afterDismissed().subscribe(() => ref.containerInstance.enter());
this._openedBottomSheetRef.dismiss();
} else {
// If no bottom sheet is in view, enter the new bottom sheet.
ref.containerInstance.enter();
}
this._openedBottomSheetRef = ref;
return ref;
}
/**
* Dismisses the currently-visible bottom sheet.
* @param result Data to pass to the bottom sheet instance.
*/
dismiss<R = any>(result?: R): void {
if (this._openedBottomSheetRef) {
this._openedBottomSheetRef.dismiss(result);
}
}
ngOnDestroy() {
if (this._bottomSheetRefAtThisLevel) {
this._bottomSheetRefAtThisLevel.dismiss();
}
}
/**
* Attaches the bottom sheet container component to the overlay.
*/
private _attachContainer(overlayRef: OverlayRef,
config: MatBottomSheetConfig): MatBottomSheetContainer {
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
const injector = Injector.create({
parent: userInjector || this._injector,
providers: [{provide: MatBottomSheetConfig, useValue: config}]
});
const containerPortal =
new ComponentPortal(MatBottomSheetContainer, config.viewContainerRef, injector);
const containerRef: ComponentRef<MatBottomSheetContainer> = overlayRef.attach(containerPortal);
return containerRef.instance;
}
/**
* Creates a new overlay and places it in the correct location.
* @param config The user-specified bottom sheet config.
*/
private _createOverlay(config: MatBottomSheetConfig): OverlayRef {
const overlayConfig = new OverlayConfig({
direction: config.direction,
hasBackdrop: config.hasBackdrop,
disposeOnNavigation: config.closeOnNavigation,
maxWidth: '100%',
scrollStrategy: config.scrollStrategy || this._overlay.scrollStrategies.block(),
positionStrategy: this._overlay.position().global().centerHorizontally().bottom('0')
});
if (config.backdropClass) {
overlayConfig.backdropClass = config.backdropClass;
}
return this._overlay.create(overlayConfig);
}
/**
* Creates an injector to be used inside of a bottom sheet component.
* @param config Config that was used to create the bottom sheet.
* @param bottomSheetRef Reference to the bottom sheet.
*/
private _createInjector<T>(config: MatBottomSheetConfig,
bottomSheetRef: MatBottomSheetRef<T>): Injector {
const userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
const providers: StaticProvider[] = [
{provide: MatBottomSheetRef, useValue: bottomSheetRef},
{provide: MAT_BOTTOM_SHEET_DATA, useValue: config.data}
];
if (config.direction && (!userInjector ||
!userInjector.get<Directionality | null>(Directionality, null, InjectFlags.Optional))) {
providers.push({
provide: Directionality,
useValue: {value: config.direction, change: observableOf()}
});
}
return Injector.create({parent: userInjector || this._injector, providers});
}
}
/**
* Applies default options to the bottom sheet config.
* @param defaults Object containing the default values to which to fall back.
* @param config The configuration to which the defaults will be applied.
* @returns The new configuration object with defaults applied.
*/
function _applyConfigDefaults(defaults: MatBottomSheetConfig,
config?: MatBottomSheetConfig): MatBottomSheetConfig {
return {...defaults, ...config};
}