|
| 1 | +import {Component, ComponentRef, ViewChild, AfterViewInit} from '@angular/core'; |
| 2 | +import { |
| 3 | + BasePortalHost, |
| 4 | + ComponentPortal, |
| 5 | + TemplatePortal |
| 6 | +} from '@angular2-material/core/portal/portal'; |
| 7 | +import {PortalHostDirective} from '@angular2-material/core/portal/portal-directives'; |
| 8 | +import {PromiseCompleter} from '@angular2-material/core/async/promise-completer'; |
| 9 | +import {MdDialogConfig} from './dialog-config'; |
| 10 | +import {MdDialogContentAlreadyAttachedError} from './dialog-errors'; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Internal component that wraps user-provided dialog content. |
| 15 | + */ |
| 16 | +@Component({ |
| 17 | + moduleId: module.id, |
| 18 | + selector: 'md-dialog-container', |
| 19 | + templateUrl: 'dialog-container.html', |
| 20 | + styleUrls: ['dialog-container.css'], |
| 21 | + directives: [PortalHostDirective], |
| 22 | + host: { |
| 23 | + 'class': 'md-dialog-container', |
| 24 | + '[attr.role]': 'dialogConfig?.role' |
| 25 | + } |
| 26 | +}) |
| 27 | +export class MdDialogContainer extends BasePortalHost implements AfterViewInit { |
| 28 | + /** The portal host inside of this container into which the dialog content will be loaded. */ |
| 29 | + @ViewChild(PortalHostDirective) private _portalHost: PortalHostDirective; |
| 30 | + |
| 31 | + /** |
| 32 | + * Completer used to resolve the promise for cases when a portal is attempted to be attached, |
| 33 | + * but AfterViewInit has not yet occured. |
| 34 | + */ |
| 35 | + private _deferredAttachCompleter: PromiseCompleter<ComponentRef<any>>; |
| 36 | + |
| 37 | + /** Portal to be attached upon AfterViewInit. */ |
| 38 | + private _deferredAttachPortal: ComponentPortal<any>; |
| 39 | + |
| 40 | + /** The dialog configuration. */ |
| 41 | + dialogConfig: MdDialogConfig; |
| 42 | + |
| 43 | + /** TODO: internal */ |
| 44 | + ngAfterViewInit() { |
| 45 | + // If there was an attempted call to `attachComponentPortal` before this lifecycle stage, |
| 46 | + // we actually perform the attachment now that the `@ViewChild` is resolved. |
| 47 | + if (this._deferredAttachCompleter) { |
| 48 | + this.attachComponentPortal(this._deferredAttachPortal).then(componentRef => { |
| 49 | + this._deferredAttachCompleter.resolve(componentRef); |
| 50 | + |
| 51 | + this._deferredAttachPortal = null; |
| 52 | + this._deferredAttachCompleter = null; |
| 53 | + }, () => { |
| 54 | + this._deferredAttachCompleter.reject(); |
| 55 | + this._deferredAttachCompleter = null; |
| 56 | + this._deferredAttachPortal = null; |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** Attach a portal as content to this dialog container. */ |
| 62 | + attachComponentPortal<T>(portal: ComponentPortal<T>): Promise<ComponentRef<T>> { |
| 63 | + if (this._portalHost) { |
| 64 | + if (this._portalHost.hasAttached()) { |
| 65 | + throw new MdDialogContentAlreadyAttachedError(); |
| 66 | + } |
| 67 | + |
| 68 | + return this._portalHost.attachComponentPortal(portal); |
| 69 | + } else { |
| 70 | + // The @ViewChild query for the portalHost is not resolved until AfterViewInit, but this |
| 71 | + // function may be called before this lifecycle event. As such, we defer the attachment of |
| 72 | + // the portal until AfterViewInit. |
| 73 | + if (this._deferredAttachCompleter) { |
| 74 | + throw new MdDialogContentAlreadyAttachedError(); |
| 75 | + } |
| 76 | + |
| 77 | + this._deferredAttachPortal = portal; |
| 78 | + this._deferredAttachCompleter = new PromiseCompleter(); |
| 79 | + return this._deferredAttachCompleter.promise; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + attachTemplatePortal(portal: TemplatePortal): Promise<Map<string, any>> { |
| 84 | + throw Error('Not yet implemented'); |
| 85 | + } |
| 86 | +} |
0 commit comments