File:  [mozdev] / calendarhelp / www / open.js
Revision 1.12: download - view: text, annotated - select for diffs - revision graph
Wed Jun 8 14:49:10 2005 UTC (13 years, 9 months ago) by whiteley
Branches: MAIN
CVS tags: HEAD
fix problem in new browser builds (c.f. Mozilla bug 221619)

/*********************************************************
 CalendarHelp source code
 Copyright (C) 2004, 2005 Rod Whiteley
 Licensed under the Open Software License version 2.1
 OSI Certified Open Source Software
 http://calendarhelp.mozdev.org
 *********************************************************/
// Calendar Help online viewer

// defaults
var lang = "en";
var locale = "en-US";
var panel = "help-toc";
var current_src = "welcome.xhtml#welcome";
var topic = "";
var product = "";

var once = false, map = false;

function init() {
	if (once) return; else once = true;

	var s = location.search;
	if (s) {
		if (s.charAt(0) == "?") s = s.substr(1);
		var ss = s.split("&");
		for (var i = 0; i < ss.length; ++i) {
			var a = "", b = "", m = "";
			var p = ss[i].indexOf("=");
			if (p > 0) a = ss[i].substr(0, p), b = ss[i].substr(p + 1);
			else a = ss[i], b = "";
			if (a == "topic" && b == "") {
				map = true;
				document.getElementById("topic-box").removeAttribute("hidden");
				}
			else if (a == "topic") topic = b;
			else if (a == "lang") lang = b;
			else if (a == "panel") panel = b;
			}
		}

	switch (lang) {
		case "de":
			locale = "de-DE";
			break;
		case "ja":
			locale = "ja-JP";
			break;
		default:
			locale = "en-US";
		}

	document.getElementById("locale-broadcaster").datasources = locale + "/open.rdf";
	document.getElementById("help-toc-panel").datasources = locale + "/toc.rdf";
	document.getElementById("help-index-panel").datasources = locale + "/index.rdf";
	document.getElementById("help-glossary-panel").datasources = locale + "/glossary.rdf";

	setTimeout(showLocale, 100);
	}


const TOC_COUNT = 18;
var localetry = 0;

function showLocale() {
	var toc = document.getElementById("help-toc-panel");
	if (!toc || !toc.view || !toc.view.rowCount || toc.view.rowCount < TOC_COUNT) {
		localetry++;
		if (localetry < 150) setTimeout(showLocale, 100);
		return;
		}

	var bs = document.getElementById("locale-broadcaster");
	var b = bs.firstChild.nextSibling;
	if (!b && localetry < 30) {

		}
	while (b) {
		var i = b.id.substr(4);	// remove urn:
		with (document.getElementById(i)) {
			if (localName == "label")
				setAttribute("value", b.getAttribute("label"));
			else 
				setAttribute("label", b.getAttribute("label"));
			setAttribute("accesskey", b.getAttribute("accesskey"));
			}
		b = b.nextSibling;
		}

	showPanel(panel);
	current_src = parseTopic(topic) || current_src;
	showContent(current_src);
	}


function parseTopic(s) {
	if (s == "") return null;

	var t = s.split(":");
	if (!t || !t[0]) return "";
	if (!t[1]) t.push(t[0]);

	var src = t[0] + ".xhtml" + "#" + t[1];
	if (verifyTopic(src)) return src;

	// temporary redirect from old chapters
	t[0] = (t[0] == "options")? "custom" : (t[0] == "using")? "basic" : "";
	if (t[0] != "") {
		src = t[0] + ".xhtml" + ((t[1])? "#" + t[1] : "");
		if (verifyTopic(src)) return src;
		}

	var m = document.getElementById("not-found");
	if (m) m = m.getAttribute("value");
	if (!m  || m.length == 0) m = "Incomplete load. Topic % not found";
	alert(m.replace(/%/, s));

	return null;
	}


function verifyTopic(src) {
	initSearch();

	var h = document.getElementById("hidden-toc");
	var l = h.getElementsByTagName("label");
	for (var i = 0; i < l.length; ++i)
		if (l[i].getAttribute("link") == src) return true;

	h = document.getElementById("hidden-glos");
	l = h.getElementsByTagName("label");
	for (var i = 0; i < l.length; ++i)
		if (l[i].getAttribute("link") == src) return true;

	h = document.getElementById("hidden-index");
	l = h.getElementsByTagName("label");
	for (var i = 0; i < l.length; ++i)
		if (l[i].getAttribute("link") == src) return true;

	return false;
	}


function showContent(src) {
	var t = src.match(/(\S+).xhtml#([^\?]+)/);
	if (map) document.getElementById("topic-text").value = (t[2] && t[2] != t[1])? t[1] + ":" + t[2] : t[1];
	var u = (product)? src.replace(/#/, "?product=" + product + "#") : src;
	document
		.getElementById("help-content")
		.setAttribute("src", locale + "/" + u);
	current_src = src;
	}


function goToTopic(e) {
	if (!e.value) return;
	var s = e.value.split(":");
	s = s[0] + ".xhtml#" + ((s[1])? s[1] : s[0]);
	showContent(s);
	}


function panelSelect(tree) {
	var l = null;
	try {
		l = tree.view.getCellText(tree.currentIndex, (tree.columns)? tree.columns["LinkColumn"] : "LinkColumn");
		}
	catch (ex) {}
	if (l) 	showContent(l);
	}


function showPanel(panelId) {
	var newPanel = document.getElementById(panelId + "-panel");
	if (!newPanel) return;

	var box = document.getElementById("helpsidebar-box");

	if (box.openPanel && box.openPanel != newPanel) with (box.openPanel) {
		setAttribute("hidden", "true");
		previousSibling.removeAttribute("selected");
		}

	with (newPanel) {
		removeAttribute("hidden");
		previousSibling.setAttribute("selected", "true");
		if (panelId == "help-search") initSearch();
		else newPanel.focus();
		}

	box.openPanel = newPanel;
	}



// search

function initSearch() {
	var h = document.getElementById("hidden-toc");
	if (h.datasources == "rdf:null") {
		h.datasources = locale + "/toc.rdf"
		document.getElementById("hidden-glos")
			.datasources = locale + "/glossary.rdf";
		document.getElementById("hidden-index")
			.datasources = locale + "/index.rdf";
		}
	document.getElementById("findText").focus();
	}

function doEnabling() {
	with (document.getElementById("findButton")) {
		var v = document.getElementById("findText").value;
		if (v.search(/\S/) == -1 || v.length < 2)
			setAttribute("disabled", "true");
		else
			removeAttribute("disabled");
		}
	}

function doFind() {
	var t = document.getElementById("findText");
	var s = t.value
		.replace(/^\s+/, "")
		.replace(/\s+$/, "")
		.replace(/\s{2,}/, " ")
		.replace(/[\\\^\$\+\*\?\[\]\(\)\{\}]/g, "");
	t.value = s;
	t.focus();
	if (s.length < 2) {
		document.getElementById("findButton").setAttribute("disabled", "true");
		return;
		}
	var re = new RegExp(s, "i");

	var rbox = document.getElementById("help-results-listbox");
	var c;
	while (c = rbox.firstChild) rbox.removeChild(c);
	var r = [];

	var h = document.getElementById("hidden-toc");
	var l = h.getElementsByTagName("label");
	for (var i = 0; i < l.length; ++i)
		if (re.test(l[i].value)) r.push({value:l[i].value, link:l[i].getAttribute("link")});

	h = document.getElementById("hidden-glos");
	var l = h.getElementsByTagName("label");
	for (var i = 0; i < l.length; ++i)
		if (re.test(l[i].value)) r.push({value:l[i].value + " (defined)", link:l[i].getAttribute("link")});

	h = document.getElementById("hidden-index");
	var l = h.getElementsByTagName("label");
	for (var i = 0; i < l.length; ++i) {
		if (re.test(l[i].value)) {
			var v = l[i].value;
			var k = l[i].getAttribute("link");
			// parent of hit
			var l0 = l[i].parentNode.parentNode.firstChild;
			if (l0 && l0 != l[i] && l0.localName == "label" && l0.value.length > 1)
				r.push({value:l0.value + "," + v, link:k});
			else if (k) r.push({value:v, link:k});
			// children of hit
			var l2 = l[i].nextSibling;
			while (l2) {
				r.push({value:v + "," + l2.firstChild.value, link:l2.firstChild.getAttribute("link")});
				l2 = l2.nextSibling;
				}
			}
		}

	r.sort(function(a, b) {
		var av = a.link, bv = b.link;
		if (av == bv) av = b.value.length, bv = a.value.length;
		return (av == bv)? 0 : (av > bv)? 1 : -1;		
		});

	var link = "";
	for (var i = 0; i < r.length; ++i) {
		l = r[i].link;
		if (l == link) r[i].link = null, r[i].value = "";
		else link = l;
		}

	r.sort(function(a, b) {
		var av = a.value.toLowerCase(), bv = b.value.toLowerCase();
		return (av == bv)? 0 : (av > bv)? 1 : -1;		
		});

	for (var i = 0; i < r.length; ++i) {
		if (r[i].link) with (rbox.appendChild(document.createElement("listitem"))) {
			setAttribute("label", r[i].value);
			setAttribute("link", r[i].link);
			}
		}

	if (rbox.firstChild) rbox.focus();
	else with (rbox.appendChild(document.createElement("listitem"))) {
		label = "- Nothing was found -";
		setAttribute("id", "nothing-found");
		}

	}

function resultSelect(listbox) {
	var e = listbox.selectedItem;
	if (!e) return;
	var l = e.getAttribute("link");
	if (l && l != "") showContent(l);
	}

function productSelect(e) {
	product = e.getAttribute("value");

	if (product == "sunbird" || product == "future") {
		document.getElementById("help-toc-panel").datasources = locale + "/tocs.rdf";
		document.getElementById("help-index-panel").datasources = locale + "/indexs.rdf";
		document.getElementById("hidden-toc").datasources = locale + "/tocs.rdf";
		document.getElementById("hidden-index").datasources = locale + "/indexs.rdf";
		}
	else {
		document.getElementById("help-toc-panel").datasources = locale + "/toc.rdf";
		document.getElementById("help-index-panel").datasources = locale + "/index.rdf";
		document.getElementById("hidden-toc").datasources = locale + "/toc.rdf";
		document.getElementById("hidden-index").datasources = locale + "/index.rdf";
		}

	with (document.getElementById("hidden-glos"))
		if (datasources == "rdf:null") datasources = locale + "/glossary.rdf";

	var rbox = document.getElementById("help-results-listbox");
	var c;
	while (c = rbox.firstChild) rbox.removeChild(c);

	document
		.getElementById("help-content")
		.removeAttribute("src");
	setTimeout(showContent, 0, current_src);
	}


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>