1- 'use strict' ;
2-
31/*
42 * Creates a colored timeline where you can hover for more info.
53 */
64
7- const d3 = require ( 'd3' ) ;
8- const Color = require ( 'color' ) ;
9- const _ = require ( 'lodash' ) ;
10- const moment = require ( 'moment' ) ;
5+ import * as d3 from 'd3' ;
6+ import Color from 'color' ;
7+ import _ from 'lodash' ;
8+ import moment from 'moment' ;
119
1210import { getColorFromString } from '../util/color' ;
13-
1411import { seconds_to_duration } from '../util/time' ;
12+ import { IEvent } from '../util/interfaces' ;
1513
16- function show_info ( container , elem_id ) {
17- const title_event_box = container . querySelector ( '#' + elem_id ) ;
18- const titleinfo = container . querySelector ( '.titleinfo-container' ) ;
14+ function show_info ( container : HTMLElement , elem_id : string ) : void {
15+ const title_event_box = container . querySelector ( '#' + elem_id ) as HTMLElement ;
16+ const titleinfo = container . querySelector ( '.titleinfo-container' ) as HTMLElement ;
1917 titleinfo . innerHTML = title_event_box . innerHTML ;
20- titleinfo . style . height = title_event_box . getAttribute ( 'height' ) ;
18+ titleinfo . style . height = title_event_box . getAttribute ( 'height' ) || '' ;
2119}
2220
23- function create ( container ) {
21+ function create ( container : HTMLElement ) : void {
2422 // Clear element
2523 container . innerHTML = '' ;
2624
@@ -32,7 +30,7 @@ function create(container) {
3230 . attr ( 'width' , '100%' ) ;
3331
3432 // Hidden svg image that stores all titleinfo for each timeperiod
35- d3 . select ( container ) . append ( 'div' ) . attr ( 'class' , 'titleinfo-list' ) . attr ( 'display' , 'none' ) ;
33+ d3 . select ( container ) . append ( 'div' ) . attr ( 'class' , 'titleinfo-list' ) . style ( 'display' , 'none' ) ;
3634
3735 // Container for titleinfo that has a fixed size and a overflow scroll
3836 const titleinfo_container = document . createElement ( 'div' ) ;
@@ -44,14 +42,14 @@ function create(container) {
4442 // Titleinfo box that changes content depending on what was timeperiod was last recently hovered on
4543 d3 . select ( titleinfo_container )
4644 . append ( 'div' )
47- . attr ( 'width' , '100%' )
45+ . style ( 'width' , '100%' )
4846 . attr ( 'class' , 'titleinfo-container' ) ;
4947}
5048
51- function set_status ( container , text ) {
52- const timeline_elem = container . querySelector ( '.apptimeline' ) ;
53- const titleinfo_list_elem = container . querySelector ( '.titleinfo-list' ) ;
54- const titleinfo_container_elem = container . querySelector ( '.titleinfo-container' ) ;
49+ function set_status ( container : HTMLElement , text : string ) : void {
50+ const timeline_elem = container . querySelector ( '.apptimeline' ) as HTMLElement ;
51+ const titleinfo_list_elem = container . querySelector ( '.titleinfo-list' ) as HTMLElement ;
52+ const titleinfo_container_elem = container . querySelector ( '.titleinfo-container' ) as HTMLElement ;
5553
5654 const timeline = d3 . select ( timeline_elem ) ;
5755 timeline_elem . innerHTML = '' ;
@@ -68,14 +66,20 @@ function set_status(container, text) {
6866 . attr ( 'fill' , 'black' ) ;
6967}
7068
71- function update ( container , events , showAFK , chunkfunc , eventfunc ) {
69+ function update (
70+ container : HTMLElement ,
71+ events : IEvent [ ] ,
72+ showAFK : boolean ,
73+ chunkfunc : ( event : IEvent ) => string ,
74+ eventfunc : ( subevent : IEvent ) => string
75+ ) : HTMLElement {
7276 const timeline = d3 . select ( container . querySelector ( '.apptimeline' ) ) . html ( null ) ;
7377 const titleinfo_list = d3 . select ( container . querySelector ( '.titleinfo-list' ) ) . html ( null ) ;
7478 d3 . select ( container . querySelector ( '.titleinfo-container' ) ) . html ( null ) ;
7579
7680 if ( events . length <= 0 ) {
7781 set_status ( container , 'No data' ) ;
78- return ;
82+ return container ;
7983 }
8084
8185 const firstEvent = events [ 0 ] ;
@@ -92,12 +96,12 @@ function update(container, events, showAFK, chunkfunc, eventfunc) {
9296 _ . each ( events , function ( e , i ) {
9397 // Timeline rect
9498
95- let eventX , eventWidth ;
99+ let eventX : string | number , eventWidth : number ;
96100 if ( showAFK ) {
97101 const eventBegin = moment ( e . timestamp ) ;
98102 eventX = eventBegin . diff ( timeStart , 'seconds' , true ) / secSinceStart ;
99103 eventX = eventX * 100 + '%' ;
100- eventWidth = ( e . duration / secSinceStart ) * 100 + '%' ;
104+ eventWidth = ( e . duration / secSinceStart ) * 100 ;
101105 } else {
102106 eventX = curr_x ;
103107 eventWidth = ( e . duration / totalDuration ) * 100 ;
@@ -112,7 +116,7 @@ function update(container, events, showAFK, chunkfunc, eventfunc) {
112116 . append ( 'rect' )
113117 . attr ( 'x' , eventX )
114118 . attr ( 'y' , 0 )
115- . attr ( 'width' , eventWidth )
119+ . attr ( 'width' , eventWidth + '%' )
116120 . attr ( 'height' , 10 )
117121 . style ( 'fill' , appcolor )
118122 . on ( 'mouseover' , ( ) => {
@@ -141,7 +145,7 @@ function update(container, events, showAFK, chunkfunc, eventfunc) {
141145
142146 // Titleinfo
143147 const infolist = infobox . append ( 'table' ) ;
144- _ . each ( e . data . subevents , function ( t ) {
148+ _ . each ( e . data . subevents , function ( t : IEvent ) {
145149 const inforow = infolist . append ( 'tr' ) ;
146150 // Clocktime
147151 const clocktime = moment ( t . timestamp ) . format ( 'HH:mm:ss' ) ;
@@ -160,7 +164,7 @@ function update(container, events, showAFK, chunkfunc, eventfunc) {
160164}
161165
162166export default {
163- create : create ,
164- update : update ,
165- set_status : set_status ,
167+ create,
168+ update,
169+ set_status,
166170} ;
0 commit comments