Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #39 from blackbaud/notifications
Browse files Browse the repository at this point in the history
Added support for notifications
  • Loading branch information
Blackbaud-PaulCrowder authored Aug 2, 2017
2 parents c200070 + f593368 commit 2badd0e
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/omnibar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from './omnibar-navigation-item';
export * from './omnibar-search-args';
export * from './omnibar-search-result-item';
export * from './omnibar-search-results';
export * from './omnibar-notifications';
export * from './omnibar-notification-item';
3 changes: 3 additions & 0 deletions src/omnibar/omnibar-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BBOmnibarNavigation } from './omnibar-navigation';
import { BBOmnibarNotificationsConfig } from './omnibar-notifications-config';
import { BBOmnibarSearchArgs } from './omnibar-search-args';
import { BBOmnibarSearchResults } from './omnibar-search-results';
import { BBOmnibarServiceItem } from './omnibar-service-item';
Expand All @@ -21,4 +22,6 @@ export class BBOmnibarConfig {
public services?: BBOmnibarServiceItem[];

public enableHelp?: boolean;

public notifications?: BBOmnibarNotificationsConfig;
}
91 changes: 91 additions & 0 deletions src/omnibar/omnibar-experimental.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,63 @@ describe('Omnibar (experimental)', () => {
// Should not throw an error.
});

it('should call the notification config\'s onReady() callback', () => {
const config = {
notifications: {
onReady: jasmine.createSpy('onReady')
}
};

loadOmnibar(config);

fireMessageEvent({
messageType: 'ready'
});

expect(config.notifications.onReady).toHaveBeenCalled();
});

it('should call the notification config\'s onNotificationRead() callback', () => {
const config = {
notifications: {
onNotificationRead: jasmine.createSpy('onNotificationRead'),
onReady: jasmine.createSpy('onReady')
}
};

loadOmnibar(config);

fireMessageEvent({
messageType: 'notification-read',
notification: {
id: 1
}
});

expect(config.notifications.onNotificationRead).toHaveBeenCalledWith({
id: 1
});
});

it('should not call the notification config\'s onNotificationRead() callback if not specified', () => {
const config = {
notifications: {
onReady: jasmine.createSpy('onReady')
}
};

loadOmnibar(config);

fireMessageEvent({
messageType: 'notification-read',
notification: {
id: 1
}
});

// Should not throw an error.
});

it('should open the help widget if the help widget is present on the page', () => {
loadOmnibar();

Expand Down Expand Up @@ -331,6 +388,7 @@ describe('Omnibar (experimental)', () => {
localNavItems,
enableHelp: undefined,
envId,
localNotifications: false,
localSearch: true,
messageType: 'nav-ready',
services: [
Expand Down Expand Up @@ -508,6 +566,39 @@ describe('Omnibar (experimental)', () => {
});
});

it('should notify the omnibar when notifications are updated', () => {
const notifications = {
items: [
{
id: 1,
title: 'Hi'
}
]
};

const config: BBOmnibarConfig = {
notifications: {
onReady: (readyArgs) => {
readyArgs.updateNotifications(notifications);
}
}
};

loadOmnibar(config);

fireMessageEvent({
messageType: 'ready'
});

expect(postOmnibarMessageSpy.calls.argsFor(2)).toEqual([
getIframeEl(),
{
messageType: 'notifications-update',
notifications
}
]);
});

});

});
35 changes: 35 additions & 0 deletions src/omnibar/omnibar-experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BBAuth } from '../auth';
import { BBAuthInterop } from '../shared/interop';
import { BBOmnibarConfig } from './omnibar-config';
import { BBOmnibarNavigationItem } from './omnibar-navigation-item';
import { BBOmnibarNotificationItem } from './omnibar-notification-item';
import { BBOmnibarSearchArgs } from './omnibar-search-args';

import { BBOmnibarUserActivity } from './omnibar-user-activity';
Expand Down Expand Up @@ -168,6 +169,14 @@ function handleHelp() {
}
}

function handleNotificationRead(notification: BBOmnibarNotificationItem) {
const notificationsConfig = omnibarConfig.notifications;

if (notificationsConfig && notificationsConfig.onNotificationRead) {
notificationsConfig.onNotificationRead(notification);
}
}

function monkeyPatchState() {
const oldPushState = history.pushState;
const oldReplaceState = history.replaceState;
Expand All @@ -192,6 +201,24 @@ function monkeyPatchState() {
history.replaceState = newReplaceState;
}

function setupNotifications() {
const notificationsConfig = omnibarConfig.notifications;

if (notificationsConfig) {
notificationsConfig.onReady({
updateNotifications: (notifications) => {
BBAuthInterop.postOmnibarMessage(
iframeEl,
{
messageType: 'notifications-update',
notifications
}
);
}
});
}
}

function messageHandler(event: MessageEvent) {
if (!BBAuthInterop.messageIsFromOmnibar(event)) {
return;
Expand Down Expand Up @@ -225,13 +252,16 @@ function messageHandler(event: MessageEvent) {
enableHelp: omnibarConfig.enableHelp,
envId: omnibarConfig.envId,
localNavItems: nav && nav.localNavItems,
localNotifications: !!omnibarConfig.notifications,
localSearch: !!omnibarConfig.onSearch,
messageType: 'nav-ready',
services: nav && nav.services,
svcId: omnibarConfig.svcId
}
);

setupNotifications();

handleStateChange();

promiseResolve();
Expand Down Expand Up @@ -265,6 +295,11 @@ function messageHandler(event: MessageEvent) {
case 'help-open':
handleHelp();
break;
case 'notification-read':
handleNotificationRead(
message.notification
);
break;
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/omnibar/omnibar-notification-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface BBOmnibarNotificationItem {
date?: Date;

title?: string;

message?: string;

read?: boolean;
}
8 changes: 8 additions & 0 deletions src/omnibar/omnibar-notifications-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { BBOmnibarNotificationItem } from './omnibar-notification-item';
import { BBOmnibarNotificationsReadyArgs } from './omnibar-notifications-ready-args';

export interface BBOmnibarNotificationsConfig {
onReady: (args: BBOmnibarNotificationsReadyArgs) => void;

onNotificationRead?: (notification: BBOmnibarNotificationItem) => void;
}
5 changes: 5 additions & 0 deletions src/omnibar/omnibar-notifications-ready-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BBOmnibarNotifications } from './omnibar-notifications';

export interface BBOmnibarNotificationsReadyArgs {
updateNotifications: (notifications: BBOmnibarNotifications) => void;
}
5 changes: 5 additions & 0 deletions src/omnibar/omnibar-notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BBOmnibarNotificationItem } from './omnibar-notification-item';

export interface BBOmnibarNotifications {
items?: BBOmnibarNotificationItem[];
}

0 comments on commit 2badd0e

Please sign in to comment.