/*======================================================================*\
// # Editor TextArea v0.0
// # ---------------------------------------------------------------- # ||
// # Copyright ©2007-2007 Francisco Fernández ||
// # This file may not be redistributed in whole or significant part. # ||
// # ---------------- TextArea IS NOT FREE SOFTWARE ---------------- # ||
// # http://criticame.buendias.com # ||
\*======================================================================*/
var CURSOR = "[*]"; // Cursor
// Etilo de los paneles
var STYLE_MAIN_PANEL = "background: #CBDCD0; border: thin solid #000000; padding: 10px;";
var STYLE_TOOL_BAR = "width: 100%; background: #EDFEF0; border: thin solid #000000; padding: 4px;";
// Estilo de un boton
var STYLE_BUTTON = "border: solid; border-color: black; border-width: 1px; background: silver;";
// Estilo de un smile
var SMILE_BUTTON = "padding: 1px;";
// Array con los botones (Esto es lo que hay que modificar para añadir botones)
var buttons = Array();
// "b31" : new Button("b31", "js/TextEditor//smiles/ok.gif", "", SMILE_BUTTON, "InsertText")
buttons = {
"b1" : new Button("b1", "js/TextEditor//icons/bold.gif", "[b]" + CURSOR + "[/b]", STYLE_BUTTON, "InsertText"),
"b2" : new Button("b2", "js/TextEditor//icons/italic.gif", "[i]" + CURSOR + "[/i]", STYLE_BUTTON, "InsertText"),
"b3" : new Button("b3", "js/TextEditor//icons/underline.gif", "[u]" + CURSOR + "[/u]", STYLE_BUTTON, "InsertText"),
"b4" : new Button("b4", "js/TextEditor//icons/image.gif", "[img]" + CURSOR + "[/img]", STYLE_BUTTON, "InsertText"),
"b5" : new Button("b5", "js/TextEditor//icons/quote.gif", "[q]" + CURSOR + "[/q]", STYLE_BUTTON, "InsertText"),
"b6" : new Button("b6", "js/TextEditor//icons/link.gif", "[url]" + CURSOR + "[/url]", STYLE_BUTTON, "InsertText"),
"b7" : new Button("b7", "js/TextEditor//icons/unorderedlist.gif", "\n[ul]\n [li]" + CURSOR + "\n [li]\n[/ul]\n", STYLE_BUTTON, "InsertText"),
"b8" : new Button("b8", "js/TextEditor//icons/orderedlist.gif", "\n[ol]\n [li]" + CURSOR + "\n [li]\n[/ol]\n", STYLE_BUTTON, "InsertText"),
"b40" : new Button("b40", "js/TextEditor//icons/youtube.gif", "[youtubeURL]" + CURSOR + "[/youtubeURL]", STYLE_BUTTON, "InsertText"),
"b9" : new Button("b9", "NEWLINE", "", STYLE_BUTTON, ""),
"b10" : new Button("b10", "js/TextEditor//smiles/regular_smile.gif", ":)", SMILE_BUTTON, "InsertText"),
"b11" : new Button("b11", "js/TextEditor//smiles/sad_smile.gif", ":(", SMILE_BUTTON, "InsertText"),
"b12" : new Button("b12", "js/TextEditor//smiles/teeth_smile.gif", ":D", SMILE_BUTTON, "InsertText"),
"b15" : new Button("b15", "js/TextEditor//smiles/tounge_smile.gif", ":p", SMILE_BUTTON, "InsertText"),
"b16" : new Button("b16", "js/TextEditor//smiles/omg_smile.gif", ":o", SMILE_BUTTON, "InsertText"),
"b17" : new Button("b17", "js/TextEditor//smiles/confused_smile.gif", ":s", SMILE_BUTTON, "InsertText"),
"b18" : new Button("b18", "js/TextEditor//smiles/cry_smile.gif", ":..(", SMILE_BUTTON, "InsertText"),
"b19" : new Button("b19", "js/TextEditor//smiles/embaressed_smile.gif", ":$", SMILE_BUTTON, "InsertText"),
"b20" : new Button("b20", "js/TextEditor//smiles/shades_smile.gif", "(H)", SMILE_BUTTON, "InsertText"),
"b21" : new Button("b21", "js/TextEditor//smiles/whatchutalkingabout_smile.gif", ":|", SMILE_BUTTON, "InsertText"),
"b22" : new Button("b22", "js/TextEditor//smiles/furious.gif", ":@", SMILE_BUTTON, "InsertText"),
"b23" : new Button("b23", "js/TextEditor//smiles/party.gif", "<:o)", SMILE_BUTTON, "InsertText"),
"b24" : new Button("b24", "js/TextEditor//smiles/angry.gif", "8o|", SMILE_BUTTON, "InsertText"),
"b25" : new Button("b25", "js/TextEditor//smiles/argh.gif", "+o(", SMILE_BUTTON, "InsertText"),
"b26" : new Button("b26", "js/TextEditor//smiles/silent.gif", ":-#", SMILE_BUTTON, "InsertText"),
"b27" : new Button("b27", "js/TextEditor//smiles/glasses.gif", "8-|", SMILE_BUTTON, "InsertText"),
"b28" : new Button("b28", "js/TextEditor//smiles/thinking.gif", "*-)", SMILE_BUTTON, "InsertText"),
"b29" : new Button("b29", "js/TextEditor//smiles/indiferent.gif", "8-)", SMILE_BUTTON, "InsertText"),
"b30" : new Button("b30", "js/TextEditor//smiles/saint.gif", "(A)", SMILE_BUTTON, "InsertText"),
"b32" : new Button("b32", "NEWLINE", "", STYLE_BUTTON, ""),
"b13" : new Button("b13", "js/TextEditor//smiles/thumbs_down.gif", "(N)", SMILE_BUTTON, "InsertText"),
"b14" : new Button("b14", "js/TextEditor//smiles/thumbs_up.gif", "(Y)", SMILE_BUTTON, "InsertText")
};
function Button(name, caption, tag, style, eventControl) {
this.name = name;
this.caption = caption;
this.tag = tag;
this.style = style;
this.eventControl = eventControl;
this.onclick = null;
this.setOnclick = function(textarea) {
var func = "";
switch(this.eventControl) {
case "TextFormat":
func = "textFormat";
break;
case "InsertText":
func = "insertText";
break;
}
this.onclick = func + "('" + textarea + "', '" + this.name + "')";
};
this.getHtml =
function (){
var html = "";
switch(this.caption) {
case "NEWLINE":
html = "
";
break;
default:
html = '';
}
return html;
};
}
function formatText(tag, text) {
text="["+tag+"]"+text+"[/"+tag+"]";
return text;
}
function getTextAtSelection(control){
// Explorer
var selText = "";
if (document.selection) {
sel = document.selection.createRange();
selText = sel.text;
}
// Mozilla
else if(control.selectionStart || control.selectionStart == '0') {
var startPos = control.selectionStart;
var endPos = control.selectionEnd;
selText = control.value.substring(startPos, endPos);
}
return selText;
}
function insertAtCursor(control, text){
// Explorer
if (document.selection) {
control.focus();
sel = document.selection.createRange();
sel.text = text;
}
// Mozilla
else if (control.selectionStart || control.selectionStart == '0') {
var startPos = control.selectionStart;
var endPos = control.selectionEnd;
control.value = control.value.substring(0, startPos) + text + control.value.substring(endPos, control.value.length);
control.focus();
startPos += text.length;
control.setSelectionRange(startPos,startPos);
}
else {
control.value += text;
}
}
function insertAtCursor(control, text, cursorText){
// Explorer
if (document.selection) {
control.focus();
sel = document.selection.createRange();
sel.text = text;
}
// Mozilla
else if (control.selectionStart || control.selectionStart == '0') {
var startPos = control.selectionStart;
var endPos = control.selectionEnd;
var cursorPos = text.indexOf(CURSOR);
if(cursorPos == -1 )
cursorPos = text.length; // Si no hay cursor, lo pone al final
text = text.replace(CURSOR, ""); // Elimina el indicador del cursor
control.value = control.value.substring(0, startPos) + text + control.value.substring(endPos, control.value.length);
control.focus();
startPos += cursorPos;
control.setSelectionRange(startPos,startPos);
}
else {
control.value += text;
}
}
//Controladores de eventos
function textFormat(textAreaName, buttonKey){
obj = document.getElementsByTagName("textarea")[textAreaName];
text = getTextAtSelection(obj);
if (text == "") {
text = prompt(langTextEditor[buttonKey], "");
}
insertAtCursor(obj, formatText(buttons[buttonKey].tag, text));
};
function insertText(textAreaName, buttonKey){
obj = document.getElementsByTagName("textarea")[textAreaName];
text = getTextAtSelection(obj);
if (text != "") { // Si se ha seleccionado texto, ponemos la seleccion el en cursor
var tag = buttons[buttonKey].tag;
insertAtCursor(obj, tag.replace(CURSOR, text));
} else { // No hay texto seleccionado
text = buttons[buttonKey].tag;
insertAtCursor(obj, text, CURSOR);
}
};
function TextEditor(name, value, width, height) {
this.name = name;
this.width = width;
this.height = height;
this.getHtml =
function () {
html = "
";
html += "| ";
for(b in buttons) {
buttons[b].setOnclick( this.name );
html += buttons[b].getHtml() + "\n";
}
html += " | ";
html += " |
";
return html;
};
}
// EJEMPLO DE LLAMADA
/*
t = new TextEditor("pop", "texto", 100, 40);
document.write(t.getHtml());
*/