Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notification): add optional callbacks to jump marks #211

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}