blob: fabb9540fbb01c7d7c6bc7cad92e007809e69beb [file] [log] [blame]
Hector Dearman3d26fdb2018-07-09 13:54:06 +01001// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Micha Schwab01b35682018-06-28 13:56:49 +010014
15import * as m from 'mithril';
Michail Schwabbeb34522018-07-20 08:15:17 -040016
Deepanjan Roy75b46a92018-07-24 09:11:29 -040017import {TrackState} from '../common/state';
18
Michail Schwabbeb34522018-07-20 08:15:17 -040019import {Milliseconds, TimeScale} from './time_scale';
Deepanjan Roy75b46a92018-07-24 09:11:29 -040020import {TrackImpl} from './track_impl';
21import {trackRegistry} from './track_registry';
Hector Dearman44869b12018-07-09 09:54:31 +010022import {TrackShell} from './track_shell';
Michail Schwabbbdd9832018-07-12 12:02:53 -040023import {VirtualCanvasContext} from './virtual_canvas_context';
Micha Schwab01b35682018-06-28 13:56:49 +010024
Hector Dearman44869b12018-07-09 09:54:31 +010025export const Track = {
Deepanjan Roy75b46a92018-07-24 09:11:29 -040026 oninit({attrs}) {
27 // TODO: Since ES6 modules are asynchronous and it is conceivable that we
28 // want to load a track implementation on demand, we should not rely here on
29 // the fact that the track is already registered. We should show some
30 // default content until a track implementation is found.
31 const trackCreator = trackRegistry.getCreator(attrs.trackState.type);
32 this.trackImpl = trackCreator.create(attrs.trackState);
33 },
34
Micha Schwabda322442018-06-28 20:19:59 +010035 view({attrs}) {
Michail Schwabbeb34522018-07-20 08:15:17 -040036 const sliceStart: Milliseconds = 100000;
37 const sliceEnd: Milliseconds = 400000;
38
39 const rectStart = attrs.timeScale.msToPx(sliceStart);
40 const rectWidth = attrs.timeScale.msToPx(sliceEnd) - rectStart;
Michail Schwab80490552018-07-10 15:45:57 -040041
Deepanjan Roya752b462018-07-04 01:24:31 +010042 return m(
43 '.track',
Michail Schwab80490552018-07-10 15:45:57 -040044 {
45 style: {
46 position: 'absolute',
47 top: attrs.top.toString() + 'px',
48 left: 0,
Deepanjan Roy75b46a92018-07-24 09:11:29 -040049 width: '100%',
50 height: `${attrs.trackState.height}px`,
Michail Schwab80490552018-07-10 15:45:57 -040051 }
52 },
Michail Schwabbeb34522018-07-20 08:15:17 -040053 m(TrackShell,
Deepanjan Roy75b46a92018-07-24 09:11:29 -040054 {name: attrs.trackState.name},
55 // TODO(dproy): Move out DOM Content from the track class.
Michail Schwabbeb34522018-07-20 08:15:17 -040056 m('.marker',
57 {
58 style: {
59 'font-size': '1.5em',
60 position: 'absolute',
61 left: rectStart.toString() + 'px',
62 width: rectWidth.toString() + 'px',
63 background: '#aca'
64 }
65 },
Deepanjan Roy75b46a92018-07-24 09:11:29 -040066 attrs.trackState.name + ' DOM Content')));
67 },
68
69 onupdate({attrs}) {
70 // TODO(dproy): Figure out how track implementations should render DOM.
71 if (attrs.trackContext.isOnCanvas()) {
72 this.trackImpl.draw(attrs.trackContext, attrs.width, attrs.timeScale);
73 }
Micha Schwab01b35682018-06-28 13:56:49 +010074 }
michaschwab3e47c902018-07-16 14:58:51 -040075} as m.Component<{
michaschwab3e47c902018-07-16 14:58:51 -040076 trackContext: VirtualCanvasContext,
77 top: number,
Michail Schwabbeb34522018-07-20 08:15:17 -040078 width: number,
Deepanjan Roy75b46a92018-07-24 09:11:29 -040079 timeScale: TimeScale,
80 trackState: TrackState,
81},
82 // TODO(dproy): Fix formatter. This is ridiculous.
83 {trackImpl: TrackImpl}>;