styles/subsilver2/template/editor.js
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:20:21 +0400
changeset 8 c9487b1e7ffc
parent 0 ceef6d64c21d
permissions -rw-r--r--
Removing install folder
     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 var bbcodeEnabled = true;
    10 
    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
    15 
    16 var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
    17 var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
    18 
    19 var baseHeight;
    20 
    21 /**
    22 * Shows the help messages in the helpline window
    23 */
    24 function helpline(help)
    25 {
    26 	document.forms[form_name].helpbox.value = help_line[help];
    27 }
    28 
    29 /**
    30 * Fix a bug involving the TextRange object. From
    31 * http://www.frostjedi.com/terra/scripts/demo/caretBug.html
    32 */ 
    33 function initInsertions() 
    34 {
    35 	var doc;
    36 
    37 	if (document.forms[form_name])
    38 	{
    39 		doc = document;
    40 	}
    41 	else 
    42 	{
    43 		doc = opener.document;
    44 	}
    45 
    46 	var textarea = doc.forms[form_name].elements[text_name];
    47 	if (is_ie && typeof(baseHeight) != 'number')
    48 	{	
    49 		textarea.focus();
    50 		baseHeight = doc.selection.createRange().duplicate().boundingHeight;
    51 
    52 		if (!document.forms[form_name])
    53 		{
    54 			document.body.focus();
    55 		}
    56 	}
    57 }
    58 
    59 /**
    60 * bbstyle
    61 */
    62 function bbstyle(bbnumber)
    63 {	
    64 	if (bbnumber != -1)
    65 	{
    66 		bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
    67 	} 
    68 	else 
    69 	{
    70 		insert_text('[*]');
    71 		document.forms[form_name].elements[text_name].focus();
    72 	}
    73 }
    74 
    75 /**
    76 * Apply bbcodes
    77 */
    78 function bbfontstyle(bbopen, bbclose)
    79 {
    80 	theSelection = false;
    81 		
    82 	var textarea = document.forms[form_name].elements[text_name];
    83 
    84 	textarea.focus();
    85 
    86 	if ((clientVer >= 4) && is_ie && is_win)
    87 	{
    88 		// Get text selection
    89 		theSelection = document.selection.createRange().text;
    90 
    91 		if (theSelection)
    92 		{
    93 			// Add tags around selection
    94 			document.selection.createRange().text = bbopen + theSelection + bbclose;
    95 			document.forms[form_name].elements[text_name].focus();
    96 			theSelection = '';
    97 			return;
    98 		}
    99 	}
   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))
   101 	{
   102 		mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
   103 		document.forms[form_name].elements[text_name].focus();
   104 		theSelection = '';
   105 		return;
   106 	}
   107 	
   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;
   111 
   112 	// Open tag
   113 	insert_text(bbopen + bbclose);
   114 
   115 	// Center the cursor when we don't have a selection
   116 	// Gecko and proper browsers
   117 	if (!isNaN(textarea.selectionStart))
   118 	{
   119 		textarea.selectionStart = new_pos;
   120 		textarea.selectionEnd = new_pos;
   121 	}	
   122 	// IE
   123 	else if (document.selection)
   124 	{
   125 		var range = textarea.createTextRange(); 
   126 		range.move("character", new_pos); 
   127 		range.select();
   128 		storeCaret(textarea);
   129 	}
   130 
   131 	textarea.focus();
   132 	return;
   133 }
   134 
   135 /**
   136 * Insert text at position
   137 */
   138 function insert_text(text, spaces, popup)
   139 {
   140 	var textarea;
   141 	
   142 	if (!popup) 
   143 	{
   144 		textarea = document.forms[form_name].elements[text_name];
   145 	} 
   146 	else 
   147 	{
   148 		textarea = opener.document.forms[form_name].elements[text_name];
   149 	}
   150 	if (spaces) 
   151 	{
   152 		text = ' ' + text + ' ';
   153 	}
   154 
   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)
   158 	{
   159 		var sel_start = textarea.selectionStart;
   160 		var sel_end = textarea.selectionEnd;
   161 
   162 		mozWrap(textarea, text, '');
   163 		textarea.selectionStart = sel_start + text.length;
   164 		textarea.selectionEnd = sel_end + text.length;
   165 	}	
   166 	
   167 	else if (textarea.createTextRange && textarea.caretPos)
   168 	{
   169 		if (baseHeight != textarea.caretPos.boundingHeight) 
   170 		{
   171 			textarea.focus();
   172 			storeCaret(textarea);
   173 		}		
   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;
   176 		
   177 	}
   178 	else
   179 	{
   180 		textarea.value = textarea.value + text;
   181 	}
   182 	if (!popup) 
   183 	{
   184 		textarea.focus();
   185 	} 	
   186 
   187 }
   188 
   189 /**
   190 * Add inline attachment at position
   191 */
   192 function attach_inline(index, filename)
   193 {
   194 	insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
   195 	document.forms[form_name].elements[text_name].focus();
   196 }
   197 
   198 /**
   199 * Add quote text to message
   200 */
   201 function addquote(post_id, username, l_wrote)
   202 {
   203 	var message_name = 'message_' + post_id;
   204 	var theSelection = '';
   205 	var divarea = false;
   206 
   207 	if (l_wrote === undefined)
   208 	{
   209 		// Backwards compatibility
   210 		l_wrote = 'wrote';
   211 	}
   212 
   213 	if (document.all)
   214 	{
   215 		divarea = document.all[message_name];
   216 	}
   217 	else
   218 	{
   219 		divarea = document.getElementById(message_name);
   220 	}
   221 
   222 	// Get text selection - not only the post content :(
   223 	// IE9 must use the document.selection method but has the *.getSelection so we just force no IE
   224 	if (window.getSelection && !is_ie)
   225 	{
   226 		theSelection = window.getSelection().toString();
   227 	}
   228 	else if (document.getSelection && !is_ie)
   229 	{
   230 		theSelection = document.getSelection();
   231 	}
   232 	else if (document.selection)
   233 	{
   234 		theSelection = document.selection.createRange().text;
   235 	}
   236 
   237 	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
   238 	{
   239 		if (divarea.innerHTML)
   240 		{
   241 			theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
   242 			theSelection = theSelection.replace(/<br\/>/ig, '\n');
   243 			theSelection = theSelection.replace(/&lt\;/ig, '<');
   244 			theSelection = theSelection.replace(/&gt\;/ig, '>');
   245 			theSelection = theSelection.replace(/&amp\;/ig, '&');
   246 			theSelection = theSelection.replace(/&nbsp\;/ig, ' ');
   247 		}
   248 		else if (document.all)
   249 		{
   250 			theSelection = divarea.innerText;
   251 		}
   252 		else if (divarea.textContent)
   253 		{
   254 			theSelection = divarea.textContent;
   255 		}
   256 		else if (divarea.firstChild.nodeValue)
   257 		{
   258 			theSelection = divarea.firstChild.nodeValue;
   259 		}
   260 	}
   261 
   262 	if (theSelection)
   263 	{
   264 		if (bbcodeEnabled)
   265 		{
   266 			insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
   267 		}
   268 		else
   269 		{
   270 			insert_text(username + ' ' + l_wrote + ':' + '\n');
   271 			var lines = split_lines(theSelection);
   272 			for (i = 0; i < lines.length; i++)
   273 			{
   274 				insert_text('> ' + lines[i] + '\n');
   275 			}
   276 		}
   277 	}
   278 
   279 	return;
   280 }
   281 
   282 
   283 function split_lines(text)
   284 {
   285 	var lines = text.split('\n');
   286 	var splitLines = new Array();
   287 	var j = 0;
   288 	for(i = 0; i < lines.length; i++)
   289 	{
   290 		if (lines[i].length <= 80)
   291 		{
   292 			splitLines[j] = lines[i];
   293 			j++;
   294 		}
   295 		else
   296 		{
   297 			var line = lines[i];
   298 			do
   299 			{
   300 				var splitAt = line.indexOf(' ', 80);
   301 				
   302 				if (splitAt == -1)
   303 				{
   304 					splitLines[j] = line;
   305 					j++;
   306 				}
   307 				else
   308 				{
   309 					splitLines[j] = line.substring(0, splitAt);
   310 					line = line.substring(splitAt);
   311 					j++;
   312 				}
   313 			}
   314 			while(splitAt != -1);
   315 		}
   316 	}
   317 	return splitLines;
   318 }
   319 
   320 /**
   321 * From http://www.massless.org/mozedit/
   322 */
   323 function mozWrap(txtarea, open, close)
   324 {
   325 	var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength;
   326 	var selStart = txtarea.selectionStart;
   327 	var selEnd = txtarea.selectionEnd;
   328 	var scrollTop = txtarea.scrollTop;
   329 
   330 	if (selEnd == 1 || selEnd == 2) 
   331 	{
   332 		selEnd = selLength;
   333 	}
   334 
   335 	var s1 = (txtarea.value).substring(0,selStart);
   336 	var s2 = (txtarea.value).substring(selStart, selEnd);
   337 	var s3 = (txtarea.value).substring(selEnd, selLength);
   338 
   339 	txtarea.value = s1 + open + s2 + close + s3;
   340 	txtarea.selectionStart = selStart + open.length;
   341 	txtarea.selectionEnd = selEnd + open.length;
   342 	txtarea.focus();
   343 	txtarea.scrollTop = scrollTop;
   344 
   345 	return;
   346 }
   347 
   348 /**
   349 * Insert at Caret position. Code from
   350 * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
   351 */
   352 function storeCaret(textEl)
   353 {
   354 	if (textEl.createTextRange)
   355 	{
   356 		textEl.caretPos = document.selection.createRange().duplicate();
   357 	}
   358 }
   359 
   360 /**
   361 * Color pallette
   362 */
   363 function colorPalette(dir, width, height)
   364 {
   365 	var r = 0, g = 0, b = 0;
   366 	var numberList = new Array(6);
   367 	var color = '';
   368 
   369 	numberList[0] = '00';
   370 	numberList[1] = '40';
   371 	numberList[2] = '80';
   372 	numberList[3] = 'BF';
   373 	numberList[4] = 'FF';
   374 
   375 	document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
   376 
   377 	for (r = 0; r < 5; r++)
   378 	{
   379 		if (dir == 'h')
   380 		{
   381 			document.writeln('<tr>');
   382 		}
   383 
   384 		for (g = 0; g < 5; g++)
   385 		{
   386 			if (dir == 'v')
   387 			{
   388 				document.writeln('<tr>');
   389 			}
   390 			
   391 			for (b = 0; b < 5; b++)
   392 			{
   393 				color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
   394 				document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
   395 				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>');
   396 				document.writeln('</td>');
   397 			}
   398 
   399 			if (dir == 'v')
   400 			{
   401 				document.writeln('</tr>');
   402 			}
   403 		}
   404 
   405 		if (dir == 'h')
   406 		{
   407 			document.writeln('</tr>');
   408 		}
   409 	}
   410 	document.writeln('</table>');
   411 }
   412 
   413 
   414 /**
   415 * Caret Position object
   416 */
   417 function caretPosition()
   418 {
   419 	var start = null;
   420 	var end = null;
   421 }
   422 
   423 
   424 /**
   425 * Get the caret position in an textarea
   426 */
   427 function getCaretPosition(txtarea)
   428 {
   429 	var caretPos = new caretPosition();
   430 	
   431 	// simple Gecko/Opera way
   432 	if(txtarea.selectionStart || txtarea.selectionStart == 0)
   433 	{
   434 		caretPos.start = txtarea.selectionStart;
   435 		caretPos.end = txtarea.selectionEnd;
   436 	}
   437 	// dirty and slow IE way
   438 	else if(document.selection)
   439 	{
   440 		// get current selection
   441 		var range = document.selection.createRange();
   442 
   443 		// a new selection of the whole textarea
   444 		var range_all = document.body.createTextRange();
   445 		range_all.moveToElementText(txtarea);
   446 		
   447 		// calculate selection start point by moving beginning of range_all to beginning of range
   448 		var sel_start;
   449 		for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
   450 		{		
   451 			range_all.moveStart('character', 1);
   452 		}
   453 	
   454 		txtarea.sel_start = sel_start;
   455 	
   456 		// we ignore the end value for IE, this is already dirty enough and we don't need it
   457 		caretPos.start = txtarea.sel_start;
   458 		caretPos.end = txtarea.sel_start;
   459 	}
   460 
   461 	return caretPos;
   462 }