home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / help / help.js < prev    next >
Encoding:
JavaScript  |  2005-10-11  |  25.2 KB  |  798 lines

  1. //@line 40 "/c/mozilla/toolkit/components/help/content/help.js"
  2.  
  3. //@line 42 "/c/mozilla/toolkit/components/help/content/help.js"
  4. var helpBrowser;
  5. var helpSearchPanel;
  6. var emptySearch;
  7. var emptySearchText;
  8. var emptySearchLink = "about:blank";
  9. var helpTocPanel;
  10. var helpIndexPanel;
  11. var helpGlossaryPanel;
  12. var strBundle;
  13. var gTocDSList = "";
  14.  
  15. //@line 54 "/c/mozilla/toolkit/components/help/content/help.js"
  16. const NC = "http://home.netscape.com/NC-rdf#";
  17. const MAX_LEVEL = 40; // maximum depth of recursion in search datasources.
  18. const MAX_HISTORY_MENU_ITEMS = 6;
  19.  
  20. //@line 60 "/c/mozilla/toolkit/components/help/content/help.js"
  21. const platform = "win";
  22. //@line 72 "/c/mozilla/toolkit/components/help/content/help.js"
  23.  
  24. //@line 74 "/c/mozilla/toolkit/components/help/content/help.js"
  25. const RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  26.     .getService(Components.interfaces.nsIRDFService);
  27. const RDF_ROOT = RDF.GetResource("urn:root");
  28. const NC_PANELLIST = RDF.GetResource(NC + "panellist");
  29. const NC_PANELID = RDF.GetResource(NC + "panelid");
  30. const NC_EMPTY_SEARCH_TEXT = RDF.GetResource(NC + "emptysearchtext");
  31. const NC_EMPTY_SEARCH_LINK = RDF.GetResource(NC + "emptysearchlink");
  32. const NC_DATASOURCES = RDF.GetResource(NC + "datasources");
  33. const NC_PLATFORM = RDF.GetResource(NC + "platform");
  34. const NC_SUBHEADINGS = RDF.GetResource(NC + "subheadings");
  35. const NC_NAME = RDF.GetResource(NC + "name");
  36. const NC_CHILD = RDF.GetResource(NC + "child");
  37. const NC_LINK = RDF.GetResource(NC + "link");
  38. const NC_TITLE = RDF.GetResource(NC + "title");
  39. const NC_BASE = RDF.GetResource(NC + "base");
  40. const NC_DEFAULTTOPIC = RDF.GetResource(NC + "defaulttopic");
  41.  
  42. const RDFContainer = Components.classes["@mozilla.org/rdf/container;1"]
  43.     .getService(Components.interfaces.nsIRDFContainer);
  44. const CONSOLE_SERVICE = Components.classes['@mozilla.org/consoleservice;1']
  45.     .getService(Components.interfaces.nsIConsoleService);
  46.  
  47. var RE;
  48.  
  49. var helpFileURI;
  50. var helpFileDS;
  51. //@line 102 "/c/mozilla/toolkit/components/help/content/help.js"
  52. var helpBaseURI;
  53.  
  54. var gIgnoreFocus = false;
  55. var gClickSelectsAll;
  56. var gIgnoreClick = false;
  57.  
  58. /* defaultTopic is either set
  59.    1. in the openHelp() call, passed as an argument to the Help window and
  60.       evaluated in init(), or
  61.    2. in nc:defaulttopic in the content pack (e.g. firebirdhelp.rdf),
  62.       evaluated in loadHelpRDF(), or
  63.    3. "welcome" as a fallback, specified in loadHelpRDF() as well;
  64.       displayTopic() then uses defaultTopic because topic is null. */
  65. var defaultTopic;
  66.  
  67. const NSRESULT_RDF_SYNTAX_ERROR = 0x804e03f7;
  68.  
  69. //@line 123 "/c/mozilla/toolkit/components/help/content/help.js"
  70. function displayTopic(topic) {
  71.     // Get the page to open.
  72.     var uri = getLink(topic);
  73.     // Use default topic if specified topic is not found.
  74.     if (!uri) {
  75.         uri = getLink(defaultTopic);
  76.     }
  77.     // Load the page.
  78.     if (uri)
  79.       loadURI(uri);
  80. }
  81.  
  82. //@line 136 "/c/mozilla/toolkit/components/help/content/help.js"
  83. function init() {
  84.   // Cache panel references.
  85.   helpSearchPanel = document.getElementById("help-search-panel");
  86.   helpTocPanel = document.getElementById("help-toc-panel");
  87.   helpIndexPanel = document.getElementById("help-index-panel");
  88.   helpGlossaryPanel = document.getElementById("help-glossary-panel");
  89.   helpBrowser = document.getElementById("help-content");
  90.  
  91.   strBundle = document.getElementById("bundle_help");
  92.   emptySearchText = strBundle.getString("emptySearchText");
  93.  
  94.   initFindBar();
  95.  
  96.   // Get the content pack, base URL, and help topic
  97.   var helpTopic = defaultTopic;
  98.   if ("arguments" in window && 
  99.        window.arguments[0] instanceof Components.interfaces.nsIDialogParamBlock) {
  100.     helpFileURI = window.arguments[0].GetString(0);
  101.     // trailing "/" included.
  102.     helpBaseURI = helpFileURI.substring(0, helpFileURI.lastIndexOf("/")+1);
  103.     helpTopic = window.arguments[0].GetString(1);
  104.   }
  105.  
  106.   loadHelpRDF();
  107.   displayTopic(helpTopic);
  108.  
  109.   // Move to Center of Screen
  110.   const width = document.documentElement.getAttribute("width");
  111.   const height = document.documentElement.getAttribute("height");
  112.   window.moveTo((screen.availWidth - width) / 2, (screen.availHeight - height) / 2);
  113.  
  114.   // Initialize history.
  115.   getWebNavigation().sessionHistory = 
  116.     Components.classes["@mozilla.org/browser/shistory;1"]
  117.               .createInstance(Components.interfaces.nsISHistory);
  118.   window.XULBrowserWindow = new nsHelpStatusHandler();
  119.  
  120.   //Start the status handler.
  121.   window.XULBrowserWindow.init();
  122.  
  123.   // Hook up UI through Progress Listener
  124.   const interfaceRequestor = helpBrowser.docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  125.   const webProgress = interfaceRequestor.getInterface(Components.interfaces.nsIWebProgress);
  126.  
  127.   webProgress.addProgressListener(window.XULBrowserWindow, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
  128.  
  129.   gClickSelectsAll = getBoolPref("browser.urlbar.clickSelectsAll", true);
  130.   
  131.   setTimeout(focusSearch, 0);
  132. }
  133.  
  134. function showSearchSidebar() {
  135.   // if you tab too quickly, you end up with stuck focus, revert focus to the searchbar
  136.   var searchTree = document.getElementById("help-toc-panel");
  137.   if (searchTree.treeBoxObject.focused) {
  138.     focusSearch();
  139.   }
  140.  
  141.   var tableOfContents = document.getElementById("help-toc-sidebar");
  142.   tableOfContents.setAttribute("hidden", "true");
  143.  
  144.   var sidebar = document.getElementById("help-search-sidebar");
  145.   sidebar.removeAttribute("hidden");
  146. }
  147.  
  148. function hideSearchSidebar(aEvent) {
  149.   // if we're focused in the search results, focus content
  150.   var searchTree = document.getElementById("help-search-tree");
  151.   if (searchTree.treeBoxObject.focused) {
  152.     content.focus();
  153.   }
  154.  
  155.   var sidebar = document.getElementById("help-search-sidebar");
  156.   sidebar.setAttribute("hidden", "true");
  157.  
  158.   var tableOfContents = document.getElementById("help-toc-sidebar");
  159.   tableOfContents.removeAttribute("hidden");
  160. }
  161.  
  162. //@line 219 "/c/mozilla/toolkit/components/help/content/help.js"
  163. function loadHelpRDF() {
  164.   if (!helpFileDS) {
  165.     try {
  166.       helpFileDS = RDF.GetDataSourceBlocking(helpFileURI);
  167.     } catch (e if (e.result == NSRESULT_RDF_SYNTAX_ERROR)) {
  168.       log("Help file: " + helpFileURI + " contains a syntax error.");
  169.     } catch (e) {
  170.       log("Help file: " + helpFileURI + " was not found.");
  171.     }
  172.  
  173.     try {
  174.       document.title = getAttribute(helpFileDS, RDF_ROOT, NC_TITLE, "");
  175.       helpBaseURI = getAttribute(helpFileDS, RDF_ROOT, NC_BASE, helpBaseURI);
  176.       // if there's no nc:defaulttopic in the content pack, set "welcome"
  177.       // as the default topic
  178.       defaultTopic = getAttribute(helpFileDS,
  179.         RDF_ROOT, NC_DEFAULTTOPIC, "welcome");
  180.  
  181.       var panelDefs = helpFileDS.GetTarget(RDF_ROOT, NC_PANELLIST, true);
  182.       RDFContainer.Init(helpFileDS, panelDefs);
  183.       var iterator = RDFContainer.GetElements();
  184.       while (iterator.hasMoreElements()) {
  185.         var panelDef = iterator.getNext();
  186.  
  187.         var panelID        = getAttribute(helpFileDS, panelDef, NC_PANELID, null);
  188.         var datasources    = getAttribute(helpFileDS, panelDef, NC_DATASOURCES, "");
  189.         var panelPlatforms = getAttribute(helpFileDS, panelDef, NC_PLATFORM, platform);
  190.  
  191.         if (panelPlatforms.split(/\s+/).indexOf(platform) == -1)
  192.           continue; // ignore datasources for other platforms
  193.  
  194.         // empty datasources are valid on search panel definitions
  195.         // convert them to "rdf:null" which can be filtered and ignored
  196.         if (!datasources)
  197.           datasources = "rdf:null";
  198.  
  199.         datasources = normalizeLinks(helpBaseURI, datasources);
  200.  
  201.         var datasourceArray = datasources.split(/\s+/)
  202.                                          .filter(function(x) { return x != "rdf:null"; })
  203.                                          .map(RDF.GetDataSourceBlocking);
  204.  
  205.         // Cache Additional Datasources to Augment Search Datasources.
  206.         if (panelID == "search") {
  207.           emptySearchText = getAttribute(helpFileDS, panelDef, NC_EMPTY_SEARCH_TEXT, emptySearchText);
  208.           emptySearchLink = getAttribute(helpFileDS, panelDef, NC_EMPTY_SEARCH_LINK, emptySearchLink);
  209.  
  210.           datasourceArray.forEach(helpSearchPanel.database.AddDataSource,
  211.                                   helpSearchPanel.database);
  212.           filterDatasourceByPlatform(helpSearchPanel.database);
  213.  
  214.           continue; // to next panel definition
  215.         }
  216.  
  217.         // cache toc datasources list for use in getLink()
  218.         if (panelID == "toc")
  219.           gTocDSList += " " + datasources;
  220.  
  221.         var tree = document.getElementById("help-" + panelID + "-panel");
  222.  
  223.         // add each datasource to the current tree
  224.         datasourceArray.forEach(tree.database.AddDataSource,
  225.                                 tree.database);
  226.  
  227.         // filter and display the current tree
  228.         filterDatasourceByPlatform(tree.database);
  229.         tree.builder.rebuild();
  230.       }
  231.     } catch (e) {
  232.       log(e + "");
  233.     }
  234.   }
  235. }
  236.  
  237. //@line 295 "/c/mozilla/toolkit/components/help/content/help.js"
  238. function filterDatasourceByPlatform(aDatasource) {
  239.   filterNodeByPlatform(aDatasource, RDF_ROOT, 0);
  240. }
  241.  
  242. //@line 301 "/c/mozilla/toolkit/components/help/content/help.js"
  243. function filterNodeByPlatform(aDatasource, aCurrentResource, aCurrentLevel) {
  244.   if (aCurrentLevel > MAX_LEVEL) {
  245.      log("Datasources over " + MAX_LEVEL + " levels deep are unsupported.");
  246.      return;
  247.   }
  248.  
  249.   // get the subheadings under aCurrentResource and filter them
  250.   var nodes = aDatasource.GetTargets(aCurrentResource, NC_SUBHEADINGS, true);
  251.   while (nodes.hasMoreElements()) {
  252.     var node = nodes.getNext();
  253.     node = node.QueryInterface(Components.interfaces.nsIRDFResource);
  254.     // should we test for rdf:Seq here?  see also doFindOnDatasource
  255.     filterSeqByPlatform(aDatasource, node, aCurrentLevel+1);
  256.   }
  257. }
  258.  
  259. //@line 320 "/c/mozilla/toolkit/components/help/content/help.js"
  260. function filterSeqByPlatform(aDatasource, aNode, aCurrentLevel) {
  261.   // get nc:subheading children into an enumerator
  262.   var RDFC = Components.classes["@mozilla.org/rdf/container;1"]
  263.                        .createInstance(Components.interfaces.nsIRDFContainer);
  264.   RDFC.Init(aDatasource, aNode);
  265.   var targets = RDFC.GetElements();
  266.  
  267.   // process items in the rdf:Seq
  268.   while (targets.hasMoreElements()) {
  269.     var currentTarget = targets.getNext();
  270.  
  271.     // find out on which platforms this node is meaningful
  272.     var nodePlatforms = getAttribute(aDatasource,
  273.                                      currentTarget.QueryInterface(Components.interfaces.nsIRDFResource),
  274.                                      NC_PLATFORM,
  275.                                      platform);
  276.  
  277.     if (nodePlatforms.split(/\s+/).indexOf(platform) == -1) { // node is for another platform
  278.       var currentNode = currentTarget.QueryInterface(Components.interfaces.nsIRDFNode);
  279.       // "false" because we don't want to renumber elements in the container
  280.       RDFC.RemoveElement(currentNode, false);
  281.  
  282.       // move to next node - ignore the children, because 1) they might be
  283.       // needed elsewhere and 2) nodes not connected to RDF_ROOT are ignored
  284.       continue;
  285.     }
  286.  
  287.     // filter any children
  288.     filterNodeByPlatform(aDatasource, currentTarget, aCurrentLevel+1);
  289.   }
  290. }
  291.  
  292. //@line 354 "/c/mozilla/toolkit/components/help/content/help.js"
  293. function normalizeLinks(helpBaseURI, links) {
  294.   if (!helpBaseURI) {
  295.     return links;
  296.   }
  297.   var ls = links.split(/\s+/);
  298.   if (ls.length == 0) {
  299.     return links;
  300.   }
  301.   for (var i=0; i < ls.length; ++i) {
  302.     if (ls[i] == "")
  303.       continue;
  304.       
  305.     if (ls[i].substr(0,7) != "chrome:" && ls[i].substr(0,4) != "rdf:")
  306.       ls[i] = helpBaseURI + ls[i];
  307.   }
  308.   return ls.join(" ");
  309. }
  310.  
  311. function getLink(ID) {
  312.     if (!ID)
  313.       return null;
  314.  
  315.     var tocDS = document.getElementById("help-toc-panel").database;
  316.     if (!tocDS)
  317.       return null;
  318.  
  319.     // URIs include both the ID part and the base file name,
  320.     // so we need to check for a matching ID in each datasource
  321.     var tocDSArray = gTocDSList.split(/\s+/)
  322.                                .filter(function(x) { return x != "rdf:null"; });
  323.  
  324.     for (var i = 0; i < tocDSArray.length; i++) {
  325.       var resource = RDF.GetResource(tocDSArray[i] + "#" + ID);
  326.       var link = tocDS.GetTarget(resource, NC_LINK, true);
  327.       if (!link)  // no such rdf:ID found
  328.         continue;
  329.       return link.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  330.     }
  331.     return null;
  332. }
  333.  
  334. //@line 397 "/c/mozilla/toolkit/components/help/content/help.js"
  335. function getHelpFileURI() {
  336.     return helpFileURI;
  337. }
  338.  
  339. function getBrowser() {
  340.   return helpBrowser;
  341. }
  342.  
  343. function getWebNavigation() {
  344.   try {
  345.     return helpBrowser.webNavigation;
  346.   } catch (e)
  347.   {
  348.     return null;
  349.   }
  350. }
  351.  
  352. function loadURI(uri) {
  353.     if (uri.substr(0,7) != "chrome:") {
  354.         uri = helpBaseURI + uri;
  355.     }
  356.     getWebNavigation().loadURI(uri, Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE,
  357.         null, null, null);
  358. }
  359.  
  360. function goBack() {
  361.   try
  362.   {
  363.     getWebNavigation().goBack();
  364.   } catch (e)
  365.   {
  366.   }
  367. }
  368.  
  369. /* copied from browser.js */
  370. function BrowserReloadWithFlags(reloadFlags) {
  371.     /* First, we'll try to use the session history object to reload so 
  372.      * that framesets are handled properly. If we're in a special 
  373.      * window (such as view-source) that has no session history, fall 
  374.      * back on using the web navigation's reload method.
  375.      */
  376.  
  377.     var webNav = getWebNavigation();
  378.     try {
  379.       var sh = webNav.sessionHistory;
  380.       if (sh)
  381.         webNav = sh.QueryInterface(Components.interfaces.nsIWebNavigation);
  382.     } catch (e) {
  383.     }
  384.  
  385.     try {
  386.       webNav.reload(reloadFlags);
  387.     } catch (e) {
  388.     }
  389. }
  390.  
  391. function reload() {
  392.   const reloadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
  393.   return BrowserReloadWithFlags(reloadFlags);
  394. }
  395.  
  396. function goForward() {
  397.     try
  398.     {
  399.       getWebNavigation().goForward();
  400.     } catch(e)
  401.     {
  402.     }
  403. }
  404.  
  405. function goHome() {
  406.     // Load "Welcome" page
  407.     displayTopic(defaultTopic);
  408. }
  409.  
  410. function print() {
  411.     try {
  412.         _content.print();
  413.     } catch (e) {
  414.     }
  415. }
  416.  
  417. function FillHistoryMenu(aParent, aMenu)
  418.   {
  419.     // Remove old entries if any
  420.     deleteHistoryItems(aParent);
  421.  
  422.     var sessionHistory = getWebNavigation().sessionHistory;
  423.  
  424.     var count = sessionHistory.count;
  425.     var index = sessionHistory.index;
  426.     var end;
  427.     var j;
  428.     var entry;
  429.  
  430.     switch (aMenu)
  431.       {
  432.         case "back":
  433.           end = (index > MAX_HISTORY_MENU_ITEMS) ? index - MAX_HISTORY_MENU_ITEMS : 0;
  434.           if ((index - 1) < end) return false;
  435.           for (j = index - 1; j >= end; j--)
  436.             {
  437.               entry = sessionHistory.getEntryAtIndex(j, false);
  438.               if (entry)
  439.                 createMenuItem(aParent, j, entry.title);
  440.             }
  441.           break;
  442.         case "forward":
  443.           end  = ((count-index) > MAX_HISTORY_MENU_ITEMS) ? index + MAX_HISTORY_MENU_ITEMS : count - 1;
  444.           if ((index + 1) > end) return false;
  445.           for (j = index + 1; j <= end; j++)
  446.             {
  447.               entry = sessionHistory.getEntryAtIndex(j, false);
  448.               if (entry)
  449.                 createMenuItem(aParent, j, entry.title);
  450.             }
  451.           break;
  452.       }
  453.     return true;
  454.   }
  455.  
  456. function createMenuItem( aParent, aIndex, aLabel)
  457.   {
  458.     var menuitem = document.createElement( "menuitem" );
  459.     menuitem.setAttribute( "label", aLabel );
  460.     menuitem.setAttribute( "index", aIndex );
  461.     aParent.appendChild( menuitem );
  462.   }
  463.  
  464. function deleteHistoryItems(aParent)
  465. {
  466.   var children = aParent.childNodes;
  467.   for (var i = children.length - 1; i >= 0; --i)
  468.     {
  469.       var index = children[i].getAttribute("index");
  470.       if (index)
  471.         aParent.removeChild(children[i]);
  472.     }
  473. }
  474.  
  475. function createBackMenu(event) {
  476.     return FillHistoryMenu(event.target, "back");
  477. }
  478.  
  479. function createForwardMenu(event) {
  480.     return FillHistoryMenu(event.target, "forward");
  481. }
  482.  
  483. function gotoHistoryIndex(aEvent) {
  484.     var index = aEvent.target.getAttribute("index");
  485.     if (!index) {
  486.         return false;
  487.     }
  488.     try {
  489.         getWebNavigation().gotoIndex(index);
  490.     } catch(ex) {
  491.         return false;
  492.     }
  493.     return true;
  494. }
  495.  
  496. function nsHelpStatusHandler() {
  497.   this.init();
  498. }
  499.  
  500. nsHelpStatusHandler.prototype = {
  501.  
  502.     onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {},
  503.     onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress,
  504.         aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {},
  505.     onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage) {},
  506.     onSecurityChange : function(aWebProgress, aRequest, state) {},
  507.     onLocationChange : function(aWebProgress, aRequest, aLocation) {
  508.         UpdateBackForwardButtons();
  509.     },
  510.     QueryInterface : function(aIID) {
  511.         if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  512.                 aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  513.                 aIID.equals(Components.interfaces.nsIXULBrowserWindow) ||
  514.                 aIID.equals(Components.interfaces.nsISupports)) {
  515.             return this;
  516.         }
  517.         throw Components.results.NS_NOINTERFACE;
  518.     },
  519.  
  520.     init : function() {},
  521.  
  522.     destroy : function() {},
  523.  
  524.     setJSStatus : function(status) {},
  525.     setJSDefaultStatus : function(status) {},
  526.     setOverLink : function(link) {}
  527. }
  528.  
  529. function UpdateBackForwardButtons() {
  530.     var backBroadcaster = document.getElementById("canGoBack");
  531.     var forwardBroadcaster = document.getElementById("canGoForward");
  532.     var webNavigation = getWebNavigation();
  533.  
  534.     // Avoid setting attributes on broadcasters if the value hasn't changed!
  535.     // Remember, guys, setting attributes on elements is expensive!  They
  536.     // get inherited into anonymous content, broadcast to other widgets, etc.!
  537.     // Don't do it if the value hasn't changed! - dwh
  538.  
  539.     var backDisabled = (backBroadcaster.getAttribute("disabled") == "true");
  540.     var forwardDisabled = (forwardBroadcaster.getAttribute("disabled") == "true");
  541.  
  542.     if (backDisabled == webNavigation.canGoBack) {
  543.       if (backDisabled)
  544.         backBroadcaster.removeAttribute("disabled");
  545.       else
  546.         backBroadcaster.setAttribute("disabled", true);
  547.     }
  548.  
  549.     if (forwardDisabled == webNavigation.canGoForward) {
  550.       if (forwardDisabled)
  551.         forwardBroadcaster.removeAttribute("disabled");
  552.       else
  553.         forwardBroadcaster.setAttribute("disabled", true);
  554.     }
  555. }
  556.  
  557. function getMarkupDocumentViewer() {
  558.     return helpBrowser.markupDocumentViewer;
  559. }
  560.  
  561. function onselect_loadURI(tree) {
  562.     try {
  563.         var resource = tree.view.getResourceAtIndex(tree.currentIndex);
  564.         var link = tree.database.GetTarget(resource, NC_LINK, true);
  565.         if (link) {
  566.             link = link.QueryInterface(Components.interfaces.nsIRDFLiteral);
  567.             loadURI(link.Value);
  568.         }
  569.     } catch (e) {
  570.     }// when switching between tabs a spurious row number is returned.
  571. }
  572.  
  573. // copied from browser.js to handle the searchbar
  574. function SearchbarFocusHandler(aEvent, aElt)
  575. {
  576.   if (gIgnoreFocus)
  577.     gIgnoreFocus = false;
  578.   else if (gClickSelectsAll)
  579.     aElt.select();
  580. }
  581.  
  582. function SearchbarMouseDownHandler(aEvent, aElt)
  583. {
  584.   if (aElt.hasAttribute("focused")) {
  585.     gIgnoreClick = true;
  586.   } else {
  587.     gIgnoreFocus = true;
  588.     gIgnoreClick = false;
  589.     aElt.setSelectionRange(0, 0);
  590.   }
  591. }
  592.  
  593. function SearchbarClickHandler(aEvent, aElt)
  594. {
  595.   if (!gIgnoreClick && gClickSelectsAll && aElt.selectionStart == aElt.selectionEnd)
  596.     aElt.select();
  597. }
  598.  
  599.  
  600. function focusSearch() {
  601.   var searchBox = document.getElementById("findText");
  602.   searchBox.focus();
  603. }
  604.  
  605. //@line 669 "/c/mozilla/toolkit/components/help/content/help.js"
  606. function doFind() {
  607.     if (document.getElementById("help-search-sidebar").hidden)
  608.       showSearchSidebar();
  609.  
  610.     var searchTree = document.getElementById("help-search-tree");
  611.     var findText = document.getElementById("findText");
  612.  
  613.     // clear any previous results.
  614.     clearDatabases(searchTree.database);
  615.  
  616.     // if the search string is empty or contains only whitespace, purge the results tree and return
  617.     RE = findText.value.match(/\S+/g);
  618.     if (!RE) {
  619.       searchTree.builder.rebuild();
  620.       hideSearchSidebar();
  621.       return;
  622.     }
  623.  
  624.     // compile the search string, which has already been split up above, into regexps
  625.     for (var i=0; i < RE.length; ++i) {
  626.       RE[i] = new RegExp(RE[i], "i");
  627.     }
  628.     emptySearch = true;
  629.  
  630.     // search TOC
  631.     var resultsDS = Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"]
  632.         .createInstance(Components.interfaces.nsIRDFDataSource);
  633.     var sourceDS = helpTocPanel.database;
  634.     doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
  635.  
  636.     // search glossary.
  637.     sourceDS = helpGlossaryPanel.database;
  638.     doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
  639.  
  640.     // search index
  641.     sourceDS = helpIndexPanel.database;
  642.     doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
  643.  
  644.     // search additional search datasources
  645.     sourceDS = helpSearchPanel.database;
  646.     doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
  647.  
  648.     if (emptySearch)
  649.         assertSearchEmpty(resultsDS);
  650.     // Add the datasource to the search tree
  651.     searchTree.database.AddDataSource(resultsDS);
  652.     searchTree.builder.rebuild();
  653. }
  654.  
  655. function clearDatabases(compositeDataSource) {
  656.     var enumDS = compositeDataSource.GetDataSources()
  657.     while (enumDS.hasMoreElements()) {
  658.         var ds = enumDS.getNext();
  659.         compositeDataSource.RemoveDataSource(ds);
  660.     }
  661. }
  662.  
  663. function doFindOnDatasource(resultsDS, sourceDS, resource, level) {
  664.     if (level > MAX_LEVEL) {
  665.         try {
  666.             log("Recursive reference to resource: " + resource.Value + ".");
  667.         } catch (e) {
  668.             log("Recursive reference to unknown resource.");
  669.         }
  670.         return;
  671.     }
  672.     // find all SUBHEADING children of current resource.
  673.     var targets = sourceDS.GetTargets(resource, NC_SUBHEADINGS, true);
  674.     while (targets.hasMoreElements()) {
  675.         var target = targets.getNext();
  676.         target = target.QueryInterface(Components.interfaces.nsIRDFResource);
  677.         // The first child of a rdf:subheading should (must) be a rdf:seq.
  678.         // Should we test for a SEQ here?
  679.         doFindOnSeq(resultsDS, sourceDS, target, level+1);
  680.     }
  681. }
  682.  
  683. function doFindOnSeq(resultsDS, sourceDS, resource, level) {
  684.     // load up an RDFContainer so we can access the contents of the current
  685.     // rdf:seq.
  686.     RDFContainer.Init(sourceDS, resource);
  687.     var targets = RDFContainer.GetElements();
  688.     while (targets.hasMoreElements()) {
  689.         var target = targets.getNext();
  690.         var link = sourceDS.GetTarget(target, NC_LINK, true);
  691.         var name = sourceDS.GetTarget(target, NC_NAME, true);
  692.         name = name.QueryInterface(Components.interfaces.nsIRDFLiteral);
  693.  
  694.         if (link && isMatch(name.Value)) {
  695.             // we have found a search entry - add it to the results datasource.
  696.             var urn = RDF.GetAnonymousResource();
  697.             resultsDS.Assert(urn, NC_NAME, name, true);
  698.             resultsDS.Assert(urn, NC_LINK, link, true);
  699.             resultsDS.Assert(RDF_ROOT, NC_CHILD, urn, true);
  700.  
  701.             emptySearch = false;
  702.         }
  703.         // process any nested rdf:seq elements.
  704.         doFindOnDatasource(resultsDS, sourceDS, target, level+1);
  705.     }
  706. }
  707.  
  708. function assertSearchEmpty(resultsDS) {
  709.     var resSearchEmpty = RDF.GetResource("urn:emptySearch");
  710.         resultsDS.Assert(RDF_ROOT,
  711.             NC_CHILD,
  712.                 resSearchEmpty,
  713.                 true);
  714.         resultsDS.Assert(resSearchEmpty,
  715.             NC_NAME,
  716.                 RDF.GetLiteral(emptySearchText),
  717.                 true);
  718.         resultsDS.Assert(resSearchEmpty,
  719.                 NC_LINK,
  720.                 RDF.GetLiteral(emptySearchLink),
  721.                 true);
  722. }
  723.  
  724. function isMatch(text) {
  725.     for (var i=0; i < RE.length; ++i ) {
  726.         if (!RE[i].test(text)) {
  727.             return false;
  728.         }
  729.     }
  730.     return true;
  731. }
  732.  
  733. function getAttribute(datasource, resource, attributeResourceName,
  734.         defaultValue) {
  735.     var literal = datasource.GetTarget(resource, attributeResourceName, true);
  736.     if (!literal) {
  737.         return defaultValue;
  738.     }
  739.     return getLiteralValue(literal, defaultValue);
  740. }
  741.  
  742. function getLiteralValue(literal, defaultValue) {
  743.     if (literal) {
  744.         literal = literal.QueryInterface(Components.interfaces.nsIRDFLiteral);
  745.         if (literal) {
  746.             return literal.Value;
  747.         }
  748.     }
  749.     if (defaultValue) {
  750.         return defaultValue;
  751.     }
  752.     return null;
  753. }
  754.  
  755. //@line 819 "/c/mozilla/toolkit/components/help/content/help.js"
  756. function log(aText) {
  757.     CONSOLE_SERVICE.logStringMessage(aText);
  758. }
  759.  
  760. function getBoolPref (aPrefname, aDefault)
  761. {
  762.   try { 
  763.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  764.                          .getService(Components.interfaces.nsIPrefBranch);
  765.     return pref.getBoolPref(aPrefname);
  766.   }
  767.   catch(e) {
  768.     return aDefault;
  769.   }
  770. }
  771.  
  772. //@line 836 "/c/mozilla/toolkit/components/help/content/help.js"
  773. function getXulWin()
  774. {
  775.   window.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  776.   var webnav = window.getInterface(Components.interfaces.nsIWebNavigation);
  777.   var dsti = webnav.QueryInterface(Components.interfaces.nsIDocShellTreeItem);
  778.   var treeowner = dsti.treeOwner;
  779.   var ifreq = treeowner.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  780.  
  781.   return ifreq.getInterface(Components.interfaces.nsIXULWindow);
  782. }
  783.  
  784. //@line 859 "/c/mozilla/toolkit/components/help/content/help.js"
  785. function toggleZLevel(element)
  786. {
  787.   var xulwin = getXulWin();
  788.   
  789.   // Now we can flip the zLevel, and set the attribute so that it persists correctly
  790.   if (xulwin.zLevel > xulwin.normalZ) {
  791.     xulwin.zLevel = xulwin.normalZ;
  792.     element.setAttribute("checked", "false");
  793.   } else {
  794.     xulwin.zLevel = xulwin.raisedZ;
  795.     element.setAttribute("checked", "true");
  796.   }
  797. }
  798.