Bug 1833753 [wpt PR 40065] - Allow newly-added test to also pass when mutation events...
[gecko.git] / toolkit / content / treeUtils.js
blob08db884d17eb3f2ba5ccddfd28234a33aed34547
1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 var gTreeUtils = {
8   deleteAll(aTree, aView, aItems, aDeletedItems) {
9     for (var i = 0; i < aItems.length; ++i) {
10       aDeletedItems.push(aItems[i]);
11     }
12     aItems.splice(0, aItems.length);
13     var oldCount = aView.rowCount;
14     aView._rowCount = 0;
15     aTree.rowCountChanged(0, -oldCount);
16   },
18   deleteSelectedItems(aTree, aView, aItems, aDeletedItems) {
19     var selection = aTree.view.selection;
20     selection.selectEventsSuppressed = true;
22     var rc = selection.getRangeCount();
23     for (var i = 0; i < rc; ++i) {
24       var min = {};
25       var max = {};
26       selection.getRangeAt(i, min, max);
27       for (let j = min.value; j <= max.value; ++j) {
28         aDeletedItems.push(aItems[j]);
29         aItems[j] = null;
30       }
31     }
33     var nextSelection = 0;
34     for (i = 0; i < aItems.length; ++i) {
35       if (!aItems[i]) {
36         let j = i;
37         while (j < aItems.length && !aItems[j]) {
38           ++j;
39         }
40         aItems.splice(i, j - i);
41         nextSelection = j < aView.rowCount ? j - 1 : j - 2;
42         aView._rowCount -= j - i;
43         aTree.rowCountChanged(i, i - j);
44       }
45     }
47     if (aItems.length) {
48       selection.select(nextSelection);
49       aTree.ensureRowIsVisible(nextSelection);
50       aTree.focus();
51     }
52     selection.selectEventsSuppressed = false;
53   },
55   sort(
56     aTree,
57     aView,
58     aDataSet,
59     aColumn,
60     aComparator,
61     aLastSortColumn,
62     aLastSortAscending
63   ) {
64     var ascending = aColumn == aLastSortColumn ? !aLastSortAscending : true;
65     if (!aDataSet.length) {
66       return ascending;
67     }
69     var sortFunction = null;
70     if (aComparator) {
71       sortFunction = function (a, b) {
72         return aComparator(a[aColumn], b[aColumn]);
73       };
74     }
75     aDataSet.sort(sortFunction);
76     if (!ascending) {
77       aDataSet.reverse();
78     }
80     aTree.view.selection.clearSelection();
81     aTree.view.selection.select(0);
82     aTree.invalidate();
83     aTree.ensureRowIsVisible(0);
85     return ascending;
86   },