summaryrefslogtreecommitdiff
path: root/web/src/components/app/index.ts
blob: aa7c7387a95352ba2b88a730d3614a00dffec378 (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
import defaultDoc from "../../defaultDoc";
import NotiveElement, { customElement } from "../../element";
import { Doc } from "../../types";
import ntvGrid, { NotiveGridElement } from "../grid";
import renderGrid from "../grid/renderGrid";
import { GridSelection } from "../grid/selection";
import ntvToolbar from "../toolbar";
import "./index.css";

@customElement("ntv-app")
export class NotiveAppElement extends NotiveElement {
  doc: Doc = defaultDoc();

  #selectedGridId?: string;
  #selection?: GridSelection;

  setSelection(gridId: string, selection: GridSelection) {
    this.#selectedGridId = gridId;
    this.#selection = selection;
    this.#updateGridSelections();
  }

  #updateGridSelections() {
    this.querySelectorAll<NotiveGridElement>("ntv-grid").forEach((grid) => {
      grid.selection =
        this.#selectedGridId === grid.grid?.id ? this.#selection : undefined;
    });
  }

  connectedCallback() {
    this.append(
      ntvToolbar(),

      ...this.doc.grids.map((grid) =>
        ntvGrid({
          grid: renderGrid(grid),
          dataset: { gridId: grid.id },
          ongridselectionchange: (event) => {
            this.setSelection(grid.id, event.selection);
          },
          oncellchange: (event) => {
            console.log(event);
          },
        }),
      ),
    );
  }
}

export default NotiveAppElement.makeFactory();