Reorganize client shared modules
[oweals/peertube.git] / client / src / app / videos / +video-watch / timestamp-route-transformer.directive.ts
1 import { Directive, EventEmitter, HostListener, Output } from '@angular/core'
2
3 @Directive({
4   selector: '[timestampRouteTransformer]'
5 })
6 export class TimestampRouteTransformerDirective {
7   @Output() timestampClicked = new EventEmitter<number>()
8
9   @HostListener('click', ['$event'])
10   public onClick ($event: Event) {
11     const target = $event.target as HTMLLinkElement
12
13     if (target.hasAttribute('href') !== true) return
14
15     const ngxLink = document.createElement('a')
16     ngxLink.href = target.getAttribute('href')
17
18     // we only care about reflective links
19     if (ngxLink.host !== window.location.host) return
20
21     const ngxLinkParams = new URLSearchParams(ngxLink.search)
22     if (ngxLinkParams.has('start') !== true) return
23
24     const separators = ['h', 'm', 's']
25     const start = ngxLinkParams
26       .get('start')
27       .match(new RegExp('(\\d{1,9}[' + separators.join('') + '])','g')) // match digits before any given separator
28       .map(t => {
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)
32       })
33       .reduce((acc, t) => acc + t)
34
35     this.timestampClicked.emit(start)
36
37     $event.preventDefault()
38   }
39 }