Advertisement
Guest User

tinyeditor ie9 fix

a guest
Oct 10th, 2011
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TINY={};
  2.  
  3. function T$(i){return document.getElementById(i)}
  4. function T$$$(){return document.all?1:0}
  5.  
  6. TINY.editor=function(){
  7.     var c=[], offset=-30;
  8.     c['cut']=[1,'Cut','a','cut',1];
  9.     c['copy']=[2,'Copy','a','copy',1];
  10.     c['paste']=[3,'Paste','a','paste',1];
  11.     c['bold']=[4,'Bold','a','bold'];
  12.     c['italic']=[5,'Italic','a','italic'];
  13.     c['underline']=[6,'Underline','a','underline'];
  14.     c['strikethrough']=[7,'Strikethrough','a','strikethrough'];
  15.     c['subscript']=[8,'Subscript','a','subscript'];
  16.     c['superscript']=[9,'Superscript','a','superscript'];
  17.     c['orderedlist']=[10,'Insert Ordered List','a','insertorderedlist'];
  18.     c['unorderedlist']=[11,'Insert Unordered List','a','insertunorderedlist'];
  19.     c['outdent']=[12,'Outdent','a','outdent'];
  20.     c['indent']=[13,'Indent','a','indent'];
  21.     c['leftalign']=[14,'Left Align','a','justifyleft'];
  22.     c['centeralign']=[15,'Center Align','a','justifycenter'];
  23.     c['rightalign']=[16,'Right Align','a','justifyright'];
  24.     c['blockjustify']=[17,'Block Justify','a','justifyfull'];
  25.     c['undo']=[18,'Undo','a','undo'];
  26.     c['redo']=[19,'Redo','a','redo'];
  27.     c['image']=[20,'Insert Image','i','insertimage','Enter Image URL:','http://'];
  28.     c['hr']=[21,'Insert Horizontal Rule','a','inserthorizontalrule'];
  29.     c['link']=[22,'Insert Hyperlink','i','createlink','Enter URL:','http://'];
  30.     c['unlink']=[23,'Remove Hyperlink','a','unlink'];
  31.     c['unformat']=[24,'Remove Formatting','a','removeformat'];
  32.     c['print']=[25,'Print','a','print'];
  33.    
  34.     function edit(n,obj){
  35.         this.n=n; window[n]=this; this.t=T$(obj.id); this.obj=obj; this.xhtml=obj.xhtml;
  36.         var p=document.createElement('div'), w=document.createElement('div'), h=document.createElement('div'),
  37.         l=obj.controls.length, i=0;
  38.         this.i=document.createElement('iframe'); this.i.frameBorder=0;
  39.         this.i.width=obj.width||'500'; this.i.height=obj.height||'250'; this.ie=T$$$();
  40.         h.className=obj.rowclass||'teheader'; p.className=obj.cssclass||'te'; p.style.width=this.i.width+'px'; p.appendChild(h);
  41.         for(i;i<l;i++){
  42.             var id=obj.controls[i];
  43.             if(id=='n'){
  44.                 h=document.createElement('div'); h.className=obj.rowclass||'teheader'; p.appendChild(h)
  45.             }else if(id=='|'){
  46.                 var d=document.createElement('div'); d.className=obj.dividerclass||'tedivider'; h.appendChild(d)
  47.             }else if(id=='font'){
  48.                 var sel=document.createElement('select'), fonts=obj.fonts||['Verdana','Arial','Georgia'], fl=fonts.length, x=0;
  49.                 sel.className='tefont'; sel.onchange=new Function(this.n+'.ddaction(this,"fontname")');
  50.                 sel.options[0]=new Option('Font','');
  51.                 for(x;x<fl;x++){
  52.                     var font=fonts[x];
  53.                     sel.options[x+1]=new Option(font,font)
  54.                 }
  55.                 h.appendChild(sel)
  56.             }else if(id=='size'){
  57.                 var sel=document.createElement('select'), sizes=obj.sizes||[1,2,3,4,5,6,7], sl=sizes.length, x=0;
  58.                 sel.className='tesize'; sel.onchange=new Function(this.n+'.ddaction(this,"fontsize")');
  59.                 for(x;x<sl;x++){
  60.                     var size=sizes[x];
  61.                     sel.options[x]=new Option(size,size)
  62.                 }
  63.                 h.appendChild(sel)
  64.             }else if(id=='style'){
  65.                 var sel=document.createElement('select'),
  66.                 styles=obj.styles||[['Style',''],['Paragraph','<p>'],['Header 1','<h1>'],['Header 2','<h2>'],['Header 3','<h3>'],['Header 4','<h4>'],['Header 5','<h5>'],['Header 6','<h6>']],
  67.                 sl=styles.length, x=0;
  68.                 sel.className='testyle'; sel.onchange=new Function(this.n+'.ddaction(this,"formatblock")');
  69.                 for(x;x<sl;x++){
  70.                     var style=styles[x];
  71.                     sel.options[x]=new Option(style[0],style[1])
  72.                 }
  73.                 h.appendChild(sel)
  74.             }else if(c[id]){
  75.                 var div=document.createElement('div'), x=c[id], func=x[2], ex, pos=x[0]*offset;
  76.                 div.className=obj.controlclass;
  77.                 div.style.backgroundPosition='0px '+pos+'px';
  78.                 div.title=x[1];
  79.                 ex=func=='a'?'.action("'+x[3]+'",0,'+(x[4]||0)+')':'.insert("'+x[4]+'","'+x[5]+'","'+x[3]+'")';
  80.                 div.onclick=new Function(this.n+(id=='print'?'.print()':ex));
  81.                 div.onmouseover=new Function(this.n+'.hover(this,'+pos+',1)');
  82.                 div.onmouseout=new Function(this.n+'.hover(this,'+pos+',0)');
  83.                 h.appendChild(div);
  84.                 if(this.ie){div.unselectable='on'}
  85.             }
  86.         }
  87.         this.t.parentNode.insertBefore(p,this.t); this.t.style.width=this.i.width+'px';
  88.         w.appendChild(this.t); w.appendChild(this.i); p.appendChild(w); this.t.style.display='none';
  89.         if(obj.footer){
  90.             var f=document.createElement('div'); f.className=obj.footerclass||'tefooter';
  91.             if(obj.toggle){
  92.                 var to=obj.toggle, ts=document.createElement('div');
  93.                 ts.className=to.cssclass||'toggle'; ts.innerHTML=obj.toggletext||'source';
  94.                 ts.onclick=new Function(this.n+'.toggle(0,this);return false');
  95.                 f.appendChild(ts)
  96.             }
  97.             if(obj.resize){
  98.                 var ro=obj.resize, rs=document.createElement('div'); rs.className=ro.cssclass||'resize';
  99.                 rs.onmousedown=new Function('event',this.n+'.resize(event);return false');
  100.                 rs.onselectstart=function(){return false};
  101.                 f.appendChild(rs)
  102.             }
  103.             p.appendChild(f)
  104.         }
  105.         this.e=this.i.contentWindow.document; this.e.open();
  106.         var m='<html><head>', bodyid=obj.bodyid?" id=\""+obj.bodyid+"\"":"";
  107.         if(obj.cssfile){m+='<link rel="stylesheet" href="'+obj.cssfile+'" />'}
  108.         if(obj.css){m+='<style type="text/css">'+obj.css+'</style>'}
  109.         m+='</head><body'+bodyid+'>'+(obj.content||this.t.value);
  110.         m+='</body></html>';
  111.         this.e.write(m);
  112.         this.e.close(); this.e.designMode='on'; this.d=1;
  113.         if(this.xhtml){
  114.             try{this.e.execCommand("styleWithCSS",0,0)}
  115.             catch(e){try{this.e.execCommand("useCSS",0,1)}catch(e){}}
  116.         }
  117.     };
  118.     edit.prototype.print=function(){
  119.         this.i.contentWindow.print()
  120.     },
  121.     edit.prototype.hover=function(div,pos,dir){
  122.     this.getSelection();
  123.         div.style.backgroundPosition=(dir?'34px ':'0px ')+(pos)+'px'
  124.     },
  125.     edit.prototype.ddaction=function(dd,a){
  126.         var i=dd.selectedIndex, v=dd.options[i].value;
  127.         this.action(a,v)
  128.     },
  129.   edit.prototype.getSelection=function(){
  130.     if (this.e.getSelection && this.ie) {
  131.       sel = this.e.getSelection();
  132.       if (sel.getRangeAt && sel.rangeCount) {
  133.         this.range = sel.getRangeAt(0);
  134.       }
  135.     }
  136.   },
  137.   edit.prototype.restoreSelection=function(){
  138.     if (this.range && this.ie) {
  139.       if (this.e.getSelection) {
  140.         sel = this.e.getSelection();
  141.         sel.removeAllRanges();
  142.         sel.addRange(this.range);
  143.       }
  144.     }
  145.   },
  146.     edit.prototype.action=function(cmd,val,ie){
  147.         if(ie&&!this.ie){
  148.             alert('Your browser does not support this function.')
  149.         }else{
  150.       this.restoreSelection();
  151.             this.e.execCommand(cmd,0,val||null)
  152.         }
  153.     },
  154.     edit.prototype.insert=function(pro,msg,cmd){
  155.         var val=prompt(pro,msg);
  156.         if(val!=null&&val!=''){this.e.execCommand(cmd,0,val)}
  157.     },
  158.     edit.prototype.setfont=function(){
  159.     this.restoreSelection();
  160.         execCommand('formatblock',0,hType)
  161.     },
  162.     edit.prototype.resize=function(e){
  163.         if(this.mv){this.freeze()}
  164.         this.i.bcs=TINY.cursor.top(e);
  165.         this.mv=new Function('event',this.n+'.move(event)');
  166.         this.sr=new Function(this.n+'.freeze()');
  167.         if(this.ie){
  168.             document.attachEvent('onmousemove',this.mv); document.attachEvent('onmouseup',this.sr)
  169.         }else{
  170.             document.addEventListener('mousemove',this.mv,1); document.addEventListener('mouseup',this.sr,1)
  171.         }
  172.     },
  173.     edit.prototype.move=function(e){
  174.         var pos=TINY.cursor.top(e);
  175.         this.i.height=parseInt(this.i.height)+pos-this.i.bcs;
  176.         this.i.bcs=pos
  177.     },
  178.     edit.prototype.freeze=function(){
  179.         if(this.ie){
  180.             document.detachEvent('onmousemove',this.mv); document.detachEvent('onmouseup',this.sr)
  181.         }else{
  182.             document.removeEventListener('mousemove',this.mv,1); document.removeEventListener('mouseup',this.sr,1)
  183.         }
  184.     },
  185.     edit.prototype.toggle=function(post,div){
  186.         if(!this.d){
  187.             var v=this.t.value;
  188.             if(div){div.innerHTML=this.obj.toggletext||'source'}
  189.             if(this.xhtml&&!this.ie){
  190.                 v=v.replace(/<strong>(.*)<\/strong>/gi,'<span style="font-weight: bold;">$1</span>');
  191.                 v=v.replace(/<em>(.*)<\/em>/gi,'<span style="font-weight: italic;">$1</span>')
  192.             }
  193.             this.e.body.innerHTML=v;
  194.             this.t.style.display='none'; this.i.style.display='block'; this.d=1
  195.         }else{
  196.             var v=this.e.body.innerHTML;
  197.             if(this.xhtml){
  198.                 v=v.replace(/<span class="apple-style-span">(.*)<\/span>/gi,'$1');
  199.                 v=v.replace(/ class="apple-style-span"/gi,'');
  200.                 v=v.replace(/<span style="">/gi,'');
  201.                 v=v.replace(/<br>/gi,'<br />');
  202.                 v=v.replace(/<br ?\/?>$/gi,'');
  203.                 v=v.replace(/^<br ?\/?>/gi,'');
  204.                 v=v.replace(/(<img [^>]+[^\/])>/gi,'$1 />');
  205.                 v=v.replace(/<b\b[^>]*>(.*?)<\/b[^>]*>/gi,'<strong>$1</strong>');
  206.                 v=v.replace(/<i\b[^>]*>(.*?)<\/i[^>]*>/gi,'<em>$1</em>');
  207.                 v=v.replace(/<u\b[^>]*>(.*?)<\/u[^>]*>/gi,'<span style="text-decoration:underline">$1</span>');
  208.                 v=v.replace(/<(b|strong|em|i|u) style="font-weight: normal;?">(.*)<\/(b|strong|em|i|u)>/gi,'$2');
  209.                 v=v.replace(/<(b|strong|em|i|u) style="(.*)">(.*)<\/(b|strong|em|i|u)>/gi,'<span style="$2"><$4>$3</$4></span>');
  210.                 v=v.replace(/<span style="font-weight: normal;?">(.*)<\/span>/gi,'$1');
  211.                 v=v.replace(/<span style="font-weight: bold;?">(.*)<\/span>/gi,'<strong>$1</strong>');
  212.                 v=v.replace(/<span style="font-style: italic;?">(.*)<\/span>/gi,'<em>$1</em>');
  213.                 v=v.replace(/<span style="font-weight: bold;?">(.*)<\/span>|<b\b[^>]*>(.*?)<\/b[^>]*>/gi,'<strong>$1</strong>')
  214.             }
  215.             if(div){div.innerHTML=this.obj.toggletext||'wysiwyg'}
  216.             this.t.value=v;
  217.             if(!post){
  218.                 this.t.style.height=this.i.height+'px';
  219.                 this.i.style.display='none'; this.t.style.display='block'; this.d=0
  220.             }
  221.         }
  222.     },
  223.     edit.prototype.post=function(){
  224.         if(this.d){this.toggle(1)}
  225.     };
  226.     return{edit:edit}
  227. }();
  228.  
  229. TINY.cursor=function(){
  230.     return{
  231.         top:function(e){
  232.             return T$$$()?window.event.clientY+document.documentElement.scrollTop+document.body.scrollTop:e.clientY+window.scrollY
  233.         }
  234.     }
  235. }();
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement