|
| 1 | +// deck.gl |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +// Copyright (c) vis.gl contributors |
| 4 | + |
| 5 | +/* global document */ |
| 6 | +import type {Deck, Widget} from '@deck.gl/core'; |
| 7 | +import {render} from 'preact'; |
| 8 | + |
| 9 | +export type TimelineWidgetProps = { |
| 10 | + /** |
| 11 | + * Widget id |
| 12 | + */ |
| 13 | + id?: string; |
| 14 | + /** |
| 15 | + * CSS inline style overrides. |
| 16 | + */ |
| 17 | + style?: Partial<CSSStyleDeclaration>; |
| 18 | + /** |
| 19 | + * Additional CSS class. |
| 20 | + */ |
| 21 | + className?: string; |
| 22 | + /** |
| 23 | + * Slider domain [min, max]. |
| 24 | + */ |
| 25 | + domain?: [number, number]; |
| 26 | + /** |
| 27 | + * Slider step. |
| 28 | + */ |
| 29 | + step?: number; |
| 30 | + /** |
| 31 | + * Current slider value. |
| 32 | + */ |
| 33 | + value?: number; |
| 34 | + /** |
| 35 | + * Callback when value changes. |
| 36 | + */ |
| 37 | + onTimeChange?: (value: number) => void; |
| 38 | + /** |
| 39 | + * Play interval in milliseconds. |
| 40 | + */ |
| 41 | + playInterval?: number; |
| 42 | +}; |
| 43 | + |
| 44 | +export class TimelineWidget implements Widget<TimelineWidgetProps> { |
| 45 | + id = 'timeline'; |
| 46 | + props: Required<TimelineWidgetProps>; |
| 47 | + deck?: Deck<any>; |
| 48 | + element?: HTMLDivElement; |
| 49 | + private playing = false; |
| 50 | + private timerId: number | null = null; |
| 51 | + |
| 52 | + static defaultProps: Required<TimelineWidgetProps> = { |
| 53 | + id: 'timeline', |
| 54 | + style: {}, |
| 55 | + className: undefined!, |
| 56 | + domain: [0, 100], |
| 57 | + step: 1, |
| 58 | + value: 0, |
| 59 | + onTimeChange: () => {}, |
| 60 | + playInterval: 1000 |
| 61 | + }; |
| 62 | + |
| 63 | + constructor(props: TimelineWidgetProps = {}) { |
| 64 | + this.id = props.id ?? this.id; |
| 65 | + this.props = {...TimelineWidget.defaultProps, ...props}; |
| 66 | + } |
| 67 | + |
| 68 | + onAdd({deck}: {deck: Deck<any>}): HTMLDivElement { |
| 69 | + this.deck = deck; |
| 70 | + const el = document.createElement('div'); |
| 71 | + el.classList.add('deck-widget', 'deck-widget-timeline'); |
| 72 | + if (this.props.className) { |
| 73 | + el.classList.add(this.props.className); |
| 74 | + } |
| 75 | + Object.assign(el.style, this.props.style); |
| 76 | + this.element = el; |
| 77 | + this.renderUI(); |
| 78 | + return el; |
| 79 | + } |
| 80 | + |
| 81 | + onRemove(): void { |
| 82 | + this.stop(); |
| 83 | + this.deck = undefined; |
| 84 | + this.element = undefined; |
| 85 | + } |
| 86 | + |
| 87 | + setProps(props: Partial<TimelineWidgetProps>): void { |
| 88 | + Object.assign(this.props, props); |
| 89 | + if (this.element) { |
| 90 | + // update className |
| 91 | + this.element.className = ['deck-widget', 'deck-widget-timeline', this.props.className] |
| 92 | + .filter(Boolean) |
| 93 | + .join(' '); |
| 94 | + // update style |
| 95 | + if (props.style) { |
| 96 | + Object.assign(this.element.style, this.props.style); |
| 97 | + } |
| 98 | + } |
| 99 | + this.renderUI(); |
| 100 | + } |
| 101 | + |
| 102 | + private handlePlayPause = (): void => { |
| 103 | + if (this.playing) { |
| 104 | + this.stop(); |
| 105 | + } else { |
| 106 | + this.start(); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + private handleSliderChange = (e: Event): void => { |
| 111 | + const input = e.target as HTMLInputElement; |
| 112 | + const val = Number(input.value); |
| 113 | + this.props.value = val; |
| 114 | + this.props.onTimeChange(val); |
| 115 | + this.renderUI(); |
| 116 | + }; |
| 117 | + |
| 118 | + private start(): void { |
| 119 | + this.playing = true; |
| 120 | + this.renderUI(); |
| 121 | + this.tick(); |
| 122 | + } |
| 123 | + |
| 124 | + private stop(): void { |
| 125 | + this.playing = false; |
| 126 | + if (this.timerId != null) { |
| 127 | + window.clearTimeout(this.timerId); |
| 128 | + this.timerId = null; |
| 129 | + } |
| 130 | + this.renderUI(); |
| 131 | + } |
| 132 | + |
| 133 | + private tick = (): void => { |
| 134 | + const [min, max] = this.props.domain; |
| 135 | + let next = this.props.value + this.props.step; |
| 136 | + if (next > max) { |
| 137 | + next = min; |
| 138 | + } |
| 139 | + this.props.value = next; |
| 140 | + this.props.onTimeChange(next); |
| 141 | + this.renderUI(); |
| 142 | + if (this.playing) { |
| 143 | + this.timerId = window.setTimeout(this.tick, this.props.playInterval); |
| 144 | + } |
| 145 | + }; |
| 146 | + |
| 147 | + private renderUI(): void { |
| 148 | + const el = this.element; |
| 149 | + if (!el) { |
| 150 | + return; |
| 151 | + } |
| 152 | + const ui = ( |
| 153 | + <div style={{display: 'flex', alignItems: 'center', pointerEvents: 'auto'}}> |
| 154 | + <button |
| 155 | + type="button" |
| 156 | + onClick={this.handlePlayPause} |
| 157 | + className="timeline-play-pause" |
| 158 | + title={this.playing ? 'Pause' : 'Play'} |
| 159 | + > |
| 160 | + {this.playing ? '⏸' : '▶'} |
| 161 | + </button> |
| 162 | + <input |
| 163 | + type="range" |
| 164 | + min={this.props.domain[0]} |
| 165 | + max={this.props.domain[1]} |
| 166 | + step={this.props.step} |
| 167 | + value={this.props.value} |
| 168 | + onInput={this.handleSliderChange} |
| 169 | + className="timeline-slider" |
| 170 | + /> |
| 171 | + </div> |
| 172 | + ); |
| 173 | + render(ui, el); |
| 174 | + } |
| 175 | +} |
0 commit comments