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/
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.
12
* The Original Code is Mozilla Communicator client code, released
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
20
* Ian Oeschger <oeschger@brownhen.com> (Original Author)
21
* Peter Wilson (added sidebar tabs)
22
* R.J. Keller <rlk@trfenv.com>
25
//-------- global variables
34
var helpGlossaryPanel;
37
const NC = "http://home.netscape.com/NC-rdf#";
38
const SN = "rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#";
39
const XML = "http://www.w3.org/XML/1998/namespace#"
40
const MAX_LEVEL = 40; // maximum depth of recursion in search datasources.
43
const RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
44
const RDF_ROOT = RDF.GetResource("urn:root");
45
const NC_PANELLIST = RDF.GetResource(NC + "panellist");
46
const NC_PANELID = RDF.GetResource(NC + "panelid");
47
const NC_EMPTY_SEARCH_TEXT = RDF.GetResource(NC + "emptysearchtext");
48
const NC_EMPTY_SEARCH_LINK = RDF.GetResource(NC + "emptysearchlink");
49
const NC_DATASOURCES = RDF.GetResource(NC + "datasources");
50
const NC_SUBHEADINGS = RDF.GetResource(NC + "subheadings");
51
const NC_NAME = RDF.GetResource(NC + "name");
52
const NC_CHILD = RDF.GetResource(NC + "child");
53
const NC_LINK = RDF.GetResource(NC + "link");
54
const NC_TITLE = RDF.GetResource(NC + "title");
55
const NC_BASE = RDF.GetResource(NC + "base");
56
const NC_DEFAULTTOPIC = RDF.GetResource(NC + "defaulttopic");
58
const RDFCUtils = Components.classes["@mozilla.org/rdf/container-utils;1"].getService(Components.interfaces.nsIRDFContainerUtils);
59
const RDFContainer = Components.classes["@mozilla.org/rdf/container;1"].getService(Components.interfaces.nsIRDFContainer);
60
const CONSOLE_SERVICE = Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService);
67
// Set from nc:base attribute on help rdf file. It may be used for prefix reduction on all links within
68
// the current help set.
71
const defaultHelpFile = "chrome://help/locale/mozillahelp.rdf";
72
// Set from nc:defaulttopic. It is used when the requested uri has no topic specified.
73
var defaultTopic = "welcome";
74
var searchDatasources = "rdf:null";
77
const NSRESULT_RDF_SYNTAX_ERROR = 0x804e03f7;
79
// This function is called by dialogs/windows that want to display context-sensitive help
80
// These dialogs/windows should include the script chrome://help/content/contextHelp.js
81
function displayTopic(topic) {
82
// Use default topic if topic is not specified.
86
// Get the help page to open.
87
var uri = getLink(topic);
89
// Use default topic if specified topic is not found.
90
if (!uri) // Topic not found - revert to default.
91
uri = getLink(defaultTopic);
95
// Initialize the Help window
97
//cache panel references.
98
helpWindow = document.getElementById("help");
99
helpSearchPanel = document.getElementById("help-search-panel");
100
helpTocPanel = document.getElementById("help-toc-panel");
101
helpIndexPanel = document.getElementById("help-index-panel");
102
helpGlossaryPanel = document.getElementById("help-glossary-panel");
103
helpBrowser = document.getElementById("help-content");
105
// Get the help content pack, base URL, and help topic
106
var helpTopic = defaultTopic;
107
if ("arguments" in window && window.arguments[0] instanceof Components.interfaces.nsIDialogParamBlock) {
108
helpFileURI = window.arguments[0].GetString(0);
109
helpBaseURI = helpFileURI.substring(0, helpFileURI.lastIndexOf("/")+1); // trailing "/" included.
110
helpTopic = window.arguments[0].GetString(1);
115
displayTopic(helpTopic);
117
// Initalize History.
118
var sessionHistory = Components.classes["@mozilla.org/browser/shistory;1"]
119
.createInstance(Components.interfaces.nsISHistory);
121
window.XULBrowserWindow = new nsHelpStatusHandler();
123
//Start the status handler.
124
window.XULBrowserWindow.init();
126
// Hook up UI through Progress Listener
127
const interfaceRequestor = helpBrowser.docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
128
const webProgress = interfaceRequestor.getInterface(Components.interfaces.nsIWebProgress);
129
webProgress.addProgressListener(window.XULBrowserWindow, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
131
//Always show the Table of Contents sidebar at startup.
132
showPanel('help-toc');
135
function loadHelpRDF() {
138
helpFileDS = RDF.GetDataSourceBlocking(helpFileURI);
140
catch (e if (e.result == NSRESULT_RDF_SYNTAX_ERROR)) {
141
log("Help file: " + helpFileURI + " contains a syntax error.");
144
log("Help file: " + helpFileURI + " was not found.");
147
helpWindow.setAttribute("title", getAttribute(helpFileDS, RDF_ROOT, NC_TITLE, ""));
148
helpBaseURI = getAttribute(helpFileDS, RDF_ROOT, NC_BASE, helpBaseURI);
149
defaultTopic = getAttribute(helpFileDS, RDF_ROOT, NC_DEFAULTTOPIC, "welcome");
151
var panelDefs = helpFileDS.GetTarget(RDF_ROOT, NC_PANELLIST, true);
152
RDFContainer.Init(helpFileDS, panelDefs);
153
var iterator = RDFContainer.GetElements();
154
while (iterator.hasMoreElements()) {
155
var panelDef = iterator.getNext();
156
var panelID = getAttribute(helpFileDS, panelDef, NC_PANELID, null);
158
var datasources = getAttribute(helpFileDS, panelDef, NC_DATASOURCES, "rdf:none");
159
datasources = normalizeLinks(helpBaseURI, datasources);
160
// cache additional datsources to augment search datasources.
161
if (panelID == "search") {
162
emptySearchText = getAttribute(helpFileDS, panelDef, NC_EMPTY_SEARCH_TEXT, null) || "No search items found." ;
163
emptySearchLink = getAttribute(helpFileDS, panelDef, NC_EMPTY_SEARCH_LINK, null) || "about:blank";
164
searchDatasources = datasources;
165
datasources = "rdf:null"; // but don't try to display them yet!
168
// cache toc datasources for use by ID lookup.
169
var tree = document.getElementById("help-" + panelID + "-panel");
170
loadDatabasesBlocking(datasources);
171
tree.setAttribute("datasources", datasources);
180
function loadDatabasesBlocking(datasources) {
181
var ds = datasources.split(/\s+/);
182
for (var i=0; i < ds.length; ++i) {
183
if (ds[i] == "rdf:null" || ds[i] == "")
186
// we need blocking here to ensure the database is loaded so getLink(topic) works.
187
var datasource = RDF.GetDataSourceBlocking(ds[i]);
190
log("Datasource: " + ds[i] + " was not found.");
195
// prepend helpBaseURI to list of space separated links if the don't start with "chrome:"
196
function normalizeLinks(helpBaseURI, links) {
199
var ls = links.split(/\s+/);
202
for (var i=0; i < ls.length; ++i) {
205
if (ls[i].substr(0,7) != "chrome:" && ls[i].substr(0,4) != "rdf:")
206
ls[i] = helpBaseURI + ls[i];
211
function getLink(ID) {
214
// Note resources are stored in fileURL#ID format.
215
// We have one possible source for an ID for each datasource in the composite datasource.
216
// The first ID which matches is returned.
217
var tocTree = document.getElementById("help-toc-panel");
218
var tocDS = tocTree.database;
221
var tocDatasources = tocTree.getAttribute("datasources");
222
var ds = tocDatasources.split(/\s+/);
223
for (var i=0; i < ds.length; ++i) {
224
if (ds[i] == "rdf:null" || ds[i] == "")
227
var rdfID = ds[i] + "#" + ID;
228
var resource = RDF.GetResource(rdfID);
230
var link = tocDS.GetTarget(resource, NC_LINK, true);
232
link = link.QueryInterface(Components.interfaces.nsIRDFLiteral);
240
catch (e) { log(rdfID + " " + e);}
245
// Called by contextHelp.js to determine if this window is displaying the requested help file.
246
function getHelpFileURI() {
251
function getWebNavigation()
253
return helpBrowser.webNavigation;
256
function loadURI(uri)
258
if (uri.substr(0,7) != "chrome:")
259
uri = helpBaseURI + uri;
260
const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
261
getWebNavigation().loadURI(uri, nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
266
var webNavigation = getWebNavigation();
267
if (webNavigation.canGoBack)
268
webNavigation.goBack();
273
var webNavigation = getWebNavigation();
274
if (webNavigation.canGoForward)
275
webNavigation.goForward();
279
// load "Welcome" page
280
displayTopic(defaultTopic);
291
function createBackMenu(event)
293
return FillHistoryMenu(event.target, "back");
296
function createForwardMenu(event)
298
return FillHistoryMenu(event.target, "forward");
301
function gotoHistoryIndex(aEvent)
303
var index = aEvent.target.getAttribute("index");
307
getWebNavigation().gotoIndex(index);
315
function nsHelpStatusHandler()
319
nsHelpStatusHandler.prototype =
321
onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
323
const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
325
// Turn on the throbber.
326
if (aStateFlags & nsIWebProgressListener.STATE_START)
327
this.throbberElement.setAttribute("busy", "true");
328
else if (aStateFlags & nsIWebProgressListener.STATE_STOP)
329
this.throbberElement.removeAttribute("busy");
331
onStatusChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {},
332
onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress,
333
aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {},
334
onSecurityChange : function(aWebProgress, aRequest, state) {},
335
onLocationChange : function(aWebProgress, aRequest, aLocation)
337
UpdateBackForwardButtons();
339
QueryInterface : function(aIID)
341
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
342
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
343
aIID.equals(Components.interfaces.nsIXULBrowserWindow) ||
344
aIID.equals(Components.interfaces.nsISupports))
346
throw Components.results.NS_NOINTERFACE;
351
this.throbberElement = document.getElementById("navigator-throbber");
356
//this is needed to avoid memory leaks, see bug 60729
357
this.throbberElement = null;
360
setJSStatus : function(status) {},
361
setJSDefaultStatus : function(status) {},
362
setOverLink : function(link) {}
365
function UpdateBackForwardButtons()
367
var backBroadcaster = document.getElementById("canGoBack");
368
var forwardBroadcaster = document.getElementById("canGoForward");
369
var webNavigation = getWebNavigation();
371
// Avoid setting attributes on broadcasters if the value hasn't changed!
372
// Remember, guys, setting attributes on elements is expensive! They
373
// get inherited into anonymous content, broadcast to other widgets, etc.!
374
// Don't do it if the value hasn't changed! - dwh
376
var backDisabled = (backBroadcaster.getAttribute("disabled") == "true");
377
var forwardDisabled = (forwardBroadcaster.getAttribute("disabled") == "true");
379
if (backDisabled == webNavigation.canGoBack)
380
backBroadcaster.setAttribute("disabled", !backDisabled);
382
if (forwardDisabled == webNavigation.canGoForward)
383
forwardBroadcaster.setAttribute("disabled", !forwardDisabled);
387
function getFindInstData()
389
if (!gFindInstData) {
390
gFindInstData = new nsFindInstData();
391
gFindInstData.browser = helpBrowser;
392
// defaults for rootSearchWindow and currentSearchWindow are fine here
394
return gFindInstData;
397
function find(again, reverse)
400
findAgainInPage(getFindInstData(), reverse);
402
findInPage(getFindInstData())
405
function getMarkupDocumentViewer()
407
return helpBrowser.markupDocumentViewer;
410
//Show the selected sidebar panel
411
function showPanel(panelId) {
412
//hide other sidebar panels and show the panel name taken in from panelID.
413
helpSearchPanel.setAttribute("hidden", "true");
414
helpTocPanel.setAttribute("hidden", "true");
415
helpIndexPanel.setAttribute("hidden", "true");
416
helpGlossaryPanel.setAttribute("hidden", "true");
417
var thePanel = document.getElementById(panelId + "-panel");
418
thePanel.setAttribute("hidden","false");
420
//remove the selected style from the previous panel selected.
421
document.getElementById("help-glossary-btn").removeAttribute("selected");
422
document.getElementById("help-index-btn").removeAttribute("selected");
423
document.getElementById("help-search-btn").removeAttribute("selected");
424
document.getElementById("help-toc-btn").removeAttribute("selected");
426
//add the selected style to the correct panel.
427
var theButton = document.getElementById(panelId + "-btn");
428
theButton.setAttribute("selected", "true");
431
function onselect_loadURI(tree, columnName) {
433
var row = tree.treeBoxObject.view.selection.currentIndex;
434
var properties = Components.classes["@mozilla.org/supports-array;1"].createInstance(Components.interfaces.nsISupportsArray);
435
tree.treeBoxObject.view.getCellProperties(row, columnName, properties);
436
if (!properties) return;
437
var uri = getPropertyValue(properties, "link-");
441
catch (e) {}// when switching between tabs a spurious row number is returned.
444
/** Search properties nsISupportsArray for an nsIAtom which starts with the given property name. **/
445
function getPropertyValue(properties, propName) {
446
for (var i=0; i< properties.Count(); ++i) {
447
var atom = properties.GetElementAt(i).QueryInterface(Components.interfaces.nsIAtom);
448
var atomValue = atom.toString();
449
if (atomValue.substr(0, propName.length) == propName)
450
return atomValue.substr(propName.length);
456
var searchTree = document.getElementById("help-search-tree");
457
var findText = document.getElementById("findText");
459
// clear any previous results.
460
clearDatabases(searchTree.database);
462
// split search string into separate terms and compile into regexp's
463
RE = findText.value.split(/\s+/);
464
for (var i=0; i < RE.length; ++i) {
467
RE[i] = new RegExp(RE[i], "i");
471
var resultsDS = Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance(Components.interfaces.nsIRDFDataSource);
472
var tree = document.getElementById("help-toc-panel");
473
var sourceDS = tree.database;
474
doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
476
// search additional search datasources
477
if (searchDatasources != "rdf:null") {
479
searchDS = loadCompositeDS(searchDatasources);
480
doFindOnDatasource(resultsDS, searchDS, RDF_ROOT, 0);
484
tree = document.getElementById("help-index-panel");
485
sourceDS = tree.database;
486
if (!sourceDS) // If the index has never been displayed this will be null.
487
sourceDS = loadCompositeDS(tree.datasources);
488
doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
491
tree = document.getElementById("help-glossary-panel");
492
sourceDS = tree.database;
493
if (!sourceDS) // If the glossary has never been displayed this will be null (sigh!).
494
sourceDS = loadCompositeDS(tree.datasources);
495
doFindOnDatasource(resultsDS, sourceDS, RDF_ROOT, 0);
498
assertSearchEmpty(resultsDS);
499
// Add the datasource to the search tree
500
searchTree.database.AddDataSource(resultsDS);
501
searchTree.builder.rebuild();
504
function doEnabling() {
505
var findButton = document.getElementById("findButton");
506
var findTextbox = document.getElementById("findText");
507
findButton.disabled = !findTextbox.value;
510
function clearDatabases(compositeDataSource) {
511
var enumDS = compositeDataSource.GetDataSources()
512
while (enumDS.hasMoreElements()) {
513
var ds = enumDS.getNext();
514
compositeDataSource.RemoveDataSource(ds);
518
function doFindOnDatasource(resultsDS, sourceDS, resource, level) {
519
if (level > MAX_LEVEL) {
521
log("Recursive reference to resource: " + resource.Value + ".");
524
log("Recursive reference to unknown resource.");
528
// find all SUBHEADING children of current resource.
529
var targets = sourceDS.GetTargets(resource, NC_SUBHEADINGS, true);
530
while (targets.hasMoreElements()) {
531
var target = targets.getNext();
532
target = target.QueryInterface(Components.interfaces.nsIRDFResource);
533
// The first child of a rdf:subheading should (must) be a rdf:seq.
534
// Should we test for a SEQ here?
535
doFindOnSeq(resultsDS, sourceDS, target, level+1);
539
function doFindOnSeq(resultsDS, sourceDS, resource, level) {
540
// load up an RDFContainer so we can access the contents of the current rdf:seq.
541
RDFContainer.Init(sourceDS, resource);
542
var targets = RDFContainer.GetElements();
543
while (targets.hasMoreElements()) {
544
var target = targets.getNext();
545
target = target.QueryInterface(Components.interfaces.nsIRDFResource);
546
var name = sourceDS.GetTarget(target, NC_NAME, true);
547
name = name.QueryInterface(Components.interfaces.nsIRDFLiteral);
549
if (isMatch(name.Value)) {
550
// we have found a search entry - add it to the results datasource.
552
// Get URL of html for this entry.
553
var link = sourceDS.GetTarget(target, NC_LINK, true);
554
link = link.QueryInterface(Components.interfaces.nsIRDFLiteral);
557
resultsDS.Assert(RDF_ROOT,
558
RDF.GetResource("http://home.netscape.com/NC-rdf#child"),
559
RDF.GetResource("urn:" + urnID),
561
resultsDS.Assert(RDF.GetResource("urn:" + urnID),
562
RDF.GetResource("http://home.netscape.com/NC-rdf#name"),
565
resultsDS.Assert(RDF.GetResource("urn:" + urnID),
566
RDF.GetResource("http://home.netscape.com/NC-rdf#link"),
572
// process any nested rdf:seq elements.
573
doFindOnDatasource(resultsDS, sourceDS, target, level+1);
577
function assertSearchEmpty(resultsDS) {
578
var resSearchEmpty = RDF.GetResource("urn:emptySearch");
579
resultsDS.Assert(RDF_ROOT,
583
resultsDS.Assert(resSearchEmpty,
585
RDF.GetLiteral(emptySearchText),
587
resultsDS.Assert(resSearchEmpty,
589
RDF.GetLiteral(emptySearchLink),
593
function isMatch(text) {
594
for (var i=0; i < RE.length; ++i ) {
595
if (!RE[i].test(text))
601
function loadCompositeDS(datasources) {
602
// We can't search on each individual datasource's - only the aggregate (for each sidebar tab)
603
// has the appropriate structure.
604
var compositeDS = Components.classes["@mozilla.org/rdf/datasource;1?name=composite-datasource"]
605
.createInstance(Components.interfaces.nsIRDFCompositeDataSource);
607
var ds = datasources.split(/\s+/);
608
for (var i=0; i < ds.length; ++i) {
609
if (ds[i] == "rdf:null" || ds[i] == "")
612
// we need blocking here to ensure the database is loaded.
613
var sourceDS = RDF.GetDataSourceBlocking(ds[i]);
614
compositeDS.AddDataSource(sourceDS);
617
log("Datasource: " + ds[i] + " was not found.");
623
function getAttribute(datasource, resource, attributeResourceName, defaultValue) {
624
var literal = datasource.GetTarget(resource, attributeResourceName, true);
627
return getLiteralValue(literal, defaultValue);
630
function getLiteralValue(literal, defaultValue) {
632
literal = literal.QueryInterface(Components.interfaces.nsIRDFLiteral);
634
return literal.Value;
640
// Write debug string to javascript console.
641
function log(aText) {
642
CONSOLE_SERVICE.logStringMessage(aText);
646
//INDEX OPENING FUNCTION -- called in oncommand for index pane
647
// iterate over all the items in the outliner;
648
// open the ones at the top-level (i.e., expose the headings underneath
649
// the letters in the list.
650
function displayIndex() {
651
var treeview = helpIndexPanel.view;
652
var i = treeview.rowCount;
654
if (!treeview.getLevel(i) && !treeview.isContainerOpen(i))
655
treeview.toggleOpenState(i);
658
// Shows the panel relative to the currently selected panel.
659
// Takes a boolean parameter - if true it will show the next panel,
660
// otherwise it will show the previous panel.
661
function showRelativePanel(goForward) {
662
var selectedIndex = -1;
663
var sidebarBox = document.getElementById("helpsidebar-box");
664
var sidebarButtons = new Array();
665
for (var i = 0; i < sidebarBox.childNodes.length; i++) {
666
var btn = sidebarBox.childNodes[i];
667
if (btn.nodeName == "button") {
668
if (btn.getAttribute("selected") == "true")
669
selectedIndex = sidebarButtons.length;
670
sidebarButtons.push(btn);
673
if (selectedIndex == -1)
675
selectedIndex += goForward ? 1 : -1;
676
if (selectedIndex >= sidebarButtons.length)
678
else if (selectedIndex < 0)
679
selectedIndex = sidebarButtons.length - 1;
680
sidebarButtons[selectedIndex].doCommand();