Guest User

Jitbit

a guest
Jun 27th, 2010
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. WYSIWYG-BBCODE editor
  3. Copyright (c) 2009, Jitbit Sotware, http://www.jitbit.com/
  4. http://code.google.com/p/jitbit-wysiwyg-bbcode/
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9.     * Redistributions of source code must retain the above copyright
  10.       notice, this list of conditions and the following disclaimer.
  11.     * Redistributions in binary form must reproduce the above copyright
  12.       notice, this list of conditions and the following disclaimer in the
  13.       documentation and/or other materials provided with the distribution.
  14.     * Neither the name of the <organization> nor the
  15.       names of its contributors may be used to endorse or promote products
  16.       derived from this software without specific prior written permission.
  17.  
  18. THIS SOFTWARE IS PROVIDED BY Jitbit Software ''AS IS'' AND ANY
  19. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. DISCLAIMED. IN NO EVENT SHALL Jitbit Software BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29.  
  30. var myeditor, ifm;
  31. var body_id, textboxelement;
  32. var content;
  33. var isIE = /msie|MSIE/.test(navigator.userAgent);
  34. var isChrome = /Chrome/.test(navigator.userAgent);
  35. var isSafari = /Safari/.test(navigator.userAgent) && !isChrome;
  36. var browser = isIE || window.opera;
  37. var textRange;
  38. var enter = 0;
  39. var editorVisible = false;
  40. var enableWysiwyg = false;
  41.  
  42. function rep(re, str) {
  43.     content = content.replace(re, str);
  44. }
  45.  
  46. function initEditor(textarea_id, wysiwyg) {
  47.     if(wysiwyg!=undefined)
  48.         enableWysiwyg = wysiwyg;
  49.     else
  50.         enableWysiwyg = true;
  51.     body_id = textarea_id;
  52.     textboxelement = document.getElementById(body_id);
  53.     textboxelement.setAttribute('class', 'editorBBCODE');
  54.     textboxelement.className = "editorBBCODE";
  55.     if (enableWysiwyg) {
  56.         ifm = document.createElement("iframe");
  57.         ifm.setAttribute("id", "rte");
  58.         ifm.setAttribute("frameborder", "0");
  59.         ifm.style.width = textboxelement.style.width;
  60.         ifm.style.height = textboxelement.style.height;
  61.         textboxelement.parentNode.appendChild(ifm);
  62.         textboxelement.style.display = 'none';
  63.         if (ifm) {
  64.             ShowEditor();
  65.         } else
  66.             setTimeout('ShowEditor()', 100);
  67.     }
  68. }
  69.  
  70. function getStyle(el,styleProp)
  71. {
  72.     var x = document.getElementById(el);
  73.     if (x.currentStyle)
  74.         var y = x.currentStyle[styleProp];
  75.     else if (window.getComputedStyle)
  76.         var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
  77.     return y;
  78. }
  79.  
  80. function ShowEditor() {
  81.     if (!enableWysiwyg) return;
  82.     editorVisible = true;
  83.     content = document.getElementById(body_id).value;
  84.     myeditor = ifm.contentWindow.document;
  85.     bbcode2html();
  86.     myeditor.designMode = "on";
  87.     myeditor.open();
  88.     myeditor.write('<html><head><link href="editor.css" rel="Stylesheet" type="text/css" /></head>');
  89.     myeditor.write('<body style="margin:0px 0px 0px 0px" class="editorWYSIWYG">');
  90.     myeditor.write(content);
  91.     myeditor.write('</body></html>');
  92.     myeditor.close();
  93.     if (myeditor.attachEvent) {
  94.         if(parent.ProcessKeyPress)
  95.             myeditor.attachEvent("onkeydown", parent.ProcessKeyPress);
  96.         myeditor.attachEvent("onkeypress", kp);
  97.     }
  98.     else if (myeditor.addEventListener) {
  99.         if (parent.ProcessKeyPress)
  100.             myeditor.addEventListener("keydown", parent.ProcessKeyPress, true);
  101.         myeditor.addEventListener("keypress",kp,true);
  102.     }
  103. }
  104.  
  105. function SwitchEditor() {
  106.     if (editorVisible) {
  107.         doCheck();
  108.         ifm.style.display = 'none';
  109.         textboxelement.style.display = '';
  110.         editorVisible = false;
  111.     }
  112.     else {
  113.         if (enableWysiwyg && ifm) {
  114.             ifm.style.display = '';
  115.             textboxelement.style.display = 'none';
  116.             ShowEditor();
  117.             editorVisible = true;
  118.         }
  119.     }
  120. }
  121.  
  122. function html2bbcode() {
  123.     rep(/<img\s[^<>]*?src=\"?([^<>]*?)\"?(\s[^<>]*)?\/?>/gi,"[img]$1[/img]");
  124.     rep(/\<code\>/gi, "[code]");
  125.     rep(/\<\/code\>/gi, "[/code]");
  126.     rep(/\<blockquote\>/gi, "[quote]");
  127.     rep(/\<\/blockquote\>/gi, "[/quote]");
  128.    
  129.     rep(/\<span style=\"color: green;\"\>\[flash: /gi, "[flash]");
  130.     rep(/ :flash\]\<\/span\>/gi, "[/flash]");
  131.     rep(/\<span style=\"color: green;\"\>\[youtube: /gi, "[youtube]");
  132.     rep(/ :youtube\]\<\/span\>/gi, "[/youtube]");
  133.     rep(/\<span style=\"color: green;\"\>\[widget: /gi, "[widget]");
  134.     rep(/ :widget\]\<\/span\>/gi, "[/widget]");
  135.    
  136.     rep(/<\/(strong|b)>/gi, "[/b]");
  137.     rep(/<(strong|b)(\s[^<>]*)?>/gi,"[b]");
  138.     rep(/<\/(em|i)>/gi,"[/i]");
  139.     rep(/<(em|i)(\s[^<>]*)?>/gi,"[i]");
  140.     rep(/<\/u>/gi, "[/u]");
  141.     rep(/\n/gi, "");
  142.     rep(/\r/gi, "");
  143.     rep(/<u(\s[^<>]*)?>/gi,"[u]");
  144.     rep(/<br(\s[^<>]*)?>/gi,"\n");
  145.     rep(/<p(\s[^<>]*)?>/gi,"");
  146.     rep(/<\/p>/gi, "\n");
  147.     rep(/<ul>/gi, "[ul]");
  148.     rep(/<\/ul>/gi, "[/ul]");
  149.     rep(/<li>/gi, "[li]");
  150.     rep(/<\/li>/gi, "[/li]");
  151.     rep(/<\/div>\s*<div([^<>]*)>/gi, "</span>\n<span$1>");//chrome-safari fix to prevent double linefeeds
  152.     rep(/<div([^<>]*)>/gi,"\n<span$1>");
  153.     rep(/<\/div>/gi,"</span>\n");
  154.     rep(/&nbsp;/gi," ");
  155.     rep(/&quot;/gi,"\"");
  156.     rep(/&amp;/gi,"&");
  157.     var sc, sc2;
  158.     do {
  159.         sc = content;
  160.         rep(/<font\s[^<>]*?color=\"?([^<>]*?)\"?(\s[^<>]*)?>([^<>]*?)<\/font>/gi,"[color=$1]$3[/color]");
  161.         if(sc==content)
  162.             rep(/<font[^<>]*>([^<>]*?)<\/font>/gi,"$1");
  163.         rep(/<a\s[^<>]*?href=\"?([^<>]*?)\"?(\s[^<>]*)?>([^<>]*?)<\/a>/gi,"[url=$1]$3[/url]");
  164.         rep(/<span\s[^<>]*?style=\"font-size: ?([^<>]*?);\"?(\s[^<>]*)?>([^<>]*?)<\/span>/gi,"[size=$1]$3[/size]");
  165.         sc2 = content;
  166.         rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-weight: ?bold;?\"?\s*([^<]*?)<\/\1>/gi,"[b]<$1 style=$2</$1>[/b]");
  167.         rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-weight: ?normal;?\"?\s*([^<]*?)<\/\1>/gi,"<$1 style=$2</$1>");
  168.         rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-style: ?italic;?\"?\s*([^<]*?)<\/\1>/gi,"[i]<$1 style=$2</$1>[/i]");
  169.         rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?font-style: ?normal;?\"?\s*([^<]*?)<\/\1>/gi,"<$1 style=$2</$1>");
  170.         rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?text-decoration: ?underline;?\"?\s*([^<]*?)<\/\1>/gi,"[u]<$1 style=$2</$1>[/u]");
  171.         rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?text-decoration: ?none;?\"?\s*([^<]*?)<\/\1>/gi,"<$1 style=$2</$1>");
  172.         rep(/<(span|blockquote|pre)\s[^<>]*?style=\"?color: ?([^<>]*?);\"?\s*([^<]*?)<\/\1>/gi,"[color=$2]<$1 style=$3</$1>[/color]");
  173.         rep(/<(blockquote|pre)\s[^<>]*?style=\"?\"? (class=|id=)([^<>]*)>([^<>]*?)<\/\1>/gi,"<$1 $2$3>$4</$1>");
  174.         rep(/<span\s[^<>]*?style=\"?\"?>([^<>]*?)<\/span>/gi, "$1");
  175.         if(sc2==content) {
  176.             rep(/<span[^<>]*>([^<>]*?)<\/span>/gi, "$1");
  177.             sc2 = content;
  178.             rep(/<pre\s[^<>]*?class=\"?code\"?[^<>]*?>([^<>]*?)<\/pre>/gi,"[code]$1[/code]");
  179.             if(sc2==content) {
  180.                 rep(/<blockquote\s[^<>]*?class=\"?memberquote\"?[^<>]*?id=\"?([^<>\"]*)\"?>([^<>]*?)<\/blockquote>/gi,"[quote$1]$2[/quote]");
  181.                 if(sc2==content) {
  182.                     rep(/<blockquote\s[^<>]*?id=\"?([^<>\"]*?)\"? class=\"?memberquote\"?[^<>]*?>([^<>]*?)<\/blockquote>/gi,"[quote$1]$2[/quote]");
  183.                     if(sc2==content)
  184.                         rep(/<blockquote\s[^<>]*?class=\"?memberquote\"?[^<>]*?>([^<>]*?)<\/blockquote>/gi,"[quote]$1[/quote]");
  185.                 }
  186.             }
  187.         }
  188.     }while(sc!=content)
  189.     rep(/<[^<>]*>/gi,"");
  190.     rep(/&lt;/gi,"<");
  191.     rep(/&gt;/gi,">");
  192.    
  193.     do {
  194.         sc = content;
  195.         rep(/\[(b|i|u)\]\[quote([^\]]*)\]([\s\S]*?)\[\/quote\]\[\/\1\]/gi, "[quote$2][$1]$3[/$1][/quote]");
  196.         rep(/\[color=([^\]]*)\]\[quote([^\]]*)\]([\s\S]*?)\[\/quote\]\[\/color\]/gi, "[quote$2][color=$1]$3[/color][/quote]");
  197.         rep(/\[(b|i|u)\]\[code\]([\s\S]*?)\[\/code\]\[\/\1\]/gi, "[code][$1]$2[/$1][/code]");
  198.         rep(/\[color=([^\]]*)\]\[code\]([\s\S]*?)\[\/code\]\[\/color\]/gi, "[code][color=$1]$2[/color][/code]");
  199.     }while(sc!=content)
  200.  
  201.     //clean up empty tags
  202.     do {
  203.         sc = content;
  204.         rep(/\[b\]\[\/b\]/gi, "");
  205.         rep(/\[i\]\[\/i\]/gi, "");
  206.         rep(/\[u\]\[\/u\]/gi, "");
  207.         rep(/\[quote[^\]]*\]\[\/quote\]/gi, "");
  208.         rep(/\[code\]\[\/code\]/gi, "");
  209.         rep(/\[url=([^\]]+)\]\[\/url\]/gi, "");
  210.         rep(/\[img\]\[\/img\]/gi, "");
  211.         rep(/\[color=([^\]]*)\]\[\/color\]/gi, "");
  212.     }while(sc!=content)
  213. }
  214.  
  215. function bbcode2html() {
  216.     // example: [b] to <strong>
  217.     rep(/--- Last Edited by [\s\S]*? ---/gi,"");
  218.     rep(/\n/gi, "<br />");
  219.     rep(/\[ul\]/gi, "<ul>");
  220.     rep(/\[\/ul\]/gi, "</ul>");
  221.     rep(/\[li\]/gi, "<li>");
  222.     rep(/\[\/li\]/gi, "</li>");
  223.     rep(/\[code\]/gi, "<code>");
  224.     rep(/\[\/code\]/gi, "</code>");
  225.     rep(/\[quote\]/gi, "<blockquote>");
  226.     rep(/\[\/quote\]/gi, "</blockquote>");
  227.     rep(/\[flash\]/gi, "<span style=\"color: green;\">[flash: ");
  228.     rep(/\[\/flash\]/gi, " :flash]</span>");
  229.     rep(/\[youtube\]/gi, "<span style=\"color: green;\">[youtube: ");
  230.     rep(/\[\/youtube\]/gi, " :youtube]</span>");
  231.     rep(/\[widget\]/gi, "<span style=\"color: green;\">[widget: ");
  232.     rep(/\[\/widget\]/gi, " :widget]</span>");
  233.     if(browser) {
  234.         rep(/\[b\]/gi,"<strong>");
  235.         rep(/\[\/b\]/gi,"</strong>");
  236.         rep(/\[i\]/gi,"<em>");
  237.         rep(/\[\/i\]/gi,"</em>");
  238.         rep(/\[u\]/gi,"<u>");
  239.         rep(/\[\/u\]/gi,"</u>");
  240.     }else {
  241.         rep(/\[b\]/gi,"<span style=\"font-weight: bold;\">");
  242.         rep(/\[i\]/gi,"<span style=\"font-style: italic;\">");
  243.         rep(/\[u\]/gi,"<span style=\"text-decoration: underline;\">");
  244.         rep(/\[\/(b|i|u)\]/gi,"</span>");
  245.     }
  246.     //rep(/\[img\]([\s\S]*?)\[\/img\]/gi,"<img src=\"$1\" />");
  247.     rep(/\[img\]([^\"]*?)\[\/img\]/gi,"<img src=\"$1\" />");
  248.     var sc;
  249.     do {
  250.         sc = content;
  251.         rep(/\[url=([^\]]+)\]([\s\S]*?)\[\/url\]/gi,"<a href=\"$1\">$2</a>");
  252.         rep(/\[url\]([\s\S]*?)\[\/url\]/gi,"<a href=\"$1\">$1</a>");
  253.         rep(/\[size=([^\]]+)\]([\s\S]*?)\[\/size\]/gi,"<span style=\"font-size: $1;\">$2</span>");
  254.         if(browser) {
  255.             rep(/\[color=([^\]]*?)\]([\s\S]*?)\[\/color\]/gi,"<font color=\"$1\">$2</font>");
  256.         }else {
  257.             rep(/\[color=([^\]]*?)\]([\s\S]*?)\[\/color\]/gi,"<span style=\"color: $1;\">$2</span>");
  258.         }
  259.         rep(/\[code\]([\s\S]*?)\[\/code\]/gi,"<pre>$1</pre>&nbsp;");
  260.         //rep(/\[quote([^\]]*?)\]([\s\S]*?)\[\/quote\]/gi,"<blockquote class=\"memberquote\" id=\"$1\">$2</blockquote>&nbsp;");
  261.     }while(sc!=content)
  262. }
  263.  
  264. function doCheck() {
  265.     if (!editorVisible) {
  266.         ShowEditor();
  267.     }
  268.     content = myeditor.body.innerHTML;
  269.     html2bbcode();
  270.     document.getElementById(body_id).value = content;
  271. }
  272.  
  273. function stopEvent(evt){
  274.     evt || window.event;
  275.     if (evt.stopPropagation){
  276.         evt.stopPropagation();
  277.         evt.preventDefault();
  278.     }else if(typeof evt.cancelBubble != "undefined"){
  279.         evt.cancelBubble = true;
  280.         evt.returnValue = false;
  281.     }
  282.     return false;
  283. }
  284.  
  285. function doQuote() {
  286.     if (editorVisible) {
  287.         ifm.contentWindow.focus();
  288.         if (isIE) {
  289.             textRange = ifm.contentWindow.document.selection.createRange();
  290.             var newTxt = "[quote]" + textRange.text + "[/quote]";
  291.             textRange.text = newTxt;
  292.         }
  293.         else {
  294.             var edittext = ifm.contentWindow.getSelection().getRangeAt(0);
  295.             var original = edittext.toString();
  296.             edittext.deleteContents();
  297.             edittext.insertNode(document.createTextNode("[quote]" + original + "[/quote]"));
  298.         }
  299.     }
  300.     else {
  301.         AddTag('[quote]', '[/quote]');
  302.     }
  303. }
  304.  
  305. function kp(e){
  306.     if(isIE)
  307.         var k = e.keyCode;
  308.     else
  309.         var k = e.which;
  310.     if(k==13) {
  311.         if(isIE) {
  312.             var r = myeditor.selection.createRange();
  313.             if (r.parentElement().tagName.toLowerCase() != "li") {
  314.                 r.pasteHTML('<br/>');
  315.                 if (r.move('character'))
  316.                     r.move('character', -1);
  317.                 r.select();
  318.                 stopEvent(e);
  319.                 return false;
  320.             }
  321.         }
  322.     }else
  323.         enter = 0;
  324. }
  325.  
  326. function InsertSmile(txt) {
  327.     InsertText(txt);
  328.     document.getElementById('divSmilies').style.display = 'none';
  329. }
  330. function InsertYoutube() {
  331.     if (editorVisible) {
  332.         ifm.contentWindow.focus();
  333.         var mylink = prompt("Enter a Youtube ID:", "");
  334.         if ((mylink != null) && (mylink != "")) {
  335.             if (isIE) { //IE
  336.                 var range = ifm.contentWindow.document.selection.createRange();
  337.                 if (range.text == '') {
  338.                     range.pasteHTML("<span style=\"color: green;\">[youtube: " + mylink + " : youtube]</span>");
  339.                 }
  340.                 else
  341.                     myeditor.execCommand("CreateLink", false, mylink);
  342.             }
  343.             else if (window.getSelection) { //FF
  344.                 var userSelection = ifm.contentWindow.getSelection().getRangeAt(0);
  345.                 if(userSelection.toString().length==0)
  346.                     myeditor.execCommand('inserthtml', false, "<span style=\"color: green;\">[youtube: " + mylink + " :youtube]</span>");
  347.                 else
  348.                     myeditor.execCommand("CreateLink", false, mylink);
  349.             }
  350.             else
  351.                 myeditor.execCommand("CreateLink", false, mylink);
  352.         }
  353.     }
  354.     else {
  355.         AddTag('[flash]http://www.youtube.com/watch?v=', '[/flash]');
  356.     }
  357. }
  358. function InsertFlash() {
  359.     if (editorVisible) {
  360.         ifm.contentWindow.focus();
  361.         var mylink = prompt("Enter a URL:", "http://");
  362.         if ((mylink != null) && (mylink != "")) {
  363.             if (isIE) { //IE
  364.                 var range = ifm.contentWindow.document.selection.createRange();
  365.                 if (range.text == '') {
  366.                     range.pasteHTML("<span style=\"color: green;\">[flash: " + mylink + " : flash]</span>");
  367.                 }
  368.                 else
  369.                     myeditor.execCommand("CreateLink", false, mylink);
  370.             }
  371.             else if (window.getSelection) { //FF
  372.                 var userSelection = ifm.contentWindow.getSelection().getRangeAt(0);
  373.                 if(userSelection.toString().length==0)
  374.                     myeditor.execCommand('inserthtml', false, "<span style=\"color: green;\">[flash: " + mylink + " :flash]</span>");
  375.                 else
  376.                     myeditor.execCommand("CreateLink", false, mylink);
  377.             }
  378.             else
  379.                 myeditor.execCommand("CreateLink", false, mylink);
  380.         }
  381.     }
  382.     else {
  383.         AddTag('[flash]', '[/flash]');
  384.     }
  385. }
  386. function InsertText(txt) {
  387.     if (editorVisible)
  388.         insertHtml(txt);
  389.     else
  390.         textboxelement.value += txt;
  391. }
  392.  
  393. function doClick(command) {
  394.     if (editorVisible) {
  395.         ifm.contentWindow.focus();
  396.         myeditor.execCommand(command, false, null);
  397.     }
  398.     else {
  399.         switch (command) {
  400.             case 'bold':
  401.                 AddTag('[b]', '[/b]'); break;
  402.             case 'italic':
  403.                 AddTag('[i]', '[/i]'); break;
  404.             case 'underline':
  405.                 AddTag('[u]', '[/u]'); break;
  406.             case 'code':
  407.                 AddTag('[code]', '[/code]'); break;
  408.             case 'quote':
  409.                 AddTag('[quote]', '[/quote]'); break;
  410.             case 'flash':
  411.                 AddTag('[flash]', '[/flash]'); break;
  412.             case 'widget':
  413.                 AddTag('[widget]', '[/widget]'); break;
  414.             case 'InsertUnorderedList':
  415.                 AddTag('[ul][li]', '[/li][/ul]'); break;
  416.         }
  417.     }
  418. }
  419.  
  420. function doColor(color) {
  421.   ifm.contentWindow.focus();
  422.   if (isIE) {
  423.       textRange = ifm.contentWindow.document.selection.createRange();
  424.       textRange.select();
  425.   }
  426.   myeditor.execCommand('forecolor', false, color);
  427. }
  428.  
  429. function doLink() {
  430.     if (editorVisible) {
  431.         ifm.contentWindow.focus();
  432.         var mylink = prompt("Enter a URL:", "http://");
  433.         if ((mylink != null) && (mylink != "")) {
  434.             if (isIE) { //IE
  435.                 var range = ifm.contentWindow.document.selection.createRange();
  436.                 if (range.text == '') {
  437.                     range.pasteHTML("<a href='" + mylink + "'>" + mylink + "</a>");
  438.                 }
  439.                 else
  440.                     myeditor.execCommand("CreateLink", false, mylink);
  441.             }
  442.             else if (window.getSelection) { //FF
  443.                 var userSelection = ifm.contentWindow.getSelection().getRangeAt(0);
  444.                 if(userSelection.toString().length==0)
  445.                     myeditor.execCommand('inserthtml', false, "<a href='" + mylink + "'>" + mylink + "</a>");
  446.                 else
  447.                     myeditor.execCommand("CreateLink", false, mylink);
  448.             }
  449.             else
  450.                 myeditor.execCommand("CreateLink", false, mylink);
  451.         }
  452.     }
  453.     else {
  454.         AddTag('[url=',']click here[/url]');
  455.     }
  456. }
  457. function doSize() {
  458.     if (editorVisible) {
  459.         ifm.contentWindow.focus();
  460.         var mylink = prompt("Enter a text-size:", "14px");
  461.         if ((mylink != null) && (mylink != "")) {
  462.             if (isIE) { //IE
  463.                 var range = ifm.contentWindow.document.selection.createRange();
  464.                 if (range.text == '') {
  465.                     range.pasteHTML("<span style='font-size: " + mylink + ";'></span>");
  466.                 }
  467.                 else
  468.                     myeditor.execCommand("CreateSize", false, mylink);
  469.             }
  470.             else if (window.getSelection) { //FF
  471.                 var userSelection = ifm.contentWindow.getSelection().getRangeAt(0);
  472.                 if(userSelection.toString().length==0)
  473.                     myeditor.execCommand('inserthtml', false, "<span style='font-size: " + mylink + "'></span>");
  474.                 else
  475.                     myeditor.execCommand("CreateSize", false, mylink);
  476.             }
  477.             else
  478.                 myeditor.execCommand("CreateSize", false, mylink);
  479.         }
  480.     }
  481.     else {
  482.         AddTag('[size=]','[/size]');
  483.     }
  484. }
  485. function doMail() {
  486.     if (editorVisible) {
  487.         ifm.contentWindow.focus();
  488.         var mylink = prompt("Enter an Email:", "");
  489.         if ((mylink != null) && (mylink != "")) {
  490.             if (isIE) { //IE
  491.                 var range = ifm.contentWindow.document.selection.createRange();
  492.                 if (range.text == '') {
  493.                     range.pasteHTML("<a href='mailto:" + mylink + "'>" + mylink + "</a>");
  494.                 }
  495.                 else
  496.                     myeditor.execCommand("CreateLink", false, mylink);
  497.             }
  498.             else if (window.getSelection) { //FF
  499.                 var userSelection = ifm.contentWindow.getSelection().getRangeAt(0);
  500.                 if(userSelection.toString().length==0)
  501.                     myeditor.execCommand('inserthtml', false, "<a href='mailto:" + mylink + "'>" + mylink + "</a>");
  502.                 else
  503.                     myeditor.execCommand("CreateLink", false, mylink);
  504.             }
  505.             else
  506.                 myeditor.execCommand("CreateLink", false, mylink);
  507.         }
  508.     }
  509.     else {
  510.         AddTag('[url=mailto:',']click here[/url]');
  511.     }
  512. }
  513. function doAddon() {
  514.     if (editorVisible) {
  515.         ifm.contentWindow.focus();
  516.         var mylink = prompt("Enter Widget Name", "");
  517.         if ((mylink != null) && (mylink != "")) {
  518.             if (isIE) { //IE
  519.                 var range = ifm.contentWindow.document.selection.createRange();
  520.                 if (range.text == '') {
  521.                     range.pasteHTML("<span style=\"color: green;\">[widget: " + mylink + " :widget]</span>");
  522.                 }
  523.                 else
  524.                     myeditor.execCommand("CreateLink", false, mylink);
  525.             }
  526.             else if (window.getSelection) { //FF
  527.                 var userSelection = ifm.contentWindow.getSelection().getRangeAt(0);
  528.                 if(userSelection.toString().length==0)
  529.                     myeditor.execCommand('inserthtml', false, "<span style=\"color: green;\">[widget: " + mylink + " :widget]</span>");
  530.                 else
  531.                     myeditor.execCommand("CreateLink", false, mylink);
  532.             }
  533.             else
  534.                 myeditor.execCommand("CreateLink", false, mylink);
  535.         }
  536.     }
  537.     else {
  538.         AddTag('[widget]','[/widget]');
  539.     }
  540. }
  541. function doImage() {
  542.     if (editorVisible) {
  543.         ifm.contentWindow.focus();
  544.         myimg = prompt('Enter Image URL:', 'http://');
  545.         if ((myimg != null) && (myimg != "")) {
  546.             myeditor.execCommand('InsertImage', false, myimg);
  547.         }
  548.     }
  549.     else {
  550.         AddTag('[img]', '[/img]');
  551.     }
  552. }
  553.  
  554. function insertHtml(html) {
  555.     ifm.contentWindow.focus();
  556.     if (isIE)
  557.         ifm.contentWindow.document.selection.createRange().pasteHTML(html);
  558.     else
  559.         myeditor.execCommand('inserthtml', false, html);
  560. }
  561.  
  562. //textarea-mode functions
  563. function MozillaInsertText(element, text, pos) {
  564.     element.value = element.value.slice(0, pos) + text + element.value.slice(pos);
  565. }
  566.  
  567. function AddTag(t1, t2) {
  568.     var element = textboxelement;
  569.     if (isIE) {
  570.         if (document.selection) {
  571.             element.focus();
  572.  
  573.             var txt = element.value;
  574.             var str = document.selection.createRange();
  575.  
  576.             if (str.text == "") {
  577.                 str.text = t1 + t2;
  578.             }
  579.             else if (txt.indexOf(str.text) >= 0) {
  580.                 str.text = t1 + str.text + t2;
  581.             }
  582.             else {
  583.                 element.value = txt + t1 + t2;
  584.             }
  585.             str.select();
  586.         }
  587.     }
  588.     else if (element.selectionStart) {
  589.         var sel_start = element.selectionStart;
  590.         var sel_end = element.selectionEnd;
  591.         MozillaInsertText(element, t1, sel_start);
  592.         MozillaInsertText(element, t2, sel_end + t1.length);
  593.         element.selectionStart = sel_start;
  594.         element.selectionEnd = sel_end + t1.length + t2.length;
  595.         element.focus();
  596.     }
  597.     else {
  598.         element.value = element.value + t1 + t2;
  599.     }
  600. }
  601.  
  602. //=======color picker
  603. function getScrollY() { var scrOfX = 0, scrOfY = 0; if (typeof (window.pageYOffset) == 'number') { scrOfY = window.pageYOffset; scrOfX = window.pageXOffset; } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) { scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft; } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) { scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft; } return scrOfY; }
  604.  
  605. document.write("<style type='text/css'>.colorpicker201{visibility:hidden;display:none;position:absolute;background:#FFF;z-index:999;filter:progid:DXImageTransform.Microsoft.Shadow(color=#D0D0D0,direction=135);}.o5582brd{padding:0;width:12px;height:14px;border-bottom:solid 1px #DFDFDF;border-right:solid 1px #DFDFDF;}a.o5582n66,.o5582n66,.o5582n66a{font-family:arial,tahoma,sans-serif;text-decoration:underline;font-size:9px;color:#666;border:none;}.o5582n66,.o5582n66a{text-align:center;text-decoration:none;}a:hover.o5582n66{text-decoration:none;color:#FFA500;cursor:pointer;}.a01p3{padding:1px 4px 1px 2px;background:whitesmoke;border:solid 1px #DFDFDF;}</style>");
  606.  
  607. function getTop2() { csBrHt = 0; if (typeof (window.innerWidth) == 'number') { csBrHt = window.innerHeight; } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { csBrHt = document.documentElement.clientHeight; } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { csBrHt = document.body.clientHeight; } ctop = ((csBrHt / 2) - 115) + getScrollY(); return ctop; }
  608. var nocol1 = "&#78;&#79;&#32;&#67;&#79;&#76;&#79;&#82;",
  609. clos1 = "X";
  610.  
  611. function getLeft2() { var csBrWt = 0; if (typeof (window.innerWidth) == 'number') { csBrWt = window.innerWidth; } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { csBrWt = document.documentElement.clientWidth; } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { csBrWt = document.body.clientWidth; } cleft = (csBrWt / 2) - 125; return cleft; }
  612.  
  613. //function setCCbldID2(val, textBoxID) { document.getElementById(textBoxID).value = val; }
  614. function setCCbldID2(val) { if (editorVisible) doColor(val); else AddTag('[color=' + val + ']', '[/color]'); }
  615.  
  616. function setCCbldSty2(objID, prop, val) {
  617.     switch (prop) {
  618.         case "bc": if (objID != 'none') { document.getElementById(objID).style.backgroundColor = val; }; break;
  619.         case "vs": document.getElementById(objID).style.visibility = val; break;
  620.         case "ds": document.getElementById(objID).style.display = val; break;
  621.         case "tp": document.getElementById(objID).style.top = val; break;
  622.         case "lf": document.getElementById(objID).style.left = val; break;
  623.     }
  624. }
  625.  
  626. function putOBJxColor2(Samp, pigMent, textBoxId) { if (pigMent != 'x') { setCCbldID2(pigMent, textBoxId); setCCbldSty2(Samp, 'bc', pigMent); } setCCbldSty2('colorpicker201', 'vs', 'hidden'); setCCbldSty2('colorpicker201', 'ds', 'none'); }
  627.  
  628. function showColorGrid2(Sam, textBoxId) {
  629.     var objX = new Array('00', '33', '66', '99', 'CC', 'FF');
  630.     var c = 0;
  631.     var xl = '"' + Sam + '","x", "' + textBoxId + '"'; var mid = '';
  632.     mid += '<table bgcolor="#FFFFFF" border="0" cellpadding="0" cellspacing="0" style="border:solid 0px #F0F0F0;padding:2px;"><tr>';
  633.     mid += "<td colspan='9' align='left' style='margin:0;padding:2px;height:12px;' ><input class='o5582n66' type='text' size='12' id='o5582n66' value='#FFFFFF'><input class='o5582n66a' type='text' size='2' style='width:14px;' id='o5582n66a' onclick='javascript:alert(\"click on selected swatch below...\");' value='' style='border:solid 1px #666;'></td><td colspan='9' align='right'><a class='o5582n66' href='javascript:onclick=putOBJxColor2(" + xl + ")'><span class='a01p3'>" + clos1 + "</span></a></td></tr><tr>";
  634.     var br = 1;
  635.     for (o = 0; o < 6; o++) {
  636.         mid += '</tr><tr>';
  637.         for (y = 0; y < 6; y++) {
  638.             if (y == 3) { mid += '</tr><tr>'; }
  639.             for (x = 0; x < 6; x++) {
  640.                 var grid = '';
  641.                 grid = objX[o] + objX[y] + objX[x];
  642.                 var b = "'" + Sam + "','" + grid + "', '" + textBoxId + "'";
  643.                 mid += '<td class="o5582brd" style="background-color:#' + grid + '"><a class="o5582n66"  href="javascript:onclick=putOBJxColor2(' + b + ');" onmouseover=javascript:document.getElementById("o5582n66").value="#' + grid + '";javascript:document.getElementById("o5582n66a").style.backgroundColor="#' + grid + '";  title="#' + grid + '"><div style="width:12px;height:14px;"></div></a></td>';
  644.                 c++;
  645.             }
  646.         }
  647.     }
  648.     mid += "</tr></table>";
  649.     //var ttop=getTop2();
  650.     //setCCbldSty2('colorpicker201','tp',ttop);
  651.     //document.getElementById('colorpicker201').style.left=getLeft2();
  652.     document.getElementById('colorpicker201').innerHTML = mid;
  653.     setCCbldSty2('colorpicker201', 'vs', 'visible');
  654.     setCCbldSty2('colorpicker201', 'ds', 'inline');
  655. }
Advertisement
Add Comment
Please, Sign In to add comment