home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Complet / thunderbird / chrome / mail.jar / content / global / nsTreeSorting.js < prev    next >
Encoding:
JavaScript  |  2003-08-16  |  7.1 KB  |  195 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Peter Annema <disttsc@bart.nl>
  24.  *   Blake Ross <blakeross@telocity.com>
  25.  *   Alec Flett <alecf@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the NPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the NPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41.  
  42. // utility routines for sorting
  43.  
  44. // re-does a sort based on the current state
  45. function RefreshSort()
  46. {
  47.   var current_column = find_sort_column();
  48.   SortColumn(current_column.id);
  49. }
  50.  
  51. // set the sort direction on the currently sorted column
  52. function SortInNewDirection(direction)
  53. {
  54.   var current_column = find_sort_column();
  55.   if (direction == "ascending")
  56.     direction = "natural";
  57.   else if (direction == "descending")
  58.     direction = "ascending";
  59.   else if (direction == "natural")
  60.     direction = "descending";
  61.   current_column.setAttribute("sortDirection", direction);
  62.   SortColumn(current_column.id);
  63. }
  64.  
  65. function SortColumn(columnID)
  66. {
  67.   var column = document.getElementById(columnID);
  68.   column.parentNode.parentNode.treeBoxObject.view.cycleHeader(columnID, column);
  69. }
  70.  
  71. // search over the columns to find the first one with an active sort
  72. function find_sort_column()
  73. {
  74.   var columns = document.getElementsByTagName('treecol');
  75.   var i = 0;
  76.   var column;
  77.   while ((column = columns.item(i++)) != null) {
  78.       if (column.getAttribute('sortDirection'))
  79.           return column;
  80.   }
  81.   return columns.item(0);
  82. }
  83.  
  84. // get the sort direction for the given column
  85. function find_sort_direction(column)
  86. {
  87.   var sortDirection = column.getAttribute('sortDirection');
  88.   return (sortDirection ? sortDirection : "natural");
  89. }
  90.  
  91. // set up the menu items to reflect the specified sort column
  92. // and direction - put check marks next to the active ones, and clear
  93. // out the old ones
  94. // - disable ascending/descending direction if the tree isn't sorted
  95. // - disable columns that are not visible
  96. function update_sort_menuitems(column, direction)
  97. {
  98.   var unsorted_menuitem = document.getElementById("unsorted_menuitem");
  99.   var sort_ascending = document.getElementById('sort_ascending');
  100.   var sort_descending = document.getElementById('sort_descending');
  101.  
  102.   // as this function may be called from various places, including the
  103.   // bookmarks sidebar panel (which doesn't have any menu items)
  104.   // ensure that the document contains the elements
  105.   if ((!unsorted_menuitem) || (!sort_ascending) || (!sort_descending))
  106.     return;
  107.  
  108.   if (direction == "natural") {
  109.     unsorted_menuitem.setAttribute('checked','true');
  110.     sort_ascending.setAttribute('disabled','true');
  111.     sort_descending.setAttribute('disabled','true');
  112.     sort_ascending.removeAttribute('checked');
  113.     sort_descending.removeAttribute('checked');
  114.   } else {
  115.     sort_ascending.removeAttribute('disabled');
  116.     sort_descending.removeAttribute('disabled');
  117.     if (direction == "ascending") {
  118.       sort_ascending.setAttribute('checked','true');
  119.     } else {
  120.       sort_descending.setAttribute('checked','true');
  121.     }
  122.  
  123.     var columns = document.getElementsByTagName('treecol');
  124.     var i = 0;
  125.     var column_node = columns[i];
  126.     var column_name = column.id;
  127.     var menuitem = document.getElementById('fill_after_this_node');
  128.     menuitem = menuitem.nextSibling
  129.     while (1) {
  130.       var name = menuitem.getAttribute('column_id');
  131.       if (!name) break;
  132.       if (column_name == name) {
  133.         menuitem.setAttribute('checked', 'true');
  134.         break;
  135.       }
  136.       menuitem = menuitem.nextSibling;
  137.       column_node = columns[++i];
  138.       if (column_node && column_node.tagName == "splitter") {
  139.         column_node = columns[++i];
  140.       }
  141.     }
  142.   }
  143.   enable_sort_menuitems();
  144. }
  145.  
  146. function enable_sort_menuitems()
  147. {
  148.   var columns = document.getElementsByTagName('treecol');
  149.   var menuitem = document.getElementById('fill_after_this_node');
  150.   menuitem = menuitem.nextSibling
  151.   for (var i = 0; (i < columns.length) && menuitem; ++i) {
  152.     var column_node = columns[i];
  153.     if (column_node.getAttribute("hidden") == "true")
  154.       menuitem.setAttribute("disabled", "true");
  155.     else
  156.       menuitem.removeAttribute("disabled");
  157.     menuitem = menuitem.nextSibling;
  158.   }
  159. }
  160.  
  161. function fillViewMenu(popup)
  162. {
  163.   var fill_after = document.getElementById('fill_after_this_node');
  164.   var fill_before = document.getElementById('fill_before_this_node');
  165.   var strBundle = document.getElementById('sortBundle');
  166.   var sortString;
  167.   if (strBundle)
  168.     sortString = strBundle.getString('SortMenuItems');
  169.   if (!sortString)
  170.     sortString = "Sorted by %COLNAME%";
  171.     
  172.   var firstTime = (fill_after.nextSibling == fill_before);
  173.   if (firstTime) {
  174.     var columns = document.getElementsByTagName('treecol');
  175.     for (var i = 0; i < columns.length; ++i) {
  176.       var column = columns[i];
  177.       // Construct an entry for each cell in the row.
  178.       var column_name = column.getAttribute("label");
  179.       var item = document.createElement("menuitem");
  180.       item.setAttribute("type", "radio");
  181.       item.setAttribute("name", "sort_column");
  182.       if (column_name == "")
  183.         column_name = column.getAttribute("display");
  184.       var name = sortString.replace(/%COLNAME%/g, column_name);
  185.       item.setAttribute("label", name);
  186.       item.setAttribute("oncommand", "SortColumn('" + column.id + "');");
  187.       item.setAttribute("column_id", column.id);
  188.       popup.insertBefore(item, fill_before);
  189.     }
  190.   }
  191.   var sort_column = find_sort_column();
  192.   var sort_direction = find_sort_direction(sort_column);
  193.   update_sort_menuitems(sort_column, sort_direction);
  194. }
  195.