Skip to content

Commit

Permalink
feat: extend ishServerHtml directive to apply default handling to 'ja…
Browse files Browse the repository at this point in the history
…vascript:' links and allow a callback function in combination with further link handling
  • Loading branch information
shauke committed Feb 20, 2020
1 parent 5efa993 commit 8716834
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/app/core/directives/server-html.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,30 @@ export class ServerHtmlDirective implements AfterContentInit, AfterViewInit, OnD
const href = el.getAttribute('href');
const cb = el.getAttribute('callback');

// apply default link handling for empty href, external links & target _blank
if (!cb && (!href || href.startsWith('http') || el.getAttribute('target') === '_blank')) {
// handle links with callback functions, e.g. <a callback="availableCallbackFunction">
if (cb && this.callbacks && typeof this.callbacks[cb] === 'function') {
this.callbacks[cb]();
}

// apply default link handling for empty href, external links, javascript links & target _blank
if (
!href ||
href.startsWith('http') ||
href.startsWith('javascript:') ||
el.getAttribute('target') === '_blank'
) {
return;
}

if (cb && this.callbacks && typeof this.callbacks[cb] === 'function') {
// handle links with callback functions, e.g. <a callback="availableCallbackFunction">
this.callbacks[cb]();
} else if (href.startsWith('#')) {
// handle fragment links / anchor navigation
// handle fragment links / anchor navigation
if (href.startsWith('#')) {
document.getElementById(href.replace('#', '')).scrollIntoView({ block: 'start', behavior: 'smooth' });
} else {
// otherwise handle as routerLink
this.router.navigateByUrl(href);
}

// prevent default link handling
event.preventDefault();
return false;
}
Expand Down

0 comments on commit 8716834

Please sign in to comment.