@@ -15,97 +15,51 @@ declare var jQuery: any;
`
})
-export class ModalComponent implements OnDestroy {
+export class ModalComponent implements OnDestroy, CanDeactivate {
- id: string = uniqueId('modal_');
- $modal: any;
- result: ModalResult = ModalResult.None;
- hiding: boolean = false;
+ instance: ModalInstance;
overrideSize: string = null;
visible: boolean = false;
@Input() animation: boolean = true;
@Input() backdrop: string;
@Input() keyboard: boolean;
@Input() size: string;
- @Output() onClose: EventEmitter
= new EventEmitter(false);
-
- init() {
- this.$modal = jQuery('#' + this.id);
- this.$modal.appendTo('body').modal({ show: false });
- this.$modal
- .off('shown.bs.modal.ng2-bs3-modal')
- .on('shown.bs.modal.ng2-bs3-modal', (e) => {
- this.visible = true;
- })
- .off('hide.bs.modal.ng2-bs3-modal')
- .on('hide.bs.modal.ng2-bs3-modal', (e) => {
- this.hiding = true;
- if (this.result === ModalResult.None) this.dismiss();
- this.result = ModalResult.None;
- })
- .off('hidden.bs.modal.ng2-bs3-modal')
- .on('hidden.bs.modal.ng2-bs3-modal', (e) => {
- this.hiding = false;
- this.overrideSize = null;
- this.visible = false;
- });
+ @Output() onClose: EventEmitter = new EventEmitter(false);
+ @Output() onDismiss: EventEmitter = new EventEmitter(false);
+
+ constructor(el: ElementRef) {
+ this.instance = new ModalInstance(el);
+ this.instance.hidden.subscribe((result) => {
+ if (result = ModalResult.Dismiss)
+ this.onDismiss.emit(undefined);
+ });
}
ngOnDestroy() {
- if (this.$modal) {
- if (this.visible) {
- this.$modal.one('hidden.bs.modal', () => {
- this.destroy();
- });
- this.$modal.hide();
- }
- else {
- this.destroy();
- }
- }
+ this.instance.destroy();
}
- private destroy() {
- this.$modal.data('bs.modal', null);
- this.$modal.remove();
+ routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction): any {
+ return this.instance.destroy();
}
open(size?: string) {
- return new Promise((resolve, reject) => {
- this.init();
- if (ModalSize.validSize(size)) this.overrideSize = size;
- this.$modal.one('shown.bs.modal', () => {
- resolve();
- });
- this.$modal.modal('show');
- });
+ if (ModalSize.validSize(size)) this.overrideSize = size;
+ return this.instance.open();
}
close() {
- return new Promise((resolve, reject) => {
- this.result = ModalResult.Close;
- this.onClose.emit(this.result);
- this.hide(resolve);
+ return this.instance.close().then(() => {
+ this.onClose.emit(undefined);
});
}
dismiss() {
- return new Promise((resolve, reject) => {
- this.result = ModalResult.Dismiss;
- this.onClose.emit(this.result);
- this.hide(resolve);
+ return this.instance.dismiss().then(() => {
+ this.onDismiss.emit(undefined);
});
}
- private hide(resolve) {
- if (!this.hiding) {
- this.$modal.one('hidden.bs.modal', () => {
- resolve();
- });
- this.$modal.modal('hide');
- }
- }
-
private isSmall() {
return this.overrideSize !== ModalSize.Large && this.size === ModalSize.Small || this.overrideSize === ModalSize.Small;
}
@@ -123,14 +77,3 @@ export class ModalSize {
return size && (size === ModalSize.Small || size === ModalSize.Large);
}
}
-
-export enum ModalResult {
- None,
- Close,
- Dismiss
-}
-
-let id: number = 0;
-export function uniqueId(prefix: string): string {
- return prefix + ++id;
-}
diff --git a/src/ng2-bs3-modal/ng2-bs3-modal.ts b/src/ng2-bs3-modal/ng2-bs3-modal.ts
index dd3c720..673e690 100644
--- a/src/ng2-bs3-modal/ng2-bs3-modal.ts
+++ b/src/ng2-bs3-modal/ng2-bs3-modal.ts
@@ -9,6 +9,7 @@ export * from './components/modal';
export * from './components/modal-header';
export * from './components/modal-body';
export * from './components/modal-footer';
+export * from './components/modal-instance';
export const MODAL_DIRECTIVES: Type[] = [
ModalComponent,