Skip to content

Commit

Permalink
feat(notification): add optional callbacks to jump marks
Browse files Browse the repository at this point in the history
Allow to perform specific actions on jump mark click.

closes sbb-design-systems#131
  • Loading branch information
daniel-sc committed Nov 4, 2019
1 parent ca67252 commit ed10da9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IconCollectionModule } from '@sbb-esta/angular-icons';
import { configureTestSuite } from 'ng-bullet';

import { NotificationComponent, NotificationType } from './notification.component';
import createSpy = jasmine.createSpy;

@Component({
selector: 'sbb-notification-mock',
Expand Down Expand Up @@ -109,5 +110,18 @@ describe('NotificationComponent', () => {
expect(componentStyles.height).toBe('92px');
});
});

it('should call callback of jump mark', () => {
const callbackMock = createSpy('mock-callback');
testComponent.jumpMarks = [{ callback: callbackMock, title: 'Here' }];
testFixture.detectChanges();
const notificationLink = testFixture.debugElement.query(
By.css('.sbb-notification-jump-mark > a')
);
notificationLink.triggerEventHandler('click', {
preventDefault: createSpy('prevent-default-mock')
});
expect(callbackMock).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export interface JumpMark {
/** Title of an element in jump marks. */
title: string;
/** Identifier of an element in jump marks. */
elementId: string;
elementId?: string;
callback?: (event$: any, jumpMark: JumpMark) => void;
}

@Component({
Expand Down Expand Up @@ -114,6 +115,11 @@ export class NotificationComponent {
*/
scrollTo($event: any, jumpMark: JumpMark) {
$event.preventDefault();
document.querySelector(jumpMark.elementId).scrollIntoView({ behavior: 'smooth' });
if (jumpMark.elementId) {
document.querySelector(jumpMark.elementId).scrollIntoView({ behavior: 'smooth' });
}
if (jumpMark.callback) {
jumpMark.callback($event, jumpMark);
}
}
}

0 comments on commit ed10da9

Please sign in to comment.