home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / wallet / nsWalletTreeUtils.js < prev    next >
Text File  |  2003-06-08  |  5KB  |  172 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/MPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  */
  22.  
  23. function DeleteAllFromTree
  24.     (tree, view, table, deletedTable, removeButton, removeAllButton) {
  25.  
  26.   // remove all items from table and place in deleted table
  27.   for (var i=0; i<table.length; i++) {
  28.     deletedTable[deletedTable.length] = table[i];
  29.   }
  30.   table.length = 0;
  31.  
  32.   // clear out selections
  33.   tree.treeBoxObject.view.selection.select(-1); 
  34.  
  35.   // redisplay
  36.   view.rowCount = 0;
  37.   tree.treeBoxObject.invalidate();
  38.  
  39.  
  40.   // disable buttons
  41.   document.getElementById(removeButton).setAttribute("disabled", "true")
  42.   document.getElementById(removeAllButton).setAttribute("disabled","true");
  43. }
  44.  
  45. function DeleteSelectedItemFromTree
  46.     (tree, view, table, deletedTable, removeButton, removeAllButton) {
  47.  
  48.   // remove selected items from list (by setting them to null) and place in deleted list
  49.   var selections = GetTreeSelections(tree);
  50.   for (var s=selections.length-1; s>= 0; s--) {
  51.     var i = selections[s];
  52.     deletedTable[deletedTable.length] = table[i];
  53.     table[i] = null;
  54.   }
  55.  
  56.   // collapse list by removing all the null entries
  57.   for (var j=0; j<table.length; j++) {
  58.     if (table[j] == null) {
  59.       var k = j;
  60.       while ((k < table.length) && (table[k] == null)) {
  61.         k++;
  62.       }
  63.       table.splice(j, k-j);
  64.     }
  65.   }
  66.  
  67.   // redisplay
  68.   var box = tree.treeBoxObject;
  69.   var firstRow = box.getFirstVisibleRow();
  70.   if (firstRow > (table.length-1) ) {
  71.     firstRow = table.length-1;
  72.   }
  73.   view.rowCount = table.length;
  74.   box.rowCountChanged(0, table.length);
  75.   box.scrollToRow(firstRow)
  76.  
  77.   // update selection and/or buttons
  78.   if (table.length) {
  79.  
  80.     // update selection
  81.     // note: we need to deselect before reselecting in order to trigger ...Selected method
  82.     var nextSelection = (selections[0] < table.length) ? selections[0] : table.length-1;
  83.     tree.treeBoxObject.view.selection.select(-1); 
  84.     tree.treeBoxObject.view.selection.select(nextSelection);
  85.  
  86.   } else {
  87.  
  88.     // disable buttons
  89.     document.getElementById(removeButton).setAttribute("disabled", "true")
  90.     document.getElementById(removeAllButton).setAttribute("disabled","true");
  91.  
  92.     // clear out selections
  93.     tree.treeBoxObject.view.selection.select(-1); 
  94.   }
  95. }
  96.  
  97. function GetTreeSelections(tree) {
  98.   var selections = [];
  99.   var select = tree.treeBoxObject.selection;
  100.   if (select) {
  101.     var count = select.getRangeCount();
  102.     var min = new Object();
  103.     var max = new Object();
  104.     for (var i=0; i<count; i++) {
  105.       select.getRangeAt(i, min, max);
  106.       for (var k=min.value; k<=max.value; k++) {
  107.         if (k != -1) {
  108.           selections[selections.length] = k;
  109.         }
  110.       }
  111.     }
  112.   }
  113.   return selections;
  114. }
  115.  
  116. function SortTree(tree, view, table, column, lastSortColumn, lastSortAscending) {
  117.  
  118.   // remember which item was selected so we can restore it after the sort
  119.   var selections = GetTreeSelections(tree);
  120.   var selectedNumber = selections.length ? table[selections[0]].number : -1;
  121.  
  122.   // determine if sort is to be ascending or descending
  123.   var ascending = (column == lastSortColumn) ? !lastSortAscending : true;
  124.  
  125.   // do the sort
  126.   BubbleSort(column, ascending, table);
  127.  
  128.   // restore the selection
  129.   var selectedRow = -1;
  130.   if (selectedNumber>=0) {
  131.     for (var s=0; s<table.length; s++) {
  132.       if (table[s].number == selectedNumber) {
  133.         // update selection
  134.         // note: we need to deselect before reselecting in order to trigger ...Selected()
  135.         tree.treeBoxObject.view.selection.select(-1);
  136.         tree.treeBoxObject.view.selection.select(s);
  137.         selectedRow = s;
  138.         break;
  139.       }
  140.     }
  141.   }
  142.  
  143.   // display the results
  144.   tree.treeBoxObject.invalidate();
  145.   if (selectedRow>0) {
  146.     tree.treeBoxObject.ensureRowIsVisible(selectedRow)
  147.   }
  148.  
  149.   return ascending;
  150. }
  151.  
  152. function BubbleSort(columnName, ascending, table) {
  153.   var len = table.length, len_1 = len - 1;
  154.  
  155.   for (var i = 0; i < len_1; i++) {
  156.     var key = table[i][columnName];
  157.     var winner = -1;
  158.     for (var j = i + 1; j < len; j++) {
  159.       var nextKey = table[j][columnName];
  160.       if (ascending ? key > nextKey : key < nextKey) {
  161.         key = nextKey;
  162.         winner = j;
  163.       }
  164.     }
  165.     if (winner != -1){
  166.       var temp = table[i];
  167.       table[i] = table[winner];
  168.       table[winner] = temp;
  169.     }
  170.   }
  171. }
  172.