/** * 对 textarea 的操作 * 现有 bbcode 插入功能 * 修改 prompt 为 popup 样式  * @author Kafeifei chenqian * * $Id: bbcode.js 20737 2009-07-23 10:45:08Z apollopy $ */ function VC_textarea(textarea_id) { /** * 构造函数, 给定文本输入框id, 返回事例 * * @param String textarea_id * @return VC_textarea */ this.__construct = function (textarea_id){ this.textareaId = textarea_id; this.textarea = $(textarea_id); if (!this.textarea){ alert('not found '+this.textareaId); } } this.__construct(textarea_id); var alwaysthis=this; var _dialogPromptID = null; var _blackoutPromptID = null; var _isIE7; var _ieCurPos=0; var _selCopy; var _isSelect; this.ieprompt=function(innertxt, def, cb) { //_isIE7 = navigator.userAgent.indexOf("MSIE 7") > 0; that=this; _isIE7=true; this.wrapupPrompt = (function(cancled) { if (_isIE7) { val = $("iepromptfield").value; _dialogPromptID.style.display = "none"; _blackoutPromptID.style.display = "none"; $("iepromptfield").value = ""; if (cancled) { val = ""; } //cb(val); } return false; }); if (def == undefined) { def = ""; } if (_isIE7) { if (_dialogPromptID == null) { var tbody = document.getElementsByTagName("body")[0]; tnode = document.createElement("div"); tnode.id = "IEPromptBox"; tbody.appendChild(tnode); _dialogPromptID = $("IEPromptBox"); tnode = document.createElement("div"); tnode.id = "promptBlackout"; tbody.appendChild(tnode); _blackoutPromptID = $("promptBlackout"); _blackoutPromptID.style.opacity = ".9"; _blackoutPromptID.style.position = "absolute"; _blackoutPromptID.style.top = "0px"; _blackoutPromptID.style.left = "0px"; _blackoutPromptID.style.backgroundColor = "#999"; _blackoutPromptID.style.filter = "alpha(opacity=90)"; _blackoutPromptID.style.height = (document.body.offsetHeight < screen.height) ? screen.height + "px": document.body.offsetHeight + 20 + "px"; _blackoutPromptID.style.display = "block"; _blackoutPromptID.style.zIndex = "50"; //_dialogPromptID.style.border = "2px solid #B22222"; _dialogPromptID.style.backgroundColor = "#DDDDDD"; _dialogPromptID.style.position = "absolute"; _dialogPromptID.style.width = "330px"; _dialogPromptID.style.zIndex = "810"; _blackoutPromptID.onclick=function(){ _dialogPromptID.style.display = "none"; _blackoutPromptID.style.display = "none"; $("iepromptfield").value = ""; } } var tmp = "
" + innertxt + "
"; tmp += "
"; //tmp += "
"; tmp += ""; tmp += "

"; tmp += ""; tmp += "      "; tmp += ""; tmp += "
"; _blackoutPromptID.style.height = (document.body.offsetHeight < screen.height) ? screen.height + "px": document.body.offsetHeight + 30 + "px"; _blackoutPromptID.style.width = "100%"; _blackoutPromptID.style.display = "block"; _dialogPromptID.innerHTML = tmp; _dialogPromptID.style.top = parseInt(document.documentElement.scrollTop + (screen.height / 4)) + "px"; _dialogPromptID.style.left = parseInt((document.body.offsetWidth - 315) / 2) + "px"; _dialogPromptID.style.display = "block"; $("iepromptfield").focus(); } else { return prompt(innertxt, def); } } /** * 判断是否有文字被选取 * * @return Boolean | String */ this.getSelectionContents = function() { if (document.selection) { // ie this.textarea.focus(); var v = document.selection.createRange(); if (!v.text) { return false; } else { return v.text; } } else if (this.textarea.selectionStart || this.textarea.selectionStart == "0") { // mozilla var v = this.textarea.value.substring(this.textarea.selectionStart, this.textarea.selectionEnd); if (!v) { return false; } else { return v; } } // other return false; } /** * 光标位置插入内容或替换掉已选中的内容 * * @param String str * @return void */ this.insertText = function(str) { var view = this.textarea; if (!str) str = ''; if (document.selection) { // ie this.textarea.focus(); //v.select(); var v = document.selection.createRange(); v.text = ""; // 有选择内容时使用此方法 if(_isSelect != undefined) { var _selCopy=v.duplicate() _selCopy.text = str; }else{ // 没有选择内容时使用此方法 view.focus(); var endPos=_ieCurPos+str.length; view.value=view.value.substring(0,_ieCurPos)+str+view.value.substring(_ieCurPos,view.value.length); var range = view.createTextRange(); range.collapse(true); range.moveEnd('character', _ieCurPos+str.length/2); range.moveStart('character', _ieCurPos+str.length/2); range.select(); } } else if (this.textarea.selectionStart || this.textarea.selectionStart == "0") { // mozilla var startPos = this.textarea.selectionStart; var endPos = this.textarea.selectionEnd; var cursorPos = endPos; var scrollTop = this.textarea.scrollTop; this.textarea.value = this.textarea.value.substring(0, startPos) + str + this.textarea.value.substring(endPos, this.textarea.value.length); var tpp = str.length + startPos; this.textarea.focus(); this.textarea.selectionStart = tpp; this.textarea.selectionEnd = tpp; this.textarea.scrollTop = scrollTop; } else { // other this.textarea.value += str; } } /** * 插入bbcode, 如果lang为数组,则为双参数 * * @param String tag * @param String | Array lang * @return Boolean */ this.addBbcode = function(tag, lang) { var bbcode, input_str; var selection_contents = this.getSelectionContents(); // 如果有选择内容 _isSelect=selection_contents||undefined; // 增加对 IE 的判断,因为焦点会切换 if(document.selection){ var c = "\001"; var sel = document.selection.createRange(); var txt = sel.text; _selCopy = sel.duplicate(); try{ _selCopy.moveToElementText(this.textarea); }catch(e) { return 0} sel.text = txt + c; _ieCurPos = (_selCopy.text.indexOf(c)); sel.moveStart('character',-1); sel.text = ""; } if (selection_contents) { switch (tag) { case 'url': if (false == bbcode) return false; bbcode = this.addBbcode_Url(tag, lang); //$(textarea_id).value = bbcode; break; case 'flash': bbcode = this.addBbcode_flash(tag, lang); if(bbcode) { this.insertText(bbcode) } break; default: bbcode = '['+tag+']'+selection_contents+'[/'+tag+']'; this.insertText(bbcode) break; } } else if (typeof lang == 'string') { // 单一参+数 <- ie傻B if(tag == 'flash') { bbcode = this.addBbcode_flash(tag, lang); } else { var iepr = this.ieprompt(lang, '') if($('sure')){ $('sure').onclick=function(){ _dialogPromptID.style.display = "none"; _blackoutPromptID.style.display = "none"; bbcode = '['+tag+']'+$("iepromptfield").value+'[/'+tag+']'; if($("iepromptfield").value){ alwaysthis.insertText(bbcode); } $("iepromptfield").value = ""; } } } } else { // 多(二)参+数 <- ie傻B var iepr = this.ieprompt(lang[0], '') $('sure').onclick = function() { //_dialogPromptID.style.display = "none"; //_blackoutPromptID.style.display = "none"; //bbcode = '['+tag+']'+$("iepromptfield").value+'[/'+tag+']'; if ($("iepromptfield").value) { bbcode = '[' + tag + '=' + $("iepromptfield").value + ']'; var iepr = alwaysthis.ieprompt(lang[1], '') if ($('sure')) { $('sure').onclick = function() { if ($("iepromptfield").value) { _dialogPromptID.style.display = "none"; _blackoutPromptID.style.display = "none"; bbcode += $("iepromptfield").value + '[/' + tag + ']'; alwaysthis.insertText(bbcode) }else return false; } } } else { return false; // 任何一步失败返回false } } } return true; } this.addBbcode_Url = function(tag, lang) { var bbcode, input_str; var selection_contents = this.getSelectionContents(); if (selection_contents) { if (selection_contents.match(/(^|\s| |^\])(http|https|ftp|mms|telnet|rtsp|ed2k)(:\/\/\S+)/gi)) { var iepr = this.ieprompt(lang[1], '') if($('sure')){ $('sure').onclick=function(){ _dialogPromptID.style.display = "none"; _blackoutPromptID.style.display = "none"; bbcode = '['+tag+'='+selection_contents+']'+$("iepromptfield").value+'[/'+tag+']'; if($("iepromptfield").value)alwaysthis.insertText(bbcode); // 插入bbcode $("iepromptfield").value = ""; } } else { return false; } } else { var iepr = this.ieprompt(lang[0], '') if($('sure')){ $('sure').onclick=function(){ _dialogPromptID.style.display = "none"; _blackoutPromptID.style.display = "none"; bbcode = '['+tag+'='+$("iepromptfield").value+']'+selection_contents+'[/'+tag+']'; if($("iepromptfield").value)alwaysthis.insertText(bbcode); // 插入bbcode $("iepromptfield").value = ""; } } else { return false; } } } return bbcode; } /** * 自动识别 url 连接,转换为url bbcode */ this.autoUrl = function () { this.textarea.value = this.textarea.value.replace(/(^|\s| |^\])([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/gi, '$1[email]$2[email]'); this.textarea.value = this.textarea.value.replace(/(^|\s| |^\])(http|https|ftp|mms|telnet|rtsp|ed2k)(:\/\/\S+)/gi, '$1[url]$2$3[/url]'); this.textarea.value = this.textarea.value.replace(/(^|\s| |^\])(([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}[\/\S]*)/gi, '$1[url=http://$2]$2[/url]'); this.textarea.value = this.textarea.value.replace(/\[url\](\S+.(gif|jpg|png|bmp))\[\/url\]/gi, '[img]$1[/img]'); this.textarea.value = this.textarea.value.replace(/\[url\](ed2k:\/\/\S+)\[\/url\]/gi, '[emule]$1[/emule]'); //str = str.replace(/\[url\](\S+.swf)\[\/url\]/gi, '[flash]$1[/flash]'); //return ; } /** * flash bbcode代码 **/ this.addBbcode_flash = function(tag, lang) { var bbcode, input_str, src; var width; var height; var selection_contents = this.getSelectionContents(); var pattern = /(\w+)=['"](.*?)['"]/g; if (selection_contents) { if(selection_contents.match(/(tudou|youku).com/g) == null) { alert("VeryCD目前仅支持优酷、土豆视频转帖。"); return false; } while((result = pattern.exec(selection_contents)) != null) { switch(result[1]) { case "src": src = result[2]; break; case "width": width = result[2]; break; case "height": height = result[2]; break; } } if(src == 'undefined' || src == null) { //如果src获取不到值 src = selection_contents; } if(width == 'undefined' || width == null) { if(selection_contents.indexOf("youku") > -1) { width = 480; } else if(selection_contents.indexOf("tudou") > -1) { width = 420; } else { width = 480; } } if(height == 'undefined' || height == null) { if(selection_contents.indexOf("youku") > -1) { height = 400; } else if(selection_contents.indexOf("tudou") > -1) { height = 363; } else { height = 400; } } bbcode = '[flash=' + width + ',' + height + ']' + src + '[/flash]'; } else { var iepr = this.ieprompt(lang, '') if($('sure')){ $('sure').onclick=function(){ var value = $("iepromptfield").value; if(value.match(/(tudou|youku).com/g) == null) { alert("VeryCD目前仅支持优酷、土豆视频转帖。"); return false; } _dialogPromptID.style.display = "none"; _blackoutPromptID.style.display = "none"; while((result = pattern.exec(value)) != null) { switch(result[1]) { case "src": src = result[2]; break; case "width": width = result[2]; break; case "height": height = result[2]; break; } } if(src == 'undefined' || src == null) { //如果src获取不到值 src = value; } if(width == 'undefined' || width == null) { if(value.indexOf("youku") > -1) { width = 480; } else if(value.indexOf("tudou") > -1) { width = 420; } } if(height == 'undefined' || height == null) { if(value.indexOf("youku") > -1) { height = 400; } else if(value.indexOf("tudou") > -1) { height = 363; } } bbcode = '[flash=' + width + ',' + height + ']' + src + '[/flash]'; if(value){ alwaysthis.insertText(bbcode); } $("iepromptfield").value = ""; } } } return bbcode; } }