summaryrefslogtreecommitdiff
path: root/apps/web/src/components/toolbar/index.ts
blob: b8a383d78d5d440842ac2ffd5bbc7afa928f4b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import NotiveElement, { customElement, eventHandler } from "../../element";
import h from "../../html";
import { minus16Icon, plus16Icon } from "../icons";
import "./index.css";

export class SubdivisionsChangeEvent extends Event {
  static readonly TYPE = "ntv:toolbar:subdivisionschange";

  constructor(public subdivisions: number | undefined) {
    super(SubdivisionsChangeEvent.TYPE);
  }
}

@customElement("ntv-toolbar")
class NotiveToolbarElement extends NotiveElement {
  #subdivisionsInputEl: HTMLInputElement = h.input({
    title: "Subdivisions",
    disabled: true,
  });

  get subdivisions(): number | undefined {
    if (this.#subdivisionsInputEl.value === "") return;
    return parseInt(this.#subdivisionsInputEl.value);
  }

  set subdivisions(n: number | undefined) {
    const m = n && Math.max(n, 1);
    this.#subdivisionsInputEl.value = m === undefined ? "" : m.toString();
  }

  @eventHandler(SubdivisionsChangeEvent.TYPE)
  onsubdivisionschange?: (event: SubdivisionsChangeEvent) => any;

  connectedCallback() {
    this.append(
      h.section(
        h.button(
          {
            dataset: { icon: "" },
            onclick: () => {
              if (!this.subdivisions) return;
              this.subdivisions = this.subdivisions - 1;
              this.dispatchEvent(
                new SubdivisionsChangeEvent(this.subdivisions),
              );
            },
          },
          h.span(minus16Icon()),
        ),
        this.#subdivisionsInputEl,
        h.button(
          {
            dataset: { icon: "" },
            onclick: () => {
              if (!this.subdivisions) return;
              this.subdivisions = this.subdivisions + 1;
              this.dispatchEvent(
                new SubdivisionsChangeEvent(this.subdivisions),
              );
            },
          },
          h.span(plus16Icon()),
        ),
      ),
      h.section(h.button("Play")),
    );
  }
}

export default NotiveToolbarElement.makeFactory();