Skip to content

Commit a71a5af

Browse files
crisbetotinayuangao
authored andcommitted
feat(dialog): allow for the dialog dimensions to be updated (#2940)
* 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. * refactor: split dimensions method into two * refactor: rename updateDimensions to updateSize * refactor: add back _getOverlayState method * chore: fix aot issue
1 parent ab77fa9 commit a71a5af

File tree

5 files changed

+119
-40
lines changed

5 files changed

+119
-40
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,29 @@ 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 dimensions</button>`
7677
})
7778
export class JazzDialog {
79+
private _dimesionToggle = false;
80+
7881
constructor(
7982
public dialogRef: MdDialogRef<JazzDialog>,
8083
@Inject(MD_DIALOG_DATA) public data: any) { }
84+
85+
togglePosition(): void {
86+
this._dimesionToggle = !this._dimesionToggle;
87+
88+
if (this._dimesionToggle) {
89+
this.dialogRef
90+
.updateSize('500px', '500px')
91+
.updatePosition({ top: '25px', left: '25px' });
92+
} else {
93+
this.dialogRef
94+
.updateSize()
95+
.updatePosition();
96+
}
97+
}
8198
}
8299

83100

src/lib/core/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export {
5050
OverlayOrigin,
5151
OverlayModule,
5252
} from './overlay/overlay-directives';
53+
export * from './overlay/position/global-position-strategy';
5354
export * from './overlay/position/connected-position-strategy';
5455
export * from './overlay/position/connected-position';
5556
export {ScrollDispatcher} from './overlay/scroll/scroll-dispatcher';

src/lib/dialog/dialog-ref.ts

Lines changed: 42 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';
@@ -51,4 +52,44 @@ export class MdDialogRef<T> {
5152
afterClosed(): Observable<any> {
5253
return this._afterClosed.asObservable();
5354
}
55+
56+
/**
57+
* Updates the dialog's position.
58+
* @param position New dialog position.
59+
*/
60+
updatePosition(position?: DialogPosition): this {
61+
let strategy = this._getPositionStrategy();
62+
63+
if (position && (position.left || position.right)) {
64+
position.left ? strategy.left(position.left) : strategy.right(position.right);
65+
} else {
66+
strategy.centerHorizontally();
67+
}
68+
69+
if (position && (position.top || position.bottom)) {
70+
position.top ? strategy.top(position.top) : strategy.bottom(position.bottom);
71+
} else {
72+
strategy.centerVertically();
73+
}
74+
75+
this._overlayRef.updatePosition();
76+
77+
return this;
78+
}
79+
80+
/**
81+
* Updates the dialog's width and height.
82+
* @param width New width of the dialog.
83+
* @param height New height of the dialog.
84+
*/
85+
updateSize(width = 'auto', height = 'auto'): this {
86+
this._getPositionStrategy().width(width).height(height);
87+
this._overlayRef.updatePosition();
88+
return this;
89+
}
90+
91+
/** Fetches the position strategy object from the overlay ref. */
92+
private _getPositionStrategy(): GlobalPositionStrategy {
93+
return this._overlayRef.getState().positionStrategy as GlobalPositionStrategy;
94+
}
5495
}

src/lib/dialog/dialog.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,38 @@ describe('MdDialog', () => {
276276
expect(overlayPane.style.marginRight).toBe('125px');
277277
});
278278

279+
it('should allow for the position to be updated', () => {
280+
let dialogRef = dialog.open(PizzaMsg, {
281+
position: {
282+
left: '250px'
283+
}
284+
});
285+
286+
viewContainerFixture.detectChanges();
287+
288+
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
289+
290+
expect(overlayPane.style.marginLeft).toBe('250px');
291+
292+
dialogRef.updatePosition({ left: '500px' });
293+
294+
expect(overlayPane.style.marginLeft).toBe('500px');
295+
});
296+
297+
it('should allow for the dimensions to be updated', () => {
298+
let dialogRef = dialog.open(PizzaMsg, { width: '100px' });
299+
300+
viewContainerFixture.detectChanges();
301+
302+
let overlayPane = overlayContainerElement.querySelector('.cdk-overlay-pane') as HTMLElement;
303+
304+
expect(overlayPane.style.width).toBe('100px');
305+
306+
dialogRef.updateSize('200px');
307+
308+
expect(overlayPane.style.width).toBe('200px');
309+
});
310+
279311
it('should close all of the dialogs', async(() => {
280312
dialog.open(PizzaMsg);
281313
dialog.open(PizzaMsg);

src/lib/dialog/dialog.ts

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,27 @@ 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 = this._getOverlayState(config);
100100
return this._overlay.create(overlayState);
101101
}
102102

103+
/**
104+
* Creates an overlay state from a dialog config.
105+
* @param dialogConfig The dialog configuration.
106+
* @returns The overlay configuration.
107+
*/
108+
private _getOverlayState(dialogConfig: MdDialogConfig): OverlayState {
109+
let overlayState = new OverlayState();
110+
overlayState.hasBackdrop = true;
111+
overlayState.positionStrategy = this._overlay.position().global();
112+
113+
return overlayState;
114+
}
115+
103116
/**
104117
* Attaches an MdDialogContainer to a dialog's already-created overlay.
105118
* @param overlay Reference to the dialog's underlying overlay.
@@ -129,10 +142,11 @@ export class MdDialog {
129142
componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
130143
dialogContainer: MdDialogContainer,
131144
overlayRef: OverlayRef,
132-
config?: MdDialogConfig): MdDialogRef<T> {
145+
config: MdDialogConfig): MdDialogRef<T> {
133146
// Create a reference to the dialog we're creating in order to give the user a handle
134147
// to modify and close it.
135-
let dialogRef = new MdDialogRef(overlayRef, dialogContainer) as MdDialogRef<T>;
148+
149+
let dialogRef = new MdDialogRef<T>(overlayRef, dialogContainer);
136150

137151
if (!config.disableClose) {
138152
// When the dialog backdrop is clicked, we want to close it.
@@ -153,37 +167,11 @@ export class MdDialog {
153167
dialogRef.componentInstance = contentRef.instance;
154168
}
155169

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-
}
183-
184-
strategy.width(dialogConfig.width).height(dialogConfig.height);
170+
dialogRef
171+
.updateSize(config.width, config.height)
172+
.updatePosition(config.position);
185173

186-
return state;
174+
return dialogRef;
187175
}
188176

189177
/**
@@ -221,10 +209,10 @@ export class MdDialog {
221209

222210
/**
223211
* Applies default options to the dialog config.
224-
* @param dialogConfig Config to be modified.
212+
* @param config Config to be modified.
225213
* @returns The new configuration object.
226214
*/
227-
function _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
228-
return extendObject(new MdDialogConfig(), dialogConfig);
215+
function _applyConfigDefaults(config: MdDialogConfig): MdDialogConfig {
216+
return extendObject(new MdDialogConfig(), config);
229217
}
230218

0 commit comments

Comments
 (0)