1 import { Directive, EventEmitter, HostListener, Output } from '@angular/core'
4 selector: '[myTimestampRouteTransformer]'
6 export class TimestampRouteTransformerDirective {
7 @Output() timestampClicked = new EventEmitter<number>()
9 @HostListener('click', [ '$event' ])
10 public onClick ($event: Event) {
11 const target = $event.target as HTMLLinkElement
13 if (target.hasAttribute('href') !== true) return
15 const ngxLink = document.createElement('a')
16 ngxLink.href = target.getAttribute('href')
18 // we only care about reflective links
19 if (ngxLink.host !== window.location.host) return
21 const ngxLinkParams = new URLSearchParams(ngxLink.search)
22 if (ngxLinkParams.has('start') !== true) return
24 const separators = [ 'h', 'm', 's' ]
25 const start = ngxLinkParams
27 .match(new RegExp('(\\d{1,9}[' + separators.join('') + '])', 'g')) // match digits before any given separator
29 if (t.includes('h')) return parseInt(t, 10) * 3600
30 if (t.includes('m')) return parseInt(t, 10) * 60
31 return parseInt(t, 10)
33 .reduce((acc, t) => acc + t)
35 this.timestampClicked.emit(start)
37 $event.preventDefault()