toolkit/content/treeUtils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/treeUtils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +var gTreeUtils = {
    1.11 +  deleteAll: function (aTree, aView, aItems, aDeletedItems)
    1.12 +  {
    1.13 +    for (var i = 0; i < aItems.length; ++i)
    1.14 +      aDeletedItems.push(aItems[i]);
    1.15 +    aItems.splice(0, aItems.length);
    1.16 +    var oldCount = aView.rowCount;
    1.17 +    aView._rowCount = 0;
    1.18 +    aTree.treeBoxObject.rowCountChanged(0, -oldCount);
    1.19 +  },
    1.20 +
    1.21 +  deleteSelectedItems: function (aTree, aView, aItems, aDeletedItems)
    1.22 +  {
    1.23 +    var selection = aTree.view.selection;
    1.24 +    selection.selectEventsSuppressed = true;
    1.25 +
    1.26 +    var rc = selection.getRangeCount();
    1.27 +    for (var i = 0; i < rc; ++i) {
    1.28 +      var min = { }; var max = { };
    1.29 +      selection.getRangeAt(i, min, max);
    1.30 +      for (var j = min.value; j <= max.value; ++j) {
    1.31 +        aDeletedItems.push(aItems[j]);
    1.32 +        aItems[j] = null;
    1.33 +      }
    1.34 +    }
    1.35 +
    1.36 +    var nextSelection = 0;
    1.37 +    for (i = 0; i < aItems.length; ++i) {
    1.38 +      if (!aItems[i]) {
    1.39 +        var j = i;
    1.40 +        while (j < aItems.length && !aItems[j])
    1.41 +          ++j;
    1.42 +        aItems.splice(i, j - i);
    1.43 +        nextSelection = j < aView.rowCount ? j - 1 : j - 2;
    1.44 +        aView._rowCount -= j - i;
    1.45 +        aTree.treeBoxObject.rowCountChanged(i, i - j);
    1.46 +      }
    1.47 +    }
    1.48 +
    1.49 +    if (aItems.length) {
    1.50 +      selection.select(nextSelection);
    1.51 +      aTree.treeBoxObject.ensureRowIsVisible(nextSelection);
    1.52 +      aTree.focus();
    1.53 +    }
    1.54 +    selection.selectEventsSuppressed = false;
    1.55 +  },
    1.56 +
    1.57 +  sort: function (aTree, aView, aDataSet, aColumn, aComparator,
    1.58 +                  aLastSortColumn, aLastSortAscending)
    1.59 +  {
    1.60 +    var ascending = (aColumn == aLastSortColumn) ? !aLastSortAscending : true;
    1.61 +    if (aDataSet.length == 0)
    1.62 +      return ascending;
    1.63 +
    1.64 +    var numericSort = !isNaN(aDataSet[0][aColumn]);
    1.65 +    var sortFunction = null;
    1.66 +    if (aComparator) {
    1.67 +      sortFunction = function (a, b) { return aComparator(a[aColumn], b[aColumn]); };
    1.68 +    }
    1.69 +    aDataSet.sort(sortFunction);
    1.70 +    if (!ascending)
    1.71 +      aDataSet.reverse();
    1.72 +
    1.73 +    aTree.view.selection.clearSelection();
    1.74 +    aTree.view.selection.select(0);
    1.75 +    aTree.treeBoxObject.invalidate();
    1.76 +    aTree.treeBoxObject.ensureRowIsVisible(0);
    1.77 +
    1.78 +    return ascending;
    1.79 +  }
    1.80 +};
    1.81 +

mercurial