Skip to content

Commit

Permalink
feat(dialog): allow for an object literal to be passed on init
Browse files Browse the repository at this point in the history
Adds the ability to pass in an object literal, that matches the signature of `MdDialogConfig`, when opening a dialog.
  • Loading branch information
crisbeto committed Nov 8, 2016
1 parent 309d54c commit 79147cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/lib/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class MdDialogConfig {
role?: DialogRole = 'dialog';

/** Whether the user can use escape or clicking outside to close a modal. */
disableClose = false;
disableClose?: boolean = false;

// TODO(jelbourn): add configuration for size, lifecycle hooks, ARIA labelling.
}
65 changes: 27 additions & 38 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {By} from '@angular/platform-browser';
import {NgModule, Component, Directive, ViewChild, ViewContainerRef} from '@angular/core';
import {MdDialog, MdDialogModule} from './dialog';
import {OverlayContainer} from '../core';
import {MdDialogConfig} from './dialog-config';
import {MdDialogRef} from './dialog-ref';
import {MdDialogContainer} from './dialog-container';

Expand Down Expand Up @@ -48,10 +47,9 @@ describe('MdDialog', () => {
});

it('should open a dialog with a component', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;

let dialogRef = dialog.open(PizzaMsg, config);
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand Down Expand Up @@ -79,11 +77,7 @@ describe('MdDialog', () => {
});

it('should apply the configured role to the dialog element', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
config.role = 'alertdialog';

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, { role: 'alertdialog' });

viewContainerFixture.detectChanges();

Expand All @@ -92,10 +86,9 @@ describe('MdDialog', () => {
});

it('should close a dialog and get back a result', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;

let dialogRef = dialog.open(PizzaMsg, config);
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -112,10 +105,9 @@ describe('MdDialog', () => {


it('should close a dialog via the escape key', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -129,10 +121,9 @@ describe('MdDialog', () => {
});

it('should close when clicking on the overlay backdrop', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -144,11 +135,10 @@ describe('MdDialog', () => {

describe('disableClose option', () => {
it('should prevent closing via clicks on the backdrop', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
config.disableClose = true;

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -159,11 +149,10 @@ describe('MdDialog', () => {
});

it('should prevent closing via the escape key', () => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
config.disableClose = true;

dialog.open(PizzaMsg, config);
dialog.open(PizzaMsg, {
disableClose: true,
viewContainerRef: testViewContainerRef
});

viewContainerFixture.detectChanges();

Expand All @@ -189,10 +178,10 @@ describe('MdDialog', () => {
});

it('should focus the first tabbable element of the dialog on open', fakeAsync(() => {
let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

dialog.open(PizzaMsg, config);
viewContainerFixture.detectChanges();
flushMicrotasks();

Expand All @@ -207,10 +196,10 @@ describe('MdDialog', () => {
document.body.appendChild(button);
button.focus();

let config = new MdDialogConfig();
config.viewContainerRef = testViewContainerRef;
let dialogRef = dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef
});

let dialogRef = dialog.open(PizzaMsg, config);
viewContainerFixture.detectChanges();
flushMicrotasks();

Expand Down
13 changes: 12 additions & 1 deletion src/lib/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class MdDialog {
* @param component Type of the component to load into the load.
* @param config
*/
open<T>(component: ComponentType<T>, config = new MdDialogConfig()): MdDialogRef<T> {
open<T>(component: ComponentType<T>, config?: MdDialogConfig): MdDialogRef<T> {
config = this._applyConfigDefaults(config);

let overlayRef = this._createOverlay(config);
let dialogContainer = this._attachDialogContainer(overlayRef, config);

Expand Down Expand Up @@ -125,6 +127,15 @@ export class MdDialog {

return state;
}

/**
* Applies default options to the dialog config.
* @param dialogConfig Config to be modified.
* @returns The new configuration object.
*/
private _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
return Object.assign(new MdDialogConfig(), dialogConfig);
}
}


Expand Down

0 comments on commit 79147cf

Please sign in to comment.