summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/megalist/content/VirtualizedList.mjs
blob: 7903a189eba7be606620afdb613c915ab967f933 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Virtualized List can efficiently show billions of lines provided
 * that all of them have the same height.
 *
 * Caller is responsible for setting createLineElement(index) function to
 * create elements as they are scrolled into the view.
 */
class VirtualizedList extends HTMLElement {
  lineHeight = 64;
  #lineCount = 0;

  get lineCount() {
    return this.#lineCount;
  }

  set lineCount(value) {
    this.#lineCount = value;
    this.#rebuildVisibleLines();
  }

  #selectedIndex = 0;

  get selectedIndex() {
    return this.#selectedIndex;
  }

  set selectedIndex(value) {
    this.#selectedIndex = value;
    if (this.#container) {
      this.updateLineSelection(true);
    }
  }

  #container;

  connectedCallback() {
    this.#container = this.ownerDocument.createElement("ul");
    this.#container.classList.add("lines-container");
    this.appendChild(this.#container);

    this.#rebuildVisibleLines();
    this.addEventListener("scroll", () => this.#rebuildVisibleLines());
  }

  requestRefresh() {
    this.#container.replaceChildren();
    this.#rebuildVisibleLines();
  }

  updateLineSelection(scrollIntoView) {
    const lineElements = this.#container.querySelectorAll(".line");
    let selectedElement;

    for (let lineElement of lineElements) {
      let isSelected = Number(lineElement.dataset.index) === this.selectedIndex;
      if (isSelected) {
        selectedElement = lineElement;
      }
      lineElement.classList.toggle("selected", isSelected);
    }

    if (scrollIntoView) {
      if (selectedElement) {
        selectedElement.scrollIntoView({ block: "nearest" });
      } else {
        let selectedTop = this.selectedIndex * this.lineHeight;
        if (this.scrollTop > selectedTop) {
          this.scrollTop = selectedTop;
        } else {
          this.scrollTop = selectedTop - this.clientHeight + this.lineHeight;
        }
      }
    }
  }

  #rebuildVisibleLines() {
    if (!this.isConnected || !this.createLineElement) {
      return;
    }

    this.#container.style.height = `${this.lineHeight * this.lineCount}px`;

    let firstLineIndex = Math.floor(this.scrollTop / this.lineHeight);
    let visibleLineCount = Math.ceil(this.clientHeight / this.lineHeight);
    let lastLineIndex = firstLineIndex + visibleLineCount;
    let extraLines = Math.ceil(visibleLineCount / 2); // They are present in DOM, but not visible

    firstLineIndex = Math.max(0, firstLineIndex - extraLines);
    lastLineIndex = Math.min(this.lineCount, lastLineIndex + extraLines);

    let previousChild = null;
    let visibleLines = new Map();

    for (let child of Array.from(this.#container.children)) {
      let index = Number(child.dataset.index);
      if (index < firstLineIndex || index > lastLineIndex) {
        child.remove();
      } else {
        visibleLines.set(index, child);
      }
    }

    for (let index = firstLineIndex; index <= lastLineIndex; index++) {
      let child = visibleLines.get(index);
      if (!child) {
        child = this.createLineElement(index);

        if (!child) {
          // Friday fix :-)
          //todo: figure out what was on that Friday and how can we fix it
          continue;
        }

        child.style.top = `${index * this.lineHeight}px`;
        child.dataset.index = index;

        if (previousChild) {
          previousChild.after(child);
        } else if (this.#container.firstElementChild?.offsetTop > top) {
          this.#container.firstElementChild.before(child);
        } else {
          this.#container.appendChild(child);
        }
      }
      previousChild = child;
    }

    this.updateLineSelection(false);
  }
}

customElements.define("virtualized-list", VirtualizedList);