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 | // Adapted from http://www.kryogenix.org/code/browser/searchhi/ |
---|
20 | function searchHighlight() { |
---|
21 | if (!document.createElement) return; |
---|
22 | |
---|
23 | var div = document.getElementById("searchable"); |
---|
24 | if (!div) return; |
---|
25 | |
---|
26 | function getSearchWords(url) { |
---|
27 | if (url.indexOf('?') == -1) return []; |
---|
28 | var queryString = url.substr(url.indexOf('?') + 1); |
---|
29 | var params = queryString.split('&'); |
---|
30 | for (var p in params) { |
---|
31 | var param = params[p].split('='); |
---|
32 | if (param.length < 2) continue; |
---|
33 | if (param[0] == 'q' || param[0] == 'p') { // q= for Google, p= for Yahoo |
---|
34 | return unescape(param[1].replace(/\+/g, ' ')).split(/\s+/); |
---|
35 | } |
---|
36 | } |
---|
37 | return []; |
---|
38 | } |
---|
39 | |
---|
40 | function highlightWord(node, word, searchwordindex) { |
---|
41 | // If this node is a text node and contains the search word, highlight it by |
---|
42 | // surrounding it with a span element |
---|
43 | if (node.nodeType == 3) { // Node.TEXT_NODE |
---|
44 | var pos = node.nodeValue.toLowerCase().indexOf(word.toLowerCase()); |
---|
45 | if (pos >= 0 && !/^searchword\d$/.test(node.parentNode.className)) { |
---|
46 | var span = document.createElement("span"); |
---|
47 | span.className = "searchword" + (searchwordindex % 5); |
---|
48 | span.appendChild(document.createTextNode( |
---|
49 | node.nodeValue.substr(pos, word.length))); |
---|
50 | var newNode = node.splitText(pos); |
---|
51 | newNode.nodeValue = newNode.nodeValue.substr(word.length); |
---|
52 | node.parentNode.insertBefore(span, newNode); |
---|
53 | return true; |
---|
54 | } |
---|
55 | } else if (!node.nodeName.match(/button|select|textarea/i)) { |
---|
56 | // Recurse into child nodes |
---|
57 | for (var i = 0; i < node.childNodes.length; i++) { |
---|
58 | if (highlightWord(node.childNodes[i], word, searchwordindex)) i++; |
---|
59 | } |
---|
60 | } |
---|
61 | return false; |
---|
62 | } |
---|
63 | |
---|
64 | var words = getSearchWords(document.URL); |
---|
65 | if (!words.length) words = getSearchWords(document.referrer); |
---|
66 | if (words.length) { |
---|
67 | for (var w in words) { |
---|
68 | if (words[w].length) highlightWord(div, words[w], w); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | function enableControl(id, enabled) { |
---|
74 | if (typeof(enabled) == "undefined") enabled = true; |
---|
75 | var control = document.getElementById(id); |
---|
76 | if (!control) return; |
---|
77 | control.disabled = !enabled; |
---|
78 | var labels = document.getElementsByTagName("label"); |
---|
79 | for (var i = 0; i < labels.length; i++) { |
---|
80 | if (labels[i].htmlFor == id) { |
---|
81 | labels[i].className = enabled ? "enabled" : "disabled"; |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | function addWikiFormattingToolbar(textarea) { |
---|
87 | if ((typeof(document["selection"]) == "undefined") |
---|
88 | && (typeof(textarea["setSelectionRange"]) == "undefined")) { |
---|
89 | return; |
---|
90 | } |
---|
91 | |
---|
92 | var toolbar = document.createElement("div"); |
---|
93 | toolbar.className = "wikitoolbar"; |
---|
94 | |
---|
95 | function addButton(id, title, fn) { |
---|
96 | var a = document.createElement("a"); |
---|
97 | a.href = "#"; |
---|
98 | a.id = id; |
---|
99 | a.title = title; |
---|
100 | a.onclick = function() { try { fn() } catch (e) { } return false }; |
---|
101 | a.tabIndex = 400; |
---|
102 | toolbar.appendChild(a); |
---|
103 | } |
---|
104 | |
---|
105 | function encloseSelection(prefix, suffix) { |
---|
106 | textarea.focus(); |
---|
107 | var start, end, sel, scrollPos, subst; |
---|
108 | if (typeof(document["selection"]) != "undefined") { |
---|
109 | sel = document.selection.createRange().text; |
---|
110 | } else if (typeof(textarea["setSelectionRange"]) != "undefined") { |
---|
111 | start = textarea.selectionStart; |
---|
112 | end = textarea.selectionEnd; |
---|
113 | scrollPos = textarea.scrollTop; |
---|
114 | sel = textarea.value.substring(start, end); |
---|
115 | } |
---|
116 | if (sel.match(/ $/)) { // exclude ending space char, if any |
---|
117 | sel = sel.substring(0, sel.length - 1); |
---|
118 | suffix = suffix + " "; |
---|
119 | } |
---|
120 | subst = prefix + sel + suffix; |
---|
121 | if (typeof(document["selection"]) != "undefined") { |
---|
122 | var range = document.selection.createRange().text = subst; |
---|
123 | textarea.caretPos -= suffix.length; |
---|
124 | } else if (typeof(textarea["setSelectionRange"]) != "undefined") { |
---|
125 | textarea.value = textarea.value.substring(0, start) + subst + |
---|
126 | textarea.value.substring(end); |
---|
127 | if (sel) { |
---|
128 | textarea.setSelectionRange(start + subst.length, start + subst.length); |
---|
129 | } else { |
---|
130 | textarea.setSelectionRange(start + prefix.length, start + prefix.length); |
---|
131 | } |
---|
132 | textarea.scrollTop = scrollPos; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | addButton("strong", "Bold text: '''Example'''", function() { |
---|
137 | encloseSelection("'''", "'''"); |
---|
138 | }); |
---|
139 | addButton("em", "Italic text: ''Example''", function() { |
---|
140 | encloseSelection("''", "''"); |
---|
141 | }); |
---|
142 | addButton("heading", "Heading: == Example ==", function() { |
---|
143 | encloseSelection("\n== ", " ==\n", "Heading"); |
---|
144 | }); |
---|
145 | addButton("link", "Link: [http://www.example.com/ Example]", function() { |
---|
146 | encloseSelection("[", "]"); |
---|
147 | }); |
---|
148 | addButton("code", "Code block: {{{ example }}}", function() { |
---|
149 | encloseSelection("\n{{{\n", "\n}}}\n"); |
---|
150 | }); |
---|
151 | addButton("hr", "Horizontal rule: ----", function() { |
---|
152 | encloseSelection("\n----\n", ""); |
---|
153 | }); |
---|
154 | |
---|
155 | textarea.parentNode.insertBefore(toolbar, textarea); |
---|
156 | var br = document.createElement("br"); |
---|
157 | br.style.clear = "left"; |
---|
158 | textarea.parentNode.insertBefore(br, textarea); |
---|
159 | } |
---|