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 / help.jar / content / help / help.js < prev    next >
Text File  |  2003-06-07  |  23KB  |  712 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * Contributor(s): 
  20.  *    Original author: oeschger@netscape.com 
  21.  *    amended by: Peter Wilson (added sidebar tabs) */
  22.  
  23. //-------- global variables
  24. var helpBrowser;
  25. var helpWindow;
  26. var helpSearchPanel;
  27. var emptySearch;
  28. var emptySearchText
  29. var emptySearchLink
  30. var helpTocPanel;
  31. var helpIndexPanel;
  32. var helpGlossaryPanel;
  33.  
  34. // Namespaces
  35. const NC = "http://home.netscape.com/NC-rdf#";
  36. const SN = "rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#";
  37. const XML = "http://www.w3.org/XML/1998/namespace#"
  38. const MAX_LEVEL = 40; // maximum depth of recursion in search datasources.
  39.  
  40. // Resources
  41. var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  42. var RDF_ROOT = RDF.GetResource("urn:root");
  43. var NC_PANELLIST = RDF.GetResource(NC + "panellist");
  44. var NC_PANELID = RDF.GetResource(NC + "panelid");
  45. var NC_EMPTY_SEARCH_TEXT = RDF.GetResource(NC + "emptysearchtext");
  46. var NC_EMPTY_SEARCH_LINK = RDF.GetResource(NC + "emptysearchlink");
  47. var NC_DATASOURCES = RDF.GetResource(NC + "datasources");
  48. var NC_SUBHEADINGS = RDF.GetResource(NC + "subheadings");
  49. var NC_NAME = RDF.GetResource(NC + "name");
  50. var NC_CHILD = RDF.GetResource(NC + "child");
  51. var NC_LINK = RDF.GetResource(NC + "link");
  52. var NC_TITLE = RDF.GetResource(NC + "title");
  53. var NC_BASE = RDF.GetResource(NC + "base"); 
  54. var NC_DEFAULTTOPIC = RDF.GetResource(NC + "defaulttopic"); 
  55.  
  56. var RDFCUtils = Components.classes["@mozilla.org/rdf/container-utils;1"].getService().
  57.    QueryInterface(Components.interfaces.nsIRDFContainerUtils);
  58. var RDFContainer = Components.classes["@mozilla.org/rdf/container;1"].getService(Components.interfaces.nsIRDFContainer);
  59. var CONSOLE_SERVICE = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
  60.             
  61. var urnID = 0;
  62. var RE;
  63.  
  64. var helpFileURI;
  65. var helpFileDS;
  66. // Set from nc:base attribute on help rdf file. It may be used for prefix reduction on all links within
  67. // the current help set.
  68. var helpBaseURI;
  69.  
  70. const defaultHelpFile = "chrome://help/locale/mozillahelp.rdf";
  71. // Set from nc:defaulttopic. It is used when the requested uri has no topic specified. 
  72. var defaultTopic = "welcome"; 
  73. var searchDatasources = "rdf:null";
  74. var searchDS = null;
  75.  
  76. const NSRESULT_RDF_SYNTAX_ERROR = 0x804e03f7; 
  77.  
  78. // This function is called by dialogs/windows that want to display context-sensitive help
  79. // These dialogs/windows should include the script chrome://help/content/contextHelp.js
  80. function displayTopic(topic) {
  81.   if (!topic)
  82.     topic = defaultTopic;
  83.   var uri = getLink(topic);
  84.   if (!uri) // Topic not found - revert to default.
  85.       uri = getLink(defaultTopic); 
  86.   loadURI(uri);
  87. }
  88.  
  89. // Initialize the Help window
  90. function init() {
  91.   //cache panel references.
  92.   helpWindow = document.getElementById("help");
  93.   helpSearchPanel = document.getElementById("help-search-panel");
  94.   helpTocPanel = document.getElementById("help-toc-tree");
  95.   helpIndexPanel = document.getElementById("help-index-tree");
  96.   helpGlossaryPanel = document.getElementById("help-glossary-tree");
  97.   helpBrowser = document.getElementById("help-content");
  98.  
  99.   var URI = normalizeURI(decodeURIComponent(window.location.search));
  100.   helpFileURI = URI.helpFileURI;
  101.   var helpTopic = URI.topic;
  102.   helpBaseURI = helpFileURI.substring(0, helpFileURI.lastIndexOf("/")+1); // trailing "/" included.
  103.  
  104.   loadHelpRDF();
  105.  
  106.   displayTopic(helpTopic);  
  107.  
  108.   // move to right end of screen
  109.   var width = document.documentElement.getAttribute("width");
  110.   var height = document.documentElement.getAttribute("height");
  111.   window.moveTo(screen.availWidth-width, (screen.availHeight-height)/2);
  112.  
  113.   var sessionHistory =  Components.classes["@mozilla.org/browser/shistory;1"]
  114.                   .createInstance(Components.interfaces.nsISHistory);
  115.  
  116.   getWebNavigation().sessionHistory = sessionHistory;
  117.   window.XULBrowserWindow = new nsHelpStatusHandler();
  118.   // hook up UI through progress listener
  119.   var interfaceRequestor = helpBrowser.docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  120.   var webProgress = interfaceRequestor.getInterface(Components.interfaces.nsIWebProgress);
  121.   webProgress.addProgressListener(window.XULBrowserWindow, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
  122. }
  123.  
  124. function normalizeURI(uri) {
  125.   // uri in format [uri of help rdf file][?initial topic]
  126.   // if the whole uri or help file uri is omitted then the default help is assumed.
  127.   // unpack uri
  128.   var URI = {};
  129.   URI.helpFileURI = defaultHelpFile;
  130.   URI.topic = null;
  131.   // Case: No help uri at all.
  132.   if (uri) {
  133.     // remove leading ?
  134.     if (uri.substr(0,1) == "?") 
  135.       uri = uri.substr(1);
  136.     var i = uri.indexOf("?");
  137.     // Case: Full uri with topic.
  138.     if ( i != -1) {
  139.         URI.helpFileURI = uri.substr(0,i);
  140.         URI.topic = uri.substr(i+1);
  141.     }
  142.     else {
  143.       // Case: uri with no topic.
  144.       if (uri.substr(0,7) == "chrome:") 
  145.         URI.helpFileURI = uri;
  146.       else {
  147.         // Case: uri with topic only.
  148.         URI.topic = uri;
  149.       }  
  150.     }
  151.   }  
  152.   URI.uri = URI.helpFileURI + ((URI.topic)? "?" + URI.topic : "");
  153.   return URI;
  154. }
  155.  
  156. function loadHelpRDF() {
  157.   if (!helpFileDS) {
  158.     try {
  159.       helpFileDS = RDF.GetDataSourceBlocking(helpFileURI);
  160.     }
  161.     catch (e if (e.result == NSRESULT_RDF_SYNTAX_ERROR)) {
  162.       log("Help file: " + helpFileURI + " contains a syntax error.");
  163.     }
  164.     catch (e) {
  165.       log("Help file: " + helpFileURI + " was not found.");
  166.     }
  167.     try {
  168.       helpWindow.setAttribute("title", getAttribute(helpFileDS, RDF_ROOT, NC_TITLE, ""));
  169.       helpBaseURI = getAttribute(helpFileDS, RDF_ROOT, NC_BASE, helpBaseURI);
  170.       defaultTopic = getAttribute(helpFileDS, RDF_ROOT, NC_DEFAULTTOPIC, "welcome");
  171.  
  172.       var panelDefs = helpFileDS.GetTarget(RDF_ROOT, NC_PANELLIST, true);      
  173.       RDFContainer.Init(helpFileDS, panelDefs);
  174.       var iterator = RDFContainer.GetElements();
  175.         while (iterator.hasMoreElements()) {
  176.         var panelDef = iterator.getNext();
  177.         var panelID = getAttribute(helpFileDS, panelDef, NC_PANELID, null);        
  178.  
  179.         var datasources = getAttribute(helpFileDS, panelDef, NC_DATASOURCES, "rdf:none");
  180.         datasources = normalizeLinks(helpBaseURI, datasources);
  181.         // cache additional datsources to augment search datasources.
  182.         if (panelID == "search") {
  183.            emptySearchText = getAttribute(helpFileDS, panelDef, NC_EMPTY_SEARCH_TEXT, null) || "No search items found." ;        
  184.            emptySearchLink = getAttribute(helpFileDS, panelDef, NC_EMPTY_SEARCH_LINK, null) || "about:blank";        
  185.           searchDatasources = datasources;
  186.           datasources = "rdf:null"; // but don't try to display them yet!
  187.         }  
  188.  
  189.         // cache toc datasources for use by ID lookup.
  190.         var tree = document.getElementById("help-" + panelID + "-tree");
  191.         tree.setAttribute("datasources", datasources);
  192.         //if (panelID == "toc") {
  193.           if (tree.database) {
  194.             loadDatabases(tree.database, datasources);
  195.             tree.builder.rebuild();
  196.           }
  197.         //}
  198.       }  
  199.     }
  200.     catch (e) {
  201.       log(e + "");      
  202.     }
  203.   }
  204. }
  205. function loadDatabases(compositeDatabase, datasources) {
  206.   var ds = datasources.split(/\s+/);
  207.   for (var i=0; i < ds.length; ++i) {
  208.     if (ds[i] == "rdf:null" || ds[i] == "")
  209.       continue;
  210.     try {  
  211.       // we need blocking here to ensure the database is loaded so getLink(topic) works.
  212.       var datasource = RDF.GetDataSourceBlocking(ds[i]);
  213.       if (datasource)  
  214.         compositeDatabase.AddDataSource(datasource);
  215.     }
  216.     catch (e) {
  217.       log("Datasource: " + ds[i] + " was not found.");
  218.     }
  219.   }
  220. }
  221.  
  222. // prepend helpBaseURI to list of space separated links if the don't start with "chrome:"
  223. function normalizeLinks(helpBaseURI, links) {
  224.   if (!helpBaseURI)
  225.     return links;
  226.   var ls = links.split(/\s+/);
  227.   if (ls.length == 0)
  228.     return links;
  229.   for (var i=0; i < ls.length; ++i) {
  230.     if (ls[i] == "")
  231.       continue;
  232.     if (ls[i].substr(0,7) != "chrome:" && ls[i].substr(0,4) != "rdf:") 
  233.       ls[i] = helpBaseURI + ls[i];
  234.   }
  235.   return ls.join(" ");  
  236. }
  237.  
  238. function getLink(ID) {
  239.   if (!ID)
  240.     return null;
  241.   // Note resources are stored in fileURL#ID format.
  242.   // We have one possible source for an ID for each datasource in the composite datasource.
  243.   // The first ID which matches is returned.
  244.   var tocTree = document.getElementById("help-toc-tree");
  245.     tocDS = tocTree.database;
  246.     if (tocDS == null)
  247.       return null;
  248.     var tocDatasources = tocTree.getAttribute("datasources");
  249.   var ds = tocDatasources.split(/\s+/);
  250.   for (var i=0; i < ds.length; ++i) {
  251.     if (ds[i] == "rdf:null" || ds[i] == "")
  252.       continue;
  253.     try {
  254.       var rdfID = ds[i] + "#" + ID;
  255.       var resource = RDF.GetResource(rdfID);
  256.       if (resource) {
  257.         var link = tocDS.GetTarget(resource, NC_LINK, true);
  258.         if (link) {
  259.           link = link.QueryInterface(Components.interfaces.nsIRDFLiteral);
  260.           if (link) 
  261.             return link.Value;
  262.           else  
  263.             return null;
  264.         }  
  265.       }
  266.     }
  267.     catch (e) { log(rdfID + " " + e);}
  268.   }
  269.   return null;
  270. }
  271.  
  272. // Called by contextHelp.js to determine if this window is displaying the requested help file.
  273. function getHelpFileURI() {
  274.   return helpFileURI;
  275. }
  276.  
  277.  
  278. function getWebNavigation()
  279. {
  280.   return helpBrowser.webNavigation;
  281. }
  282.  
  283. function loadURI(uri)
  284. {
  285.   if (uri.substr(0,7) != "chrome:")
  286.     uri = helpBaseURI + uri;
  287.   const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
  288.   getWebNavigation().loadURI(uri, nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
  289. }
  290.  
  291. function goBack()
  292. {
  293.   var webNavigation = getWebNavigation();
  294.   if (webNavigation.canGoBack)
  295.     webNavigation.goBack();
  296. }
  297.  
  298. function goForward()
  299. {
  300.   var webNavigation = getWebNavigation();
  301.   if (webNavigation.canGoForward)
  302.     webNavigation.goForward();
  303. }
  304.  
  305. function goHome() {
  306.   // load "Welcome" page
  307.   displayTopic(defaultTopic);
  308. }
  309.  
  310. function print()
  311. {
  312.   try {
  313.     _content.print();
  314.   } catch (e) {
  315.   }
  316. }
  317.  
  318. function createBackMenu(event)
  319. {
  320.   return FillHistoryMenu(event.target, "back");
  321. }
  322.  
  323. function createForwardMenu(event)
  324. {
  325.   return FillHistoryMenu(event.target, "forward");
  326. }
  327.  
  328. function gotoHistoryIndex(aEvent)
  329. {
  330.   var index = aEvent.target.getAttribute("index");
  331.   if (!index)
  332.     return false;
  333.   try {
  334.     getWebNavigation().gotoIndex(index);
  335.   }
  336.   catch(ex) {
  337.     return false;
  338.   }
  339.   return true;
  340. }
  341.  
  342. function BrowserBack()
  343. {
  344.   try {
  345.     getWebNavigation().goBack();
  346.   }
  347.   catch(ex) {
  348.   }
  349.   UpdateBackForwardButtons();
  350. }
  351.  
  352. function BrowserForward()
  353. {
  354.   try {
  355.     getWebNavigation().goForward();
  356.   }
  357.   catch(ex) {
  358.   }
  359.   UpdateBackForwardButtons();
  360. }
  361.  
  362. function nsHelpStatusHandler()
  363. {
  364. }
  365.  
  366. nsHelpStatusHandler.prototype =
  367. {
  368.   onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {},
  369.   onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress,
  370.                               aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {},
  371.   onSecurityChange : function(aWebProgress, aRequest, state) {},
  372.   onLocationChange : function(aWebProgress, aRequest, aLocation)
  373.   {
  374.     UpdateBackForwardButtons();
  375.   },
  376.   QueryInterface : function(aIID)
  377.   {
  378.     if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  379.       aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  380.       aIID.equals(Components.interfaces.nsIXULBrowserWindow) ||
  381.       aIID.equals(Components.interfaces.nsISupports))
  382.       return this;
  383.     throw Components.results.NS_NOINTERFACE;
  384.   },
  385.   setJSStatus : function(status) {},
  386.   setJSDefaultStatus : function(status) {},
  387.   setOverLink : function(link) {}
  388. }
  389.  
  390. function UpdateBackForwardButtons()
  391. {
  392.   var backBroadcaster = document.getElementById("canGoBack");
  393.   var forwardBroadcaster = document.getElementById("canGoForward");
  394.   var webNavigation = getWebNavigation();
  395.  
  396.   // Avoid setting attributes on broadcasters if the value hasn't changed!
  397.   // Remember, guys, setting attributes on elements is expensive!  They
  398.   // get inherited into anonymous content, broadcast to other widgets, etc.!
  399.   // Don't do it if the value hasn't changed! - dwh
  400.  
  401.   var backDisabled = (backBroadcaster.getAttribute("disabled") == "true");
  402.   var forwardDisabled = (forwardBroadcaster.getAttribute("disabled") == "true");
  403.  
  404.   if (backDisabled == webNavigation.canGoBack)
  405.     backBroadcaster.setAttribute("disabled", !backDisabled);
  406.   
  407.   if (forwardDisabled == webNavigation.canGoForward)
  408.     forwardBroadcaster.setAttribute("disabled", !forwardDisabled);
  409. }
  410.  
  411. function find(again, reverse)
  412. {
  413.   var focusedWindow = document.commandDispatcher.focusedWindow;
  414.   if (!focusedWindow || focusedWindow == window)
  415.     focusedWindow = window._content;
  416.   if (again)
  417.     findAgainInPage(helpBrowser, window._content, focusedWindow, reverse);
  418.   else
  419.     findInPage(helpBrowser, window._content, focusedWindow)
  420. }
  421.  
  422. function getMarkupDocumentViewer()
  423. {
  424.   return helpBrowser.markupDocumentViewer;
  425. }
  426.  
  427. function BrowserReload()
  428. {
  429.   const reloadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
  430.   return BrowserReloadWithFlags(reloadFlags);
  431. }
  432.  
  433. function BrowserReloadWithFlags(reloadFlags)
  434. {
  435.    try {
  436.      /* Need to get SessionHistory from docshell so that
  437.       * reload on framed pages will work right. This 
  438.       * method should not be used for the context menu item "Reload frame".
  439.       * "Reload frame" should directly call into docshell as it does right now
  440.       */
  441.      var sh = getWebNavigation().sessionHistory;
  442.      var webNav = sh.QueryInterface(Components.interfaces.nsIWebNavigation);      
  443.      webNav.reload(reloadFlags);
  444.    }
  445.    catch(ex) {
  446.    }
  447.  }
  448.  
  449.  // doc=null for regular page info, doc=owner document for frame info 
  450.  function BrowserPageInfo(doc)
  451.  {
  452.    window.openDialog("chrome://navigator/content/pageInfo.xul",
  453.                      "_blank",
  454.                      "chrome,dialog=no",
  455.                      doc);
  456. }
  457.  
  458. function BrowserViewSource()
  459. {
  460.   var focusedWindow = document.commandDispatcher.focusedWindow;
  461.   if (focusedWindow == window)
  462.     focusedWindow = _content;
  463.  
  464.   if (focusedWindow)
  465.     var docCharset = "charset=" + focusedWindow.document.characterSet;
  466.  
  467.   BrowserViewSourceOfURL(_content.location, docCharset);
  468. }
  469.  
  470. function BrowserViewSourceOfURL(url, charset)
  471. {
  472.   // try to open a view-source window while inheriting the charset (if any)
  473.   openDialog("chrome://navigator/content/viewSource.xul",
  474.              "_blank",
  475.              "scrollbars,resizable,chrome,dialog=no",
  476.              url, charset);
  477. }
  478.  
  479. //Show the selected sidebar panel
  480. function showPanel(panelId) {
  481.   helpSearchPanel.setAttribute("hidden", "true");
  482.   helpTocPanel.setAttribute("hidden", "true");
  483.   helpIndexPanel.setAttribute("hidden", "true");
  484.   helpGlossaryPanel.setAttribute("hidden", "true");
  485.   var thePanel = document.getElementById(panelId);
  486.   thePanel.setAttribute("hidden","false");
  487. }
  488.  
  489. function onselect_loadURI(tree, columnName) {
  490.   try {
  491.     var row = tree.treeBoxObject.view.selection.currentIndex;
  492.     var properties = Components.classes["@mozilla.org/supports-array;1"].createInstance(Components.interfaces.nsISupportsArray);
  493.     tree.treeBoxObject.view.getCellProperties(row, columnName, properties);
  494.     if (!properties) return;
  495.     var uri = getPropertyValue(properties, "link-");
  496.     if (uri)
  497.       loadURI(uri);
  498.   }
  499.   catch (e) {}// when switching between tabs a spurious row number is returned.
  500. }
  501.  
  502. /** Search properties nsISupportsArray for an nsIAtom which starts with the given property name. **/
  503. function getPropertyValue(properties, propName) {
  504.   for (var i=0; i< properties.Count(); ++i) {
  505.     var atom = properties.GetElementAt(i).QueryInterface(Components.interfaces.nsIAtom);
  506.     var atomValue = atom.GetUnicode();
  507.     if (atomValue.substr(0, propName.length) == propName)
  508.       return atomValue.substr(propName.length);
  509.   }
  510.   return null;
  511. }
  512.  
  513. function doFind() {
  514.   var searchTree = document.getElementById("help-search-tree");
  515.   var findText = document.getElementById("findText");
  516.  
  517.   // clear any previous results.
  518.   clearDatabases(searchTree.database);
  519.  
  520.   // split search string into separate terms and compile into regexp's
  521.   RE = findText.value.split(/\s+/);
  522.   for (var i=0; i < RE.length; ++i) {
  523.     if (RE[i] == "")
  524.       continue;
  525.     if (RE[i].length > 3) {
  526.         RE[i] = new RegExp(RE[i].substring(0, RE[i].length-1) +"\w?", "i");
  527.     } else {
  528.         RE[i] = new RegExp(RE[i], "i");
  529.     }
  530.  
  531.   }
  532.  emptySearch = true;     
  533.   // search TOC
  534.   var resultsDS =  Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance(Components.interfaces.nsIRDFDataSource);
  535.   var tree = document.getElementById("help-toc-tree");
  536.   var sourceDS = tree.database;
  537.   doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
  538.  
  539.   // search additional search datasources                                       
  540.   if (searchDatasources != "rdf:null") {
  541.     if (!searchDS)
  542.       searchDS = loadCompositeDS(searchDatasources);
  543.     doFindOnDatasource(resultsDS, searchDS, RDF_ROOT, 0);
  544.   }
  545.  
  546.   // search glossary.
  547.   tree = document.getElementById("help-glossary-tree");
  548.   sourceDS = tree.database;
  549.   if (!sourceDS) // If the glossary has never been displayed this will be null (sigh!).
  550.     sourceDS = loadCompositeDS(tree.datasources);
  551.   doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
  552.   
  553.   if (emptySearch)
  554.         assertSearchEmpty(resultsDS);
  555.   // Add the datasource to the search tree
  556.   searchTree.database.AddDataSource(resultsDS);
  557.   searchTree.builder.rebuild();
  558. }
  559.  
  560. function doEnabling() {
  561.   var findButton = document.getElementById("findButton");
  562.   var findTextbox = document.getElementById("findText");
  563.   findButton.disabled = !findTextbox.value;
  564. }
  565.  
  566. function clearDatabases(compositeDataSource) {
  567.   var enumDS = compositeDataSource.GetDataSources()
  568.   while (enumDS.hasMoreElements()) {
  569.     var ds = enumDS.getNext();
  570.         compositeDataSource.RemoveDataSource(ds);
  571.   }
  572. }
  573.  
  574. function doFindOnDatasource(resultsDS, sourceDS, resource, level) {
  575.   if (level > MAX_LEVEL) {
  576.     try {
  577.       log("Recursive reference to resource: " + resource.Value + ".");
  578.     }
  579.     catch (e) {
  580.       log("Recursive reference to unknown resource.");
  581.     }
  582.     return;
  583.   }
  584.   // find all SUBHEADING children of current resource.
  585.   var targets = sourceDS.GetTargets(resource, NC_SUBHEADINGS, true);
  586.   while (targets.hasMoreElements()) {
  587.       var target = targets.getNext();
  588.       target = target.QueryInterface(Components.interfaces.nsIRDFResource);
  589.         // The first child of a rdf:subheading should (must) be a rdf:seq.
  590.         // Should we test for a SEQ here?
  591.     doFindOnSeq(resultsDS, sourceDS, target, level+1);       
  592.   }  
  593. }
  594.  
  595. function doFindOnSeq(resultsDS, sourceDS, resource, level) {
  596.   // load up an RDFContainer so we can access the contents of the current rdf:seq.    
  597.     RDFContainer.Init(sourceDS, resource);
  598.     var targets = RDFContainer.GetElements();
  599.     while (targets.hasMoreElements()) {
  600.     var target = targets.getNext();
  601.         target = target.QueryInterface(Components.interfaces.nsIRDFResource);
  602.         var name = sourceDS.GetTarget(target, NC_NAME, true);
  603.         name = name.QueryInterface(Components.interfaces.nsIRDFLiteral);
  604.         
  605.         if (isMatch(name.Value)) {
  606.           // we have found a search entry - add it to the results datasource.
  607.           
  608.           // Get URL of html for this entry.
  609.       var link = sourceDS.GetTarget(target, NC_LINK, true);
  610.       link = link.QueryInterface(Components.interfaces.nsIRDFLiteral);        
  611.  
  612.       urnID++;
  613.       resultsDS.Assert(RDF_ROOT,
  614.              RDF.GetResource("http://home.netscape.com/NC-rdf#child"),
  615.              RDF.GetResource("urn:" + urnID),
  616.              true);
  617.       resultsDS.Assert(RDF.GetResource("urn:" + urnID),
  618.              RDF.GetResource("http://home.netscape.com/NC-rdf#name"),
  619.              name,
  620.              true);
  621.       resultsDS.Assert(RDF.GetResource("urn:" + urnID),
  622.              RDF.GetResource("http://home.netscape.com/NC-rdf#link"),
  623.              link,
  624.              true);
  625.           emptySearch = false;     
  626.              
  627.     }
  628.     // process any nested rdf:seq elements.
  629.     doFindOnDatasource(resultsDS, sourceDS, target, level+1);       
  630.     }  
  631. }
  632. function assertSearchEmpty(resultsDS) {
  633.     var resSearchEmpty = RDF.GetResource("urn:emptySearch");
  634.     resultsDS.Assert(RDF_ROOT,
  635.              NC_CHILD,
  636.              resSearchEmpty,
  637.              true);
  638.     resultsDS.Assert(resSearchEmpty,
  639.              NC_NAME,
  640.              RDF.GetLiteral(emptySearchText),
  641.              true);
  642.     resultsDS.Assert(resSearchEmpty,
  643.              NC_LINK,
  644.              RDF.GetLiteral(emptySearchLink),
  645.              true);
  646. }
  647.  
  648. function isMatch(text) {
  649.   for (var i=0; i < RE.length; ++i ) {
  650.     if (!RE[i].test(text))
  651.       return false;
  652.   }
  653.   return true;
  654. }
  655. function loadCompositeDS(datasources) {
  656.   // We can't search on each individual datasource's - only the aggregate (for each sidebar tab)
  657.   // has the appropriate structure.
  658.   var compositeDS =  Components.classes["@mozilla.org/rdf/datasource;1?name=composite-datasource"]
  659.       .createInstance(Components.interfaces.nsIRDFCompositeDataSource);
  660.   
  661.   var ds = datasources.split(/\s+/);
  662.   for (var i=0; i < ds.length; ++i) {
  663.     if (ds[i] == "rdf:null" || ds[i] == "")
  664.       continue;
  665.     try {  
  666.       // we need blocking here to ensure the database is loaded.
  667.       var sourceDS = RDF.GetDataSourceBlocking(ds[i]);
  668.       compositeDS.AddDataSource(sourceDS);
  669.     }
  670.     catch (e) {
  671.       log("Datasource: " + ds[i] + " was not found.");
  672.     }
  673.   }
  674.   return compositeDS;
  675. }
  676.  
  677. function getAttribute(datasource, resource, attributeResourceName, defaultValue) {
  678.   var literal = datasource.GetTarget(resource, attributeResourceName, true);
  679.   if (!literal)
  680.     return defaultValue;
  681.   return getLiteralValue(literal, defaultValue);  
  682. }
  683.  
  684. function getLiteralValue(literal, defaultValue) {
  685.   if (literal) {
  686.       literal = literal.QueryInterface(Components.interfaces.nsIRDFLiteral);
  687.       if (literal)
  688.         return literal.Value;
  689.   }
  690.   if (defaultValue)
  691.     return defaultValue;
  692.   return null;
  693. }
  694. // Write debug string to javascript console.
  695. function log(aText) {
  696.   CONSOLE_SERVICE.logStringMessage(aText);
  697. }
  698.  
  699.  
  700. //INDEX OPENING FUNCTION -- called in oncommand for index pane
  701. // iterate over all the items in the outliner;
  702. // open the ones at the top-level (i.e., expose the headings underneath
  703. // the letters in the list.
  704. function displayIndex() {
  705.   var treeview = helpIndexPanel.view;
  706.   var i = treeview.rowCount;
  707.   while (i--)
  708.     if (!treeview.getLevel(i) && !treeview.isContainerOpen(i))
  709.       treeview.toggleOpenState(i);
  710. }
  711.  
  712.