adm/style/editor.js
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/adm/style/editor.js	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,403 @@
     1.4 +/**
     1.5 +* bbCode control by subBlue design [ www.subBlue.com ]
     1.6 +* Includes unixsafe colour palette selector by SHS`
     1.7 +*/
     1.8 +
     1.9 +// Startup variables
    1.10 +var imageTag = false;
    1.11 +var theSelection = false;
    1.12 +
    1.13 +// Check for Browser & Platform for PC & IE specific bits
    1.14 +// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
    1.15 +var clientPC = navigator.userAgent.toLowerCase(); // Get client info
    1.16 +var clientVer = parseInt(navigator.appVersion); // Get browser version
    1.17 +
    1.18 +var is_ie = ((clientPC.indexOf('msie') != -1) && (clientPC.indexOf('opera') == -1));
    1.19 +var is_win = ((clientPC.indexOf('win') != -1) || (clientPC.indexOf('16bit') != -1));
    1.20 +
    1.21 +var baseHeight;
    1.22 +
    1.23 +/**
    1.24 +* Shows the help messages in the helpline window
    1.25 +*/
    1.26 +function helpline(help)
    1.27 +{
    1.28 +	document.forms[form_name].helpbox.value = help_line[help];
    1.29 +}
    1.30 +
    1.31 +/**
    1.32 +* Fix a bug involving the TextRange object. From
    1.33 +* http://www.frostjedi.com/terra/scripts/demo/caretBug.html
    1.34 +*/ 
    1.35 +function initInsertions() 
    1.36 +{
    1.37 +	var doc;
    1.38 +	if(document.forms[form_name])
    1.39 +	{
    1.40 +		doc = document;
    1.41 +	}
    1.42 +	else
    1.43 +	{
    1.44 +		doc = opener.document;
    1.45 +	}
    1.46 +
    1.47 +	var textarea = doc.forms[form_name].elements[text_name];
    1.48 +	if (is_ie && typeof(baseHeight) != 'number')
    1.49 +	{
    1.50 +		textarea.focus();
    1.51 +		baseHeight = doc.selection.createRange().duplicate().boundingHeight;
    1.52 +
    1.53 +		if (!document.forms[form_name])
    1.54 +		{
    1.55 +			document.body.focus();
    1.56 +		}
    1.57 +	}
    1.58 +}
    1.59 +
    1.60 +/**
    1.61 +* bbstyle
    1.62 +*/
    1.63 +function bbstyle(bbnumber)
    1.64 +{
    1.65 +	if (bbnumber != -1)
    1.66 +	{
    1.67 +		bbfontstyle(bbtags[bbnumber], bbtags[bbnumber+1]);
    1.68 +	}
    1.69 +	else
    1.70 +	{
    1.71 +		insert_text('[*]');
    1.72 +		document.forms[form_name].elements[text_name].focus();		
    1.73 +	}
    1.74 +}
    1.75 +
    1.76 +/**
    1.77 +* Apply bbcodes
    1.78 +*/
    1.79 +function bbfontstyle(bbopen, bbclose)
    1.80 +{
    1.81 +	theSelection = false;
    1.82 +		
    1.83 +	var textarea = document.forms[form_name].elements[text_name];
    1.84 +
    1.85 +	textarea.focus();
    1.86 +
    1.87 +	if ((clientVer >= 4) && is_ie && is_win)
    1.88 +	{
    1.89 +		// Get text selection
    1.90 +		theSelection = document.selection.createRange().text;
    1.91 +
    1.92 +		if (theSelection)
    1.93 +		{
    1.94 +			// Add tags around selection
    1.95 +			document.selection.createRange().text = bbopen + theSelection + bbclose;
    1.96 +			document.forms[form_name].elements[text_name].focus();
    1.97 +			theSelection = '';
    1.98 +			return;
    1.99 +		}
   1.100 +	}
   1.101 +	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))
   1.102 +	{
   1.103 +		mozWrap(document.forms[form_name].elements[text_name], bbopen, bbclose);
   1.104 +		document.forms[form_name].elements[text_name].focus();
   1.105 +		theSelection = '';
   1.106 +		return;
   1.107 +	}
   1.108 +	
   1.109 +	//The new position for the cursor after adding the bbcode
   1.110 +	var caret_pos = getCaretPosition(textarea).start;
   1.111 +	var new_pos = caret_pos + bbopen.length;		
   1.112 +
   1.113 +	// Open tag
   1.114 +	insert_text(bbopen + bbclose);
   1.115 +
   1.116 +	// Center the cursor when we don't have a selection
   1.117 +	// Gecko and proper browsers
   1.118 +	if (!isNaN(textarea.selectionStart))
   1.119 +	{
   1.120 +		textarea.selectionStart = new_pos;
   1.121 +		textarea.selectionEnd = new_pos;
   1.122 +	}	
   1.123 +	// IE
   1.124 +	else if (document.selection)
   1.125 +	{
   1.126 +		var range = textarea.createTextRange(); 
   1.127 +		range.move("character", new_pos); 
   1.128 +		range.select();
   1.129 +		storeCaret(textarea);
   1.130 +	}
   1.131 +
   1.132 +	textarea.focus();
   1.133 +	return;
   1.134 +}
   1.135 +
   1.136 +/**
   1.137 +* Insert text at position
   1.138 +*/
   1.139 +function insert_text(text, spaces, popup)
   1.140 +{
   1.141 +	var textarea;
   1.142 +	
   1.143 +	if (!popup)
   1.144 +	{
   1.145 +		textarea = document.forms[form_name].elements[text_name];
   1.146 +	}
   1.147 +	else
   1.148 +	{
   1.149 +		textarea = opener.document.forms[form_name].elements[text_name];
   1.150 +	}
   1.151 +
   1.152 +	if (spaces)
   1.153 +	{
   1.154 +		text = ' ' + text + ' ';
   1.155 +	}
   1.156 +
   1.157 +	if (!isNaN(textarea.selectionStart))
   1.158 +	{
   1.159 +		var sel_start = textarea.selectionStart;
   1.160 +		var sel_end = textarea.selectionEnd;
   1.161 +
   1.162 +		mozWrap(textarea, text, '');
   1.163 +		textarea.selectionStart = sel_start + text.length;
   1.164 +		textarea.selectionEnd = sel_end + text.length;
   1.165 +	}	
   1.166 +	
   1.167 +	else if (textarea.createTextRange && textarea.caretPos)
   1.168 +	{
   1.169 +		if (baseHeight != textarea.caretPos.boundingHeight) 
   1.170 +		{
   1.171 +			textarea.focus();
   1.172 +			storeCaret(textarea);
   1.173 +		}
   1.174 +		var caret_pos = textarea.caretPos;
   1.175 +		caret_pos.text = caret_pos.text.charAt(caret_pos.text.length - 1) == ' ' ? caret_pos.text + text + ' ' : caret_pos.text + text;
   1.176 +		
   1.177 +	}
   1.178 +	else
   1.179 +	{
   1.180 +		textarea.value = textarea.value + text;
   1.181 +	}
   1.182 +
   1.183 +	if (!popup)
   1.184 +	{
   1.185 +		textarea.focus();
   1.186 +	}
   1.187 +
   1.188 +}
   1.189 +
   1.190 +/**
   1.191 +* Add inline attachment at position
   1.192 +*/
   1.193 +function attach_inline(index, filename)
   1.194 +{
   1.195 +	insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
   1.196 +	document.forms[form_name].elements[text_name].focus();
   1.197 +}
   1.198 +
   1.199 +/**
   1.200 +* Add quote text to message
   1.201 +*/
   1.202 +function addquote(post_id, username)
   1.203 +{
   1.204 +	var message_name = 'message_' + post_id;
   1.205 +	var theSelection = '';
   1.206 +	var divarea = false;
   1.207 +
   1.208 +	if (document.all)
   1.209 +	{
   1.210 +		divarea = document.all[message_name];
   1.211 +	}
   1.212 +	else
   1.213 +	{
   1.214 +		divarea = document.getElementById(message_name);
   1.215 +	}
   1.216 +
   1.217 +	// Get text selection - not only the post content :(
   1.218 +	if (window.getSelection)
   1.219 +	{
   1.220 +		theSelection = window.getSelection().toString();
   1.221 +	}
   1.222 +	else if (document.getSelection)
   1.223 +	{
   1.224 +		theSelection = document.getSelection();
   1.225 +	}
   1.226 +	else if (document.selection)
   1.227 +	{
   1.228 +		theSelection = document.selection.createRange().text;
   1.229 +	}
   1.230 +
   1.231 +	if (theSelection == '' || typeof theSelection == 'undefined' || theSelection == null)
   1.232 +	{
   1.233 +		if (divarea.innerHTML)
   1.234 +		{
   1.235 +			theSelection = divarea.innerHTML.replace(/<br>/ig, '\n');
   1.236 +			theSelection = theSelection.replace(/<br\/>/ig, '\n');
   1.237 +			theSelection = theSelection.replace(/&lt\;/ig, '<');
   1.238 +			theSelection = theSelection.replace(/&gt\;/ig, '>');
   1.239 +			theSelection = theSelection.replace(/&amp\;/ig, '&');			
   1.240 +			theSelection = theSelection.replace(/&nbsp\;/ig, ' ');
   1.241 +		}
   1.242 +		else if (document.all)
   1.243 +		{
   1.244 +			theSelection = divarea.innerText;
   1.245 +		}
   1.246 +		else if (divarea.textContent)
   1.247 +		{
   1.248 +			theSelection = divarea.textContent;
   1.249 +		}
   1.250 +		else if (divarea.firstChild.nodeValue)
   1.251 +		{
   1.252 +			theSelection = divarea.firstChild.nodeValue;
   1.253 +		}
   1.254 +	}
   1.255 +
   1.256 +	if (theSelection)
   1.257 +	{
   1.258 +		insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
   1.259 +	}
   1.260 +
   1.261 +	return;
   1.262 +}
   1.263 +
   1.264 +/**
   1.265 +* From http://www.massless.org/mozedit/
   1.266 +*/
   1.267 +function mozWrap(txtarea, open, close)
   1.268 +{
   1.269 +	var selLength = (typeof(txtarea.textLength) == 'undefined') ? txtarea.value.length : txtarea.textLength;
   1.270 +	var selStart = txtarea.selectionStart;
   1.271 +	var selEnd = txtarea.selectionEnd;
   1.272 +	var scrollTop = txtarea.scrollTop;
   1.273 +
   1.274 +	if (selEnd == 1 || selEnd == 2) 
   1.275 +	{
   1.276 +		selEnd = selLength;
   1.277 +	}
   1.278 +
   1.279 +	var s1 = (txtarea.value).substring(0,selStart);
   1.280 +	var s2 = (txtarea.value).substring(selStart, selEnd);
   1.281 +	var s3 = (txtarea.value).substring(selEnd, selLength);
   1.282 +
   1.283 +	txtarea.value = s1 + open + s2 + close + s3;
   1.284 +	txtarea.selectionStart = selStart + open.length;
   1.285 +	txtarea.selectionEnd = selEnd + open.length;
   1.286 +	txtarea.focus();
   1.287 +	txtarea.scrollTop = scrollTop;
   1.288 +
   1.289 +	return;
   1.290 +}
   1.291 +
   1.292 +/**
   1.293 +* Insert at Caret position. Code from
   1.294 +* http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
   1.295 +*/
   1.296 +function storeCaret(textEl)
   1.297 +{
   1.298 +	if (textEl.createTextRange)
   1.299 +	{
   1.300 +		textEl.caretPos = document.selection.createRange().duplicate();
   1.301 +	}
   1.302 +}
   1.303 +
   1.304 +/**
   1.305 +* Color pallette
   1.306 +*/
   1.307 +function colorPalette(dir, width, height)
   1.308 +{
   1.309 +	var r = 0, g = 0, b = 0;
   1.310 +	var numberList = new Array(6);
   1.311 +	var color = '';
   1.312 +
   1.313 +	numberList[0] = '00';
   1.314 +	numberList[1] = '40';
   1.315 +	numberList[2] = '80';
   1.316 +	numberList[3] = 'BF';
   1.317 +	numberList[4] = 'FF';
   1.318 +
   1.319 +	document.writeln('<table class="type2">');
   1.320 +
   1.321 +	for (r = 0; r < 5; r++)
   1.322 +	{
   1.323 +		if (dir == 'h')
   1.324 +		{
   1.325 +			document.writeln('<tr>');
   1.326 +		}
   1.327 +
   1.328 +		for (g = 0; g < 5; g++)
   1.329 +		{
   1.330 +			if (dir == 'v')
   1.331 +			{
   1.332 +				document.writeln('<tr>');
   1.333 +			}
   1.334 +			
   1.335 +			for (b = 0; b < 5; b++)
   1.336 +			{
   1.337 +				color = String(numberList[r]) + String(numberList[g]) + String(numberList[b]);
   1.338 +				document.write('<td bgcolor="#' + color + '" style="width: ' + width + 'px; height: ' + height + 'px;">');
   1.339 +				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>');
   1.340 +				document.writeln('</td>');
   1.341 +			}
   1.342 +
   1.343 +			if (dir == 'v')
   1.344 +			{
   1.345 +				document.writeln('</tr>');
   1.346 +			}
   1.347 +		}
   1.348 +
   1.349 +		if (dir == 'h')
   1.350 +		{
   1.351 +			document.writeln('</tr>');
   1.352 +		}
   1.353 +	}
   1.354 +	document.writeln('</table>');
   1.355 +}
   1.356 +
   1.357 +
   1.358 +/**
   1.359 +* Caret Position object
   1.360 +*/
   1.361 +function caretPosition()
   1.362 +{
   1.363 +	var start = null;
   1.364 +	var end = null;
   1.365 +}
   1.366 +
   1.367 +
   1.368 +/**
   1.369 +* Get the caret position in an textarea
   1.370 +*/
   1.371 +function getCaretPosition(txtarea)
   1.372 +{
   1.373 +	var caretPos = new caretPosition();
   1.374 +	
   1.375 +	// simple Gecko/Opera way
   1.376 +	if (txtarea.selectionStart || txtarea.selectionStart == 0)
   1.377 +	{
   1.378 +		caretPos.start = txtarea.selectionStart;
   1.379 +		caretPos.end = txtarea.selectionEnd;
   1.380 +	}
   1.381 +	// dirty and slow IE way
   1.382 +	else if (document.selection)
   1.383 +	{
   1.384 +		// get current selection
   1.385 +		var range = document.selection.createRange();
   1.386 +
   1.387 +		// a new selection of the whole textarea
   1.388 +		var range_all = document.body.createTextRange();
   1.389 +		range_all.moveToElementText(txtarea);
   1.390 +		
   1.391 +		// calculate selection start point by moving beginning of range_all to beginning of range
   1.392 +		var sel_start;
   1.393 +		for (sel_start = 0; range_all.compareEndPoints('StartToStart', range) < 0; sel_start++)
   1.394 +		{
   1.395 +			range_all.moveStart('character', 1);
   1.396 +		}
   1.397 +	
   1.398 +		txtarea.sel_start = sel_start;
   1.399 +	
   1.400 +		// we ignore the end value for IE, this is already dirty enough and we don't need it
   1.401 +		caretPos.start = txtarea.sel_start;
   1.402 +		caretPos.end = txtarea.sel_start;			
   1.403 +	}
   1.404 +
   1.405 +	return caretPos;
   1.406 +}
   1.407 \ No newline at end of file