summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/widgets/tree-listbox.js
blob: 81d42ca72b0139112b2ad68f0630101f9b17f416 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/* 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/. */

{
  // Animation variables for expanding and collapsing child lists.
  const ANIMATION_DURATION_MS = 200;
  const ANIMATION_EASING = "ease";
  let reducedMotionMedia = matchMedia("(prefers-reduced-motion)");

  /**
   * Provides keyboard and mouse interaction to a (possibly nested) list.
   * It is intended for lists with a small number (up to 1000?) of items.
   * Only one item can be selected at a time. Maintenance of the items in the
   * list is not managed here. Styling of the list is not managed here.
   *
   * The following class names apply to list items:
   * - selected: Indicates the currently selected list item.
   * - children: If the list item has descendants.
   * - collapsed: If the list item's descendants are hidden.
   *
   * List items can provide their own twisty element, which will operate when
   * clicked on if given the class name "twisty".
   *
   * This class fires "collapsed", "expanded" and "select" events.
   */
  let TreeListboxMixin = Base =>
    class extends Base {
      /**
       * The selected and focused item, or null if there is none.
       *
       * @type {?HTMLLIElement}
       */
      _selectedRow = null;

      connectedCallback() {
        if (this.hasConnected) {
          return;
        }
        this.hasConnected = true;

        this.setAttribute("is", "tree-listbox");
        switch (this.getAttribute("role")) {
          case "tree":
            this.isTree = true;
            break;
          case "listbox":
            this.isTree = false;
            break;
          default:
            throw new RangeError(
              `Unsupported role ${this.getAttribute("role")}`
            );
        }
        this.tabIndex = 0;

        this.domChanged();
        this._initRows();
        let rows = this.rows;
        if (!this.selectedRow && rows.length) {
          // TODO: This should only really happen on "focus".
          this.selectedRow = rows[0];
        }

        this.addEventListener("click", this);
        this.addEventListener("keydown", this);
        this._mutationObserver.observe(this, {
          subtree: true,
          childList: true,
        });
      }

      handleEvent(event) {
        switch (event.type) {
          case "click":
            this._onClick(event);
            break;
          case "keydown":
            this._onKeyDown(event);
            break;
        }
      }

      _onClick(event) {
        if (event.button !== 0) {
          return;
        }

        let row = event.target.closest("li:not(.unselectable)");
        if (!row) {
          return;
        }

        if (
          row.classList.contains("children") &&
          (event.target.closest(".twisty") || event.detail == 2)
        ) {
          if (row.classList.contains("collapsed")) {
            this.expandRow(row);
          } else {
            this.collapseRow(row);
          }
          return;
        }

        this.selectedRow = row;
        if (document.activeElement != this) {
          // Overflowing elements with tabindex=-1 steal focus. Grab it back.
          this.focus();
        }
      }

      _onKeyDown(event) {
        if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
          return;
        }

        switch (event.key) {
          case "ArrowUp":
            this.selectedIndex = this._clampIndex(this.selectedIndex - 1);
            break;
          case "ArrowDown":
            this.selectedIndex = this._clampIndex(this.selectedIndex + 1);
            break;
          case "Home":
            this.selectedIndex = 0;
            break;
          case "End":
            this.selectedIndex = this.rowCount - 1;
            break;
          case "PageUp": {
            if (!this.selectedRow) {
              break;
            }
            // Get the top of the selected row, and remove the page height.
            let selectedBox = this.selectedRow.getBoundingClientRect();
            let y = selectedBox.top - this.clientHeight;

            // Find the last row below there.
            let rows = this.rows;
            let i = this.selectedIndex - 1;
            while (i > 0 && rows[i].getBoundingClientRect().top >= y) {
              i--;
            }
            this.selectedIndex = i;
            break;
          }
          case "PageDown": {
            if (!this.selectedRow) {
              break;
            }
            // Get the top of the selected row, and add the page height.
            let selectedBox = this.selectedRow.getBoundingClientRect();
            let y = selectedBox.top + this.clientHeight;

            // Find the last row below there.
            let rows = this.rows;
            let i = rows.length - 1;
            while (
              i > this.selectedIndex &&
              rows[i].getBoundingClientRect().top >= y
            ) {
              i--;
            }
            this.selectedIndex = i;
            break;
          }
          case "ArrowLeft":
          case "ArrowRight": {
            let selected = this.selectedRow;
            if (!selected) {
              break;
            }

            let isArrowRight = event.key == "ArrowRight";
            let isRTL = this.matches(":dir(rtl)");
            if (isArrowRight == isRTL) {
              let parent = selected.parentNode.closest(
                ".children:not(.unselectable)"
              );
              if (
                parent &&
                (!selected.classList.contains("children") ||
                  selected.classList.contains("collapsed"))
              ) {
                this.selectedRow = parent;
                break;
              }
              if (selected.classList.contains("children")) {
                this.collapseRow(selected);
              }
            } else if (selected.classList.contains("children")) {
              if (selected.classList.contains("collapsed")) {
                this.expandRow(selected);
              } else {
                this.selectedRow = selected.querySelector("li");
              }
            }
            break;
          }
          case "Enter": {
            const selected = this.selectedRow;
            if (!selected?.classList.contains("children")) {
              return;
            }
            if (selected.classList.contains("collapsed")) {
              this.expandRow(selected);
            } else {
              this.collapseRow(selected);
            }
            break;
          }
          default:
            return;
        }

        event.preventDefault();
      }

      /**
       * Data for the rows in the DOM.
       *
       * @typedef {object} TreeRowData
       * @property {HTMLLIElement} row - The row item.
       * @property {HTMLLIElement[]} ancestors - The ancestors of the row,
       *   ordered closest to furthest away.
       */

      /**
       * Data for all items beneath this node, including collapsed items,
       * ordered as they are in the DOM.
       *
       * @type {TreeRowData[]}
       */
      _rowsData = [];

      /**
       * Call whenever the tree nodes or ordering changes. This should only be
       * called externally if the mutation observer has been dis-connected and
       * re-connected.
       */
      domChanged() {
        this._rowsData = Array.from(this.querySelectorAll("li"), row => {
          let ancestors = [];
          for (
            let parentRow = row.parentNode.closest("li");
            this.contains(parentRow);
            parentRow = parentRow.parentNode.closest("li")
          ) {
            ancestors.push(parentRow);
          }
          return { row, ancestors };
        });
      }

      _mutationObserver = new MutationObserver(mutations => {
        for (let mutation of mutations) {
          for (let node of mutation.addedNodes) {
            if (node.nodeType != Node.ELEMENT_NODE || !node.matches("li")) {
              continue;
            }
            // No item can already be selected on addition.
            node.classList.remove("selected");
          }
        }
        let oldRowsData = this._rowsData;
        this.domChanged();
        this._initRows();
        let newRows = this.rows;
        if (!newRows.length) {
          this.selectedRow = null;
          return;
        }
        if (!this.selectedRow) {
          // TODO: This should only really happen on "focus".
          this.selectedRow = newRows[0];
          return;
        }
        if (newRows.includes(this.selectedRow)) {
          // Selected row is still visible.
          return;
        }
        let oldSelectedIndex = oldRowsData.findIndex(
          entry => entry.row == this.selectedRow
        );
        if (oldSelectedIndex < 0) {
          // Unexpected, the selectedRow was not in our _rowsData list.
          this.selectedRow = newRows[0];
          return;
        }
        // Find the closest ancestor that is still shown.
        let existingAncestor = oldRowsData[oldSelectedIndex].ancestors.find(
          row => newRows.includes(row)
        );
        if (existingAncestor) {
          // We search as if the existingAncestor is the full list. This keeps
          // the selection within the ancestor, or moves it to the ancestor if
          // no child is found.
          // NOTE: Includes existingAncestor itself, so should be non-empty.
          newRows = newRows.filter(row => existingAncestor.contains(row));
        }
        // We have lost the selectedRow, so we select a new row.  We want to try
        // and find the element that exists both in the new rows and in the old
        // rows, that directly preceded the previously selected row. We then
        // want to select the next visible row that follows this found element
        // in the new rows.
        // If rows were replaced with new rows, this will select the first of
        // the new rows.
        // If rows were simply removed, this will select the next row that was
        // not removed.
        let beforeIndex = -1;
        for (let i = oldSelectedIndex; i >= 0; i--) {
          beforeIndex = this._rowsData.findIndex(
            entry => entry.row == oldRowsData[i].row
          );
          if (beforeIndex >= 0) {
            break;
          }
        }
        // Start from just after the found item, or 0 if none were found
        // (beforeIndex == -1), find the next visible item. Otherwise we default
        // to selecting the last row.
        let selectRow = newRows[newRows.length - 1];
        for (let i = beforeIndex + 1; i < this._rowsData.length; i++) {
          if (newRows.includes(this._rowsData[i].row)) {
            selectRow = this._rowsData[i].row;
            break;
          }
        }
        this.selectedRow = selectRow;
      });

      /**
       * Set the role attribute and classes for all descendants of the widget.
       */
      _initRows() {
        let descendantItems = this.querySelectorAll("li");
        let descendantLists = this.querySelectorAll("ol, ul");

        for (let i = 0; i < descendantItems.length; i++) {
          let row = descendantItems[i];
          row.setAttribute("role", this.isTree ? "treeitem" : "option");
          if (
            i + 1 < descendantItems.length &&
            row.contains(descendantItems[i + 1])
          ) {
            row.classList.add("children");
            if (this.isTree) {
              row.setAttribute(
                "aria-expanded",
                !row.classList.contains("collapsed")
              );
            }
          } else {
            row.classList.remove("children");
            row.classList.remove("collapsed");
            row.removeAttribute("aria-expanded");
          }
          row.setAttribute("aria-selected", row.classList.contains("selected"));
        }

        if (this.isTree) {
          for (let list of descendantLists) {
            list.setAttribute("role", "group");
          }
        }

        for (let childList of this.querySelectorAll(
          "li.collapsed > :is(ol, ul)"
        )) {
          childList.style.height = "0";
        }
      }

      /**
       * Every visible row. Rows with collapsed ancestors are not included.
       *
       * @type {HTMLLIElement[]}
       */
      get rows() {
        return [...this.querySelectorAll("li:not(.unselectable)")].filter(
          row => {
            let collapsed = row.parentNode.closest("li.collapsed");
            if (collapsed && this.contains(collapsed)) {
              return false;
            }
            return true;
          }
        );
      }

      /**
       * The number of visible rows.
       *
       * @type {integer}
       */
      get rowCount() {
        return this.rows.length;
      }

      /**
       * Clamps `index` to a value between 0 and `rowCount - 1`.
       *
       * @param {integer} index
       * @returns {integer}
       */
      _clampIndex(index) {
        if (index >= this.rowCount) {
          return this.rowCount - 1;
        }
        if (index < 0) {
          return 0;
        }
        return index;
      }

      /**
       * Ensures that the row at `index` is on the screen.
       *
       * @param {integer} index
       */
      scrollToIndex(index) {
        this.getRowAtIndex(index)?.scrollIntoView({ block: "nearest" });
      }

      /**
       * Returns the row element at `index` or null if `index` is out of range.
       *
       * @param {integer} index
       * @returns {HTMLLIElement?}
       */
      getRowAtIndex(index) {
        return this.rows[index];
      }

      /**
       * The index of the selected row. If there are no rows, the value is -1.
       * Otherwise, should always have a value between 0 and `rowCount - 1`.
       * It is set to 0 in `connectedCallback` if there are rows.
       *
       * @type {integer}
       */
      get selectedIndex() {
        return this.rows.findIndex(row => row == this.selectedRow);
      }

      set selectedIndex(index) {
        index = this._clampIndex(index);
        this.selectedRow = this.getRowAtIndex(index);
      }

      /**
       * The selected and focused item, or null if there is none.
       *
       * @type {?HTMLLIElement}
       */
      get selectedRow() {
        return this._selectedRow;
      }

      set selectedRow(row) {
        if (row == this._selectedRow) {
          return;
        }

        if (this._selectedRow) {
          this._selectedRow.classList.remove("selected");
          this._selectedRow.setAttribute("aria-selected", "false");
        }

        this._selectedRow = row ?? null;
        if (row) {
          row.classList.add("selected");
          row.setAttribute("aria-selected", "true");
          this.setAttribute("aria-activedescendant", row.id);
          row.firstElementChild.scrollIntoView({ block: "nearest" });
        } else {
          this.removeAttribute("aria-activedescendant");
        }

        this.dispatchEvent(new CustomEvent("select"));
      }

      /**
       * Collapses the row at `index` if it can be collapsed. If the selected
       * row is a descendant of the collapsing row, selection is moved to the
       * collapsing row.
       *
       * @param {integer} index
       */
      collapseRowAtIndex(index) {
        this.collapseRow(this.getRowAtIndex(index));
      }

      /**
       * Expands the row at `index` if it can be expanded.
       *
       * @param {integer} index
       */
      expandRowAtIndex(index) {
        this.expandRow(this.getRowAtIndex(index));
      }

      /**
       * Collapses the row if it can be collapsed. If the selected row is a
       * descendant of the collapsing row, selection is moved to the collapsing
       * row.
       *
       * @param {HTMLLIElement} row - The row to collapse.
       */
      collapseRow(row) {
        if (
          row.classList.contains("children") &&
          !row.classList.contains("collapsed")
        ) {
          if (row.contains(this.selectedRow)) {
            this.selectedRow = row;
          }
          row.classList.add("collapsed");
          if (this.isTree) {
            row.setAttribute("aria-expanded", "false");
          }
          row.dispatchEvent(new CustomEvent("collapsed", { bubbles: true }));
          this._animateCollapseRow(row);
        }
      }

      /**
       * Expands the row if it can be expanded.
       *
       * @param {HTMLLIElement} row - The row to expand.
       */
      expandRow(row) {
        if (
          row.classList.contains("children") &&
          row.classList.contains("collapsed")
        ) {
          row.classList.remove("collapsed");
          if (this.isTree) {
            row.setAttribute("aria-expanded", "true");
          }
          row.dispatchEvent(new CustomEvent("expanded", { bubbles: true }));
          this._animateExpandRow(row);
        }
      }

      /**
       * Animate the collapsing of a row containing child items.
       *
       * @param {HTMLLIElement} row - The parent row element.
       */
      _animateCollapseRow(row) {
        let childList = row.querySelector("ol, ul");

        if (reducedMotionMedia.matches) {
          if (childList) {
            childList.style.height = "0";
          }
          return;
        }

        let childListHeight = childList.scrollHeight;

        let animation = childList.animate(
          [{ height: `${childListHeight}px` }, { height: "0" }],
          {
            duration: ANIMATION_DURATION_MS,
            easing: ANIMATION_EASING,
            fill: "both",
          }
        );
        animation.onfinish = () => {
          childList.style.height = "0";
          animation.cancel();
        };
      }

      /**
       * Animate the revealing of a row containing child items.
       *
       * @param {HTMLLIElement} row - The parent row element.
       */
      _animateExpandRow(row) {
        let childList = row.querySelector("ol, ul");

        if (reducedMotionMedia.matches) {
          if (childList) {
            childList.style.height = null;
          }
          return;
        }

        let childListHeight = childList.scrollHeight;

        let animation = childList.animate(
          [{ height: "0" }, { height: `${childListHeight}px` }],
          {
            duration: ANIMATION_DURATION_MS,
            easing: ANIMATION_EASING,
            fill: "both",
          }
        );
        animation.onfinish = () => {
          childList.style.height = null;
          animation.cancel();
        };
      }
    };

  /**
   * An unordered list with the functionality of TreeListboxMixin.
   */
  class TreeListbox extends TreeListboxMixin(HTMLUListElement) {}
  customElements.define("tree-listbox", TreeListbox, { extends: "ul" });

  /**
   * An ordered list with the functionality of TreeListboxMixin, plus the
   * ability to re-order the top-level list by drag-and-drop/Alt+Up/Alt+Down.
   *
   * This class fires an "ordered" event when the list is re-ordered.
   *
   * @note All children of this element should be HTML. If there are XUL
   * elements, you're gonna have a bad time.
   */
  class OrderableTreeListbox extends TreeListboxMixin(HTMLOListElement) {
    connectedCallback() {
      super.connectedCallback();
      this.setAttribute("is", "orderable-tree-listbox");

      this.addEventListener("dragstart", this);
      window.addEventListener("dragover", this);
      window.addEventListener("drop", this);
      window.addEventListener("dragend", this);
    }

    handleEvent(event) {
      super.handleEvent(event);

      switch (event.type) {
        case "dragstart":
          this._onDragStart(event);
          break;
        case "dragover":
          this._onDragOver(event);
          break;
        case "drop":
          this._onDrop(event);
          break;
        case "dragend":
          this._onDragEnd(event);
          break;
      }
    }

    /**
     * An array of all top-level rows that can be reordered. Override this
     * getter to prevent reordering of one or more rows.
     *
     * @note So far this has only been used to prevent the last row being
     *   moved. Any other use is untested. It likely also works for rows at
     *   the top of the list.
     *
     * @returns {HTMLLIElement[]}
     */
    get _orderableChildren() {
      return [...this.children];
    }

    _onKeyDown(event) {
      super._onKeyDown(event);

      if (
        !event.altKey ||
        event.ctrlKey ||
        event.metaKey ||
        event.shiftKey ||
        !["ArrowUp", "ArrowDown"].includes(event.key)
      ) {
        return;
      }

      let row = this.selectedRow;
      if (!row || row.parentElement != this) {
        return;
      }

      let otherRow;
      if (event.key == "ArrowUp") {
        otherRow = row.previousElementSibling;
      } else {
        otherRow = row.nextElementSibling;
      }
      if (!otherRow) {
        return;
      }

      // Check we can move these rows.
      let orderable = this._orderableChildren;
      if (!orderable.includes(row) || !orderable.includes(otherRow)) {
        return;
      }

      let reducedMotion = reducedMotionMedia.matches;

      this.scrollToIndex(this.rows.indexOf(otherRow));

      // Temporarily disconnect the mutation observer to stop it changing things.
      this._mutationObserver.disconnect();
      if (event.key == "ArrowUp") {
        if (!reducedMotion) {
          let { top: otherTop } = otherRow.getBoundingClientRect();
          let { top: rowTop, height: rowHeight } = row.getBoundingClientRect();
          OrderableTreeListbox._animateTranslation(otherRow, 0 - rowHeight);
          OrderableTreeListbox._animateTranslation(row, rowTop - otherTop);
        }
        this.insertBefore(row, otherRow);
      } else {
        if (!reducedMotion) {
          let { top: otherTop, height: otherHeight } =
            otherRow.getBoundingClientRect();
          let { top: rowTop, height: rowHeight } = row.getBoundingClientRect();
          OrderableTreeListbox._animateTranslation(otherRow, rowHeight);
          OrderableTreeListbox._animateTranslation(
            row,
            rowTop - otherTop - otherHeight + rowHeight
          );
        }
        this.insertBefore(row, otherRow.nextElementSibling);
      }
      this._mutationObserver.observe(this, { subtree: true, childList: true });

      // Rows moved.
      this.domChanged();
      this.dispatchEvent(new CustomEvent("ordered", { detail: row }));
    }

    _onDragStart(event) {
      if (!event.target.closest("[draggable]")) {
        // This shouldn't be necessary, but is?!
        event.preventDefault();
        return;
      }

      let orderable = this._orderableChildren;
      if (orderable.length < 2) {
        return;
      }

      for (let topLevelRow of orderable) {
        if (topLevelRow.contains(event.target)) {
          let rect = topLevelRow.getBoundingClientRect();
          this._dragInfo = {
            row: topLevelRow,
            // How far can we move `topLevelRow` upwards?
            min: orderable[0].getBoundingClientRect().top - rect.top,
            // How far can we move `topLevelRow` downwards?
            max:
              orderable[orderable.length - 1].getBoundingClientRect().bottom -
              rect.bottom,
            // Where is the pointer relative to the scroll box of the list?
            // (Not quite, the Y position of `this` is not removed, but we'd
            // only have to do the same where this value is used.)
            scrollY: event.clientY + this.scrollTop,
            // Where is the pointer relative to `topLevelRow`?
            offsetY: event.clientY - rect.top,
          };
          topLevelRow.classList.add("dragging");

          // Prevent `topLevelRow` being used as the drag image. We don't
          // really want any drag image, but there's no way to not have one.
          event.dataTransfer.setDragImage(document.createElement("img"), 0, 0);
          return;
        }
      }
    }

    _onDragOver(event) {
      if (!this._dragInfo) {
        return;
      }

      let { row, min, max, scrollY, offsetY } = this._dragInfo;

      // Move `row` with the mouse pointer.
      let dragY = Math.min(
        max,
        Math.max(min, event.clientY + this.scrollTop - scrollY)
      );
      row.style.transform = `translateY(${dragY}px)`;

      let thisRect = this.getBoundingClientRect();
      // How much space is there above `row`? We'll see how many rows fit in
      // the space and put `row` in after them.
      let spaceAbove = Math.max(
        0,
        event.clientY + this.scrollTop - offsetY - thisRect.top
      );
      // The height of all rows seen in the loop so far.
      let totalHeight = 0;
      // If we've looped past the row being dragged.
      let afterDraggedRow = false;
      // The row before where a drop would take place. If null, drop would
      // happen at the start of the list.
      let targetRow = null;

      for (let topLevelRow of this._orderableChildren) {
        if (topLevelRow == row) {
          afterDraggedRow = true;
          continue;
        }

        let rect = topLevelRow.getBoundingClientRect();
        let enoughSpace = spaceAbove > totalHeight + rect.height / 2;

        let multiplier = 0;
        if (enoughSpace) {
          if (afterDraggedRow) {
            multiplier = -1;
          }
          targetRow = topLevelRow;
        } else if (!afterDraggedRow) {
          multiplier = 1;
        }
        OrderableTreeListbox._transitionTranslation(
          topLevelRow,
          multiplier * row.clientHeight
        );

        totalHeight += rect.height;
      }

      this._dragInfo.dropTarget = targetRow;
      event.preventDefault();
    }

    _onDrop(event) {
      if (!this._dragInfo) {
        return;
      }

      let { row, dropTarget } = this._dragInfo;

      let targetRow;
      if (dropTarget) {
        targetRow = dropTarget.nextElementSibling;
      } else {
        targetRow = this.firstElementChild;
      }

      event.preventDefault();
      // Temporarily disconnect the mutation observer to stop it changing things.
      this._mutationObserver.disconnect();
      this.insertBefore(row, targetRow);
      this._mutationObserver.observe(this, { subtree: true, childList: true });
      // Rows moved.
      this.domChanged();
      this.dispatchEvent(new CustomEvent("ordered", { detail: row }));
    }

    _onDragEnd(event) {
      if (!this._dragInfo) {
        return;
      }

      this._dragInfo.row.classList.remove("dragging");
      delete this._dragInfo;

      for (let topLevelRow of this.children) {
        topLevelRow.style.transition = null;
        topLevelRow.style.transform = null;
      }
    }

    /**
     * Used to animate a real change in the order. The element is moved in the
     * DOM, then the animation makes it appear to move from the original
     * position to the new position
     *
     * @param {HTMLLIElement} element - The row to animate.
     * @param {number} from - Original Y position of the element relative to
     *   its current position.
     */
    static _animateTranslation(element, from) {
      let animation = element.animate(
        [
          { transform: `translateY(${from}px)` },
          { transform: "translateY(0px)" },
        ],
        {
          duration: ANIMATION_DURATION_MS,
          fill: "both",
        }
      );
      animation.onfinish = () => animation.cancel();
    }

    /**
     * Used to simulate a change in the order. The element remains in the same
     * DOM position.
     *
     * @param {HTMLLIElement} element - The row to animate.
     * @param {number} to - The new Y position of the element after animation.
     */
    static _transitionTranslation(element, to) {
      if (!reducedMotionMedia.matches) {
        element.style.transition = `transform ${ANIMATION_DURATION_MS}ms`;
      }
      element.style.transform = to ? `translateY(${to}px)` : null;
    }
  }
  customElements.define("orderable-tree-listbox", OrderableTreeListbox, {
    extends: "ol",
  });
}