includes/clientside/static/preformat.js
changeset 1231 4797a4a88533
equal deleted inserted replaced
1230:31d226269d2f 1231:4797a4a88533
       
     1 //
       
     2 // Add the wrappers for preformatted tags within content.
       
     3 //
       
     4 
       
     5 addOnloadHook(function()
       
     6 	{
       
     7 		preformat_process_all();
       
     8 	});
       
     9 
       
    10 function preformat_process_all()
       
    11 {
       
    12 	var aec = document.getElementById('ajaxEditContainer');
       
    13 	if ( !aec )
       
    14 		return false;
       
    15 	var pres = aec.getElementsByTagName('pre');
       
    16 	for ( var i = 0; i < pres.length; i++ )
       
    17 	{
       
    18 		if ( pres[i].hasButtonPanel )
       
    19 			continue;
       
    20 		pres[i].hasButtonPanel = true;
       
    21 		
       
    22 		var btnp = document.createElement('div');
       
    23 		btnp.mypre = pres[i];
       
    24 		btnp.className = 'preformat-panel';
       
    25 		btnp.appendChild(document.createTextNode($lang.get('onpage_pre_lbl_code')));
       
    26 		btnp.appendChild(document.createTextNode(' <'));
       
    27 		var a_sel = document.createElement('a');
       
    28 		a_sel.href = '#';
       
    29 		a_sel.onclick = function()
       
    30 			{
       
    31 				preformat_handle_select_click(this.parentNode);
       
    32 				return false;
       
    33 			};
       
    34 		a_sel.appendChild(document.createTextNode($lang.get('onpage_pre_btn_select')));
       
    35 		btnp.appendChild(a_sel);
       
    36 		btnp.appendChild(document.createTextNode('> <'));
       
    37 		var a_pop = document.createElement('a');
       
    38 		a_pop.href = '#';
       
    39 		a_pop.onclick = function()
       
    40 			{
       
    41 				preformat_handle_popup_click(this.parentNode);
       
    42 				return false;
       
    43 			};
       
    44 		a_pop.appendChild(document.createTextNode($lang.get('onpage_pre_btn_popup')));
       
    45 		btnp.appendChild(a_pop);
       
    46 		btnp.appendChild(document.createTextNode('>'));
       
    47 		pres[i].parentNode.insertBefore(btnp, pres[i]);
       
    48 	}
       
    49 }
       
    50 
       
    51 function preformat_handle_select_click(btnp)
       
    52 {
       
    53 	var pre = btnp.mypre;
       
    54 	select_element(pre);
       
    55 }
       
    56 
       
    57 function preformat_handle_popup_click(btnp)
       
    58 {
       
    59 	var pre = btnp.mypre;
       
    60 	var text = pre.innerHTML;
       
    61 	var newwin = window.open('about:blank', 'codepopwin', 'width=800,height=600,status=no,toolbars=no,toolbar=no,address=no,scroll=yes');
       
    62 	newwin.document.open();
       
    63 	newwin.document.write('<html><head><title>' + $lang.get('onpage_pre_popup_title') + '</title></head><body>');
       
    64 	newwin.document.write('<pre>' + text + '</pre>');
       
    65 	newwin.document.write('</body></html>');
       
    66 	newwin.document.close();
       
    67 }
       
    68 
       
    69 function select_element(element)
       
    70 {
       
    71 	if (IE)
       
    72 	{
       
    73 		// IE
       
    74 		var range = document.body.createTextRange();
       
    75 		range.moveToElementText(element);
       
    76 		range.select();
       
    77 	}
       
    78 	else if (is_Gecko || is_Opera)
       
    79 	{
       
    80 		// Mozilla/Opera
       
    81 		var selection = window.getSelection();
       
    82 		var range = document.createRange();
       
    83 		range.selectNodeContents(element);
       
    84 		selection.removeAllRanges();
       
    85 		selection.addRange(range);
       
    86 	}
       
    87 	else if (is_Webkit)
       
    88 	{
       
    89 		// Safari (Chrome?)
       
    90 		var selection = window.getSelection();
       
    91 		selection.setBaseAndExtent(element, 0, element, 1);
       
    92 	}
       
    93 }
       
    94