[ADF-4896] Merging AMA and ADF notifications for a better world

This commit is contained in:
Andras Popovics
2019-09-17 14:33:35 +01:00
committed by Denys Vuika
parent 922a85935e
commit 14800ce399
35 changed files with 276 additions and 217 deletions

View File

@@ -0,0 +1,33 @@
<div (keyup)="onKeyPress($event)">
<button mat-button [matMenuTriggerFor]="menu" class="adf-notification-history-menu_button"
id="adf-notification-history-open-button">
<mat-icon>mail_outline</mat-icon>
</button>
<mat-menu #menu="matMenu" [xPosition]="menuPositionX" [yPosition]="menuPositionY"
[overlapTrigger]="false" id="adf-notification-history-menu" class="adf-notification-history-menu">
<div id="adf-notification-history-list">
<mat-list>
<mat-list-item>
<h6 mat-line>{{ 'NOTIFICATIONS.TITLE' | translate }}</h6>
</mat-list-item>
</mat-list>
<mat-divider></mat-divider>
<mat-list>
<mat-list-item *ngFor="let notification of notifications">
<mat-icon mat-list-icon>{{notification.info ? notification.info: 'info'}}</mat-icon>
<h4 *ngFor="let message of notification.messages" mat-line>{{ message }}</h4>
<p mat-line> {{notification.datetime | date}} </p>
</mat-list-item>
<mat-list-item *ngIf="isEmptyNotification()" id="adf-notification-history-component-no-message">
<h4 mat-line>{{ 'NOTIFICATIONS.NO_MESSAGE' | translate }}</h4>
</mat-list-item>
<mat-action-list *ngIf="!isEmptyNotification()" id="adf-notification-history-mark-as-read">
<button mat-list-item (click)="markAsRead()">{{ 'NOTIFICATIONS.MARK_AS_READ' | translate }}
</button>
</mat-action-list>
</mat-list>
</div>
</mat-menu>
</div>

View File

@@ -0,0 +1,23 @@
.adf {
&-notification-history-menu_button.mat-button {
margin-right: 0;
border-radius: 90%;
padding: 0;
min-width: 40px;
height: 40px;
}
}
@media only screen and (min-device-width: 480px) {
.mat-menu-panel.adf-notification-history-menu {
max-height: 450px;
min-width: 450px;
overflow: auto;
padding: 0;
}
}
.mat-menu-panel.adf-notification-history-menu .mat-menu-content {
padding: 0;
}

View File

@@ -0,0 +1,80 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
import { setupTestBed } from '../../testing/setupTestBed';
import { CoreTestingModule } from '../../testing/core.testing.module';
import { NotificationHistoryComponent } from './notification-history.component';
import { OverlayContainer } from '@angular/cdk/overlay';
import { NotificationService } from '../services/notification.service';
describe('Notification History Component', () => {
let fixture: ComponentFixture<NotificationHistoryComponent>;
let element: HTMLElement;
let notificationService: NotificationService;
let overlayContainerElement: HTMLElement;
function openNotification() {
fixture.detectChanges();
const button: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#adf-notification-history-open-button');
button.click();
fixture.detectChanges();
}
setupTestBed({
imports: [CoreTestingModule]
});
beforeEach(async(() => {
fixture = TestBed.createComponent(NotificationHistoryComponent);
element = fixture.nativeElement;
notificationService = TestBed.get(NotificationService);
}));
beforeEach(inject([OverlayContainer], (oc: OverlayContainer) => {
overlayContainerElement = oc.getContainerElement();
}));
afterEach(() => {
fixture.destroy();
});
describe('ui ', () => {
it('should empty message be present when there are no notifications in the history', (done) => {
openNotification();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(overlayContainerElement.querySelector('#adf-notification-history-component-no-message')).toBeDefined();
done();
});
});
it('should message be present and empty message not be present when there are notifications in the history', (done) => {
notificationService.showInfo('Example Message');
openNotification();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(overlayContainerElement.querySelector('#adf-notification-history-component-no-message')).toBeNull();
expect(overlayContainerElement.querySelector('#adf-notification-history-list').innerHTML).toContain('Example Message');
done();
});
});
});
});

View File

@@ -0,0 +1,78 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, Input, ViewChild, OnDestroy } from '@angular/core';
import { NotificationService } from '../services/notification.service';
import { Notification } from '../models/notification.model';
import { MatMenuTrigger } from '@angular/material';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
@Component({
selector: 'adf-notification-history',
styleUrls: ['notification-history.component.scss'],
templateUrl: 'notification-history.component.html'
})
export class NotificationHistoryComponent implements OnDestroy {
onDestroy$ = new Subject<boolean>();
notifications: Notification[] = [];
@ViewChild(MatMenuTrigger)
trigger: MatMenuTrigger;
/** Custom choice for opening the menu at the bottom. Can be `before` or `after`. */
@Input()
menuPositionX: string = 'after';
/** Custom choice for opening the menu at the bottom. Can be `above` or `below`. */
@Input()
menuPositionY: string = 'below';
constructor(
private notificationService: NotificationService) {
this.notificationService.notifications$
.pipe(takeUntil(this.onDestroy$))
.subscribe((message) => {
this.notifications.push(message);
});
}
isEmptyNotification(): boolean {
return (!this.notifications || this.notifications.length === 0);
}
onKeyPress(event: KeyboardEvent) {
this.closeUserModal(event);
}
markAsRead() {
this.notifications = [];
}
ngOnDestroy() {
this.onDestroy$.next(true);
this.onDestroy$.complete();
}
private closeUserModal($event: KeyboardEvent) {
if ($event.keyCode === 27) {
this.trigger.closeMenu();
}
}
}

View File

@@ -0,0 +1,54 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
NotificationInitiator,
NOTIFICATION_TYPE,
Notification
} from '../models/notification.model';
export const rootInitiator: NotificationInitiator = {
key: '*',
displayName: 'NOTIFICATIONS.SYSTEM'
};
export function info(messages: string | string[], initiator: NotificationInitiator = rootInitiator): Notification {
return {
type: NOTIFICATION_TYPE.INFO,
datetime: new Date(),
initiator,
messages: [].concat(messages)
};
}
export function warning(messages: string | string[], initiator: NotificationInitiator = rootInitiator): Notification {
return {
type: NOTIFICATION_TYPE.WARN,
datetime: new Date(),
initiator,
messages: [].concat(messages)
};
}
export function error(messages: string | string[], initiator: NotificationInitiator = rootInitiator): Notification {
return {
type: NOTIFICATION_TYPE.ERROR,
datetime: new Date(),
initiator,
messages: [].concat(messages)
};
}

View File

@@ -0,0 +1,18 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './public-api';

View File

@@ -0,0 +1,35 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export enum NOTIFICATION_TYPE {
INFO = 'info',
WARN = 'warning',
ERROR = 'error'
}
export interface NotificationInitiator {
key: string | Symbol;
displayName: string;
extra?: any;
}
export interface Notification {
type: NOTIFICATION_TYPE;
initiator: NotificationInitiator;
datetime: Date;
messages: string[];
}

View File

@@ -0,0 +1,38 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { MaterialModule } from '../material.module';
import { NotificationHistoryComponent } from './components/notification-history.component';
import { TranslateModule } from '@ngx-translate/core';
@NgModule({
imports: [
CommonModule,
MaterialModule,
TranslateModule.forChild()
],
declarations: [
NotificationHistoryComponent
],
exports: [
NotificationHistoryComponent
]
})
export class NotificationHistoryModule {}

View File

@@ -0,0 +1,22 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './components/notification-history.component';
export * from './helpers/notification.factory';
export * from './models/notification.model';
export * from './services/notification.service';
export * from './notification-history.module';

View File

@@ -0,0 +1,193 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LiveAnnouncer } from '@angular/cdk/a11y';
import { OVERLAY_PROVIDERS, OverlayModule } from '@angular/cdk/overlay';
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatSnackBar, MatSnackBarModule, MatSnackBarConfig } from '@angular/material';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NotificationService } from './notification.service';
import { TranslationMock } from '../../mock/translation.service.mock';
import { TranslationService } from '../../services/translation.service';
import { HttpClientModule } from '@angular/common/http';
import { setupTestBed } from '../../testing/setupTestBed';
@Component({
template: '',
providers: [NotificationService]
})
class ProvidesNotificationServiceComponent {
constructor(public notificationService: NotificationService) {
}
sendMessageWithoutConfig() {
return this.notificationService.openSnackMessage('Test notification', 1000);
}
sendMessage() {
return this.notificationService.openSnackMessage('Test notification', 1000);
}
sendMessageWithArgs() {
return this.notificationService.openSnackMessage('Test notification {{ arg }}', 1000, {arg: 'arg'});
}
sendCustomMessage() {
const matSnackBarConfig = new MatSnackBarConfig();
matSnackBarConfig.duration = 1000;
return this.notificationService.openSnackMessage('Test notification', matSnackBarConfig);
}
sendMessageActionWithoutConfig() {
return this.notificationService.openSnackMessageAction('Test notification', 'TestWarn', 1000);
}
sendMessageAction() {
return this.notificationService.openSnackMessageAction('Test notification', 'TestWarn', 1000);
}
sendCustomMessageAction() {
const matSnackBarConfig = new MatSnackBarConfig();
matSnackBarConfig.duration = 1000;
return this.notificationService.openSnackMessageAction('Test notification', 'TestWarn', matSnackBarConfig);
}
}
describe('NotificationService', () => {
let fixture: ComponentFixture<ProvidesNotificationServiceComponent>;
let translationService: TranslationService;
setupTestBed({
imports: [
NoopAnimationsModule,
OverlayModule,
MatSnackBarModule,
HttpClientModule
],
declarations: [ProvidesNotificationServiceComponent],
providers: [
NotificationService,
MatSnackBar,
OVERLAY_PROVIDERS,
LiveAnnouncer,
{ provide: TranslationService, useClass: TranslationMock }
]
});
beforeEach(() => {
translationService = TestBed.get(TranslationService);
fixture = TestBed.createComponent(ProvidesNotificationServiceComponent);
fixture.detectChanges();
});
it('should translate messages', (done) => {
spyOn(translationService, 'instant').and.callThrough();
const promise = fixture.componentInstance.sendMessage();
promise.afterDismissed().subscribe(() => {
expect(translationService.instant).toHaveBeenCalled();
done();
});
fixture.detectChanges();
});
it('should translate messages with args', (done) => {
spyOn(translationService, 'instant').and.callThrough();
const promise = fixture.componentInstance.sendMessageWithArgs();
promise.afterDismissed().subscribe(() => {
expect(translationService.instant).toHaveBeenCalledWith('Test notification {{ arg }}', {arg: 'arg'});
done();
});
fixture.detectChanges();
});
it('should open a message notification bar', (done) => {
const promise = fixture.componentInstance.sendMessage();
promise.afterDismissed().subscribe(() => {
done();
});
fixture.detectChanges();
expect(document.querySelector('snack-bar-container')).not.toBeNull();
});
it('should open a message notification bar without custom configuration', (done) => {
const promise = fixture.componentInstance.sendMessageWithoutConfig();
promise.afterDismissed().subscribe(() => {
done();
});
fixture.detectChanges();
expect(document.querySelector('snack-bar-container')).not.toBeNull();
});
it('should open a message notification bar with custom configuration', (done) => {
const promise = fixture.componentInstance.sendCustomMessage();
promise.afterDismissed().subscribe(() => {
done();
});
fixture.detectChanges();
expect(document.querySelector('snack-bar-container')).not.toBeNull();
});
it('should open a message notification bar with action', (done) => {
const promise = fixture.componentInstance.sendMessageAction();
promise.afterDismissed().subscribe(() => {
done();
});
fixture.detectChanges();
expect(document.querySelector('snack-bar-container')).not.toBeNull();
});
it('should open a message notification bar with action and custom configuration', (done) => {
const promise = fixture.componentInstance.sendCustomMessageAction();
promise.afterDismissed().subscribe(() => {
done();
});
fixture.detectChanges();
expect(document.querySelector('snack-bar-container')).not.toBeNull();
});
it('should open a message notification bar with action and no custom configuration', (done) => {
const promise = fixture.componentInstance.sendMessageActionWithoutConfig();
promise.afterDismissed().subscribe(() => {
done();
});
fixture.detectChanges();
expect(document.querySelector('snack-bar-container')).not.toBeNull();
});
});

View File

@@ -0,0 +1,129 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { MatSnackBar, MatSnackBarRef, MatSnackBarConfig } from '@angular/material';
import { TranslationService } from '../../services/translation.service';
import { AppConfigService, AppConfigValues } from '../../app-config/app-config.service';
import { Subject } from 'rxjs';
import { Notification } from '../models/notification.model';
import { info, warning, error } from '../helpers/notification.factory';
const INFO_SNACK_CLASS = 'adf-info-snackbar';
const WARN_SNACK_CLASS = 'adf-warning-snackbar';
const ERROR_SNACK_CLASS = 'adf-error-snackbar';
@Injectable({
providedIn: 'root'
})
export class NotificationService {
DEFAULT_DURATION_MESSAGE: number = 5000;
notifications$: Subject<Notification> = new Subject();
constructor(private snackBar: MatSnackBar,
private translationService: TranslationService,
private appConfigService: AppConfigService) {
this.DEFAULT_DURATION_MESSAGE = this.appConfigService.get<number>(AppConfigValues.NOTIFY_DURATION) || this.DEFAULT_DURATION_MESSAGE;
}
/**
* Opens a SnackBar notification to show a message.
* @param message The message (or resource key) to show.
* @param config Time before notification disappears after being shown or MatSnackBarConfig object
* @param interpolateArgs The interpolation parameters to add for the translation
* @returns Information/control object for the SnackBar
*/
openSnackMessage(message: string, config?: number | MatSnackBarConfig, interpolateArgs?: any): MatSnackBarRef<any> {
return this.dispatchNotification(message, null, config, interpolateArgs);
}
/**
* Opens a SnackBar notification with a message and a response button.
* @param message The message (or resource key) to show.
* @param action Caption for the response button
* @param config Time before notification disappears after being shown or MatSnackBarConfig object
* @param interpolateArgs The interpolation parameters to add for the translation
* @returns Information/control object for the SnackBar
*/
openSnackMessageAction(message: string, action: string, config?: number | MatSnackBarConfig, interpolateArgs?: any): MatSnackBarRef<any> {
return this.dispatchNotification(message, action, config, interpolateArgs);
}
/**
* Rase error message
* @param message Text message or translation key for the message.
* @param action Action name
* @param interpolateArgs The interpolation parameters to add for the translation
*/
showError(message: string, action?: string, interpolateArgs?: any): MatSnackBarRef<any> {
return this.dispatchNotification(message, action, { panelClass: ERROR_SNACK_CLASS }, interpolateArgs);
}
/**
* Rase info message
* @param message Text message or translation key for the message.
* @param action Action name
* @param interpolateArgs The interpolation parameters to add for the translation
*/
showInfo(message: string, action?: string, interpolateArgs?: any): MatSnackBarRef<any> {
return this.dispatchNotification(message, action, { panelClass: INFO_SNACK_CLASS }, interpolateArgs);
}
/**
* Rase warning message
* @param message Text message or translation key for the message.
* @param action Action name
* @param interpolateArgs The interpolation parameters to add for the translation
*/
showWarning(message: string, action?: string, interpolateArgs?: any): MatSnackBarRef<any> {
return this.dispatchNotification(message, action, { panelClass: WARN_SNACK_CLASS }, interpolateArgs);
}
/**
* dismiss the notification snackbar
*/
dismissSnackMessageAction() {
return this.snackBar.dismiss();
}
private dispatchNotification(message: string, action?: string, config?: number | MatSnackBarConfig, interpolateArgs?: any): MatSnackBarRef<any> {
const translatedMessage: string = this.translationService.instant(message, interpolateArgs);
const createNotification = this.getNotificationCreator(config);
this.notifications$.next(createNotification(translatedMessage));
return this.snackBar.open(translatedMessage, action, {
duration: (typeof config === 'number') ? config : this.DEFAULT_DURATION_MESSAGE,
panelClass: INFO_SNACK_CLASS,
...( (typeof config === 'object') ? config : {} )
});
}
private getNotificationCreator(config?: number | MatSnackBarConfig) {
const panelClass: string = config && (<MatSnackBarConfig> config).panelClass && (<MatSnackBarConfig> config).panelClass[0] || '';
switch (panelClass) {
case ERROR_SNACK_CLASS:
return error;
case WARN_SNACK_CLASS:
return warning;
default:
return info;
}
}
}