Hector Dearman | 3d26fdb | 2018-07-09 13:54:06 +0100 | [diff] [blame] | 1 | // 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 Schwab | 01b3568 | 2018-06-28 13:56:49 +0100 | [diff] [blame] | 14 | |
| 15 | import * as m from 'mithril'; |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 16 | |
Deepanjan Roy | 75b46a9 | 2018-07-24 09:11:29 -0400 | [diff] [blame^] | 17 | import {TrackState} from '../common/state'; |
| 18 | |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 19 | import {Milliseconds, TimeScale} from './time_scale'; |
Deepanjan Roy | 75b46a9 | 2018-07-24 09:11:29 -0400 | [diff] [blame^] | 20 | import {TrackImpl} from './track_impl'; |
| 21 | import {trackRegistry} from './track_registry'; |
Hector Dearman | 44869b1 | 2018-07-09 09:54:31 +0100 | [diff] [blame] | 22 | import {TrackShell} from './track_shell'; |
Michail Schwab | bbdd983 | 2018-07-12 12:02:53 -0400 | [diff] [blame] | 23 | import {VirtualCanvasContext} from './virtual_canvas_context'; |
Micha Schwab | 01b3568 | 2018-06-28 13:56:49 +0100 | [diff] [blame] | 24 | |
Hector Dearman | 44869b1 | 2018-07-09 09:54:31 +0100 | [diff] [blame] | 25 | export const Track = { |
Deepanjan Roy | 75b46a9 | 2018-07-24 09:11:29 -0400 | [diff] [blame^] | 26 | 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 Schwab | da32244 | 2018-06-28 20:19:59 +0100 | [diff] [blame] | 35 | view({attrs}) { |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 36 | 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 Schwab | 8049055 | 2018-07-10 15:45:57 -0400 | [diff] [blame] | 41 | |
Deepanjan Roy | a752b46 | 2018-07-04 01:24:31 +0100 | [diff] [blame] | 42 | return m( |
| 43 | '.track', |
Michail Schwab | 8049055 | 2018-07-10 15:45:57 -0400 | [diff] [blame] | 44 | { |
| 45 | style: { |
| 46 | position: 'absolute', |
| 47 | top: attrs.top.toString() + 'px', |
| 48 | left: 0, |
Deepanjan Roy | 75b46a9 | 2018-07-24 09:11:29 -0400 | [diff] [blame^] | 49 | width: '100%', |
| 50 | height: `${attrs.trackState.height}px`, |
Michail Schwab | 8049055 | 2018-07-10 15:45:57 -0400 | [diff] [blame] | 51 | } |
| 52 | }, |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 53 | m(TrackShell, |
Deepanjan Roy | 75b46a9 | 2018-07-24 09:11:29 -0400 | [diff] [blame^] | 54 | {name: attrs.trackState.name}, |
| 55 | // TODO(dproy): Move out DOM Content from the track class. |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 56 | 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 Roy | 75b46a9 | 2018-07-24 09:11:29 -0400 | [diff] [blame^] | 66 | 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 Schwab | 01b3568 | 2018-06-28 13:56:49 +0100 | [diff] [blame] | 74 | } |
michaschwab | 3e47c90 | 2018-07-16 14:58:51 -0400 | [diff] [blame] | 75 | } as m.Component<{ |
michaschwab | 3e47c90 | 2018-07-16 14:58:51 -0400 | [diff] [blame] | 76 | trackContext: VirtualCanvasContext, |
| 77 | top: number, |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 78 | width: number, |
Deepanjan Roy | 75b46a9 | 2018-07-24 09:11:29 -0400 | [diff] [blame^] | 79 | timeScale: TimeScale, |
| 80 | trackState: TrackState, |
| 81 | }, |
| 82 | // TODO(dproy): Fix formatter. This is ridiculous. |
| 83 | {trackImpl: TrackImpl}>; |