home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2006 April / PCgo_0406_DVD.iso / lib / chrome / embed.jar / content / global / nsTreeSorting.js < prev    next >
Encoding:
JavaScript  |  2002-03-28  |  6.9 KB  |  194 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.   for (var i = 0; i < columns.length; ++i) {
  76.     if (columns[i].getAttribute('sortDirection'))
  77.       return columns[i];
  78.   }
  79.   return columns[0];
  80. }
  81.  
  82. // get the sort direction for the given column
  83. function find_sort_direction(column)
  84. {
  85.   var sortDirection = column.getAttribute('sortDirection');
  86.   return (sortDirection ? sortDirection : "natural");
  87. }
  88.  
  89. // set up the menu items to reflect the specified sort column
  90. // and direction - put check marks next to the active ones, and clear
  91. // out the old ones
  92. // - disable ascending/descending direction if the tree isn't sorted
  93. // - disable columns that are not visible
  94. function update_sort_menuitems(column, direction)
  95. {
  96.   var unsorted_menuitem = document.getElementById("unsorted_menuitem");
  97.   var sort_ascending = document.getElementById('sort_ascending');
  98.   var sort_descending = document.getElementById('sort_descending');
  99.  
  100.   // as this function may be called from various places, including the
  101.   // bookmarks sidebar panel (which doesn't have any menu items)
  102.   // ensure that the document contains the elements
  103.   if ((!unsorted_menuitem) || (!sort_ascending) || (!sort_descending))
  104.     return;
  105.  
  106.   if (direction == "natural") {
  107.     unsorted_menuitem.setAttribute('checked','true');
  108.     sort_ascending.setAttribute('disabled','true');
  109.     sort_descending.setAttribute('disabled','true');
  110.     sort_ascending.removeAttribute('checked');
  111.     sort_descending.removeAttribute('checked');
  112.   } else {
  113.     sort_ascending.removeAttribute('disabled');
  114.     sort_descending.removeAttribute('disabled');
  115.     if (direction == "ascending") {
  116.       sort_ascending.setAttribute('checked','true');
  117.     } else {
  118.       sort_descending.setAttribute('checked','true');
  119.     }
  120.  
  121.     var columns = document.getElementsByTagName('treecol');
  122.     var i = 0;
  123.     var column_node = columns[i];
  124.     var column_name = column.id;
  125.     var menuitem = document.getElementById('fill_after_this_node');
  126.     menuitem = menuitem.nextSibling
  127.     while (1) {
  128.       var name = menuitem.getAttribute('column_id');
  129.       if (!name) break;
  130.       if (column_name == name) {
  131.         menuitem.setAttribute('checked', 'true');
  132.         break;
  133.       }
  134.       menuitem = menuitem.nextSibling;
  135.       column_node = columns[++i];
  136.       if (column_node && column_node.tagName == "splitter") {
  137.         column_node = columns[++i];
  138.       }
  139.     }
  140.   }
  141.   enable_sort_menuitems();
  142. }
  143.  
  144. function enable_sort_menuitems()
  145. {
  146.   var columns = document.getElementsByTagName('treecol');
  147.   var menuitem = document.getElementById('fill_after_this_node');
  148.   menuitem = menuitem.nextSibling
  149.   for (var i = 0; (i < columns.length) && menuitem; ++i) {
  150.     var column_node = columns[i];
  151.     if (column_node.getAttribute("hidden") == "true")
  152.       menuitem.setAttribute("disabled", "true");
  153.     else
  154.       menuitem.removeAttribute("disabled");
  155.     menuitem = menuitem.nextSibling;
  156.   }
  157. }
  158.  
  159. function fillViewMenu(popup)
  160. {
  161.   var fill_after = document.getElementById('fill_after_this_node');
  162.   var fill_before = document.getElementById('fill_before_this_node');
  163.   var columns = document.getElementsByTagName('treecol');
  164.   var strBundle = document.getElementById('sortBundle');
  165.   var sortString;
  166.   if (strBundle)
  167.     sortString = strBundle.getString('SortMenuItems');
  168.   if (!sortString)
  169.     sortString = "Sorted by %COLNAME%";
  170.     
  171.   var popupChild = popup.firstChild.nextSibling.nextSibling;
  172.   var firstTime = (fill_after.nextSibling == fill_before);
  173.   for (var i = 0; i < columns.length; ++i) {
  174.     var column = columns[i];
  175.     if (firstTime) {
  176.       // Construct an entry for each cell in the row.
  177.       var column_name = column.getAttribute("label");
  178.       var item = document.createElement("menuitem");
  179.       item.setAttribute("type", "radio");
  180.       item.setAttribute("name", "sort_column");
  181.       if (column_name == "")
  182.         column_name = column.getAttribute("display");
  183.       var name = sortString.replace(/%COLNAME%/g, column_name);
  184.       item.setAttribute("label", name);
  185.       item.setAttribute("oncommand", "SortColumn('" + column.id + "');");
  186.       item.setAttribute("column_id", column.id);
  187.       popup.insertBefore(item, fill_before);
  188.     }
  189.   }
  190.   var sort_column = find_sort_column();
  191.   var sort_direction = find_sort_direction(sort_column);
  192.   update_sort_menuitems(sort_column, sort_direction);
  193. }
  194.