Skip to content

Commit 2bb20e5

Browse files
committed
add OpenCloseRect as an additional option for openFrom and closeTo
1 parent 13d992e commit 2bb20e5

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

src/lib/components/dialog/mdl-dialog-configuration.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export interface IMdlDialogAction {
3232
isClosingAction?: boolean;
3333
}
3434

35+
/**
36+
* rect from where the dialog should be opened or closed to.
37+
*/
38+
export interface OpenCloseRect {
39+
height: number;
40+
left: number;
41+
top: number;
42+
width: number;
43+
}
3544
/**
3645
* Dialog configuration for all dialogs (simple or custom)
3746
*/
@@ -67,12 +76,12 @@ export interface IMdlDialogConfiguration {
6776
/**
6877
* MdlButtonComponent or mouse event the dialog open animation should start from.
6978
*/
70-
openFrom?: MdlButtonComponent | MouseEvent;
79+
openFrom?: MdlButtonComponent | MouseEvent | OpenCloseRect;
7180

7281
/**
7382
* MdlButtonComponent or mouse event the dialog close animation shoul end.
7483
*/
75-
closeTo?: MdlButtonComponent | MouseEvent;
84+
closeTo?: MdlButtonComponent | MouseEvent | OpenCloseRect;
7685

7786
}
7887

src/lib/components/dialog/mdl-dialog-host.component.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
MIN_DIALOG_Z_INDEX,
1717
MDL_CONFIGUARTION
1818
} from './mdl-dialog.service';
19-
import { IMdlDialogConfiguration } from './mdl-dialog-configuration';
19+
import { IMdlDialogConfiguration, OpenCloseRect } from './mdl-dialog-configuration';
2020
import { MdlButtonComponent } from '../button/mdl-button.component';
2121
import { InternalMdlDialogReference } from './internal-dialog-reference';
2222

@@ -242,26 +242,37 @@ export class MdlDialogHostComponent implements OnInit {
242242
return this.config.animate;
243243
}
244244

245-
private getClientRect(input: MdlButtonComponent | MouseEvent): ClientRect {
245+
private getClientRect(input: MdlButtonComponent | MouseEvent | OpenCloseRect): OpenCloseRect {
246246

247247
if(input instanceof MdlButtonComponent){
248248

249249
const elRef = (input as MdlButtonComponent).elementRef;
250-
return elRef.nativeElement.getBoundingClientRect();
250+
const rect: ClientRect = elRef.nativeElement.getBoundingClientRect();
251+
return this.createOpenCloseRect(rect);
251252

252-
} else if(input instanceof MouseEvent){
253+
} else if (input instanceof MouseEvent) {
253254

254255
const evt: MouseEvent = input as MouseEvent;
255256
// just to make it possible to test this code with a fake event - target is
256257
// readonly and con not be mutated.
257258
const htmlElement = (evt.target || evt['testtarget']) as HTMLElement;
258-
return htmlElement.getBoundingClientRect();
259+
const rect: ClientRect = htmlElement.getBoundingClientRect();
260+
return this.createOpenCloseRect(rect);
259261

260262
}
263+
return input as OpenCloseRect;
264+
}
261265

266+
private createOpenCloseRect(rect: ClientRect) : OpenCloseRect {
267+
return {
268+
height: rect.top - rect.bottom,
269+
left: rect.left,
270+
top: rect.top,
271+
width: rect.right-rect.left
272+
}
262273
}
263274

264-
private getCenterInScreen(rect: ClientRect) {
275+
private getCenterInScreen(rect: OpenCloseRect) {
265276
return {
266277
cx: Math.round(rect.left + (rect.width / 2)),
267278
cy: Math.round(rect.top + (rect.height / 2))

src/lib/components/dialog/mdl-dialog.service.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from './mdl-dialog.service';
1313
import { MdlDialogHostComponent } from './mdl-dialog-host.component';
1414
import { MdlSimpleDialogComponent } from './mdl-simple-dialog.component';
15-
import { IMdlDialogAction } from './mdl-dialog-configuration';
15+
import { IMdlDialogAction, OpenCloseRect } from './mdl-dialog-configuration';
1616
import { MdlDialogOutletModule } from '../dialog-outlet/index';
1717
import { MdlBackdropOverlayComponent } from '../dialog-outlet/mdl-backdrop-overlay.component';
1818
import { MdlDialogOutletService } from '../dialog-outlet/mdl-dialog-outlet.service';
@@ -371,6 +371,26 @@ describe('Service: MdlDialog', () => {
371371

372372
}));
373373

374+
it('should open a dialog from a OpenCloseRect ', async(() => {
375+
376+
let fixture = TestBed.createComponent(MdlTestViewComponent);
377+
fixture.detectChanges();
378+
379+
let p = mdlDialogService.showCustomDialog({
380+
component: TestCustomDialog,
381+
styles: {'width':'350px'},
382+
classes: 'a b',
383+
openFrom: ({ height: 10, left: 0, top: 0, width: 0} as OpenCloseRect)
384+
});
385+
386+
p.subscribe( ( dialogRef ) => {
387+
388+
dialogRef.hide();
389+
390+
});
391+
392+
}));
393+
374394

375395
});
376396

0 commit comments

Comments
 (0)