function UBBEditor () {
var textArea;
this.setTextArea = function (textarea) {
textArea = textarea;
}
this.insertImageLabel = function () {
var p = showImageSelector();
if (p!=null) {
this.setLabel("IMAGE", null, null, [["连接", "SRC", p]]);
}
}
function showImageSelector () {
var r = window.showModalDialog("fmsSelectFrame.jsp");
if(r!=null) {
return r;
} else {
return "";
}
}
this.setLabel = function (labelHead, labelEnd, text, attributes) {
var ra = null;
if (document.selection && document.selection.type.toUpperCase()=="TEXT") {
ra = document.selection.createRange();
}
var sAtts = "";
if (attributes+""!="undefined") {
for (var i=0; i<attributes.length; i++) {
var at = attributes[i];
var promptText = at[0] + "";
var key = at[1] + "";
var value = at[2] + "";
if (value=="undefined") {
value = prompt(promptText, "") + "";
if (value=="undefined") {
value = "";
}
}
if (attributes.length==1) {
sAtts = "=" + value + "";
} else {
sAtts += " " + key + "=" + value + "";
}
}
}
if (ra!=null) {
if (text==null && text+""!="undefined") {
text = ra.text;
}
var finalText = "[" + labelHead + sAtts + "]" + text;
if (labelEnd!=null) {
finalText += "[/" + labelEnd + "]";
}
ra.text = finalText;
} else {
if (text==null && text+""!="undefined") {
text = textArea.innerHTML;
}
var finalText = "[" + labelHead + sAtts + "]" + text;
if (labelEnd!=null) {
finalText += "[/" + labelEnd + "]";
}
textArea.innerHTML = finalText;
}
}
}
to app demo:
<script language="javascript">
var ubb = new UBBEditor();
ubb.setTextArea(document.forms["editForm"].elements["content"]);
</script>
<a href="javascript:ubb.setLabel('B', 'B', null);"><img src="../images/ubb/b_03.gif" width="22" height="22"></a>
<a href="javascript:ubb.setLabel('I', 'I', null);"><img src="../images/ubb/b_05.gif" width="23" height="22"></a>
<a href="javascript:ubb.setLabel('U', 'U', null);"><img src="../images/ubb/b_07.gif" width="23" height="22"></a>
<a href="javascript:ubb.setLabel('CENTER', 'CENTER', null);"><img src="../images/ubb/b_09.gif" width="22" height="22"></a>
<a href="javascript:ubb.setLabel('LINK', 'LINK', null, [['地址','URL']]);"><img src="../images/ubb/b_11.gif" width="22" height="22"></a>
<a href="javascript:ubb.setLabel('MAIL', null, null, [['邮址','URL']]);"><img src="../images/ubb/b_13.gif" width="23" height="22"></a>
<a href="javascript:ubb.insertImageLabel();"><img src="../images/ubb/b_15.gif" width="23" height="22"></a>
<a href="javascript:ubb.setLabel('QUOTE', 'QUOTE', null, [['标题','text']]);"><img src="../images/ubb/b_17.gif" width="23" height="22"></a>
<a href="javascript:ubb.setLabel('LIGHT', 'LIGHT', null, []);"><img src="../images/ubb/b_19.gif" width="22" height="22"></a>
<a href="javascript:ubb.setLabel('SHADOW', 'SHADOW', null, []);"><img src="../images/ubb/b_21.gif" width="22" height="22"></a>