Skip to content

Commit 0c174d2

Browse files
committed
feat(dialog): allow for the dialog dimensions to be updated
Adds an `updateDimensions` method to the `MdDialogRef` which allows the user to update a dialog's dimensions after it has been created. Fixes #2930.
1 parent 8d900e0 commit 0c174d2

File tree

5 files changed

+86
-47
lines changed

5 files changed

+86
-47
lines changed

src/demo-app/dialog/dialog-demo.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,32 @@ export class DialogDemo {
5757
@Component({
5858
selector: 'demo-jazz-dialog',
5959
template: `
60-
<p>It's Jazz!</p>
61-
<p><label>How much? <input #howMuch></label></p>
62-
<p> {{ jazzMessage }} </p>
63-
<button type="button" (click)="dialogRef.close(howMuch.value)">Close dialog</button>`
60+
<p>It's Jazz!</p>
61+
<p><label>How much? <input #howMuch></label></p>
62+
<p>{{ jazzMessage }}</p>
63+
<button (click)="dialogRef.close(howMuch.value)">Close dialog</button>
64+
<button (click)="togglePosition()">Change position</button>
65+
`
6466
})
6567
export class JazzDialog {
68+
private _positionToggle = false;
69+
6670
jazzMessage = 'Jazzy jazz jazz';
6771

6872
constructor(public dialogRef: MdDialogRef<JazzDialog>) { }
73+
74+
togglePosition(): void {
75+
this._positionToggle = !this._positionToggle;
76+
77+
if (this._positionToggle) {
78+
this.dialogRef.updateDimensions(null, null, {
79+
top: '25px',
80+
left: '25px'
81+
});
82+
} else {
83+
this.dialogRef.updateDimensions();
84+
}
85+
}
6986
}
7087

7188

@@ -104,7 +121,7 @@ export class JazzDialog {
104121
color="primary"
105122
href="https://en.wikipedia.org/wiki/Neptune"
106123
target="_blank">Read more on Wikipedia</a>
107-
124+
108125
<button
109126
md-button
110127
color="secondary"

src/lib/core/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export {
5353
OverlayOrigin,
5454
OverlayModule,
5555
} from './overlay/overlay-directives';
56+
export * from './overlay/position/global-position-strategy';
5657
export * from './overlay/position/connected-position-strategy';
5758
export * from './overlay/position/connected-position';
5859
export {ScrollDispatcher} from './overlay/scroll/scroll-dispatcher';

src/lib/dialog/dialog-ref.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {OverlayRef} from '../core';
1+
import {OverlayRef, GlobalPositionStrategy} from '../core';
2+
import {DialogPosition} from './dialog-config';
23
import {Observable} from 'rxjs/Observable';
34
import {Subject} from 'rxjs/Subject';
45

@@ -35,4 +36,29 @@ export class MdDialogRef<T> {
3536
afterClosed(): Observable<any> {
3637
return this._afterClosed.asObservable();
3738
}
39+
40+
/**
41+
* Updates the dialog's dimensions.
42+
* @param width New width of the dialog.
43+
* @param height New height of the dialog.
44+
* @param position New position of the dialog.
45+
*/
46+
updateDimensions(width?: string, height?: string, position?: DialogPosition): void {
47+
let strategy = this._overlayRef.getState().positionStrategy as GlobalPositionStrategy;
48+
49+
if (position && (position.left || position.right)) {
50+
position.left ? strategy.left(position.left) : strategy.right(position.right);
51+
} else {
52+
strategy.centerHorizontally();
53+
}
54+
55+
if (position && (position.top || position.bottom)) {
56+
position.top ? strategy.top(position.top) : strategy.bottom(position.bottom);
57+
} else {
58+
strategy.centerVertically();
59+
}
60+
61+
strategy.width(width).height(height);
62+
this._overlayRef.updatePosition();
63+
}
3864
}

src/lib/dialog/dialog.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,26 @@ describe('MdDialog', () => {
259259
expect(overlayPane.style.marginRight).toBe('125px');
260260
});
261261

262+
it('should allow for the dimensions to be updated', () => {
263+
let dialogRef = dialog.open(PizzaMsg, {
264+
position: {
265+
left: '250px'
266+
}
267+
});
268+
269+
viewContainerFixture.detectChanges();
270+
271+
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
272+
273+
expect(overlayPane.style.marginLeft).toBe('250px');
274+
275+
dialogRef.updateDimensions(null, null, {
276+
left: '500px'
277+
});
278+
279+
expect(overlayPane.style.marginLeft).toBe('500px');
280+
});
281+
262282
it('should close all of the dialogs', () => {
263283
dialog.open(PizzaMsg);
264284
dialog.open(PizzaMsg);

src/lib/dialog/dialog.ts

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ export class MdDialog {
8888

8989
/**
9090
* Creates the overlay into which the dialog will be loaded.
91-
* @param dialogConfig The dialog configuration.
91+
* @param config The dialog configuration.
9292
* @returns A promise resolving to the OverlayRef for the created overlay.
9393
*/
94-
private _createOverlay(dialogConfig: MdDialogConfig): OverlayRef {
95-
let overlayState = this._getOverlayState(dialogConfig);
94+
private _createOverlay(config: MdDialogConfig): OverlayRef {
95+
let overlayState = new OverlayState();
96+
let strategy = this._overlay.position().global();
97+
98+
overlayState.hasBackdrop = true;
99+
overlayState.positionStrategy = strategy;
100+
96101
return this._overlay.create(overlayState);
97102
}
98103

@@ -124,10 +129,10 @@ export class MdDialog {
124129
component: ComponentType<T>,
125130
dialogContainer: MdDialogContainer,
126131
overlayRef: OverlayRef,
127-
config?: MdDialogConfig): MdDialogRef<T> {
132+
config: MdDialogConfig): MdDialogRef<T> {
128133
// Create a reference to the dialog we're creating in order to give the user a handle
129134
// to modify and close it.
130-
let dialogRef = <MdDialogRef<T>> new MdDialogRef(overlayRef);
135+
let dialogRef = new MdDialogRef<T>(overlayRef);
131136

132137
if (!dialogContainer.dialogConfig.disableClose) {
133138
// When the dialog backdrop is clicked, we want to close it.
@@ -140,47 +145,17 @@ export class MdDialog {
140145
// We create an injector specifically for the component we're instantiating so that it can
141146
// inject the MdDialogRef. This allows a component loaded inside of a dialog to close itself
142147
// and, optionally, to return a value.
143-
let userInjector = config && config.viewContainerRef && config.viewContainerRef.injector;
148+
let userInjector = config.viewContainerRef && config.viewContainerRef.injector;
144149
let dialogInjector = new DialogInjector(dialogRef, userInjector || this._injector);
145-
146150
let contentPortal = new ComponentPortal(component, null, dialogInjector);
147-
148151
let contentRef = dialogContainer.attachComponentPortal(contentPortal);
152+
149153
dialogRef.componentInstance = contentRef.instance;
154+
dialogRef.updateDimensions(config.width, config.height, config.position);
150155

151156
return dialogRef;
152157
}
153158

154-
/**
155-
* Creates an overlay state from a dialog config.
156-
* @param dialogConfig The dialog configuration.
157-
* @returns The overlay configuration.
158-
*/
159-
private _getOverlayState(dialogConfig: MdDialogConfig): OverlayState {
160-
let state = new OverlayState();
161-
let strategy = this._overlay.position().global();
162-
let position = dialogConfig.position;
163-
164-
state.hasBackdrop = true;
165-
state.positionStrategy = strategy;
166-
167-
if (position && (position.left || position.right)) {
168-
position.left ? strategy.left(position.left) : strategy.right(position.right);
169-
} else {
170-
strategy.centerHorizontally();
171-
}
172-
173-
if (position && (position.top || position.bottom)) {
174-
position.top ? strategy.top(position.top) : strategy.bottom(position.bottom);
175-
} else {
176-
strategy.centerVertically();
177-
}
178-
179-
strategy.width(dialogConfig.width).height(dialogConfig.height);
180-
181-
return state;
182-
}
183-
184159
/**
185160
* Removes a dialog from the array of open dialogs.
186161
* @param dialogRef Dialog to be removed.
@@ -201,10 +176,10 @@ export class MdDialog {
201176

202177
/**
203178
* Applies default options to the dialog config.
204-
* @param dialogConfig Config to be modified.
179+
* @param config Config to be modified.
205180
* @returns The new configuration object.
206181
*/
207-
function _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
208-
return extendObject(new MdDialogConfig(), dialogConfig);
182+
function _applyConfigDefaults(config: MdDialogConfig): MdDialogConfig {
183+
return extendObject(new MdDialogConfig(), config);
209184
}
210185

0 commit comments

Comments
 (0)