|
| 1 | +import {Injector, ComponentRef, Injectable} from '@angular/core'; |
| 2 | +import {Overlay} from '@angular2-material/core/overlay/overlay'; |
| 3 | +import {OverlayRef} from '@angular2-material/core/overlay/overlay-ref'; |
| 4 | +import {OverlayState} from '@angular2-material/core/overlay/overlay-state'; |
| 5 | +import {ComponentPortal} from '@angular2-material/core/portal/portal'; |
| 6 | +import {ComponentType} from '@angular2-material/core/overlay/generic-component-type'; |
| 7 | +import {MdDialogConfig} from './dialog-config'; |
| 8 | +import {MdDialogRef} from './dialog-ref'; |
| 9 | +import {DialogInjector} from './dialog-injector'; |
| 10 | +import {MdDialogContainer} from './dialog-container'; |
| 11 | + |
| 12 | + |
| 13 | +export {MdDialogConfig} from './dialog-config'; |
| 14 | +export {MdDialogRef} from './dialog-ref'; |
| 15 | + |
| 16 | + |
| 17 | +// TODO(jelbourn): add shortcuts for `alert` and `confirm`. |
| 18 | +// TODO(jelbourn): add support for opening with a TemplateRef |
| 19 | +// TODO(jelbourn): add `closeAll` method |
| 20 | +// TODO(jelbourn): add backdrop |
| 21 | +// TODO(jelbourn): default dialog config |
| 22 | +// TODO(jelbourn): focus trapping |
| 23 | +// TODO(jelbourn): potentially change API from accepting component constructor to component factory. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Service to open Material Design modal dialogs. |
| 29 | + */ |
| 30 | +@Injectable() |
| 31 | +export class MdDialog { |
| 32 | + constructor(private _overlay: Overlay, private _injector: Injector) { } |
| 33 | + |
| 34 | + /** |
| 35 | + * Opens a modal dialog containing the given component. |
| 36 | + * @param component Type of the component to load into the load. |
| 37 | + * @param config |
| 38 | + */ |
| 39 | + open<T>(component: ComponentType<T>, config: MdDialogConfig): Promise<MdDialogRef<T>> { |
| 40 | + return this._createOverlay(config) |
| 41 | + .then(overlayRef => this._attachDialogContainer(overlayRef, config)) |
| 42 | + .then(containerRef => this._attachDialogContent(component, containerRef)); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates the overlay into which the dialog will be loaded. |
| 47 | + * @param dialogConfig The dialog configuration. |
| 48 | + * @returns A promise resolving to the OverlayRef for the created overlay. |
| 49 | + */ |
| 50 | + private _createOverlay(dialogConfig: MdDialogConfig): Promise<OverlayRef> { |
| 51 | + let overlayState = this._getOverlayState(dialogConfig); |
| 52 | + return this._overlay.create(overlayState); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Attaches an MdDialogContainer to a dialog's already-created overlay. |
| 57 | + * @param overlayRef Reference to the dialog's underlying overlay. |
| 58 | + * @param config The dialog configuration. |
| 59 | + * @returns A promise resolving to a ComponentRef for the attached container. |
| 60 | + */ |
| 61 | + private _attachDialogContainer(overlayRef: OverlayRef, config: MdDialogConfig): |
| 62 | + Promise<ComponentRef<MdDialogContainer>> { |
| 63 | + let containerPortal = new ComponentPortal(MdDialogContainer, config.viewContainerRef); |
| 64 | + return overlayRef.attach(containerPortal).then(containerRef => { |
| 65 | + // Pass the config directly to the container so that it can consume any relevant settings. |
| 66 | + containerRef.instance.dialogConfig = config; |
| 67 | + return containerRef; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Attaches the user-provided component to the already-created MdDialogContainer. |
| 73 | + * @param component The type of component being loaded into the dialog. |
| 74 | + * @param containerRef Reference to the wrapping MdDialogContainer. |
| 75 | + * @returns A promise resolving to the MdDialogRef that should be returned to the user. |
| 76 | + */ |
| 77 | + private _attachDialogContent<T>( |
| 78 | + component: ComponentType<T>, |
| 79 | + containerRef: ComponentRef<MdDialogContainer>): Promise<MdDialogRef<T>> { |
| 80 | + let dialogContainer = containerRef.instance; |
| 81 | + |
| 82 | + // Create a reference to the dialog we're creating in order to give the user a handle |
| 83 | + // to modify and close it. |
| 84 | + let dialogRef = new MdDialogRef(); |
| 85 | + |
| 86 | + // We create an injector specifically for the component we're instantiating so that it can |
| 87 | + // inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself |
| 88 | + // and, optionally, to return a value. |
| 89 | + let dialogInjector = new DialogInjector(dialogRef, this._injector); |
| 90 | + |
| 91 | + let contentPortal = new ComponentPortal(component, null, dialogInjector); |
| 92 | + return dialogContainer.attachComponentPortal(contentPortal).then(contentRef => { |
| 93 | + dialogRef.componentInstance = contentRef.instance; |
| 94 | + return dialogRef; |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates an overlay state from a dialog config. |
| 100 | + * @param dialogConfig The dialog configuration. |
| 101 | + * @returns The overlay configuration. |
| 102 | + */ |
| 103 | + private _getOverlayState(dialogConfig: MdDialogConfig): OverlayState { |
| 104 | + let state = new OverlayState(); |
| 105 | + |
| 106 | + state.positionStrategy = this._overlay.position() |
| 107 | + .global() |
| 108 | + .centerHorizontally() |
| 109 | + .centerVertically(); |
| 110 | + |
| 111 | + return state; |
| 112 | + } |
| 113 | +} |
0 commit comments