Skip to content

Commit 02daf3f

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 94adecd commit 02daf3f

File tree

5 files changed

+81
-40
lines changed

5 files changed

+81
-40
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,28 @@ export class DialogDemo {
7272
<p>It's Jazz!</p>
7373
<p><label>How much? <input #howMuch></label></p>
7474
<p> {{ data.message }} </p>
75-
<button type="button" (click)="dialogRef.close(howMuch.value)">Close dialog</button>`
75+
<button type="button" (click)="dialogRef.close(howMuch.value)">Close dialog</button>
76+
<button (click)="togglePosition()">Change position</button>`
7677
})
7778
export class JazzDialog {
79+
private _positionToggle = false;
80+
7881
constructor(
7982
public dialogRef: MdDialogRef<JazzDialog>,
8083
@Inject(MD_DIALOG_DATA) public data: any) { }
84+
85+
togglePosition(): void {
86+
this._positionToggle = !this._positionToggle;
87+
88+
if (this._positionToggle) {
89+
this.dialogRef.updateDimensions(null, null, {
90+
top: '25px',
91+
left: '25px'
92+
});
93+
} else {
94+
this.dialogRef.updateDimensions();
95+
}
96+
}
8197
}
8298

8399

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
import {MdDialogContainer, MdDialogContainerAnimationState} from './dialog-container';
@@ -50,4 +51,29 @@ export class MdDialogRef<T> {
5051
afterClosed(): Observable<any> {
5152
return this._afterClosed.asObservable();
5253
}
54+
55+
/**
56+
* Updates the dialog's dimensions.
57+
* @param width New width of the dialog.
58+
* @param height New height of the dialog.
59+
* @param position New position of the dialog.
60+
*/
61+
updateDimensions(width?: string, height?: string, position?: DialogPosition): void {
62+
let strategy = this._overlayRef.getState().positionStrategy as GlobalPositionStrategy;
63+
64+
if (position && (position.left || position.right)) {
65+
position.left ? strategy.left(position.left) : strategy.right(position.right);
66+
} else {
67+
strategy.centerHorizontally();
68+
}
69+
70+
if (position && (position.top || position.bottom)) {
71+
position.top ? strategy.top(position.top) : strategy.bottom(position.bottom);
72+
} else {
73+
strategy.centerVertically();
74+
}
75+
76+
strategy.width(width).height(height);
77+
this._overlayRef.updatePosition();
78+
}
5379
}

src/lib/dialog/dialog.spec.ts

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

277+
it('should allow for the dimensions to be updated', () => {
278+
let dialogRef = dialog.open(PizzaMsg, {
279+
position: {
280+
left: '250px'
281+
}
282+
});
283+
284+
viewContainerFixture.detectChanges();
285+
286+
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
287+
288+
expect(overlayPane.style.marginLeft).toBe('250px');
289+
290+
dialogRef.updateDimensions(null, null, {
291+
left: '500px'
292+
});
293+
294+
expect(overlayPane.style.marginLeft).toBe('500px');
295+
});
296+
277297
it('should close all of the dialogs', async(() => {
278298
dialog.open(PizzaMsg);
279299
dialog.open(PizzaMsg);

src/lib/dialog/dialog.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,16 @@ export class MdDialog {
9292

9393
/**
9494
* Creates the overlay into which the dialog will be loaded.
95-
* @param dialogConfig The dialog configuration.
95+
* @param config The dialog configuration.
9696
* @returns A promise resolving to the OverlayRef for the created overlay.
9797
*/
98-
private _createOverlay(dialogConfig: MdDialogConfig): OverlayRef {
99-
let overlayState = this._getOverlayState(dialogConfig);
98+
private _createOverlay(config: MdDialogConfig): OverlayRef {
99+
let overlayState = new OverlayState();
100+
let strategy = this._overlay.position().global();
101+
102+
overlayState.hasBackdrop = true;
103+
overlayState.positionStrategy = strategy;
104+
100105
return this._overlay.create(overlayState);
101106
}
102107

@@ -129,10 +134,11 @@ export class MdDialog {
129134
componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
130135
dialogContainer: MdDialogContainer,
131136
overlayRef: OverlayRef,
132-
config?: MdDialogConfig): MdDialogRef<T> {
137+
config: MdDialogConfig): MdDialogRef<T> {
133138
// Create a reference to the dialog we're creating in order to give the user a handle
134139
// to modify and close it.
135-
let dialogRef = new MdDialogRef(overlayRef, dialogContainer) as MdDialogRef<T>;
140+
141+
let dialogRef = new MdDialogRef<T>(overlayRef, dialogContainer);
136142

137143
if (!config.disableClose) {
138144
// When the dialog backdrop is clicked, we want to close it.
@@ -153,37 +159,9 @@ export class MdDialog {
153159
dialogRef.componentInstance = contentRef.instance;
154160
}
155161

156-
return dialogRef;
157-
}
158-
159-
/**
160-
* Creates an overlay state from a dialog config.
161-
* @param dialogConfig The dialog configuration.
162-
* @returns The overlay configuration.
163-
*/
164-
private _getOverlayState(dialogConfig: MdDialogConfig): OverlayState {
165-
let state = new OverlayState();
166-
let strategy = this._overlay.position().global();
167-
let position = dialogConfig.position;
168-
169-
state.hasBackdrop = true;
170-
state.positionStrategy = strategy;
171-
172-
if (position && (position.left || position.right)) {
173-
position.left ? strategy.left(position.left) : strategy.right(position.right);
174-
} else {
175-
strategy.centerHorizontally();
176-
}
177-
178-
if (position && (position.top || position.bottom)) {
179-
position.top ? strategy.top(position.top) : strategy.bottom(position.bottom);
180-
} else {
181-
strategy.centerVertically();
182-
}
162+
dialogRef.updateDimensions(config.width, config.height, config.position);
183163

184-
strategy.width(dialogConfig.width).height(dialogConfig.height);
185-
186-
return state;
164+
return dialogRef;
187165
}
188166

189167
/**
@@ -221,10 +199,10 @@ export class MdDialog {
221199

222200
/**
223201
* Applies default options to the dialog config.
224-
* @param dialogConfig Config to be modified.
202+
* @param config Config to be modified.
225203
* @returns The new configuration object.
226204
*/
227-
function _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
228-
return extendObject(new MdDialogConfig(), dialogConfig);
205+
function _applyConfigDefaults(config: MdDialogConfig): MdDialogConfig {
206+
return extendObject(new MdDialogConfig(), config);
229207
}
230208

0 commit comments

Comments
 (0)