Service provided with methods that wrap the @angular/material [MatDialog] service and provide an easier experience for simple dialogs.
Note: if no [ViewContainerRef] is provided, [TdDialogService] will throw an error.
TypeScript
import { ViewContainerRef } from '@angular/core'; import { TdDialogService } from '@covalent/core/dialogs'; ... }) export class Demo { constructor(private _dialogService: TdDialogService, private _viewContainerRef: ViewContainerRef) { ... } openAlert(): void { this._dialogService.openAlert({ message: 'This is how simple it is to create an alert with this wrapper service.', disableClose: true | false, // defaults to false viewContainerRef: this._viewContainerRef, //OPTIONAL title: 'Alert', //OPTIONAL, hides if not provided closeButton: 'Close', //OPTIONAL, defaults to 'CLOSE' width: '400px', //OPTIONAL, defaults to 400px }); } openConfirm(): void { this._dialogService.openConfirm({ message: 'This is how simple it is to create a confirm with this wrapper service. Do you agree?', disableClose: true | false, // defaults to false viewContainerRef: this._viewContainerRef, //OPTIONAL title: 'Confirm', //OPTIONAL, hides if not provided cancelButton: 'Disagree', //OPTIONAL, defaults to 'CANCEL' acceptButton: 'Agree', //OPTIONAL, defaults to 'ACCEPT' isDestructive: false, //OPTIONAL, defaults to false width: '500px', //OPTIONAL, defaults to 400px }).afterClosed().subscribe((accept: boolean) => { if (accept) { // DO SOMETHING } else { // DO SOMETHING ELSE } }); } openPrompt(): void { this._dialogService.openPrompt({ message: 'This is how simple it is to create a prompt with this wrapper service. Prompt something.', disableClose: true | false, // defaults to false viewContainerRef: this._viewContainerRef, //OPTIONAL title: 'Prompt', //OPTIONAL, hides if not provided value: 'Prepopulated value', //OPTIONAL cancelButton: 'Cancel', //OPTIONAL, defaults to 'CANCEL' acceptButton: 'Ok', //OPTIONAL, defaults to 'ACCEPT' width: '400px', //OPTIONAL, defaults to 400px }).afterClosed().subscribe((newValue: string) => { if (newValue) { // DO SOMETHING } else { // DO SOMETHING ELSE } }); } openDraggable(): void { this._dialogService.openDraggable({ component: DraggableDemoComponent, config: { height: '300px' }, // CSS selectors of element(s) inside the component meant to be drag handle(s) dragHandleSelectors: ['.drag-handle'], // Class that will be added to the component signifying drag-ability draggableClass: 'custom-class', }); } }
import { Component } from '@angular/core'; @Component({ selector: 'app-draggable-demo', template: ` <h2>Draggable dialog</h2> <p>Draggable via the drag handle</p> <button mat-icon-button class="drag-handle"> <mat-icon>drag_handle</mat-icon> </button> `, }) export class DraggableDemoComponent {}
Import the [CovalentDialogsModule] in your NgModule:
import { CovalentDialogsModule } from '@covalent/core/dialogs'; @NgModule({ imports: [ CovalentDialogsModule, ... ], ... }) export class MyModule {}
After that, just inject [TdDialogService] and use it for your dialogs.
A utility to make a draggable dialog resizable.
constructor( private _dialogService: TdDialogService, @Inject(DOCUMENT) private _document: any, private _renderer2: Renderer2, ) {}
const { matDialogRef, dragRefSubject, }: IDraggableRefs<DraggableResizableDialogComponent> = this._dialogService.openDraggable({ component: DraggableResizableDialogComponent, // CSS selectors of element(s) inside the component meant to be drag handle(s) dragHandleSelectors: ['.drag-handle'], }); let resizableDraggableDialog: ResizableDraggableDialog; dragRefSubject.subscribe((dragRf: DragRef) => { resizableDraggableDialog = new ResizableDraggableDialog(this._document, this._renderer2, matDialogRef, dragRf); }); // Detach resize-ability event listeners after dialog closes matDialogRef.afterClosed().subscribe(() => resizableDraggableDialog.detach());
A component that can be utilized to create a dialog with a toolbar
@Component({ template: ` <td-window-dialog [title]="'Title'" [toolbarColor]="'accent'" [closeLabel]="'Close'" (closed)="closed.emit()" > <p>Comes with a handy toolbar</p> </td-window-dialog> `, }) export class DraggableResizableWindowDialogComponent { @Output() closed: EventEmitter<void> = new EventEmitter(); }
const matDialogRef: MatDialogRef<DraggableResizableWindowDialogComponent> = this._dialogService.open( DraggableResizableWindowDialogComponent, ); // listen to close event matDialogRef.componentInstance.closed.subscribe(() => matDialogRef.close());