|
1 /** |
|
2 * bbCode control by subBlue design [ www.subBlue.com ] |
|
3 * Includes unixsafe colour palette selector by SHS` |
|
4 */ |
|
5 |
|
6 // Startup variables |
|
7 var imageTag = false; |
|
8 var theSelection = false; |
|
9 |
|
10 // Check for Browser & Platform for PC & IE specific bits |
|
11 // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html |
|
12 var clientPC = navigator.userAgent.toLowerCase(); // Get client info |
|
13 var clientVer = parseInt(navigator.appVersion); // Get browser version |
|
14 |
|
15 var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1)); |
|
16 var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1)); |
|
17 |
|
18 var baseHeight; |
|
19 |
|
20 /** |
|
21 * Shows the help messages in the helpline window |
|
22 */ |
|
23 function helpline(help) |
|
24 { |
|
25 document.forms[form_name].helpbox.value = help_line[help]; |
|
26 } |
|
27 |
|
28 /** |
|
29 * Fix a bug involving the TextRange object. From |
|
30 * http://www.frostjedi.com/terra/scripts/demo/caretBug.html |
|
31 */ |
|
32 function initInsertions() |
|
33 { |
|
34 var doc; |
|
35 if(document.forms[form_name]) |
|
36 { |
|
37 doc = document; |
|
38 } |
|
39 else |
|
40 { |
|
41 doc = opener.document; |
|
42 } |
|
43 |
|
44 var textarea = doc.forms[form_name].elements[text_name]; |
|
45 if (is_ie && typeof(baseHeight) != 'number') |
|
46 { |
|
47 textarea.focus(); |
|
48 baseHeight = doc.selection.createRange().duplicate().boundingHeight; |
|
49 |
|
50 if (!document.forms[form_name]) |
|
51 { |
|
52 document.body.focus(); |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 /** |
|
58 * bbstyle |
|
59 */ |
|
60 function bbstyle(bbnumber) |
|
61 { |
|
62 if (bbnumber != -1) |
|
63 { |
|
64 bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]); |
|
65 } |
|
66 else |
|
67 { |
|
68 insert_text('[*]'); |
|
69 document.forms[form_name].elements[text_name].focus(); |
|
70 } |
|
71 } |
|
72 |
|
73 /** |
|
74 * Apply bbcodes |
|
75 */ |
|
76 function bbfontstyle(bbopen, bbclose) |
|
77 { |
|
78 theSelection = false; |
|
79 |
|
80 var textarea = document.forms[form_name].elements[text_name]; |
|
81 |
|
82 textarea.focus(); |
|
83 |
|
84 if ((clientVer >= 4) && is_ie && is_win) |
|
85 { |
|
86 // Get text selection |
|
87 theSelection = document.selection.createRange().text; |
|
88 |
|
89 if (theSelection) |
|
90 { |
|
91 // Add tags around selection |
|
92 document.selection.createRange().text = bbopen + theSelection + bbclose; |
|
93 document.forms[form_name].elements[text_name].focus(); |
|
94 theSelection = ''; |
|
95 return; |
|
96 } |
|
97 } |
|
98 else if (document.forms[form_name].elements[text_name].selectionEnd && (document.forms[form_name].elements[text_name].selectionEnd - document.forms[form_name].elements[text_name].selectionStart > 0)) |
|
99 { |
|
100 mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose); |
|
101 document.forms[form_name].elements[text_name].focus(); |
|
102 theSelection = ''; |
|
103 return; |
|
104 } |
|
105 |
|
106 //The new position for the cursor after adding the bbcode |
|
107 var caret_pos = getCaretPosition(textarea).start; |
|
108 var new_pos = caret_pos + bbopen.length; |
|
109 |
|
110 // Open tag |
|
111 insert_text(bbopen + bbclose); |
|
112 |
|
113 // Center the cursor when we don't have a selection |
|
114 // Gecko and proper browsers |
|
115 if (!isNaN(textarea.selectionStart)) |
|
116 { |
|
117 textarea.selectionStart = new_pos; |
|
118 textarea.selectionEnd = new_pos; |
|
119 } |
|
120 // IE |
|
121 else if (document.selection) |
|
122 { |
|
123 var range = textarea.createTextRange(); |
|
124 range.move("character", new_pos); |
|
125 range.select(); |
|
126 storeCaret(textarea); |
|
127 } |
|
128 |
|
129 textarea.focus(); |
|
130 return; |
|
131 } |
|
132 |
|
133 /** |
|
134 * Insert text at position |
|
135 */ |
|
136 function insert_text(text, spaces, popup) |
|
137 { |
|
138 var textarea; |
|
139 |
|
140 if (!popup) |
|
141 { |
|
142 textarea = document.forms[form_name].elements[text_name]; |
|
143 } |
|
144 else |
|
145 { |
|
146 textarea = opener.document.forms[form_name].elements[text_name]; |
|
147 } |
|
148 |
|
149 if (spaces) |
|
150 { |
|
151 text = ' ' + text + ' '; |
|
152 } |
|
153 |
|
154 if (!isNaN(textarea.selectionStart)) |
|
155 { |
|
156 var sel_start = textarea.selectionStart; |
|
157 var sel_end = textarea.selectionEnd; |
|
158 |
|
159 mozWrap(textarea, text, ''); |
|
160 textarea.selectionStart = sel_start + text.length; |
|
161 textarea.selectionEnd = sel_end + text.length; |
|
162 } |
|
163 |
|
164 else if (textarea.createTextRange && textarea.caretPos) |
|
165 { |
|
166 if (baseHeight != textarea.caretPos.boundingHeight) |
|
167 { |
|
168 textarea.focus(); |
|
169 storeCaret(textarea); |
|
170 } |
|
171 var caret_pos = textarea.caretPos; |
|
172 caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text; |
|
173 |
|
174 } |
|
175 else |
|
176 { |
|
177 textarea.value = textarea.value + text; |
|
178 } |
|
179 |
|
180 if (!popup) |
|
181 { |
|
182 textarea.focus(); |
|
183 } |
|
184 |
|
185 } |
|
186 |
|
187 /** |
|
188 * Add inline attachment at position |
|
189 */ |
|
190 function attach_inline(index, filename) |
|
191 { |
|
192 insert_text('[attachment=' + index + ']' + filename + '[/attachment]'); |
|
193 document.forms[form_name].elements[text_name].focus(); |
|
194 } |
|
195 |
|
196 /** |
|
197 * Add quote text to message |
|
198 */ |
|
199 function addquote(post_id, username) |
|
200 { |
|
201 var message_name = 'message_' + post_id; |
|
202 var theSelection = ''; |
|
203 var divarea = false; |
|
204 |
|
205 if (document.all) |
|
206 { |
|
207 divarea = document.all[message_name]; |
|
208 } |
|
209 else |
|
210 { |
|
211 divarea = document.getElementById(message_name); |
|
212 } |
|
213 |
|
214 // Get text selection - not only the post content :( |
|
215 if (window.getSelection) |
|
216 { |
|
217 theSelection = window.getSelection().toString(); |
|
218 } |
|
219 else if (document.getSelection) |
|
220 { |
|
221 theSelection = document.getSelection(); |
|
222 } |
|
223 else if (document.selection) |
|
224 { |
|
225 theSelection = document.selection.createRange().text; |
|
226 } |
|
227 |
|
228 if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null) |
|
229 { |
|
230 if (divarea.innerHTML) |
|
231 { |
|
232 theSelection = divarea.innerHTML.replace(/<br>/ig, '\n'); |
|
233 theSelection = theSelection.replace(/<br\/>/ig, '\n'); |
|
234 theSelection = theSelection.replace(/<\;/ig, '<'); |
|
235 theSelection = theSelection.replace(/>\;/ig, '>'); |
|
236 theSelection = theSelection.replace(/&\;/ig, '&'); |
|
237 theSelection = theSelection.replace(/ \;/ig, ' '); |
|
238 } |
|
239 else if (document.all) |
|
240 { |
|
241 theSelection = divarea.innerText; |
|
242 } |
|
243 else if (divarea.textContent) |
|
244 { |
|
245 theSelection = divarea.textContent; |
|
246 } |
|
247 else if (divarea.firstChild.nodeValue) |
|
248 { |
|
249 theSelection = divarea.firstChild.nodeValue; |
|
250 } |
|
251 } |
|
252 |
|
253 if (theSelection) |
|
254 { |
|
255 insert_text('[quote="' + username + '"]' + theSelection + '[/quote]'); |
|
256 } |
|
257 |
|
258 return; |
|
259 } |
|
260 |
|
261 /** |
|
262 * From http://www.massless.org/mozedit/ |
|
263 */ |
|
264 function mozWrap(txtarea, open, close) |
|
265 { |
|
266 var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength; |
|
267 var selStart = txtarea.selectionStart; |
|
268 var selEnd = txtarea.selectionEnd; |
|
269 var scrollTop = txtarea.scrollTop; |
|
270 |
|
271 if (selEnd == 1 || selEnd == 2) |
|
272 { |
|
273 selEnd = selLength; |
|
274 } |
|
275 |
|
276 var s1 = (txtarea.value).substring(0,selStart); |
|
277 var s2 = (txtarea.value).substring(selStart, selEnd); |
|
278 var s3 = (txtarea.value).substring(selEnd, selLength); |
|
279 |
|
280 txtarea.value = s1 + open + s2 + close + s3; |
|
281 txtarea.selectionStart = selStart + open.length; |
|
282 txtarea.selectionEnd = selEnd + open.length; |
|
283 txtarea.focus(); |
|
284 txtarea.scrollTop = scrollTop; |
|
285 |
|
286 return; |
|
287 } |
|
288 |
|
289 /** |
|
290 * Insert at Caret position. Code from |
|
291 * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130 |
|
292 */ |
|
293 function storeCaret(textEl) |
|
294 { |
|
295 if (textEl.createTextRange) |
|
296 { |
|
297 textEl.caretPos = document.selection.createRange().duplicate(); |
|
298 } |
|
299 } |
|
300 |
|
301 /** |
|
302 * Color pallette |
|
303 */ |
|
304 function colorPalette(dir, width, height) |
|
305 { |
|
306 var r = 0, g = 0, b = 0; |
|
307 var numberList = new Array(6); |
|
308 var color = ''; |
|
309 |
|
310 numberList[0] = '00'; |
|
311 numberList[1] = '40'; |
|
312 numberList[2] = '80'; |
|
313 numberList[3] = 'BF'; |
|
314 numberList[4] = 'FF'; |
|
315 |
|
316 document.writeln('<table class="type2">'); |
|
317 |
|
318 for (r = 0; r < 5; r++) |
|
319 { |
|
320 if (dir == 'h') |
|
321 { |
|
322 document.writeln('<tr>'); |
|
323 } |
|
324 |
|
325 for (g = 0; g < 5; g++) |
|
326 { |
|
327 if (dir == 'v') |
|
328 { |
|
329 document.writeln('<tr>'); |
|
330 } |
|
331 |
|
332 for (b = 0; b < 5; b++) |
|
333 { |
|
334 color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]); |
|
335 document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">'); |
|
336 document.write('<a href="#" onclick="bbfontstyle(\'[color=#' + color + ']\', \'[/color]\'); return false;"><img src="images/spacer.gif" width="' + width + '" height="' + height + '" alt="#' + color + '" title="#' + color + '" /></a>'); |
|
337 document.writeln('</td>'); |
|
338 } |
|
339 |
|
340 if (dir == 'v') |
|
341 { |
|
342 document.writeln('</tr>'); |
|
343 } |
|
344 } |
|
345 |
|
346 if (dir == 'h') |
|
347 { |
|
348 document.writeln('</tr>'); |
|
349 } |
|
350 } |
|
351 document.writeln('</table>'); |
|
352 } |
|
353 |
|
354 |
|
355 /** |
|
356 * Caret Position object |
|
357 */ |
|
358 function caretPosition() |
|
359 { |
|
360 var start = null; |
|
361 var end = null; |
|
362 } |
|
363 |
|
364 |
|
365 /** |
|
366 * Get the caret position in an textarea |
|
367 */ |
|
368 function getCaretPosition(txtarea) |
|
369 { |
|
370 var caretPos = new caretPosition(); |
|
371 |
|
372 // simple Gecko/Opera way |
|
373 if (txtarea.selectionStart || txtarea.selectionStart == 0) |
|
374 { |
|
375 caretPos.start = txtarea.selectionStart; |
|
376 caretPos.end = txtarea.selectionEnd; |
|
377 } |
|
378 // dirty and slow IE way |
|
379 else if (document.selection) |
|
380 { |
|
381 // get current selection |
|
382 var range = document.selection.createRange(); |
|
383 |
|
384 // a new selection of the whole textarea |
|
385 var range_all = document.body.createTextRange(); |
|
386 range_all.moveToElementText(txtarea); |
|
387 |
|
388 // calculate selection start point by moving beginning of range_all to beginning of range |
|
389 var sel_start; |
|
390 for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++) |
|
391 { |
|
392 range_all.moveStart('character', 1); |
|
393 } |
|
394 |
|
395 txtarea.sel_start = sel_start; |
|
396 |
|
397 // we ignore the end value for IE, this is already dirty enough and we don't need it |
|
398 caretPos.start = txtarea.sel_start; |
|
399 caretPos.end = txtarea.sel_start; |
|
400 } |
|
401 |
|
402 return caretPos; |
|
403 } |