Modal finalized

This commit is contained in:
kiswa 2016-07-31 19:54:30 +00:00
parent 3d42003cf3
commit 33f535d30a
6 changed files with 76 additions and 20 deletions

View File

@ -6,7 +6,7 @@ import { enableProdMode } from '@angular/core';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS } from './app.routes'; import { APP_ROUTER_PROVIDERS } from './app.routes';
import { API_HTTP_PROVIDERS } from './app.api-http'; import { API_HTTP_PROVIDERS } from './app.api-http';
import { NotificationsService } from './shared/index'; import { NotificationsService, ModalService } from './shared/index';
import { Constants } from './shared/constants'; import { Constants } from './shared/constants';
// enableProdMode(); // enableProdMode();
@ -18,6 +18,7 @@ bootstrap(AppComponent, [
APP_ROUTER_PROVIDERS, APP_ROUTER_PROVIDERS,
API_HTTP_PROVIDERS, API_HTTP_PROVIDERS,
Constants, Constants,
NotificationsService NotificationsService,
ModalService
]); ]);

View File

@ -3,5 +3,5 @@ export * from './auth/index';
export * from './models/index'; export * from './models/index';
export * from './notifications/index'; export * from './notifications/index';
export * from './constants'; export * from './constants';
export * from './modal/modal.component' export * from './modal/index'

View File

@ -0,0 +1,3 @@
export * from './modal.component';
export * from './modal.service';

View File

@ -2,7 +2,7 @@
<div class="modal-overlay" (click)="close(true)"></div> <div class="modal-overlay" (click)="close(true)"></div>
<div class="modal"> <div class="modal">
<div class="title"> <div class="title" *ngIf="modalTitle">
<h2> <h2>
{{ modalTitle }} {{ modalTitle }}
<span class="right" (click)="close()"> <span class="right" (click)="close()">

View File

@ -1,37 +1,36 @@
import { import {
Component, Component,
Input, Input,
Output, OnInit
EventEmitter
} from '@angular/core'; } from '@angular/core';
import { ModalService } from './modal.service';
@Component({ @Component({
selector: 'tb-modal', selector: 'tb-modal',
templateUrl: 'app/shared/modal/modal.component.html', templateUrl: 'app/shared/modal/modal.component.html',
host: { '(document:keyup)': 'keyup($event)' } host: { '(document:keyup)': 'keyup($event)' }
}) })
export class Modal { export class Modal implements OnInit {
@Input('modal-id') modalId: string;
@Input('modal-title') modalTitle: string; @Input('modal-title') modalTitle: string;
@Input() blocking = false; @Input() blocking = false;
@Input('is-open') isOpen = false; isOpen: boolean = false;
@Output() onClose = new EventEmitter<boolean>();
open() { constructor(private modalService: ModalService) {
this.isOpen = true;
} }
close(checkBlocking = false) { ngOnInit() {
if (checkBlocking && this.blocking) { this.modalService.registerModal(this);
return;
} }
this.isOpen = false; private close(checkBlocking = false): void {
this.onClose.next(false); this.modalService.close(this.modalId, checkBlocking);
} }
keyup(event: KeyboardEvent) { private keyup(event: KeyboardEvent): void {
if (event.keyCode === 27) { if (event.keyCode === 27) {
this.close(true); this.modalService.close(this.modalId, true);
} }
} }
} }

View File

@ -0,0 +1,53 @@
import { Injectable } from '@angular/core';
import { Modal } from './modal.component';
@Injectable()
export class ModalService {
private modals: Array<Modal>;
constructor() {
this.modals = [];
}
registerModal(newModal: Modal): void {
var modal = this.findModal(newModal.modalId);
if (modal) {
return;
}
this.modals.push(newModal);
}
open(modalId: string): void {
var modal = this.findModal(modalId);
if (modal) {
modal.isOpen = true;
}
}
close(modalId: string, checkBlocking = false): void {
var modal = this.findModal(modalId);
if (modal) {
if (checkBlocking && modal.blocking) {
return;
}
modal.isOpen = false;
}
}
private findModal(modalId: string): Modal {
for (var modal of this.modals) {
if (modal.modalId === modalId) {
return modal;
}
}
return null;
}
}