diff --git a/demo/modal-demo.component.html b/demo/modal-demo.component.html index eb97894..86fe0de 100644 --- a/demo/modal-demo.component.html +++ b/demo/modal-demo.component.html @@ -10,7 +10,7 @@

Modal

Selection from a modal: {{ selected }}
- + @@ -25,7 +25,7 @@ - + @@ -40,7 +40,7 @@ - + diff --git a/demo/modal-demo.component.ts b/demo/modal-demo.component.ts index 85b62d0..2f80cd0 100644 --- a/demo/modal-demo.component.ts +++ b/demo/modal-demo.component.ts @@ -15,15 +15,15 @@ export class ModalDemoComponent { constructor(private router: Router) { } - onClose(result: ModalResult) { - if (result === ModalResult.Close) { - this.selected = this.modalSelected; - } + closed() { + this.selected = '(closed) ' + this.modalSelected; + } + + dismissed() { + this.selected = '(dismissed)'; } navigate(result: ModalResult) { - if (result === ModalResult.Close) { - this.router.navigateByUrl('/hello'); - } + this.router.navigateByUrl('/hello'); } } \ No newline at end of file diff --git a/package.json b/package.json index 39bf479..da6e89e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ng2-bs3-modal", - "version": "0.3.1", + "version": "0.4.0", "description": "Angular2 Boostrap3 Modal Component", "main": "ng2-bs3-modal.js", "scripts": { diff --git a/src/ng2-bs3-modal/components/modal-instance.ts b/src/ng2-bs3-modal/components/modal-instance.ts new file mode 100644 index 0000000..54a0069 --- /dev/null +++ b/src/ng2-bs3-modal/components/modal-instance.ts @@ -0,0 +1,98 @@ +import { ElementRef } from 'angular2/core'; +import { Observable } from 'rxjs/Observable'; +import { Subscriber } from 'rxjs/Subscriber'; +import 'rxjs/add/operator/toPromise'; + +declare var jQuery: any; + +export class ModalInstance { + + private suffix: string = 'ng2-bs3-modal'; + private shownEventName: string = 'shown.bs.modal'; + private hiddenEventName: string = 'hidden.bs.modal'; + + $modal: any; + shown: Observable; + shownSubscriber: Subscriber; + hidden: Observable; + hiddenSubscriber: Subscriber; + result: ModalResult; + visible: boolean = false; + + constructor(private element: ElementRef) { + this.init(); + } + + open(): Promise { + this.create(); + return this.show(); + } + + close(): Promise { + this.result = ModalResult.Close; + return this.hide(); + } + + dismiss(): Promise { + this.result = ModalResult.Dismiss; + return this.hide(); + } + + destroy(): Promise { + return this.hide().then(() => { + this.$modal.data('bs.modal', null); + this.$modal.remove(); + }); + } + + private show() { + this.$modal.appendTo('body'); + this.$modal.modal('show'); + return this.shown.toPromise(); + } + + private hide(): Promise { + if (this.$modal) this.$modal.modal('hide'); + return this.hidden.toPromise(); + } + + private init() { + this.shown = new Observable((subscriber: Subscriber) => { + this.shownSubscriber = subscriber; + }); + + this.hidden = new Observable((subscriber: Subscriber) => { + this.hiddenSubscriber = subscriber; + }); + } + + private create() { + if (!this.$modal) { + this.$modal = jQuery(this.element.nativeElement.firstElementChild); + this.$modal.appendTo('body').modal({ show: false }); + } + + this.$modal + .off(`${this.shownEventName}.${this.suffix}`) + .on(`${this.shownEventName}.${this.suffix}`, () => { + this.visible = true; + this.shownSubscriber.next(); + this.shownSubscriber.complete(); + }) + .off(`${this.hiddenEventName}.${this.suffix}`) + .on(`${this.hiddenEventName}.${this.suffix}`, () => { + this.visible = false; + if (this.result === ModalResult.None) { + this.result = ModalResult.Dismiss; + }; + this.hiddenSubscriber.next(this.result); + this.hiddenSubscriber.complete(); + }); + } +} + +export enum ModalResult { + None, + Close, + Dismiss +} \ No newline at end of file diff --git a/src/ng2-bs3-modal/components/modal.ts b/src/ng2-bs3-modal/components/modal.ts index 598917f..4b9baf4 100644 --- a/src/ng2-bs3-modal/components/modal.ts +++ b/src/ng2-bs3-modal/components/modal.ts @@ -1,11 +1,11 @@ -import { Component, AfterViewInit, OnDestroy, Input, Output, EventEmitter, Type } from 'angular2/core'; - -declare var jQuery: any; +import { Component, AfterViewInit, OnDestroy, Input, Output, EventEmitter, Type, ElementRef } from 'angular2/core'; +import { CanDeactivate, ComponentInstruction } from 'angular2/router'; +import { ModalInstance, ModalResult } from './modal-instance'; @Component({ selector: 'modal', template: ` -