Generalnie to chodzi mi o zrozumienie co robi ta funkcja tag poniżej. Nie kapuje co robią metody setSelectionRange + document.selection.createRange().text + scrollTop, wujek Google nie za bardzo pomaga tym razem. Dodatkowo co to za dziwna składnia i czemu to służy -> start = '[func]'+url+':|:'; lub start = '[break]';
function tag(tag) {
var src = document.getElementById('txt');
var start, end, url, alt, title='';
switch(tag) {
case 'underline':
start = '<span class="underline">';
end = '<\/span>';
break;
case 'intro':
start = '[break]';
end = '\n';
break;
// function use
case 'func':// adding function insertion
// function name no brackets
url = prompt("<?php echo l('js_func1'); ?>", '');
//params, seperated by comma.
title = prompt("<?php echo l('js_func2'); ?>", "");
if (url != null) {
start = '[func]'+url+':|:';
end = '[\/func]';
} else {
start = '';
end = '';
title = '';
}
break;
// function use end
case 'include':
url = prompt("<?php echo l('js_file'); ?>", '');
start = url !=null ? '[include]'+url+'[\/include]' : '';
end = '';
break;
case 'br':
start = '';
end = '<br \/>\n';
break;
case 'img':
url = prompt("<?php echo l('js_image1'); ?>", '');
alt = prompt("<?php echo l('js_image2'); ?>", '');
start = url != null ? '<img src="'+url+'" alt="'+alt+'" \/>' : '';
end = '';
break;
case 'link':
url = prompt("<?php echo l('js_link1'); ?>", '');
title = prompt("<?php echo l('js_link2'); ?>", '');
if (url != null) {
start = '<a href="'+url+'" title="'+title+'">';
end = '<\/a>';
} else {
start = '';
end = '';
title = '';
}
break;
default:
start = '<'+tag+'>';
end = '<\/'+tag+'>\n';
}
if (!src.setSelectionRange) {
var selected = document.selection.createRange().text;
src.focus();
if (selected.length <= 0) {
var codetext = start + title + end;
} else {
var codetext = start + selected + end;
}
document.selection.createRange().text = codetext;
} else {
var h = src.scrollTop;
var pretext = src.value.substring(0, src.selectionStart);
var codetext = start + src.value.substring(src.selectionStart, src.selectionEnd) + end;
var posttext = src.value.substring(src.selectionEnd, src.value.length);
if (codetext == start + end) {
codetext = start + title + end;
}
src.value = pretext + codetext + posttext;
document.getElementById("txt").scrollTop = h;
src.selectionStart=pretext.length;
src.selectionEnd=pretext.length+codetext.length;
src.focus();
}
}
Przykładowe zastosowanie:
function buttons(){
echo '<div class="clearer"></div> <p>'.l('formatting').':
<br class="clearer" />';
'strong' => '',
'em' => 'key',
'underline' => 'key',
'del' => 'key',
'p' => '',
'br' => ''
);
foreach ($formatting as $key => $var) {
$css = $var == 'key' ? $key :'buttons';
echo '<input type="button" name="'.$key.'" title="'.l
($key).'" class="'.$css.'" onclick="tag(\''.$key.'\')" value="'. l($key.'_value').'" />';
}
echo '</p><br class="clearer" /><p>'.l
('insert').': <br class="clearer" />'; $insert = array('img', 'link', 'include', 'func','intro'); foreach ($insert as $key) {
echo '<input type="button" name="'.$key.'" title="'.l
($key).'" class="buttons" onclick="tag(\''. $key.'\')" value="'.l($key.'_value').'" />';
}
echo '<br class="clearer" /></p>'; }
}