Browse Source

use ng-bootstrap modal inside modal component

pull/7481/head
mehmet-erim 6 years ago
parent
commit
e49b8cbc33
  1. 60
      npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.html
  2. 62
      npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts

60
npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.html

@ -1,43 +1,23 @@
<ng-template #template> <ng-content></ng-content>
<div
*ngIf="visible" <ng-template #modalContent let-modal>
[@fade]="isModalOpen" <div id="abp-modal-header" class="modal-header">
id="modal-container" <ng-container *ngTemplateOutlet="abpHeader"></ng-container>
class="modal show {{ modalClass }}" ​
tabindex="-1" <button
role="dialog" id="abp-modal-close-button"
> type="button"
<div class="modal-backdrop" (click)="close()"></div> class="close"
<div aria-label="Close"
id="abp-modal-dialog" (click)="modal.dismiss()"
class="modal-dialog modal-{{ size }}"
role="document"
[class.modal-dialog-centered]="centered"
#abpModalContent
> >
<div id="abp-modal-content" class="modal-content"> <span aria-hidden="true">&times;</span>
<div id="abp-modal-header" class="modal-header"> </button>
<ng-container *ngTemplateOutlet="abpHeader"></ng-container> </div>
​ <div id="abp-modal-body" class="modal-body">
<button <ng-container *ngTemplateOutlet="abpBody"></ng-container>
id="abp-modal-close-button" </div>
type="button" <div id="abp-modal-footer" class="modal-footer">
class="close" <ng-container *ngTemplateOutlet="abpFooter"></ng-container>
aria-label="Close"
(click)="close()"
>
<span aria-hidden="true">&times;</span>
</button>
</div>
<div id="abp-modal-body" class="modal-body">
<ng-container *ngTemplateOutlet="abpBody"></ng-container>
</div>
<div id="abp-modal-footer" class="modal-footer">
<ng-container *ngTemplateOutlet="abpFooter"></ng-container>
</div>
</div>
</div>
</div> </div>
</ng-template> </ng-template>
<ng-content></ng-content>

62
npm/ng-packs/packages/theme-shared/src/lib/components/modal/modal.component.ts

@ -9,17 +9,14 @@ import {
OnDestroy, OnDestroy,
Optional, Optional,
Output, Output,
Renderer2,
TemplateRef, TemplateRef,
ViewChild, ViewChild,
ViewChildren,
} from '@angular/core'; } from '@angular/core';
import { NgbModal, NgbModalOptions, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { fromEvent, Subject } from 'rxjs'; import { fromEvent, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, takeUntil } from 'rxjs/operators'; import { debounceTime, distinctUntilChanged, filter, takeUntil } from 'rxjs/operators';
import { fadeAnimation } from '../../animations/modal.animations';
import { Confirmation } from '../../models/confirmation'; import { Confirmation } from '../../models/confirmation';
import { ConfirmationService } from '../../services/confirmation.service'; 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 { SUPPRESS_UNSAVED_CHANGES_WARNING } from '../../tokens/suppress-unsaved-changes-warning.token';
import { ButtonComponent } from '../button/button.component'; import { ButtonComponent } from '../button/button.component';
@ -28,9 +25,8 @@ export type ModalSize = 'sm' | 'md' | 'lg' | 'xl';
@Component({ @Component({
selector: 'abp-modal', selector: 'abp-modal',
templateUrl: './modal.component.html', templateUrl: './modal.component.html',
animations: [fadeAnimation],
styleUrls: ['./modal.component.scss'], styleUrls: ['./modal.component.scss'],
providers: [ModalService, SubscriptionService], providers: [SubscriptionService],
}) })
export class ModalComponent implements OnDestroy { export class ModalComponent implements OnDestroy {
@Input() @Input()
@ -59,11 +55,11 @@ export class ModalComponent implements OnDestroy {
@Input() modalClass = ''; @Input() modalClass = '';
@Input() size: ModalSize = 'lg'; @Input() size: ModalSize = 'lg';
@Input() options: NgbModalOptions = {};
@Input() suppressUnsavedChangesWarning = this.suppressUnsavedChangesWarningToken; @Input() suppressUnsavedChangesWarning = this.suppressUnsavedChangesWarningToken;
@ContentChild(ButtonComponent, { static: false, read: ButtonComponent }) @ViewChild('modalContent') modalContent: TemplateRef<any>;
abpSubmit: ButtonComponent;
@ContentChild('abpHeader', { static: false }) abpHeader: TemplateRef<any>; @ContentChild('abpHeader', { static: false }) abpHeader: TemplateRef<any>;
@ -71,15 +67,12 @@ export class ModalComponent implements OnDestroy {
@ContentChild('abpFooter', { static: false }) abpFooter: TemplateRef<any>; @ContentChild('abpFooter', { static: false }) abpFooter: TemplateRef<any>;
@ContentChild(ButtonComponent, { static: false, read: ButtonComponent })
abpSubmit: ButtonComponent;
@ContentChild('abpClose', { static: false, read: ElementRef }) @ContentChild('abpClose', { static: false, read: ElementRef })
abpClose: ElementRef<any>; abpClose: ElementRef<any>;
@ViewChild('template', { static: false }) template: TemplateRef<any>;
@ViewChild('abpModalContent', { static: false }) modalContent: ElementRef;
@ViewChildren('abp-button') abpButtons;
@Output() readonly visibleChange = new EventEmitter<boolean>(); @Output() readonly visibleChange = new EventEmitter<boolean>();
@Output() readonly init = new EventEmitter<void>(); @Output() readonly init = new EventEmitter<void>();
@ -92,7 +85,7 @@ export class ModalComponent implements OnDestroy {
_busy = false; _busy = false;
isModalOpen = false; modalRef: NgbModalRef;
isConfirmationOpen = false; isConfirmationOpen = false;
@ -105,13 +98,12 @@ export class ModalComponent implements OnDestroy {
} }
constructor( constructor(
private renderer: Renderer2,
private confirmationService: ConfirmationService, private confirmationService: ConfirmationService,
private modalService: ModalService,
private subscription: SubscriptionService, private subscription: SubscriptionService,
@Optional() @Optional()
@Inject(SUPPRESS_UNSAVED_CHANGES_WARNING) @Inject(SUPPRESS_UNSAVED_CHANGES_WARNING)
private suppressUnsavedChangesWarningToken: boolean, private suppressUnsavedChangesWarningToken: boolean,
private modal: NgbModal,
) { ) {
this.initToggleStream(); this.initToggleStream();
} }
@ -123,21 +115,34 @@ export class ModalComponent implements OnDestroy {
} }
private toggle(value: boolean) { private toggle(value: boolean) {
this.isModalOpen = value;
this._visible = value; this._visible = value;
this.visibleChange.emit(value); this.visibleChange.emit(value);
if (value) { if (!value) {
this.modalService.renderTemplate(this.template); this.modalRef?.dismiss();
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');
this.disappear.emit(); this.disappear.emit();
this.destroy$.next(); 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 { ngOnDestroy(): void {
@ -190,10 +195,7 @@ export class ModalComponent implements OnDestroy {
setTimeout(() => { setTimeout(() => {
if (!this.abpClose) return; if (!this.abpClose) return;
fromEvent(this.abpClose.nativeElement, 'click') fromEvent(this.abpClose.nativeElement, 'click')
.pipe( .pipe(takeUntil(this.destroy$))
takeUntil(this.destroy$),
filter(() => !!this.modalContent),
)
.subscribe(() => this.close()); .subscribe(() => this.close());
}, 0); }, 0);

Loading…
Cancel
Save