~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/help/resources/content/help.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *    Ian Oeschger <oeschger@brownhen.com> (Original Author)
 
21
 *    Peter Wilson (added sidebar tabs)
 
22
 *    R.J. Keller <rlk@trfenv.com>
 
23
 */
 
24
 
 
25
//-------- global variables
 
26
var helpBrowser;
 
27
var helpWindow;
 
28
var helpSearchPanel;
 
29
var emptySearch;
 
30
var emptySearchText
 
31
var emptySearchLink
 
32
var helpTocPanel;
 
33
var helpIndexPanel;
 
34
var helpGlossaryPanel;
 
35
 
 
36
// Namespaces
 
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.
 
41
 
 
42
// Resources
 
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"); 
 
57
 
 
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);
 
61
            
 
62
var urnID = 0;
 
63
var RE;
 
64
 
 
65
var helpFileURI;
 
66
var helpFileDS;
 
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.
 
69
var helpBaseURI;
 
70
 
 
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";
 
75
var searchDS = null;
 
76
 
 
77
const NSRESULT_RDF_SYNTAX_ERROR = 0x804e03f7; 
 
78
 
 
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.
 
83
  if (!topic)
 
84
    topic = defaultTopic;
 
85
 
 
86
  // Get the help page to open.
 
87
  var uri = getLink(topic);
 
88
 
 
89
  // Use default topic if specified topic is not found.
 
90
  if (!uri) // Topic not found - revert to default.
 
91
    uri = getLink(defaultTopic);
 
92
  loadURI(uri);
 
93
}
 
94
 
 
95
// Initialize the Help window
 
96
function init() {
 
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");
 
104
 
 
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);
 
111
  }
 
112
 
 
113
  loadHelpRDF();
 
114
 
 
115
  displayTopic(helpTopic);
 
116
 
 
117
  // Initalize History.
 
118
  var sessionHistory =  Components.classes["@mozilla.org/browser/shistory;1"]
 
119
                                  .createInstance(Components.interfaces.nsISHistory);
 
120
 
 
121
  window.XULBrowserWindow = new nsHelpStatusHandler();
 
122
 
 
123
  //Start the status handler.
 
124
  window.XULBrowserWindow.init();
 
125
 
 
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);
 
130
 
 
131
  //Always show the Table of Contents sidebar at startup.
 
132
  showPanel('help-toc');
 
133
}
 
134
 
 
135
function loadHelpRDF() {
 
136
  if (!helpFileDS) {
 
137
    try {
 
138
      helpFileDS = RDF.GetDataSourceBlocking(helpFileURI);
 
139
    }
 
140
    catch (e if (e.result == NSRESULT_RDF_SYNTAX_ERROR)) {
 
141
      log("Help file: " + helpFileURI + " contains a syntax error.");
 
142
    }
 
143
    catch (e) {
 
144
      log("Help file: " + helpFileURI + " was not found.");
 
145
    }
 
146
    try {
 
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");
 
150
 
 
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);        
 
157
 
 
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!
 
166
        }  
 
167
 
 
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);
 
172
      }  
 
173
    }
 
174
    catch (e) {
 
175
      log(e + "");      
 
176
    }
 
177
  }
 
178
}
 
179
 
 
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] == "")
 
184
      continue;
 
185
    try {  
 
186
      // we need blocking here to ensure the database is loaded so getLink(topic) works.
 
187
      var datasource = RDF.GetDataSourceBlocking(ds[i]);
 
188
    }
 
189
    catch (e) {
 
190
      log("Datasource: " + ds[i] + " was not found.");
 
191
    }
 
192
  }
 
193
}
 
194
 
 
195
// prepend helpBaseURI to list of space separated links if the don't start with "chrome:"
 
196
function normalizeLinks(helpBaseURI, links) {
 
197
  if (!helpBaseURI)
 
198
    return links;
 
199
  var ls = links.split(/\s+/);
 
200
  if (ls.length == 0)
 
201
    return links;
 
202
  for (var i=0; i < ls.length; ++i) {
 
203
    if (ls[i] == "")
 
204
      continue;
 
205
    if (ls[i].substr(0,7) != "chrome:" && ls[i].substr(0,4) != "rdf:") 
 
206
      ls[i] = helpBaseURI + ls[i];
 
207
  }
 
208
  return ls.join(" ");  
 
209
}
 
210
 
 
211
function getLink(ID) {
 
212
  if (!ID)
 
213
    return null;
 
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;
 
219
    if (tocDS == null)
 
220
      return null;
 
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] == "")
 
225
      continue;
 
226
    try {
 
227
      var rdfID = ds[i] + "#" + ID;
 
228
      var resource = RDF.GetResource(rdfID);
 
229
      if (resource) {
 
230
        var link = tocDS.GetTarget(resource, NC_LINK, true);
 
231
        if (link) {
 
232
          link = link.QueryInterface(Components.interfaces.nsIRDFLiteral);
 
233
          if (link) 
 
234
            return link.Value;
 
235
          else  
 
236
            return null;
 
237
        }  
 
238
      }
 
239
    }
 
240
    catch (e) { log(rdfID + " " + e);}
 
241
  }
 
242
  return null;
 
243
}
 
244
 
 
245
// Called by contextHelp.js to determine if this window is displaying the requested help file.
 
246
function getHelpFileURI() {
 
247
  return helpFileURI;
 
248
}
 
249
 
 
250
 
 
251
function getWebNavigation()
 
252
{
 
253
  return helpBrowser.webNavigation;
 
254
}
 
255
 
 
256
function loadURI(uri)
 
257
{
 
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);
 
262
}
 
263
 
 
264
function goBack()
 
265
{
 
266
  var webNavigation = getWebNavigation();
 
267
  if (webNavigation.canGoBack)
 
268
    webNavigation.goBack();
 
269
}
 
270
 
 
271
function goForward()
 
272
{
 
273
  var webNavigation = getWebNavigation();
 
274
  if (webNavigation.canGoForward)
 
275
    webNavigation.goForward();
 
276
}
 
277
 
 
278
function goHome() {
 
279
  // load "Welcome" page
 
280
  displayTopic(defaultTopic);
 
281
}
 
282
 
 
283
function print()
 
284
{
 
285
  try {
 
286
    _content.print();
 
287
  } catch (e) {
 
288
  }
 
289
}
 
290
 
 
291
function createBackMenu(event)
 
292
{
 
293
  return FillHistoryMenu(event.target, "back");
 
294
}
 
295
 
 
296
function createForwardMenu(event)
 
297
{
 
298
  return FillHistoryMenu(event.target, "forward");
 
299
}
 
300
 
 
301
function gotoHistoryIndex(aEvent)
 
302
{
 
303
  var index = aEvent.target.getAttribute("index");
 
304
  if (!index)
 
305
    return false;
 
306
  try {
 
307
    getWebNavigation().gotoIndex(index);
 
308
  }
 
309
  catch(ex) {
 
310
    return false;
 
311
  }
 
312
  return true;
 
313
}
 
314
 
 
315
function nsHelpStatusHandler()
 
316
{
 
317
}
 
318
 
 
319
nsHelpStatusHandler.prototype =
 
320
{
 
321
  onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
 
322
  {
 
323
    const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
 
324
 
 
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");
 
330
  },
 
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)
 
336
  {
 
337
    UpdateBackForwardButtons();
 
338
  },
 
339
  QueryInterface : function(aIID)
 
340
  {
 
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))
 
345
      return this;
 
346
    throw Components.results.NS_NOINTERFACE;
 
347
  },
 
348
 
 
349
  init : function()
 
350
  {
 
351
    this.throbberElement = document.getElementById("navigator-throbber");
 
352
  },
 
353
 
 
354
  destroy : function()
 
355
  {
 
356
    //this is needed to avoid memory leaks, see bug 60729
 
357
    this.throbberElement = null;
 
358
  },
 
359
 
 
360
  setJSStatus : function(status) {},
 
361
  setJSDefaultStatus : function(status) {},
 
362
  setOverLink : function(link) {}
 
363
}
 
364
 
 
365
function UpdateBackForwardButtons()
 
366
{
 
367
  var backBroadcaster = document.getElementById("canGoBack");
 
368
  var forwardBroadcaster = document.getElementById("canGoForward");
 
369
  var webNavigation = getWebNavigation();
 
370
 
 
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
 
375
 
 
376
  var backDisabled = (backBroadcaster.getAttribute("disabled") == "true");
 
377
  var forwardDisabled = (forwardBroadcaster.getAttribute("disabled") == "true");
 
378
 
 
379
  if (backDisabled == webNavigation.canGoBack)
 
380
    backBroadcaster.setAttribute("disabled", !backDisabled);
 
381
  
 
382
  if (forwardDisabled == webNavigation.canGoForward)
 
383
    forwardBroadcaster.setAttribute("disabled", !forwardDisabled);
 
384
}
 
385
 
 
386
var gFindInstData;
 
387
function getFindInstData()
 
388
{
 
389
  if (!gFindInstData) {
 
390
    gFindInstData = new nsFindInstData();
 
391
    gFindInstData.browser = helpBrowser;
 
392
    // defaults for rootSearchWindow and currentSearchWindow are fine here
 
393
  }
 
394
  return gFindInstData;
 
395
}
 
396
 
 
397
function find(again, reverse)
 
398
{
 
399
  if (again)
 
400
    findAgainInPage(getFindInstData(), reverse);
 
401
  else
 
402
    findInPage(getFindInstData())
 
403
}
 
404
 
 
405
function getMarkupDocumentViewer()
 
406
{
 
407
  return helpBrowser.markupDocumentViewer;
 
408
}
 
409
 
 
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");
 
419
 
 
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");
 
425
 
 
426
  //add the selected style to the correct panel.
 
427
  var theButton = document.getElementById(panelId + "-btn");
 
428
  theButton.setAttribute("selected", "true");
 
429
}
 
430
 
 
431
function onselect_loadURI(tree, columnName) {
 
432
  try {
 
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-");
 
438
    if (uri)
 
439
      loadURI(uri);
 
440
  }
 
441
  catch (e) {}// when switching between tabs a spurious row number is returned.
 
442
}
 
443
 
 
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);
 
451
  }
 
452
  return null;
 
453
}
 
454
 
 
455
function doFind() {
 
456
  var searchTree = document.getElementById("help-search-tree");
 
457
  var findText = document.getElementById("findText");
 
458
 
 
459
  // clear any previous results.
 
460
  clearDatabases(searchTree.database);
 
461
 
 
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) {
 
465
    if (RE[i] == "")
 
466
      continue;
 
467
    RE[i] = new RegExp(RE[i], "i");
 
468
  }
 
469
 emptySearch = true;
 
470
  // search TOC
 
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);
 
475
 
 
476
  // search additional search datasources
 
477
  if (searchDatasources != "rdf:null") {
 
478
    if (!searchDS)
 
479
      searchDS = loadCompositeDS(searchDatasources);
 
480
    doFindOnDatasource(resultsDS, searchDS, RDF_ROOT, 0);
 
481
  }
 
482
 
 
483
  // search index.
 
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);
 
489
 
 
490
  // search glossary.
 
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);
 
496
 
 
497
  if (emptySearch)
 
498
                assertSearchEmpty(resultsDS);
 
499
  // Add the datasource to the search tree
 
500
  searchTree.database.AddDataSource(resultsDS);
 
501
  searchTree.builder.rebuild();
 
502
}
 
503
 
 
504
function doEnabling() {
 
505
  var findButton = document.getElementById("findButton");
 
506
  var findTextbox = document.getElementById("findText");
 
507
  findButton.disabled = !findTextbox.value;
 
508
}
 
509
 
 
510
function clearDatabases(compositeDataSource) {
 
511
  var enumDS = compositeDataSource.GetDataSources()
 
512
  while (enumDS.hasMoreElements()) {
 
513
    var ds = enumDS.getNext();
 
514
        compositeDataSource.RemoveDataSource(ds);
 
515
  }
 
516
}
 
517
 
 
518
function doFindOnDatasource(resultsDS, sourceDS, resource, level) {
 
519
  if (level > MAX_LEVEL) {
 
520
    try {
 
521
      log("Recursive reference to resource: " + resource.Value + ".");
 
522
    }
 
523
    catch (e) {
 
524
      log("Recursive reference to unknown resource.");
 
525
    }
 
526
    return;
 
527
  }
 
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);       
 
536
  }  
 
537
}
 
538
 
 
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);
 
548
        
 
549
        if (isMatch(name.Value)) {
 
550
          // we have found a search entry - add it to the results datasource.
 
551
          
 
552
          // Get URL of html for this entry.
 
553
      var link = sourceDS.GetTarget(target, NC_LINK, true);
 
554
      link = link.QueryInterface(Components.interfaces.nsIRDFLiteral);        
 
555
 
 
556
      urnID++;
 
557
      resultsDS.Assert(RDF_ROOT,
 
558
             RDF.GetResource("http://home.netscape.com/NC-rdf#child"),
 
559
             RDF.GetResource("urn:" + urnID),
 
560
             true);
 
561
      resultsDS.Assert(RDF.GetResource("urn:" + urnID),
 
562
             RDF.GetResource("http://home.netscape.com/NC-rdf#name"),
 
563
             name,
 
564
             true);
 
565
      resultsDS.Assert(RDF.GetResource("urn:" + urnID),
 
566
             RDF.GetResource("http://home.netscape.com/NC-rdf#link"),
 
567
             link,
 
568
             true);
 
569
                emptySearch = false;    
 
570
             
 
571
    }
 
572
    // process any nested rdf:seq elements.
 
573
    doFindOnDatasource(resultsDS, sourceDS, target, level+1);       
 
574
    }  
 
575
}
 
576
 
 
577
function assertSearchEmpty(resultsDS) {
 
578
        var resSearchEmpty = RDF.GetResource("urn:emptySearch");
 
579
        resultsDS.Assert(RDF_ROOT,
 
580
                         NC_CHILD,
 
581
                         resSearchEmpty,
 
582
                         true);
 
583
        resultsDS.Assert(resSearchEmpty,
 
584
                         NC_NAME,
 
585
                         RDF.GetLiteral(emptySearchText),
 
586
                         true);
 
587
        resultsDS.Assert(resSearchEmpty,
 
588
                         NC_LINK,
 
589
                         RDF.GetLiteral(emptySearchLink),
 
590
                         true);
 
591
}
 
592
 
 
593
function isMatch(text) {
 
594
  for (var i=0; i < RE.length; ++i ) {
 
595
    if (!RE[i].test(text))
 
596
      return false;
 
597
  }
 
598
  return true;
 
599
}
 
600
 
 
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);
 
606
  
 
607
  var ds = datasources.split(/\s+/);
 
608
  for (var i=0; i < ds.length; ++i) {
 
609
    if (ds[i] == "rdf:null" || ds[i] == "")
 
610
      continue;
 
611
    try {  
 
612
      // we need blocking here to ensure the database is loaded.
 
613
      var sourceDS = RDF.GetDataSourceBlocking(ds[i]);
 
614
      compositeDS.AddDataSource(sourceDS);
 
615
    }
 
616
    catch (e) {
 
617
      log("Datasource: " + ds[i] + " was not found.");
 
618
    }
 
619
  }
 
620
  return compositeDS;
 
621
}
 
622
 
 
623
function getAttribute(datasource, resource, attributeResourceName, defaultValue) {
 
624
  var literal = datasource.GetTarget(resource, attributeResourceName, true);
 
625
  if (!literal)
 
626
    return defaultValue;
 
627
  return getLiteralValue(literal, defaultValue);  
 
628
}
 
629
 
 
630
function getLiteralValue(literal, defaultValue) {
 
631
  if (literal) {
 
632
      literal = literal.QueryInterface(Components.interfaces.nsIRDFLiteral);
 
633
      if (literal)
 
634
        return literal.Value;
 
635
  }
 
636
  if (defaultValue)
 
637
    return defaultValue;
 
638
  return null;
 
639
}
 
640
// Write debug string to javascript console.
 
641
function log(aText) {
 
642
  CONSOLE_SERVICE.logStringMessage(aText);
 
643
}
 
644
 
 
645
 
 
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;
 
653
  while (i--)
 
654
    if (!treeview.getLevel(i) && !treeview.isContainerOpen(i))
 
655
      treeview.toggleOpenState(i);
 
656
}
 
657
 
 
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);
 
671
    }
 
672
  }
 
673
  if (selectedIndex == -1)
 
674
    return;
 
675
  selectedIndex += goForward ? 1 : -1;
 
676
  if (selectedIndex >= sidebarButtons.length)
 
677
    selectedIndex = 0;
 
678
  else if (selectedIndex < 0)
 
679
    selectedIndex = sidebarButtons.length - 1;
 
680
  sidebarButtons[selectedIndex].doCommand();
 
681
}