Blame Identity/Webenv/phpBB/3.0.4/adm/style/editor.js

ef5584
/**
ef5584
* bbCode control by subBlue design [ www.subBlue.com ]
ef5584
* Includes unixsafe colour palette selector by SHS`
ef5584
*/
ef5584
ef5584
// Startup variables
ef5584
var imageTag = false;
ef5584
var theSelection = false;
ef5584
ef5584
// Check for Browser & Platform for PC & IE specific bits
ef5584
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
ef5584
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
ef5584
var clientVer = parseInt(navigator.appVersion); // Get browser version
ef5584
ef5584
var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
ef5584
var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
ef5584
ef5584
var baseHeight;
ef5584
window.onload = initInsertions;
ef5584
ef5584
/**
ef5584
* Shows the help messages in the helpline window
ef5584
*/
ef5584
function helpline(help)
ef5584
{
ef5584
	document.forms[form_name].helpbox.value = help_line[help];
ef5584
}
ef5584
ef5584
/**
ef5584
* Fix a bug involving the TextRange object. From
ef5584
* http://www.frostjedi.com/terra/scripts/demo/caretBug.html
ef5584
*/ 
ef5584
function initInsertions() 
ef5584
{
ef5584
	var doc;
ef5584
	if(document.forms[form_name])
ef5584
	{
ef5584
		doc = document;
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		doc = opener.document;
ef5584
	}
ef5584
ef5584
	var textarea = doc.forms[form_name].elements[text_name];
ef5584
	if (is_ie && typeof(baseHeight) != 'number')
ef5584
	{
ef5584
		textarea.focus();
ef5584
		baseHeight = doc.selection.createRange().duplicate().boundingHeight;
ef5584
		// document.body.focus();
ef5584
	}
ef5584
}
ef5584
ef5584
/**
ef5584
* bbstyle
ef5584
*/
ef5584
function bbstyle(bbnumber)
ef5584
{
ef5584
	if (bbnumber != -1)
ef5584
	{
ef5584
		bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		insert_text('[*]');
ef5584
		document.forms[form_name].elements[text_name].focus();		
ef5584
	}
ef5584
}
ef5584
ef5584
/**
ef5584
* Apply bbcodes
ef5584
*/
ef5584
function bbfontstyle(bbopen, bbclose)
ef5584
{
ef5584
	theSelection = false;
ef5584
		
ef5584
	var textarea = document.forms[form_name].elements[text_name];
ef5584
ef5584
	textarea.focus();
ef5584
ef5584
	if ((clientVer >= 4) && is_ie && is_win)
ef5584
	{
ef5584
		// Get text selection
ef5584
		theSelection = document.selection.createRange().text;
ef5584
ef5584
		if (theSelection)
ef5584
		{
ef5584
			// Add tags around selection
ef5584
			document.selection.createRange().text = bbopen + theSelection + bbclose;
ef5584
			document.forms[form_name].elements[text_name].focus();
ef5584
			theSelection = '';
ef5584
			return;
ef5584
		}
ef5584
	}
ef5584
	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))
ef5584
	{
ef5584
		mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
ef5584
		document.forms[form_name].elements[text_name].focus();
ef5584
		theSelection = '';
ef5584
		return;
ef5584
	}
ef5584
	
ef5584
	//The new position for the cursor after adding the bbcode
ef5584
	var caret_pos = getCaretPosition(textarea).start;
ef5584
	var new_pos = caret_pos + bbopen.length;		
ef5584
ef5584
	// Open tag
ef5584
	insert_text(bbopen + bbclose);
ef5584
ef5584
	// Center the cursor when we don't have a selection
ef5584
	// Gecko and proper browsers
ef5584
	if (!isNaN(textarea.selectionStart))
ef5584
	{
ef5584
		textarea.selectionStart = new_pos;
ef5584
		textarea.selectionEnd = new_pos;
ef5584
	}	
ef5584
	// IE
ef5584
	else if (document.selection)
ef5584
	{
ef5584
		var range = textarea.createTextRange(); 
ef5584
		range.move("character", new_pos); 
ef5584
		range.select();
ef5584
		storeCaret(textarea);
ef5584
	}
ef5584
ef5584
	textarea.focus();
ef5584
	return;
ef5584
}
ef5584
ef5584
/**
ef5584
* Insert text at position
ef5584
*/
ef5584
function insert_text(text, spaces, popup)
ef5584
{
ef5584
	var textarea;
ef5584
	
ef5584
	if (!popup)
ef5584
	{
ef5584
		textarea = document.forms[form_name].elements[text_name];
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		textarea = opener.document.forms[form_name].elements[text_name];
ef5584
	}
ef5584
ef5584
	if (spaces)
ef5584
	{
ef5584
		text = ' ' + text + ' ';
ef5584
	}
ef5584
ef5584
	if (!isNaN(textarea.selectionStart))
ef5584
	{
ef5584
		var sel_start = textarea.selectionStart;
ef5584
		var sel_end = textarea.selectionEnd;
ef5584
ef5584
		mozWrap(textarea, text, '')
ef5584
		textarea.selectionStart = sel_start + text.length;
ef5584
		textarea.selectionEnd = sel_end + text.length;
ef5584
	}	
ef5584
	
ef5584
	else if (textarea.createTextRange && textarea.caretPos)
ef5584
	{
ef5584
		if (baseHeight != textarea.caretPos.boundingHeight) 
ef5584
		{
ef5584
			textarea.focus();
ef5584
			storeCaret(textarea);
ef5584
		}
ef5584
		var caret_pos = textarea.caretPos;
ef5584
		caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
ef5584
		
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		textarea.value = textarea.value + text;
ef5584
	}
ef5584
ef5584
	if (!popup)
ef5584
	{
ef5584
		textarea.focus();
ef5584
	}
ef5584
ef5584
}
ef5584
ef5584
/**
ef5584
* Add inline attachment at position
ef5584
*/
ef5584
function attach_inline(index, filename)
ef5584
{
ef5584
	insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
ef5584
	document.forms[form_name].elements[text_name].focus();
ef5584
}
ef5584
ef5584
/**
ef5584
* Add quote text to message
ef5584
*/
ef5584
function addquote(post_id, username)
ef5584
{
ef5584
	var message_name = 'message_' + post_id;
ef5584
	var theSelection = '';
ef5584
	var divarea = false;
ef5584
ef5584
	if (document.all)
ef5584
	{
ef5584
		divarea = document.all[message_name];
ef5584
	}
ef5584
	else
ef5584
	{
ef5584
		divarea = document.getElementById(message_name);
ef5584
	}
ef5584
ef5584
	// Get text selection - not only the post content :(
ef5584
	if (window.getSelection)
ef5584
	{
ef5584
		theSelection = window.getSelection().toString();
ef5584
	}
ef5584
	else if (document.getSelection)
ef5584
	{
ef5584
		theSelection = document.getSelection();
ef5584
	}
ef5584
	else if (document.selection)
ef5584
	{
ef5584
		theSelection = document.selection.createRange().text;
ef5584
	}
ef5584
ef5584
	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
ef5584
	{
ef5584
		if (divarea.innerHTML)
ef5584
		{
ef5584
			theSelection = divarea.innerHTML.replace(/
/ig, '\n');
ef5584
			theSelection = theSelection.replace(/<br\/>/ig, '\n');
ef5584
			theSelection = theSelection.replace(/<\;/ig, '<');
ef5584
			theSelection = theSelection.replace(/>\;/ig, '>');
ef5584
			theSelection = theSelection.replace(/&\;/ig, '&';;			
ef5584
		}
ef5584
		else if (document.all)
ef5584
		{
ef5584
			theSelection = divarea.innerText;
ef5584
		}
ef5584
		else if (divarea.textContent)
ef5584
		{
ef5584
			theSelection = divarea.textContent;
ef5584
		}
ef5584
		else if (divarea.firstChild.nodeValue)
ef5584
		{
ef5584
			theSelection = divarea.firstChild.nodeValue;
ef5584
		}
ef5584
	}
ef5584
ef5584
	if (theSelection)
ef5584
	{
ef5584
		insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
ef5584
	}
ef5584
ef5584
	return;
ef5584
}
ef5584
ef5584
/**
ef5584
* From http://www.massless.org/mozedit/
ef5584
*/
ef5584
function mozWrap(txtarea, open, close)
ef5584
{
ef5584
	var selLength = txtarea.textLength;
ef5584
	var selStart = txtarea.selectionStart;
ef5584
	var selEnd = txtarea.selectionEnd;
ef5584
	var scrollTop = txtarea.scrollTop;
ef5584
ef5584
	if (selEnd == 1 || selEnd == 2) 
ef5584
	{
ef5584
		selEnd = selLength;
ef5584
	}
ef5584
ef5584
	var s1 = (txtarea.value).substring(0,selStart);
ef5584
	var s2 = (txtarea.value).substring(selStart, selEnd)
ef5584
	var s3 = (txtarea.value).substring(selEnd, selLength);
ef5584
ef5584
	txtarea.value = s1 + open + s2 + close + s3;
ef5584
	txtarea.selectionStart = selEnd + open.length + close.length;
ef5584
	txtarea.selectionEnd = txtarea.selectionStart;
ef5584
	txtarea.focus();
ef5584
	txtarea.scrollTop = scrollTop;
ef5584
ef5584
	return;
ef5584
}
ef5584
ef5584
/**
ef5584
* Insert at Caret position. Code from
ef5584
* http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
ef5584
*/
ef5584
function storeCaret(textEl)
ef5584
{
ef5584
	if (textEl.createTextRange)
ef5584
	{
ef5584
		textEl.caretPos = document.selection.createRange().duplicate();
ef5584
	}
ef5584
}
ef5584
ef5584
/**
ef5584
* Color pallette
ef5584
*/
ef5584
function colorPalette(dir, width, height)
ef5584
{
ef5584
	var r = 0, g = 0, b = 0;
ef5584
	var numberList = new Array(6);
ef5584
	var color = '';
ef5584
ef5584
	numberList[0] = '00';
ef5584
	numberList[1] = '40';
ef5584
	numberList[2] = '80';
ef5584
	numberList[3] = 'BF';
ef5584
	numberList[4] = 'FF';
ef5584
ef5584
	document.writeln('');
ef5584
ef5584
	for (r = 0; r < 5; r++)
ef5584
	{
ef5584
		if (dir == 'h')
ef5584
		{
ef5584
			document.writeln('');
ef5584
		}
ef5584
ef5584
		for (g = 0; g < 5; g++)
ef5584
		{
ef5584
			if (dir == 'v')
ef5584
			{
ef5584
				document.writeln('');
ef5584
			}
ef5584
			
ef5584
			for (b = 0; b < 5; b++)
ef5584
			{
ef5584
				color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
ef5584
				document.write('');
ef5584
				document.write('#' + color + '');
ef5584
				document.writeln('');
ef5584
			}
ef5584
ef5584
			if (dir == 'v')
ef5584
			{
ef5584
				document.writeln('');
ef5584
			}
ef5584
		}
ef5584
ef5584
		if (dir == 'h')
ef5584
		{
ef5584
			document.writeln('');
ef5584
		}
ef5584
	}
ef5584
	document.writeln('');
ef5584
}
ef5584
ef5584
ef5584
/**
ef5584
* Caret Position object
ef5584
*/
ef5584
function caretPosition()
ef5584
{
ef5584
	var start = null;
ef5584
	var end = null;
ef5584
}
ef5584
ef5584
ef5584
/**
ef5584
* Get the caret position in an textarea
ef5584
*/
ef5584
function getCaretPosition(txtarea)
ef5584
{
ef5584
	var caretPos = new caretPosition();
ef5584
	
ef5584
	// simple Gecko/Opera way
ef5584
	if (txtarea.selectionStart || txtarea.selectionStart == 0)
ef5584
	{
ef5584
		caretPos.start = txtarea.selectionStart;
ef5584
		caretPos.end = txtarea.selectionEnd;
ef5584
	}
ef5584
	// dirty and slow IE way
ef5584
	else if (document.selection)
ef5584
	{
ef5584
		// get current selection
ef5584
		var range = document.selection.createRange();
ef5584
ef5584
		// a new selection of the whole textarea
ef5584
		var range_all = document.body.createTextRange();
ef5584
		range_all.moveToElementText(txtarea);
ef5584
		
ef5584
		// calculate selection start point by moving beginning of range_all to beginning of range
ef5584
		var sel_start;
ef5584
		for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
ef5584
		{
ef5584
			range_all.moveStart('character', 1);
ef5584
		}
ef5584
	
ef5584
		txtarea.sel_start = sel_start;
ef5584
	
ef5584
		// we ignore the end value for IE, this is already dirty enough and we don't need it
ef5584
		caretPos.start = txtarea.sel_start;
ef5584
		caretPos.end = txtarea.sel_start;			
ef5584
	}
ef5584
ef5584
	return caretPos;
ef5584
}