styles/cubometry-style/template/editor.js
changeset 16 d6012dd9347f
parent 10 2829504a06be
equal deleted inserted replaced
15:8804e4d3de74 16:d6012dd9347f
     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 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
       
    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 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 
       
    36 	if (document.forms[form_name])
       
    37 	{
       
    38 		doc = document;
       
    39 	}
       
    40 	else 
       
    41 	{
       
    42 		doc = opener.document;
       
    43 	}
       
    44 
       
    45 	var textarea = doc.forms[form_name].elements[text_name];
       
    46 
       
    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 	else if (textarea.createTextRange && textarea.caretPos)
       
   167 	{
       
   168 		if (baseHeight != textarea.caretPos.boundingHeight) 
       
   169 		{
       
   170 			textarea.focus();
       
   171 			storeCaret(textarea);
       
   172 		}
       
   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 	else
       
   178 	{
       
   179 		textarea.value = textarea.value + text;
       
   180 	}
       
   181 	if (!popup) 
       
   182 	{
       
   183 		textarea.focus();
       
   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, l_wrote)
       
   200 {
       
   201 	var message_name = 'message_' + post_id;
       
   202 	var theSelection = '';
       
   203 	var divarea = false;
       
   204 
       
   205 	if (l_wrote === undefined)
       
   206 	{
       
   207 		// Backwards compatibility
       
   208 		l_wrote = 'wrote';
       
   209 	}
       
   210 
       
   211 	if (document.all)
       
   212 	{
       
   213 		divarea = document.all[message_name];
       
   214 	}
       
   215 	else
       
   216 	{
       
   217 		divarea = document.getElementById(message_name);
       
   218 	}
       
   219 
       
   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)
       
   223 	{
       
   224 		theSelection = window.getSelection().toString();
       
   225 	}
       
   226 	else if (document.getSelection && !is_ie)
       
   227 	{
       
   228 		theSelection = document.getSelection();
       
   229 	}
       
   230 	else if (document.selection)
       
   231 	{
       
   232 		theSelection = document.selection.createRange().text;
       
   233 	}
       
   234 
       
   235 	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
       
   236 	{
       
   237 		if (divarea.innerHTML)
       
   238 		{
       
   239 			theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
       
   240 			theSelection = theSelection.replace(/<br\/>/ig, '\n');
       
   241 			theSelection = theSelection.replace(/&lt\;/ig, '<');
       
   242 			theSelection = theSelection.replace(/&gt\;/ig, '>');
       
   243 			theSelection = theSelection.replace(/&amp\;/ig, '&');
       
   244 			theSelection = theSelection.replace(/&nbsp\;/ig, ' ');
       
   245 		}
       
   246 		else if (document.all)
       
   247 		{
       
   248 			theSelection = divarea.innerText;
       
   249 		}
       
   250 		else if (divarea.textContent)
       
   251 		{
       
   252 			theSelection = divarea.textContent;
       
   253 		}
       
   254 		else if (divarea.firstChild.nodeValue)
       
   255 		{
       
   256 			theSelection = divarea.firstChild.nodeValue;
       
   257 		}
       
   258 	}
       
   259 
       
   260 	if (theSelection)
       
   261 	{
       
   262 		if (bbcodeEnabled)
       
   263 		{
       
   264 			insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
       
   265 		}
       
   266 		else
       
   267 		{
       
   268 			insert_text(username + ' ' + l_wrote + ':' + '\n');
       
   269 			var lines = split_lines(theSelection);
       
   270 			for (i = 0; i < lines.length; i++)
       
   271 			{
       
   272 				insert_text('> ' + lines[i] + '\n');
       
   273 			}
       
   274 		}
       
   275 	}
       
   276 
       
   277 	return;
       
   278 }
       
   279 
       
   280 function split_lines(text)
       
   281 {
       
   282 	var lines = text.split('\n');
       
   283 	var splitLines = new Array();
       
   284 	var j = 0;
       
   285 	for(i = 0; i < lines.length; i++)
       
   286 	{
       
   287 		if (lines[i].length <= 80)
       
   288 		{
       
   289 			splitLines[j] = lines[i];
       
   290 			j++;
       
   291 		}
       
   292 		else
       
   293 		{
       
   294 			var line = lines[i];
       
   295 			do
       
   296 			{
       
   297 				var splitAt = line.indexOf(' ', 80);
       
   298 				
       
   299 				if (splitAt == -1)
       
   300 				{
       
   301 					splitLines[j] = line;
       
   302 					j++;
       
   303 				}
       
   304 				else
       
   305 				{
       
   306 					splitLines[j] = line.substring(0, splitAt);
       
   307 					line = line.substring(splitAt);
       
   308 					j++;
       
   309 				}
       
   310 			}
       
   311 			while(splitAt != -1);
       
   312 		}
       
   313 	}
       
   314 	return splitLines;
       
   315 }
       
   316 /**
       
   317 * From http://www.massless.org/mozedit/
       
   318 */
       
   319 function mozWrap(txtarea, open, close)
       
   320 {
       
   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;
       
   325 
       
   326 	if (selEnd == 1 || selEnd == 2) 
       
   327 	{
       
   328 		selEnd = selLength;
       
   329 	}
       
   330 
       
   331 	var s1 = (txtarea.value).substring(0,selStart);
       
   332 	var s2 = (txtarea.value).substring(selStart, selEnd);
       
   333 	var s3 = (txtarea.value).substring(selEnd, selLength);
       
   334 
       
   335 	txtarea.value = s1 + open + s2 + close + s3;
       
   336 	txtarea.selectionStart = selStart + open.length;
       
   337 	txtarea.selectionEnd = selEnd + open.length;
       
   338 	txtarea.focus();
       
   339 	txtarea.scrollTop = scrollTop;
       
   340 
       
   341 	return;
       
   342 }
       
   343 
       
   344 /**
       
   345 * Insert at Caret position. Code from
       
   346 * http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
       
   347 */
       
   348 function storeCaret(textEl)
       
   349 {
       
   350 	if (textEl.createTextRange)
       
   351 	{
       
   352 		textEl.caretPos = document.selection.createRange().duplicate();
       
   353 	}
       
   354 }
       
   355 
       
   356 /**
       
   357 * Color pallette
       
   358 */
       
   359 function colorPalette(dir, width, height)
       
   360 {
       
   361 	var r = 0, g = 0, b = 0;
       
   362 	var numberList = new Array(6);
       
   363 	var color = '';
       
   364 
       
   365 	numberList[0] = '00';
       
   366 	numberList[1] = '40';
       
   367 	numberList[2] = '80';
       
   368 	numberList[3] = 'BF';
       
   369 	numberList[4] = 'FF';
       
   370 
       
   371 	document.writeln('<table cellspacing="1" cellpadding="0" border="0">');
       
   372 
       
   373 	for (r = 0; r < 5; r++)
       
   374 	{
       
   375 		if (dir == 'h')
       
   376 		{
       
   377 			document.writeln('<tr>');
       
   378 		}
       
   379 
       
   380 		for (g = 0; g < 5; g++)
       
   381 		{
       
   382 			if (dir == 'v')
       
   383 			{
       
   384 				document.writeln('<tr>');
       
   385 			}
       
   386 			
       
   387 			for (b = 0; b < 5; b++)
       
   388 			{
       
   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>');
       
   393 			}
       
   394 
       
   395 			if (dir == 'v')
       
   396 			{
       
   397 				document.writeln('</tr>');
       
   398 			}
       
   399 		}
       
   400 
       
   401 		if (dir == 'h')
       
   402 		{
       
   403 			document.writeln('</tr>');
       
   404 		}
       
   405 	}
       
   406 	document.writeln('</table>');
       
   407 }
       
   408 
       
   409 
       
   410 /**
       
   411 * Caret Position object
       
   412 */
       
   413 function caretPosition()
       
   414 {
       
   415 	var start = null;
       
   416 	var end = null;
       
   417 }
       
   418 
       
   419 
       
   420 /**
       
   421 * Get the caret position in an textarea
       
   422 */
       
   423 function getCaretPosition(txtarea)
       
   424 {
       
   425 	var caretPos = new caretPosition();
       
   426 	
       
   427 	// simple Gecko/Opera way
       
   428 	if(txtarea.selectionStart || txtarea.selectionStart == 0)
       
   429 	{
       
   430 		caretPos.start = txtarea.selectionStart;
       
   431 		caretPos.end = txtarea.selectionEnd;
       
   432 	}
       
   433 	// dirty and slow IE way
       
   434 	else if(document.selection)
       
   435 	{
       
   436 	
       
   437 		// get current selection
       
   438 		var range = document.selection.createRange();
       
   439 
       
   440 		// a new selection of the whole textarea
       
   441 		var range_all = document.body.createTextRange();
       
   442 		range_all.moveToElementText(txtarea);
       
   443 		
       
   444 		// calculate selection start point by moving beginning of range_all to beginning of range
       
   445 		var sel_start;
       
   446 		for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
       
   447 		{		
       
   448 			range_all.moveStart('character', 1);
       
   449 		}
       
   450 	
       
   451 		txtarea.sel_start = sel_start;
       
   452 	
       
   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;			
       
   456 	}
       
   457 
       
   458 	return caretPos;
       
   459 }