Bumping manifests a=b2g-bump
[gecko.git] / toolkit / content / treeUtils.js
blob0de3a650f0200106e026dd6d7379749df6ad0647
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: function (aTree, aView, aItems, aDeletedItems)
9   {
10     for (var i = 0; i < aItems.length; ++i)
11       aDeletedItems.push(aItems[i]);
12     aItems.splice(0, aItems.length);
13     var oldCount = aView.rowCount;
14     aView._rowCount = 0;
15     aTree.treeBoxObject.rowCountChanged(0, -oldCount);
16   },
18   deleteSelectedItems: function (aTree, aView, aItems, aDeletedItems)
19   {
20     var selection = aTree.view.selection;
21     selection.selectEventsSuppressed = true;
23     var rc = selection.getRangeCount();
24     for (var i = 0; i < rc; ++i) {
25       var min = { }; var max = { };
26       selection.getRangeAt(i, min, max);
27       for (var 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         var j = i;
37         while (j < aItems.length && !aItems[j])
38           ++j;
39         aItems.splice(i, j - i);
40         nextSelection = j < aView.rowCount ? j - 1 : j - 2;
41         aView._rowCount -= j - i;
42         aTree.treeBoxObject.rowCountChanged(i, i - j);
43       }
44     }
46     if (aItems.length) {
47       selection.select(nextSelection);
48       aTree.treeBoxObject.ensureRowIsVisible(nextSelection);
49       aTree.focus();
50     }
51     selection.selectEventsSuppressed = false;
52   },
54   sort: function (aTree, aView, aDataSet, aColumn, aComparator,
55                   aLastSortColumn, aLastSortAscending)
56   {
57     var ascending = (aColumn == aLastSortColumn) ? !aLastSortAscending : true;
58     if (aDataSet.length == 0)
59       return ascending;
61     var numericSort = !isNaN(aDataSet[0][aColumn]);
62     var sortFunction = null;
63     if (aComparator) {
64       sortFunction = function (a, b) { return aComparator(a[aColumn], b[aColumn]); };
65     }
66     aDataSet.sort(sortFunction);
67     if (!ascending)
68       aDataSet.reverse();
70     aTree.view.selection.clearSelection();
71     aTree.view.selection.select(0);
72     aTree.treeBoxObject.invalidate();
73     aTree.treeBoxObject.ensureRowIsVisible(0);
75     return ascending;
76   }