angular-confirm
Version:
Angular Confirm Modal
81 lines (70 loc) • 2.8 kB
text/typescript
import {ElementRef, Component, TemplateRef, ViewChild} from '@angular/core';
import {ConfirmService} from './confirm.service';
import {Input} from '@angular/core/src/metadata/directives';
import {NgbModalRef} from '@ng-bootstrap/ng-bootstrap';
({
selector: '[confirm]',
template: `
<template #template>
<div class='modal-header'>
<button type='button' class='close' aria-label='Close' (click)='dismiss()'>
<span aria-hidden='true'>×</span>
</button>
<h4 class='modal-title'>{{confirmTitle}}</h4>
</div>
<div class='modal-body'>
<p>{{confirmTitle}}</p>
</div>
<div class='modal-footer'>
<button type='button' class='btn btn-secondary' (click)='close()'>{{confirmOk}}</button>
<button type='button' class='btn btn-secondary' (click)='dismiss()'>{{confirmCancel}}</button>
</div>
</template>
<ng-content></ng-content>
`,
})
export class ConfirmComponent {
() confirmOk: string = 'Ok';
() confirmCancel: string = 'Cancel';
() confirmTitle: string = 'Confirm';
() confirm: string;
() confirmIf: boolean = true;
() confirmSettings: Object = {};
() confirmTemplate: string | TemplateRef<any> = null;
(TemplateRef) template: TemplateRef<any>;
private modal: NgbModalRef;
constructor(private el: ElementRef, private confirmService: ConfirmService) {
console.log('inside constructor');
let element: HTMLElement = el.nativeElement;
let oldAddEventListener: Function = element.addEventListener;
let events: Object[] = [];
function success(clickEvent) {
events.forEach((evt: any) => {
evt.listener(clickEvent);
});
}
element.addEventListener('click', (event) => {
if (this.confirmIf) {
this.modal = confirmService.confirm(this.confirmTemplate || this.template, this.confirmSettings);
this.modal.result.then(() => {
success(event);
});
} else {
success(event);
}
});
element.addEventListener = function(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean) {
if (type === 'click') {
events.push({type: type, listener: listener, useCapture: useCapture});
} else {
oldAddEventListener(type, listener, useCapture);
}
};
}
close() {
this.modal.close();
}
dismiss() {
this.modal.dismiss();
}
}