Take notification title and content as Observable

This commit is contained in:
Giuseppe Digilio
2018-03-08 15:47:14 +01:00
parent ac350bd813
commit 58cc17f1db
4 changed files with 34 additions and 30 deletions

View File

@@ -1,32 +1,29 @@
import { INotificationOptions, NotificationOptions } from './notification-options.model'; import { INotificationOptions, NotificationOptions } from './notification-options.model';
import { NotificationType } from './notification-type'; import { NotificationType } from './notification-type';
import { isEmpty } from '../../empty.util'; import { isEmpty } from '../../empty.util';
import { ElementRef, TemplateRef } from '@angular/core'; import { Observable } from 'rxjs/Observable';
import { Deserialize, Serialize, serialize } from 'cerialize';
import { deserialize } from 'cerialize';
import { NotificationsService } from '../notifications.service';
export interface INotification { export interface INotification {
id: string id: string;
type: NotificationType type: NotificationType;
title?: any title?: Observable<string>;
content?: any content?: Observable<string>;
options?: INotificationOptions options?: INotificationOptions;
html?: any; html?: any;
} }
export class Notification implements INotification { export class Notification implements INotification {
public id: string; public id: string;
public type: NotificationType; public type: NotificationType;
public title: any; public title: Observable<string>;
public content: any; public content: Observable<string>;
public options: INotificationOptions; public options: INotificationOptions;
public html: any; public html: any;
constructor(id: string, constructor(id: string,
type: NotificationType, type: NotificationType,
title?: any, title?: Observable<string>,
content?: any, content?: Observable<string>,
options?: NotificationOptions, options?: NotificationOptions,
html?: any) { html?: any) {
@@ -38,13 +35,4 @@ export class Notification implements INotification {
this.html = html; this.html = html;
} }
// get html() {
// if (this.title === '' && this.content === '') {
// return NotificationsService.htmlArray.get(this.id);
// } else {
// return null;
// }
// }
} }

View File

@@ -22,7 +22,7 @@
</div> </div>
<ng-template #regularTitle> <ng-template #regularTitle>
<div class="sn-title" [innerHTML]="title"></div> <div class="sn-title" [innerHTML]="(title | async)"></div>
</ng-template> </ng-template>
<div class="sn-content" *ngIf="contentIsTemplate; else regularContent"> <div class="sn-content" *ngIf="contentIsTemplate; else regularContent">
@@ -30,7 +30,7 @@
</div> </div>
<ng-template #regularContent> <ng-template #regularContent>
<div class="sn-content" [innerHTML]="content"></div> <div class="sn-content" [innerHTML]="(content | async)"></div>
</ng-template> </ng-template>
</div> </div>

View File

@@ -21,6 +21,7 @@ import { fromLeftEnter, fromLeftInState, fromLeftLeave, fromLeftOutState } from
import { fromTopEnter, fromTopInState, fromTopLeave, fromTopOutState } from '../../animations/fromTop'; import { fromTopEnter, fromTopInState, fromTopLeave, fromTopOutState } from '../../animations/fromTop';
import { fadeInEnter, fadeInState, fadeOutLeave, fadeOutState } from '../../animations/fade'; import { fadeInEnter, fadeInState, fadeOutLeave, fadeOutState } from '../../animations/fade';
import { NotificationAnimationsStatus } from '../models/notification-animations-type'; import { NotificationAnimationsStatus } from '../models/notification-animations-type';
import { Observable } from 'rxjs/Observable';
@Component({ @Component({
selector: 'ds-notification', selector: 'ds-notification',
@@ -46,8 +47,8 @@ export class NotificationComponent implements OnInit, OnDestroy {
@Input() public item: INotification; @Input() public item: INotification;
// Progress bar variables // Progress bar variables
public title: any; public title: Observable<string>;
public content: any; public content: Observable<string>;
public html: any; public html: any;
public showProgressBar = false; public showProgressBar = false;
public titleIsTemplate = false; public titleIsTemplate = false;
@@ -125,6 +126,8 @@ export class NotificationComponent implements OnInit, OnDestroy {
private contentType(item: any, key: string) { private contentType(item: any, key: string) {
if (item instanceof TemplateRef) { if (item instanceof TemplateRef) {
this[key] = item; this[key] = item;
} else if (item instanceof Observable) {
this[key] = item;
} else { } else {
this[key] = this.domSanitizer.bypassSecurityTrustHtml(item); this[key] = this.domSanitizer.bypassSecurityTrustHtml(item);
} }

View File

@@ -11,6 +11,7 @@ import {
RemoveNotificationAction RemoveNotificationAction
} from './notifications.actions'; } from './notifications.actions';
import { DomSanitizer } from '@angular/platform-browser'; import { DomSanitizer } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable';
@Injectable() @Injectable()
export class NotificationsService { export class NotificationsService {
@@ -28,25 +29,37 @@ export class NotificationsService {
this.store.dispatch(notificationAction); this.store.dispatch(notificationAction);
} }
success(title: any = '', content: any = '', options = new NotificationOptions(), html?: any): INotification { success(title: any = Observable.of(''),
content: any = Observable.of(''),
options = new NotificationOptions(),
html?: any): INotification {
const notification = new Notification(uniqueId(), NotificationType.Success, title, content, options, html); const notification = new Notification(uniqueId(), NotificationType.Success, title, content, options, html);
this.add(notification); this.add(notification);
return notification; return notification;
} }
error(title: any = '', content: any = '', options = new NotificationOptions(), html?: any): INotification { error(title: any = Observable.of(''),
content: any = Observable.of(''),
options = new NotificationOptions(),
html?: any): INotification {
const notification = new Notification(uniqueId(), NotificationType.Error, title, content, options, html); const notification = new Notification(uniqueId(), NotificationType.Error, title, content, options, html);
this.add(notification); this.add(notification);
return notification; return notification;
} }
info(title: any = '', content: any = '', options = new NotificationOptions(), html?: any): INotification { info(title: any = Observable.of(''),
content: any = Observable.of(''),
options = new NotificationOptions(),
html?: any): INotification {
const notification = new Notification(uniqueId(), NotificationType.Info, title, content, options, html); const notification = new Notification(uniqueId(), NotificationType.Info, title, content, options, html);
this.add(notification); this.add(notification);
return notification; return notification;
} }
warning(title: any = '', content: any = '', options = new NotificationOptions(), html?: any): INotification { warning(title: any = Observable.of(''),
content: any = Observable.of(''),
options = new NotificationOptions(),
html?: any): INotification {
const notification = new Notification(uniqueId(), NotificationType.Warning, title, content, options, html); const notification = new Notification(uniqueId(), NotificationType.Warning, title, content, options, html);
this.add(notification); this.add(notification);
return notification; return notification;