2 * bbCode control by subBlue design [ www.subBlue.com ]
3 * Includes unixsafe colour palette selector by SHS`
8 var theSelection = false;
10 var bbcodeEnabled = true;
11 // Check for Browser & Platform for PC & IE specific bits
12 // More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
13 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
14 var clientVer = parseInt(navigator.appVersion); // Get browser version
16 var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
17 var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
21 * Shows the help messages in the helpline window
23 function helpline(help)
25 document.forms[form_name].helpbox.value = help_line[help];
29 * Fix a bug involving the TextRange object. From
30 * http://www.frostjedi.com/terra/scripts/demo/caretBug.html
32 function initInsertions()
36 if (document.forms[form_name])
42 doc = opener.document;
45 var textarea = doc.forms[form_name].elements[text_name];
47 if (is_ie && typeof(baseHeight) != 'number')
50 baseHeight = doc.selection.createRange().duplicate().boundingHeight;
52 if (!document.forms[form_name])
54 document.body.focus();
62 function bbstyle(bbnumber)
66 bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
71 document.forms[form_name].elements[text_name].focus();
78 function bbfontstyle(bbopen, bbclose)
82 var textarea = document.forms[form_name].elements[text_name];
86 if ((clientVer >= 4) && is_ie && is_win)
89 theSelection = document.selection.createRange().text;
93 // Add tags around selection
94 document.selection.createRange().text = bbopen + theSelection + bbclose;
95 document.forms[form_name].elements[text_name].focus();
100 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))
102 mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
103 document.forms[form_name].elements[text_name].focus();
108 //The new position for the cursor after adding the bbcode
109 var caret_pos = getCaretPosition(textarea).start;
110 var new_pos = caret_pos + bbopen.length;
113 insert_text(bbopen + bbclose);
115 // Center the cursor when we don't have a selection
116 // Gecko and proper browsers
117 if (!isNaN(textarea.selectionStart))
119 textarea.selectionStart = new_pos;
120 textarea.selectionEnd = new_pos;
123 else if (document.selection)
125 var range = textarea.createTextRange();
126 range.move("character", new_pos);
128 storeCaret(textarea);
136 * Insert text at position
138 function insert_text(text, spaces, popup)
144 textarea = document.forms[form_name].elements[text_name];
148 textarea = opener.document.forms[form_name].elements[text_name];
152 text = ' ' + text + ' ';
155 // Since IE9, IE also has textarea.selectionStart, but it still needs to be treated the old way.
156 // Therefore we simply add a !is_ie here until IE fixes the text-selection completely.
157 if (!isNaN(textarea.selectionStart) && !is_ie)
159 var sel_start = textarea.selectionStart;
160 var sel_end = textarea.selectionEnd;
162 mozWrap(textarea, text, '');
163 textarea.selectionStart = sel_start + text.length;
164 textarea.selectionEnd = sel_end + text.length;
166 else if (textarea.createTextRange && textarea.caretPos)
168 if (baseHeight != textarea.caretPos.boundingHeight)
171 storeCaret(textarea);
174 var caret_pos = textarea.caretPos;
175 caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
179 textarea.value = textarea.value + text;
188 * Add inline attachment at position
190 function attach_inline(index, filename)
192 insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
193 document.forms[form_name].elements[text_name].focus();
197 * Add quote text to message
199 function addquote(post_id, username, l_wrote)
201 var message_name = 'message_' + post_id;
202 var theSelection = '';
205 if (l_wrote === undefined)
207 // Backwards compatibility
213 divarea = document.all[message_name];
217 divarea = document.getElementById(message_name);
220 // Get text selection - not only the post content :(
221 // IE9 must use the document.selection method but has the *.getSelection so we just force no IE
222 if (window.getSelection && !is_ie)
224 theSelection = window.getSelection().toString();
226 else if (document.getSelection && !is_ie)
228 theSelection = document.getSelection();
230 else if (document.selection)
232 theSelection = document.selection.createRange().text;
235 if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
237 if (divarea.innerHTML)
239 theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
240 theSelection = theSelection.replace(/<br\/>/ig, '\n');
241 theSelection = theSelection.replace(/<\;/ig, '<');
242 theSelection = theSelection.replace(/>\;/ig, '>');
243 theSelection = theSelection.replace(/&\;/ig, '&');
244 theSelection = theSelection.replace(/ \;/ig, ' ');
246 else if (document.all)
248 theSelection = divarea.innerText;
250 else if (divarea.textContent)
252 theSelection = divarea.textContent;
254 else if (divarea.firstChild.nodeValue)
256 theSelection = divarea.firstChild.nodeValue;
264 insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
268 insert_text(username + ' ' + l_wrote + ':' + '\n');
269 var lines = split_lines(theSelection);
270 for (i = 0; i < lines.length; i++)
272 insert_text('> ' + lines[i] + '\n');
280 function split_lines(text)
282 var lines = text.split('\n');
283 var splitLines = new Array();
285 for(i = 0; i < lines.length; i++)
287 if (lines[i].length <= 80)
289 splitLines[j] = lines[i];
297 var splitAt = line.indexOf(' ', 80);
301 splitLines[j] = line;
306 splitLines[j] = line.substring(0, splitAt);
307 line = line.substring(splitAt);
311 while(splitAt != -1);
317 * From http://www.massless.org/mozedit/
319 function mozWrap(txtarea, open, close)
321 var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength;
322 var selStart = txtarea.selectionStart;
323 var selEnd = txtarea.selectionEnd;
324 var scrollTop = txtarea.scrollTop;
326 if (selEnd == 1 || selEnd == 2)
331 var s1 = (txtarea.value).substring(0,selStart);
332 var s2 = (txtarea.value).substring(selStart, selEnd);
333 var s3 = (txtarea.value).substring(selEnd, selLength);
335 txtarea.value = s1 + open + s2 + close + s3;
336 txtarea.selectionStart = selStart + open.length;
337 txtarea.selectionEnd = selEnd + open.length;
339 txtarea.scrollTop = scrollTop;
345 * Insert at Caret position. Code from
346 * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
348 function storeCaret(textEl)
350 if (textEl.createTextRange)
352 textEl.caretPos = document.selection.createRange().duplicate();
359 function colorPalette(dir, width, height)
361 var r = 0, g = 0, b = 0;
362 var numberList = new Array(6);
365 numberList[0] = '00';
366 numberList[1] = '40';
367 numberList[2] = '80';
368 numberList[3] = 'BF';
369 numberList[4] = 'FF';
371 document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
373 for (r = 0; r < 5; r++)
377 document.writeln('<tr>');
380 for (g = 0; g < 5; g++)
384 document.writeln('<tr>');
387 for (b = 0; b < 5; b++)
389 color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
390 document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
391 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>');
392 document.writeln('</td>');
397 document.writeln('</tr>');
403 document.writeln('</tr>');
406 document.writeln('</table>');
411 * Caret Position object
413 function caretPosition()
421 * Get the caret position in an textarea
423 function getCaretPosition(txtarea)
425 var caretPos = new caretPosition();
427 // simple Gecko/Opera way
428 if(txtarea.selectionStart || txtarea.selectionStart == 0)
430 caretPos.start = txtarea.selectionStart;
431 caretPos.end = txtarea.selectionEnd;
433 // dirty and slow IE way
434 else if(document.selection)
437 // get current selection
438 var range = document.selection.createRange();
440 // a new selection of the whole textarea
441 var range_all = document.body.createTextRange();
442 range_all.moveToElementText(txtarea);
444 // calculate selection start point by moving beginning of range_all to beginning of range
446 for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
448 range_all.moveStart('character', 1);
451 txtarea.sel_start = sel_start;
453 // we ignore the end value for IE, this is already dirty enough and we don't need it
454 caretPos.start = txtarea.sel_start;
455 caretPos.end = txtarea.sel_start;