39
|
1 // Used for dynamically updating the height of a textarea
|
|
2 function resizeTextArea(id, rows) {
|
|
3 var textarea = document.getElementById(id);
|
|
4 if (!textarea || (typeof(textarea.rows) == "undefined")) return;
|
|
5 textarea.rows = rows;
|
|
6 }
|
|
7
|
|
8 // A better way than for example hardcoding foo.onload
|
|
9 function addEvent(element, type, func){
|
|
10 if (element.addEventListener) {
|
|
11 element.addEventListener(type, func, false);
|
|
12 return true;
|
|
13 } else if (element.attachEvent) {
|
|
14 return element.attachEvent("on" + type, func);
|
|
15 }
|
|
16 return false;
|
|
17 }
|
|
18
|
|
19 // Convenience function for the nearest ancestor element with a specific tag
|
|
20 // name
|
|
21 function getAncestorByTagName(e, tagName) {
|
|
22 tagName = tagName.toLowerCase();
|
|
23 do {
|
|
24 e = e.parentNode;
|
|
25 } while ((e.nodeType == 1) && (e.tagName.toLowerCase() != tagName));
|
|
26 return (e.nodeType == 1) ? e : null;
|
|
27 }
|
|
28
|
|
29 // Adapted from http://www.kryogenix.org/code/browser/searchhi/
|
|
30 function searchHighlight() {
|
|
31 if (!document.createElement) return;
|
|
32
|
|
33 var div = document.getElementById("searchable");
|
|
34 if (!div) return;
|
|
35
|
|
36 function getSearchWords(url) {
|
|
37 if (url.indexOf('?') == -1) return [];
|
|
38 var queryString = url.substr(url.indexOf('?') + 1);
|
|
39 var params = queryString.split('&');
|
|
40 for (var p in params) {
|
|
41 var param = params[p].split('=');
|
|
42 if (param.length < 2) continue;
|
|
43 if (param[0] == 'q' || param[0] == 'p') { // q= for Google, p= for Yahoo
|
|
44 var query = unescape(param[1].replace(/\+/g, ' '));
|
|
45 if (query[0] == '!') query = query.slice(1);
|
|
46 words = query.split(/(".*?")|('.*?')|(\s+)/);
|
|
47 var words2 = new Array();
|
|
48 for (var w in words) {
|
|
49 words[w] = words[w].replace(/^\s+$/, '');
|
|
50 if (words[w] != '') {
|
|
51 words2.push(words[w].replace(/^['"]/, '').replace(/['"]$/, ''));
|
|
52 }
|
|
53 }
|
|
54 return words2;
|
|
55 }
|
|
56 }
|
|
57 return [];
|
|
58 }
|
|
59
|
|
60 function highlightWord(node, word, searchwordindex) {
|
|
61 // If this node is a text node and contains the search word, highlight it by
|
|
62 // surrounding it with a span element
|
|
63 if (node.nodeType == 3) { // Node.TEXT_NODE
|
|
64 var pos = node.nodeValue.toLowerCase().indexOf(word.toLowerCase());
|
|
65 if (pos >= 0 && !/^searchword\d$/.test(node.parentNode.className)) {
|
|
66 var span = document.createElement("span");
|
|
67 span.className = "searchword" + (searchwordindex % 5);
|
|
68 span.appendChild(document.createTextNode(
|
|
69 node.nodeValue.substr(pos, word.length)));
|
|
70 node.parentNode.insertBefore(span, node.parentNode.insertBefore(
|
|
71 document.createTextNode(node.nodeValue.substr(pos + word.length)),
|
|
72 node.nextSibling));
|
|
73 node.nodeValue = node.nodeValue.substr(0, pos);
|
|
74 return true;
|
|
75 }
|
|
76 } else if (!node.nodeName.match(/button|select|textarea/i)) {
|
|
77 // Recurse into child nodes
|
|
78 for (var i = 0; i < node.childNodes.length; i++) {
|
|
79 if (highlightWord(node.childNodes[i], word, searchwordindex)) i++;
|
|
80 }
|
|
81 }
|
|
82 return false;
|
|
83 }
|
|
84
|
|
85 var words = getSearchWords(document.URL);
|
|
86 if (!words.length) words = getSearchWords(document.referrer);
|
|
87 if (words.length) {
|
|
88 for (var w in words) {
|
|
89 if (words[w].length) highlightWord(div, words[w], w);
|
|
90 }
|
|
91 }
|
|
92 }
|
|
93
|
|
94 function enableControl(id, enabled) {
|
|
95 if (typeof(enabled) == "undefined") enabled = true;
|
|
96 var control = document.getElementById(id);
|
|
97 if (!control) return;
|
|
98 control.disabled = !enabled;
|
|
99 var label = getAncestorByTagName(control, "label");
|
|
100 if (label) {
|
|
101 label.className = enabled ? "enabled" : "disabled";
|
|
102 } else {
|
|
103 var labels = document.getElementsByTagName("label");
|
|
104 for (var i = 0; i < labels.length; i++) {
|
|
105 if (labels[i].htmlFor == id) {
|
|
106 labels[i].className = enabled ? "enabled" : "disabled";
|
|
107 break;
|
|
108 }
|
|
109 }
|
|
110 }
|
|
111 }
|
|
112
|
|
113 function addHeadingLinks(container) {
|
|
114 var base = document.location.pathname;
|
|
115 function addLinks(elems) {
|
|
116 for (var i = 0; i < elems.length; i++) {
|
|
117 var hn = elems[i];
|
|
118 if (hn.id) {
|
|
119 var link = document.createElement('a');
|
|
120 link.href = base + '#' + hn.id;
|
|
121 link.className = 'anchor';
|
|
122 link.title = "Link to this section";
|
|
123 link.appendChild(document.createTextNode(" \u00B6"));
|
|
124 hn.appendChild(link);
|
|
125 }
|
|
126 }
|
|
127 }
|
|
128 for (var lvl = 0; lvl <= 6; lvl++) {
|
|
129 addLinks(container.getElementsByTagName('h' + lvl));
|
|
130 }
|
|
131 }
|