From e49b8cbc33c2ffc4b4cd0e3cc4976ba7a21eb4dd Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 27 Jan 2021 12:40:47 +0300 Subject: [PATCH] use ng-bootstrap modal inside modal component --- .../lib/components/modal/modal.component.html | 60 ++++++------------ .../lib/components/modal/modal.component.ts | 62 ++++++++++--------- 2 files changed, 52 insertions(+), 70 deletions(-) diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.html b/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.html index b37f5b6ce3..bc70c79fc1 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.html +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.html @@ -1,43 +1,23 @@ - - + + + + + - - diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts b/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts index b1fbef79be..496ddabc41 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts @@ -9,17 +9,14 @@ import { OnDestroy, Optional, Output, - Renderer2, TemplateRef, ViewChild, - ViewChildren, } from '@angular/core'; +import { NgbModal, NgbModalOptions, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; import { fromEvent, Subject } from 'rxjs'; import { debounceTime, distinctUntilChanged, filter, takeUntil } from 'rxjs/operators'; -import { fadeAnimation } from '../../animations/modal.animations'; import { Confirmation } from '../../models/confirmation'; import { ConfirmationService } from '../../services/confirmation.service'; -import { ModalService } from '../../services/modal.service'; import { SUPPRESS_UNSAVED_CHANGES_WARNING } from '../../tokens/suppress-unsaved-changes-warning.token'; import { ButtonComponent } from '../button/button.component'; @@ -28,9 +25,8 @@ export type ModalSize = 'sm' | 'md' | 'lg' | 'xl'; @Component({ selector: 'abp-modal', templateUrl: './modal.component.html', - animations: [fadeAnimation], styleUrls: ['./modal.component.scss'], - providers: [ModalService, SubscriptionService], + providers: [SubscriptionService], }) export class ModalComponent implements OnDestroy { @Input() @@ -59,11 +55,11 @@ export class ModalComponent implements OnDestroy { @Input() modalClass = ''; @Input() size: ModalSize = 'lg'; + @Input() options: NgbModalOptions = {}; @Input() suppressUnsavedChangesWarning = this.suppressUnsavedChangesWarningToken; - @ContentChild(ButtonComponent, { static: false, read: ButtonComponent }) - abpSubmit: ButtonComponent; + @ViewChild('modalContent') modalContent: TemplateRef; @ContentChild('abpHeader', { static: false }) abpHeader: TemplateRef; @@ -71,15 +67,12 @@ export class ModalComponent implements OnDestroy { @ContentChild('abpFooter', { static: false }) abpFooter: TemplateRef; + @ContentChild(ButtonComponent, { static: false, read: ButtonComponent }) + abpSubmit: ButtonComponent; + @ContentChild('abpClose', { static: false, read: ElementRef }) abpClose: ElementRef; - @ViewChild('template', { static: false }) template: TemplateRef; - - @ViewChild('abpModalContent', { static: false }) modalContent: ElementRef; - - @ViewChildren('abp-button') abpButtons; - @Output() readonly visibleChange = new EventEmitter(); @Output() readonly init = new EventEmitter(); @@ -92,7 +85,7 @@ export class ModalComponent implements OnDestroy { _busy = false; - isModalOpen = false; + modalRef: NgbModalRef; isConfirmationOpen = false; @@ -105,13 +98,12 @@ export class ModalComponent implements OnDestroy { } constructor( - private renderer: Renderer2, private confirmationService: ConfirmationService, - private modalService: ModalService, private subscription: SubscriptionService, @Optional() @Inject(SUPPRESS_UNSAVED_CHANGES_WARNING) private suppressUnsavedChangesWarningToken: boolean, + private modal: NgbModal, ) { this.initToggleStream(); } @@ -123,21 +115,34 @@ export class ModalComponent implements OnDestroy { } private toggle(value: boolean) { - this.isModalOpen = value; this._visible = value; this.visibleChange.emit(value); - if (value) { - this.modalService.renderTemplate(this.template); - setTimeout(() => this.listen(), 0); - this.renderer.addClass(document.body, 'modal-open'); - this.appear.emit(); - } else { - this.modalService.clearModal(); - this.renderer.removeClass(document.body, 'modal-open'); + if (!value) { + this.modalRef?.dismiss(); this.disappear.emit(); this.destroy$.next(); + return; } + + setTimeout(() => this.listen(), 0); + this.modalRef = this.modal.open(this.modalContent, { + //TODO: set size to 'lg' when removed the size variable + size: this.size, + windowClass: this.modalClass, + centered: this.centered, + keyboard: false, + scrollable: true, + beforeDismiss: () => { + if (!this.visible) return true; + + this.close(); + return !this.visible; + }, + ...this.options, + }); + + this.appear.emit(); } ngOnDestroy(): void { @@ -190,10 +195,7 @@ export class ModalComponent implements OnDestroy { setTimeout(() => { if (!this.abpClose) return; fromEvent(this.abpClose.nativeElement, 'click') - .pipe( - takeUntil(this.destroy$), - filter(() => !!this.modalContent), - ) + .pipe(takeUntil(this.destroy$)) .subscribe(() => this.close()); }, 0);