var tpk=240; //超过此宽度的图片在编辑器中缩略 //绑定编辑器到textarea,可添加指定ID值 function ubbEditor(tid){ tid = (typeof tid == "undefined") ? "nr" : tid; sEdit.init({id : tid}); sEdit.event.add(window, 'load', function(){sEdit.create(tid);sEdit.util.pToBr(tid);}); } var sEdit={};sEdit.lang={source:'UBB代码',fullscreen:'全屏显示',paste:'粘贴',justifyleft:'左对齐',justifycenter:'居中',justifyright:'右对齐',fontsize:'文字大小',textcolor:'文字颜色',bold:'粗体',italic:'斜体',underline:'下划线',image:'插入图片',webplay:'插入多媒体/Flash',table:'插入表格',emoticons:'插入表情',link:'超级连接',unlink:'取消超级连接',quote:'引用',yes:'确定',no:'取消',close:'关闭',colorTable:[["Black","#8b0000","#006400","Navy","#800080"],["#d40a00","#ff8c00","Green","Blue","#9932cc"],["Red","#f4a460","#2e8b57","#4169e1","#666666"],["#ff00ff","#ffa500","Lime","#00bfff","#999999"],["Pink","Wheat","#98fb98","#add8e6","White"]]};sEdit.$=function(id,doc){var doc=doc||document;return doc.getElementById(id);};sEdit.$$=function(name,doc){var doc=doc||document;return doc.createElement(name);};sEdit.event={add:function(el,event,listener){if(el.addEventListener){el.addEventListener(event,listener,false);}else if(el.attachEvent){el.attachEvent('on'+event,listener);}},remove:function(el,event,listener){if(el.removeEventListener){el.removeEventListener(event,listener,false);}else if(el.detachEvent){el.detachEvent('on'+event,listener);}}};sEdit.each=function(obj,func){for(var key in obj){if(obj.hasOwnProperty(key))func(key,obj[key]);}};sEdit.util={getDocumentElement:function(){return(document.compatMode!="CSS1Compat")?document.body:document.documentElement;},getDocumentHeight:function(){var el=this.getDocumentElement();return Math.max(el.scrollHeight,el.clientHeight);},getDocumentWidth:function(){var el=this.getDocumentElement();return Math.max(el.scrollWidth,el.clientWidth);},getHtmlPath:function(){return location.href.substring(0,location.href.lastIndexOf('/')+1);},getBrowser:function(){var browser='';var ua=navigator.userAgent.toLowerCase();if(ua.indexOf("msie")>-1){browser='IE';}else if(ua.indexOf("gecko")>-1){browser='GECKO';}else if(ua.indexOf("opera")>-1){browser='OPERA';} return browser;},loadStyle:function(path){var link=sEdit.$$('link');link.setAttribute('type','text/css');link.setAttribute('rel','stylesheet');link.setAttribute('href',path);document.getElementsByTagName("head")[0].appendChild(link);},inArray:function(str,arr){for(var i=0;i/g,">");return html;},getElementPos:function(el){var x=0;var y=0;if(el.getBoundingClientRect){var box=el.getBoundingClientRect();var el=this.getDocumentElement();x=box.left+el.scrollLeft-el.clientLeft;y=box.top+el.scrollTop-el.clientTop;}else{x=el.offsetLeft;y=el.offsetTop;var parent=el.offsetParent;while(parent){x+=parent.offsetLeft;y+=parent.offsetTop;parent=parent.offsetParent;}} return{'x':x,'y':y};},getCoords:function(ev){ev=ev||window.event;var el=this.getDocumentElement();if(ev.pageX)return{x:ev.pageX,y:ev.pageY};return{x:ev.clientX+el.scrollLeft-el.clientLeft,y:ev.clientY+el.scrollTop-el.clientTop};},setOpacity:function(el,opacity){if(sEdit.browser=='IE'){el.style.filter=(opacity==100)?"":"gray() alpha(opacity="+opacity+")";}else{el.style.opacity=(opacity==100)?"":"0."+opacity.toString();}},showBottom:function(id){sEdit.g[id].bottom.style.display='block';},hideBottom:function(id){sEdit.g[id].bottom.style.display='none';},setDefaultPlugin:function(id){var items=['justifyleft','justifycenter','justifyright','bold','italic','underline','unlink'];for(var i=0;i';html+='editor';html += '';html+='';html+='';html+='';html+='';return html;},resize:function(id,width,height){var obj=sEdit.g[id];if(width<=obj.minWidth||height<=obj.minHeight)return;obj.container.style.width=width+'px';obj.container.style.height=height+'px';obj.formDiv.style.height=height+'px';var diff=obj.toolbarDiv.offsetHeight+obj.bottom.offsetHeight;var formBorder=obj.formDiv.offsetHeight-obj.formDiv.clientHeight;height-=diff+formBorder;if(sEdit.browser=='IE'){var border=obj.container.offsetWidth-obj.container.clientWidth;if(document.compatMode!="CSS1Compat"){height-=border;width-=border;obj.formDiv.style.height=(height+formBorder)+'px';}else{obj.formDiv.style.height=height+'px';} obj.iframe.style.height=height+'px';obj.newTextarea.style.width=(width-border)+'px';obj.newTextarea.style.height=(height-formBorder)+'px';}else{obj.formDiv.style.height=height+'px';obj.iframe.style.height=height+'px';obj.newTextarea.style.width='100%';obj.newTextarea.style.height=height+'px';}},getData:function(id){var data;if(sEdit.g[id].wyswygMode){data=sEdit.g[id].iframeDoc.body.innerHTML;}else{data=sEdit.g[id].newTextarea.value;} return sEdit.util.toBBCode(data);},setData:function(id){var data=this.getData(id);sEdit.g[id].srcTextarea.value=data;},getPureData:function(id){var data=this.getData(id,false);data=data.replace(//ig,"\r\n");data=data.replace(/<.*?>/ig,"");return data;},setPureData:function(id){var data=this.getPureData(id);sEdit.g[id].srcTextarea.value=data;},focus:function(id){if(sEdit.g[id].wyswygMode){sEdit.g[id].iframeWin.focus();}else{sEdit.g[id].newTextarea.focus();}},click:function(id,cmd){sEdit.layout.hide(id);sEdit.util.focus(id);sEdit.plugin[cmd].click(id);},selection:function(id){var win=sEdit.g[id].iframeWin;var doc=sEdit.g[id].iframeDoc;var sel=win.getSelection?win.getSelection():doc.selection;var range;try{if(sel.rangeCount>0){range=sel.getRangeAt(0);}else{range=sel.createRange?sel.createRange():doc.createRange();}}catch(e){} if(!range){range=(sEdit.browser=='IE')?doc.body.createTextRange():doc.createRange();} sEdit.g[id].selection=sel;sEdit.g[id].range=range;},select:function(id){if(sEdit.browser=='IE')sEdit.g[id].range.select();},pToBr:function(id){if(sEdit.browser=='IE'){sEdit.event.add(sEdit.g[id].iframeDoc,'keydown',function(e){if(e.keyCode==13){sEdit.util.selection(id);if(sEdit.g[id].range.parentElement().tagName!='LI'){sEdit.util.insertHtml(id,'
');return false;}}});}},execCommand:function(id,cmd,value){try{sEdit.g[id].iframeDoc.execCommand(cmd,false,value);}catch(e){} sEdit.history.add(id,false);},insertHtml:function(id,html){if(html=='')return;sEdit.util.select(id);if(sEdit.browser=='IE'){if(sEdit.g[id].selection.type.toLowerCase()=='control'){sEdit.g[id].range.item(0).outerHTML=html;}else{sEdit.g[id].range.pasteHTML(html);} sEdit.history.add(id,false);}else{this.execCommand(id,'inserthtml',html);}},insertImage:function(id,str){if(str=='')return;var image=new Image();image.src=str;if(image.width>tpk){str='';} else if(image.width>0){str='';} else{str='';} sEdit.util.insertHtml(id,str);},toBBCode:function(xhtml){function rp(r,m){xhtml=xhtml.replace(r,m);} function tabletag(attributes){var width='';re=/width=(["']?)(\d{1,3}%?)(\1)/ig;var matches=re.exec(attributes);if(matches!=null&&matches.length){width=matches[2].substr(matches[2].length-1,matches[2].length)=='%'?(matches[2].substr(0,matches[2].length-1)<=98?matches[2]:'98%'):(matches[2]<=700?matches[2]:'98%');} else{re=/width\s?:\s?(\d{1,3})([px|%])/ig;var matches=re.exec(attributes);if(matches!=null&&matches.length){width=matches[2]=='%'?(matches[1]<=98?matches[1]:'98%'):(matches[1]<=700?matches[1]:'98%');}} return width==''?'[table]':'[table='+width+']';} function in_array(needle,haystack){if(typeof needle=='string'){for(var i in haystack){if(haystack[i]==needle){return true;}}} return false;} function tablebr(str){str=str.replace(/\n/gim,"");str=str.replace(/\r/gim,"");return str;} function removeDomain(url){var domain="http://"+document.location.hostname;if(url.indexOf(domain)==0)return url.substr(domain.length);return url;} function tdtag(attributes){var colspan=1;var rowspan=1;var width='';re=/colspan=(["']?)(\d{1,2})(\1)/ig;var matches=re.exec(attributes);if(matches!=null){colspan=matches[2];} re=/rowspan=(["']?)(\d{1,2})(\1)/ig;var matches=re.exec(attributes);if(matches!=null){rowspan=matches[2];} re=/width=(["']?)(\d{1,3}%?)(\1)/ig;var matches=re.exec(attributes);if(matches!=null){width=matches[2];} return in_array(width,['','0','100%'])?(colspan==1&&rowspan==1?'[td]':'[td='+colspan+','+rowspan+']'):'[td='+colspan+','+rowspan+','+width+']';} function fonts(tab,str){re=/^( ||\s| |  )*$/gi;if(re.test(str)){return str;}re=/.*?size=[\"\']?(\d{1})[\"\']?.*?/i;var matches=re.exec(tab);if(matches!=null){str="[size="+matches[1]+"]"+str+"[/size]";} re=/.*?color=[\"\']?([#\w]{3,})[\"\']?.*?/i;var matches=re.exec(tab);if(matches!=null){str="[color="+matches[1]+"]"+str+"[/color]";} return str;} rp(/

([\s\S]*?)<\/p>/gim,"[align=$1]$2[/align]");rp(/([\s\S]*?)<\/a>/gim,function($1,$2,$3){return "[url="+removeDomain($2)+"]"+$3+"[/url]";});for(i=0;i<2;i++){rp(/]*?)>([\s\S]*?)<\/font>/gim,function($1,$2,$3){return fonts($2,$3);});} if(xhtml.match(//ig,function($1){return tablebr($1);});rp(/]*width[^>]*)>/ig,function($1,$2){return tabletag($2);});rp(/]*>/ig,'[table]');rp(/]*>/ig,'[tr]');rp(/');rp(/\[td\]/ig,'');rp(/\[\/tr\]/ig,'');}bbCode = preg_replace(['\t', ' ', '(\r\n|\n|\r)'], ['    ', '  ', '
'], bbCode); return bbCode;}};sEdit.layout={show:function(id,div) {sEdit.layout.hide(id);sEdit.g[id].hideDiv.appendChild(div);sEdit.g[id].hideDiv.style.display='block';sEdit.g[id].layoutDiv=div;},hide:function(id) {try{sEdit.g[id].hideDiv.removeChild(sEdit.g[id].layoutDiv);}catch(e){} sEdit.g[id].hideDiv.style.display='none';sEdit.g[id].maskDiv.style.display='none';sEdit.util.focus(id);},make:function(id) {var div=sEdit.$$('div');div.style.position='absolute';div.style.zIndex=19811214;return div;}};sEdit.menu=function(arg){this.arg=arg;var div=sEdit.layout.make(arg.id);div.className='e-menu';var obj=sEdit.g[arg.id].toolbarIcon[arg.cmd];var pos=sEdit.util.getElementPos(obj);div.style.top=pos.y+obj.offsetHeight+'px';div.style.left=pos.x+'px';this.div=div;this.add=function(html,event) {var cDiv=sEdit.$$('div');cDiv.className='e-menu-noselected';cDiv.style.width=this.arg.width;cDiv.onmouseover=function(){this.className='e-menu-selected';} cDiv.onmouseout=function(){this.className='e-menu-noselected';} cDiv.onclick=event;cDiv.innerHTML=html;this.append(cDiv);};this.append=function(el) {this.div.appendChild(el);};this.insert=function(html) {this.div.innerHTML=html;};this.show=function() {sEdit.layout.show(this.arg.id,this.div);};this.picker=function() {var colorTable=sEdit.lang['colorTable'];var table=sEdit.$$('table');table.cellPadding=0;table.cellSpacing=0;table.border=0;table.style.margin=0;table.style.padding=0;table.style.borderCollapse='separate';for(var i=0;i0){var prevHtml=obj.undoStack[obj.undoStack.length-1];if(html==prevHtml)return;if(minChangeFlag&&Math.abs(html.length-prevHtml.length)'+i+'';menu.add(html,new Function('sEdit.plugin["'+cmd+'"].exec("'+id+'", "'+i+'")'));};menu.show();},exec:function(id,value){sEdit.util.select(id);sEdit.util.execCommand(id,'fontsize',value.substr(0,1));sEdit.layout.hide(id);sEdit.util.focus(id);}};sEdit.plugin['source']={click:function(id){var obj=sEdit.g[id];if(obj.wyswygMode){sEdit.layout.hide(id);obj.newTextarea.value=sEdit.util.toBBCode(obj.iframeDoc.body.innerHTML);obj.iframe.style.display='none';obj.newTextarea.style.display='block';sEdit.g[id].toolbarIcon['source'].style.background="#B2BBD0";sEdit.g[id].toolbarIcon['source'].style.border='1px solid #1F3D74';sEdit.toolbar.disable(id,['source']);obj.wyswygMode=false;}else{obj.iframeDoc.body.innerHTML=sEdit.util.fromBBCode(obj.newTextarea.value);obj.iframe.style.display='block';obj.newTextarea.style.display='none';sEdit.g[id].toolbarIcon['source'].style.background='';sEdit.g[id].toolbarIcon['source'].style.border='0px';sEdit.toolbar.able(id,['source']);obj.wyswygMode=true;} sEdit.util.focus(id);}};sEdit.plugin['textcolor']={click:function(id){sEdit.util.selection(id);var menu=new sEdit.menu({id:id,cmd:'textcolor'});menu.picker();},exec:function(id,value){sEdit.util.select(id);sEdit.util.execCommand(id,'forecolor',value);sEdit.layout.hide(id);sEdit.util.focus(id);}};sEdit.plugin['paste']={click:function(id){sEdit.util.selection(id);var dialog=new sEdit.dialog({src:'/edit/ubb/paste.htm?id='+id,id:id,cmd:'paste',width:220,height:95,d_height:130,title:'插入剪切板中文本内容'});dialog.show();},insert:function(id,html){sEdit.util.insertHtml(id,sEdit.util.fromBBCode(html));sEdit.layout.hide(id);sEdit.util.focus(id);}};sEdit.plugin['table']={click:function(id){sEdit.util.selection(id);var dialog=new sEdit.dialog({src:'/edit/ubb/table.htm?id='+id,id:id,cmd:'table',width:170,height:95,d_height:132,title:'插入表格'});dialog.show();},insert:function(id,value){sEdit.util.insertHtml(id,value);sEdit.layout.hide(id);sEdit.util.focus(id);}};sEdit.plugin['quote']={click:function(id){var cmd='quote';sEdit.util.selection(id);if(sEdit.browser=='IE'){if(sEdit.g[id].selection.type.toLowerCase()=='control'){sEdit.g[id].range.item(0).outerHTML='[quote][/quote]';}else{sEdit.g[id].range.pasteHTML('[quote]'+sEdit.g[id].iframeDoc.selection.createRange().htmlText+'[/quote]');}} else{sEdit.util.execCommand(id,'inserthtml','[quote]'+sEdit.g[id].iframeDoc.getSelection()+'[/quote]');} sEdit.history.add(id,false);sEdit.util.focus(id);}};sEdit.plugin['emoticons']={click:function(id){var emoticonTable=[['1','2','3','4','5','6','7','8'],['9','10','11','12','13','14','15','16'],['17','18','19','20','21','22','23','24'],['25','26','27','28','29','30','31','32'],['33','34','35','36','37','38','38','40']];var cmd='emoticons';sEdit.util.selection(id);var table=sEdit.$$('table');table.cellPadding=0;table.cellSpacing=2;table.border=0;table.style.margin=0;table.style.padding=0;table.style.borderCollapse='separate';for(var i=0;i';sEdit.util.insertHtml(id,html);sEdit.layout.hide(id);sEdit.util.focus(id);}};sEdit.plugin['image']={click:function(id){sEdit.util.selection(id);var dialog=new sEdit.dialog({src:'/edit/ubb/ShuzirenImage.aspx?id='+id,id:id,cmd:'image',width:440,height:280,title:sEdit.lang['image'],yesButton:sEdit.lang['yes'],noButton:sEdit.lang['no']});dialog.show();},check:function(id){var dialogDoc=sEdit.util.getIframeDoc(sEdit.g[id].dialog);var type=sEdit.$('type',dialogDoc).value;if(type>1){return true;} var url='';if(type==1){url=sEdit.$('imgFile',dialogDoc).value;}else{url=sEdit.$('url',dialogDoc).value;} if(url.match(/\.(jpg|jpeg|gif|bmp|png|psd)/i)==null){alert("请输入有效的图片URL地址。\n只允许jpg,gif,bmp格式。"+url);window.focus();sEdit.g[id].yesButton.focus();return false;} return true;},exec:function(id){sEdit.util.select(id);var dialogDoc=sEdit.util.getIframeDoc(sEdit.g[id].dialog);var type=sEdit.$('type',dialogDoc).value;if(!this.check(id))return false;if(type==1){dialogDoc.uploadForm.submit();return false;}else{var url=sEdit.$('url',dialogDoc).value;this.insert(id,url);}},insert:function(id,url){var a=url.split(",");for(var i=0;i0){sEdit.util.insertHtml(id,"
");} sEdit.util.insertImage(id,a[i]);} sEdit.layout.hide(id);sEdit.util.focus(id);},insertUp:function(id,url,ks){var html='';var a=url.split(",");var b=ks.split(",");for(var i=0;i0){html+='
';} if(parseInt(b[i])>tpk){html+='';} else{html+='';}}} sEdit.util.insertHtml(id,html);sEdit.layout.hide(id);sEdit.util.focus(id);}};sEdit.plugin['link']={click:function(id){sEdit.util.selection(id);var dialog=new sEdit.dialog({src:'/edit/ubb/link.htm',id:id,cmd:'link',width:300,height:30,title:sEdit.lang['link'],yesButton:sEdit.lang['yes'],noButton:sEdit.lang['no']});dialog.show();},exec:function(id){sEdit.util.select(id);var iframeDoc=sEdit.g[id].iframeDoc;var range=sEdit.g[id].range;var dialogDoc=sEdit.util.getIframeDoc(sEdit.g[id].dialog);var url=sEdit.$('url',dialogDoc).value;if(url.length<1){alert("请填写超链接URL地址");window.focus();sEdit.g[id].yesButton.focus();return false;} var node;if(sEdit.browser=='IE'){node=range.item?range.item(0).parentNode:iframeDoc.body;}else{node=(range.startContainer==range.endContainer)?range.startContainer.parentNode:iframeDoc.body;} if(node&&node.tagName=='A')node=node.parentNode;if(!node)node=iframeDoc.body;iframeDoc.execCommand("createlink",false,"__temp_url__");var arr=node.getElementsByTagName('a');for(var i=0,l=arr.length;i
/ig,'[td]');rp(/]+)>/ig,function($1,$2){return tdtag($2);});rp(/<\/td>/ig,'[/td]');rp(/<\/tr>/ig,'[/tr]');rp(/<\/table>/ig,'[/table]');} rp(/([ |\s]{0,}[ |\s]{0,}){3,}/gi,"\n\n");rp(//gi,"\n");rp(/<(b|strong|u|i)>( ||\s| |  ){1,}<\/(b|strong|u|i)>/gi,"");rp(/(.*?)<\/strong>/gi,"[b]$1[/b]");rp(/(.*?)<\/b>/gi,"[b]$1[/b]");rp(/(.*?)<\/em>/gi,"[i]$1[/i]");rp(/(.*?)<\/u>/gi,"[u]$1[/u]");rp(//gi,"[em$1]");rp(//gi,function($1,$2){return"[img]"+removeDomain($2)+"[/img]";});rp(/ /gi," ");rp(/<[^>]+>/g,'');return xhtml;},fromBBCode:function(bbCode){function rp(r,m){bbCode=bbCode.replace(r,m);} function parsetable(width){if(typeof width=='undefined'){var width='98%';} else{width=width.substr(width.length-1,width.length)=='%'?(width.substr(0,width.length-1)<=98?width:'98%'):(width<=700?width:'98%');} return'';} function DrawImage(str){var image=new Image();image.src=str;if(image.width>tpk){return'';} else if(image.width>0){return'';} else{return'';}} function preg_replace(search,replace,str,regswitch){var regswitch=!regswitch?'ig':regswitch;var len=search.length;for(var i=0;i$1");rp(/\[i\](.*?)\[\/i\]/gi,"$1");rp(/\[u\](.*?)\[\/u\]/gi,"$1");rp(/\[em(.[^\[]*)\]/gi,"");rp(/\[color=([#\w]{3,})\]/gi,"");rp(/\[size=(\d{1})\]/gi,"");rp(/\[\/(color|size)\]/gi,"");rp(/\[img\](.+?)\[\/img\]/gi,function($1,$2){return DrawImage($2);});rp(/\[url=([^\]]+)\](.*?)\[\/url\]/gim,"$2");rp(/\[url\](.*?)\[\/url\]/gim,"$1");rp(/\[align=(left|center|right)\]([\s\S]*?)\[\/align\]/gim,"

$2

");if(bbCode.match(/\[table/ig)){rp(/\[table(=(\d{1,3}%?))?\]/ig,function($1,$2,$3){return parsetable($3);});rp(/\[\/table\]/ig,'
');rp(/\[td=(\d{1,2}),(\d{1,2})(,(\d{1,3}%?))?\]/ig,'
');rp(/\[tr\]/ig,'
');rp(/\[\/td\]/ig,'