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 / navigator / sessionHistoryUI.js < prev    next >
Text File  |  2003-06-08  |  11KB  |  318 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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.  *  Jason Eager <jce2@po.cwru.edu>
  24.  *  Blake Ross <BlakeR1234@aol.com>
  25.  *  Peter Annema <disttsc@bart.nl>
  26.  *  Dean Tessman <dean_tessman@hotmail.com>
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the NPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the NPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41. const MAX_HISTORY_MENU_ITEMS = 15;
  42. const MAX_HISTORY_ITEMS = 100;
  43. var gRDF = null;
  44. var gRDFC = null;
  45. var gGlobalHistory = null;
  46. var gURIFixup = null;
  47. var gLocalStore = null;
  48.  
  49. function FillHistoryMenu(aParent, aMenu)
  50.   {
  51.     // Remove old entries if any
  52.     deleteHistoryItems(aParent);
  53.  
  54.     var sessionHistory = getWebNavigation().sessionHistory;
  55.  
  56.     var count = sessionHistory.count;
  57.     var index = sessionHistory.index;
  58.     var end;
  59.     var j;
  60.     var entry;
  61.  
  62.     switch (aMenu)
  63.       {
  64.         case "back":
  65.           end = (index > MAX_HISTORY_MENU_ITEMS) ? index - MAX_HISTORY_MENU_ITEMS : 0;
  66.           if ((index - 1) < end) return false;
  67.           for (j = index - 1; j >= end; j--)
  68.             {
  69.               entry = sessionHistory.getEntryAtIndex(j, false);
  70.               if (entry)
  71.                 createMenuItem(aParent, j, entry.title);
  72.             }
  73.           break;
  74.         case "forward":
  75.           end  = ((count-index) > MAX_HISTORY_MENU_ITEMS) ? index + MAX_HISTORY_MENU_ITEMS : count;
  76.           if ((index + 1) >= end) return false;
  77.           for (j = index + 1; j < end; j++)
  78.             {
  79.               entry = sessionHistory.getEntryAtIndex(j, false);
  80.               if (entry)
  81.                 createMenuItem(aParent, j, entry.title);
  82.             }
  83.           break;
  84.         case "go":
  85.           aParent.lastChild.hidden = (count == 0);
  86.           end = count > MAX_HISTORY_MENU_ITEMS ? count - MAX_HISTORY_MENU_ITEMS : 0;
  87.           for (j = count - 1; j >= end; j--)
  88.             {
  89.               entry = sessionHistory.getEntryAtIndex(j, false);
  90.               if (entry)
  91.                 createRadioMenuItem(aParent, j, entry.title, j==index);
  92.             }
  93.           break;
  94.       }
  95.     return true;
  96.   }
  97.  
  98. function executeUrlBarHistoryCommand( aTarget )
  99.   {
  100.     var index = aTarget.getAttribute("index");
  101.     var label = aTarget.getAttribute("label");
  102.     if (index != "nothing_available" && label)
  103.       {
  104.         var uri = getShortcutOrURI(label);
  105.         if (gURLBar) {
  106.           gURLBar.value = uri;
  107.           addToUrlbarHistory();
  108.           BrowserLoadURL();
  109.         }
  110.         else
  111.           loadURI(uri);
  112.       }
  113.   }
  114.  
  115. function createUBHistoryMenu( aParent )
  116.   {
  117.     if (!gRDF)
  118.       gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  119.                        .getService(Components.interfaces.nsIRDFService);
  120.  
  121.     if (!gLocalStore)
  122.       gLocalStore = gRDF.GetDataSource("rdf:local-store");
  123.  
  124.     if (gLocalStore) {
  125.       if (!gRDFC)
  126.         gRDFC = Components.classes["@mozilla.org/rdf/container-utils;1"]
  127.                           .getService(Components.interfaces.nsIRDFContainerUtils);
  128.  
  129.       var entries = gRDFC.MakeSeq(gLocalStore, gRDF.GetResource("nc:urlbar-history")).GetElements();
  130.       var i= MAX_HISTORY_MENU_ITEMS;
  131.  
  132.       // Delete any old menu items only if there are legitimate
  133.       // urls to display, otherwise we want to display the
  134.       // '(Nothing Available)' item.
  135.       deleteHistoryItems(aParent);
  136.       if (!entries.hasMoreElements()) {
  137.         //Create the "Nothing Available" Menu item and disable it.
  138.         var na = gNavigatorBundle.getString("nothingAvailable");
  139.         createMenuItem(aParent, "nothing_available", na);
  140.         aParent.firstChild.setAttribute("disabled", "true");
  141.       }
  142.  
  143.       while (entries.hasMoreElements() && (i-- > 0)) {
  144.         var entry = entries.getNext();
  145.         if (entry) {
  146.           try {
  147.             entry = entry.QueryInterface(Components.interfaces.nsIRDFLiteral);
  148.           } catch(ex) {
  149.             // XXXbar not an nsIRDFLiteral for some reason. see 90337.
  150.             continue;
  151.           }
  152.           var url = entry.Value;
  153.           createMenuItem(aParent, i, url);
  154.         }
  155.       }
  156.     }
  157.   }
  158.  
  159. function addToUrlbarHistory()
  160. {
  161.   var urlToAdd = gURLBar.value;
  162.   if (!urlToAdd)
  163.      return;
  164.   if (urlToAdd.search(/[\x00-\x1F]/) != -1) // don't store bad URLs
  165.      return;
  166.  
  167.   if (!gRDF)
  168.      gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  169.                       .getService(Components.interfaces.nsIRDFService);
  170.  
  171.   if (!gGlobalHistory)
  172.     gGlobalHistory = Components.classes["@mozilla.org/browser/global-history;1"]
  173.                                .getService(Components.interfaces.nsIBrowserHistory);
  174.  
  175.   if (!gURIFixup)
  176.     gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
  177.                           .getService(Components.interfaces.nsIURIFixup);
  178.   if (!gLocalStore)
  179.      gLocalStore = gRDF.GetDataSource("rdf:local-store");
  180.  
  181.   if (gLocalStore) {
  182.      if (!gRDFC)
  183.         gRDFC = Components.classes["@mozilla.org/rdf/container-utils;1"]
  184.                           .getService(Components.interfaces.nsIRDFContainerUtils);
  185.  
  186.        var entries = gRDFC.MakeSeq(gLocalStore, gRDF.GetResource("nc:urlbar-history"));
  187.        if (!entries)
  188.           return;
  189.        var elements = entries.GetElements();
  190.        if (!elements)
  191.           return;
  192.        var index = 0;
  193.        // create the nsIURI objects for comparing the 2 urls
  194.        var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  195.                      .getService(Components.interfaces.nsIIOService);
  196.        
  197.        var entryToAdd = gRDF.GetLiteral(urlToAdd);
  198.  
  199.        try {
  200.          ioService.extractScheme(urlToAdd, {}, {});
  201.        } catch(e) {
  202.          urlToAdd = "http://" + urlToAdd;
  203.        }
  204.        
  205.        try {
  206.          var uriToAdd  = ioService.newURI(urlToAdd, null, null);
  207.        }
  208.        catch(e) {
  209.          // it isn't a valid url
  210.          // we'll leave uriToAdd as "undefined" and handle that later
  211.        }
  212.  
  213.        while(elements.hasMoreElements()) {
  214.           var entry = elements.getNext();
  215.           if (!entry) continue;
  216.  
  217.           index ++;
  218.           try {
  219.             entry = entry.QueryInterface(Components.interfaces.nsIRDFLiteral);
  220.           } catch(ex) {
  221.             // XXXbar not an nsIRDFLiteral for some reason. see 90337.
  222.             continue;
  223.           }
  224.           var rdfValue = entry.Value;
  225.  
  226.           try {
  227.             ioService.extractScheme(rdfValue, {}, {});
  228.           } catch(e) {
  229.             rdfValue = "http://" + rdfValue;
  230.           }
  231.  
  232.           if (uriToAdd) {
  233.             try {
  234.               var rdfUri = ioService.newURI(rdfValue, null, null);
  235.                  
  236.               if (rdfUri.equals(uriToAdd)) {
  237.                 // URI already present in the database
  238.                 // Remove it from its current position.
  239.                 // It is inserted to the top after the while loop.
  240.                 entries.RemoveElementAt(index, true);
  241.                 break;
  242.               }
  243.             }
  244.                
  245.             // the uri is still not recognized by the ioservice
  246.             catch(ex) {
  247.               // no problem, we'll handle this below
  248.             }
  249.           }
  250.  
  251.           // if we got this far, then something is funky with the URIs,
  252.           // so we need to do a straight string compare of the raw strings
  253.           if (urlToAdd == rdfValue) {
  254.             entries.RemoveElementAt(index, true);
  255.             break;
  256.           }
  257.        }   // while
  258.  
  259.        // Otherwise, we've got a new URL in town. Add it!
  260.  
  261.        try {
  262.          var url = entryToAdd.Value;
  263.          if (url.indexOf(" ") == -1) {
  264.            var fixedUpURI = gURIFixup.createFixupURI(url, 0);
  265.            gGlobalHistory.markPageAsTyped(fixedUpURI.spec);
  266.          }
  267.        }
  268.        catch(ex) {
  269.        }
  270.  
  271.        // Put the value as it was typed by the user in to RDF
  272.        // Insert it to the beginning of the list.
  273.        entries.InsertElementAt(entryToAdd, 1, true);
  274.  
  275.        // Remove any expired history items so that we don't let
  276.        // this grow without bound.
  277.        for (index = entries.GetCount(); index > MAX_HISTORY_ITEMS; --index) {
  278.            entries.RemoveElementAt(index, true);
  279.        }  // for
  280.    }  // localstore
  281. }
  282.  
  283. function createMenuItem( aParent, aIndex, aLabel)
  284.   {
  285.     var menuitem = document.createElement( "menuitem" );
  286.     menuitem.setAttribute( "label", aLabel );
  287.     menuitem.setAttribute( "index", aIndex );
  288.     aParent.appendChild( menuitem );
  289.   }
  290.  
  291. function createRadioMenuItem( aParent, aIndex, aLabel, aChecked)
  292.   {
  293.     var menuitem = document.createElement( "menuitem" );
  294.     menuitem.setAttribute( "type", "radio" );
  295.     menuitem.setAttribute( "label", aLabel );
  296.     menuitem.setAttribute( "index", aIndex );
  297.     if (aChecked==true)
  298.       menuitem.setAttribute( "checked", "true" );
  299.     aParent.appendChild( menuitem );
  300.   }
  301.  
  302. function deleteHistoryItems(aParent)
  303.   {
  304.     var children = aParent.childNodes;
  305.     for (var i = 0; i < children.length; i++ )
  306.       {
  307.         var index = children[i].getAttribute( "index" );
  308.         if (index)
  309.           aParent.removeChild( children[i] );
  310.       }
  311.   }
  312.  
  313. function updateGoMenu(event)
  314.   {
  315.     FillHistoryMenu(event.target, "go");
  316.   }
  317.  
  318.