Advertisement
Guest User

BB-Code PunBB

a guest
Aug 10th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. var texttools = {
  3.                 "[b]":       { "pattern": "[b]%s[/b]", "accesskey": "b" },
  4.                 "[u]": { "pattern": "[u]%s[/u]", "accesskey": "u" },
  5.                 "[i]":     { "pattern": "[i]%s[/i]", "accesskey": "i" },
  6.                 "[s]":     { "pattern": "[s]%s[/s]", "accesskey": "s" },
  7.                 "img":        { "pattern": "[img]%s[/img]", "accesskey": "k" },
  8.                 "kolor":     { "pattern": "[color=%p]%s[/color]", "accesskey": "c", "prompt": "Wpisz nazwę koloru (red, #ff6, #ccff33, itp.)" },
  9.                 "url":        { "pattern": "[url=%p]%s[/url]", "accesskey": "l", "prompt": "Wpisz adres strony" },
  10.                 "cytat":      { "pattern": "[quote=%p]%s[/quote]
  11. ", "accesskey": "c", "prompt": "Wpisz nick osoby, której tekst cytujesz (opcjonalne)" },
  12.                 "kod":       { "pattern": "[code]%s[/code]
  13. ", "accesskey": "o" },
  14.                 "html":       { "pattern": "[html]%s[/html]", "accesskey": "h" },
  15.                 "YT":       { "pattern": "[youtube]%s[/youtube]", "accesskey": "y" },
  16.                 "Class":     { "pattern": "[class=%p]%s[/class]", "accesskey": "a", "prompt": "Wpisz nazwę klasy" },
  17.         };
  18.             var textarea = document.getElementsByTagName("textarea");
  19.             var form = null;
  20.             var submittedButt = null;
  21.             var operaAccessKeyHack = {};
  22.             for (var ita=0; ita<textarea.length; ita++) {
  23.                     if (textarea[ita].getAttribute("name") == "req_message") {
  24.                             textarea = textarea[ita];
  25.                             form = getParent(textarea, "form");
  26.                             tt = document.createElement("div");
  27.                             tt.style.lineHeight= "2em";
  28.                             tt.id= "bbcode";
  29.                             tt.appendChild(document.createTextNode("BBCode: "));
  30.                             if (typeof opera != "undefined" && operaAccessKeyHack) {
  31.                                     //textarea.addEventListener("keypress", function(e) { if (e.altKey && e.ctrlKey && (butt = operaAccessKeyHack[e.keyCode])) { submittedButt = butt; butt.click(); } }, false);
  32.                                     form.addEventListener("submit", function(e) {
  33.                                             if (e.shiftKey || e.ctrlKey) {
  34.                                                     setTimeout(function() {
  35.                                                             submittedButt.removeAttribute("disabled");
  36.                                                             var click = document.createEvent("MouseEvents");
  37.                                                             click.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  38.                                                             submittedButt.dispatchEvent(click);
  39.                                                     }, 100);
  40.                                                     e.cancelBubble = true;
  41.                                                     if (e.stopPropagation) e.stopPropagation();
  42.                                                     e.preventDefault();
  43.                                                     return false;
  44.                                             } else {
  45.                                                     submittedButt = null;
  46.                                                     return true;
  47.                                             }
  48.                                     }, false);
  49.                             }
  50.                             for (tool in texttools) {
  51.                                     if (texttools.hasOwnProperty(tool)) {
  52.                                             tt.appendChild(createBBCodeButt(tool, function(e) {
  53.                                                     textarea.focus();
  54.                                                     var osel = [textarea.value.substring(textarea.selectionStart, textarea.selectionEnd), textarea.selectionStart, textarea.selectionEnd];
  55.                                                     var tool = this.textContent;
  56.                                                     var nsel = texttools[tool]["pattern"].replace(/%s/g, osel[0]);
  57.                                                     var p = null;
  58.                                                     if (texttools[tool]["pattern"].indexOf("=%p") != -1) {
  59.                                                             p = prompt(texttools[tool]["prompt"]?texttools[tool]["prompt"]:"Type parameter (%p) for \u201c"+nsel+"\u201d\u00a0:", texttools[tool]["default"]?texttools[tool]["default"]:"");
  60.                                                             nsel = nsel.replace(/=%p/g, p?"="+p:"");
  61.                                                     }
  62.                                                     textarea.value = textarea.value.substr(0, osel[1]) + nsel + textarea.value.substr(osel[2]);
  63.                                                     if (osel[1] != osel[2]) {
  64.                                                             textarea.selectionStart = osel[1];
  65.                                                             textarea.selectionEnd = osel[1] + nsel.length;
  66.                                                     } else {
  67.                                                             textarea.selectionStart = osel[1] + nsel.indexOf("[", 1);
  68.                                                             textarea.selectionEnd = textarea.selectionStart;
  69.                                                     }
  70.                                             }, texttools[tool]["pattern"], texttools[tool]["accesskey"]));
  71.                                             tt.appendChild(document.createTextNode(" "));
  72.                                     }
  73.                             }
  74.                             textarea.parentNode.insertBefore(tt, textarea);
  75.                             if (form != null) {
  76.                                     var action = form.getAttribute("action");
  77.                                     var tid = action.match(/^post\.php\?tid=([0-9]+)$/i);
  78.                                     if (tid) {
  79.                                             tid = tid[1];
  80.                                             var stuff = document.createElement("span");
  81.                                             form.setAttribute("action", "post.php?action=post&tid="+tid);
  82.                                             form.setAttribute("id", "post");
  83.                                             var lbl = stuff.appendChild(document.createElement("label"));
  84.                                             lbl.style.display = "inline";
  85.                                             lbl.appendChild(createInput("checkbox", "subscribe", "1"));
  86.                                             lbl.appendChild(document.createTextNode("Subskrybuj ten temat"));
  87.                                             var ou = form.getElementsByTagName("p");
  88.                                             if (ou.length>0) { ou = ou[ou.length-1]; }
  89.                                             else { ou = form; }
  90.                                             ou.appendChild(stuff);
  91.                                     }
  92.                                     var subs = form.getElementsByTagName("input");
  93.                                     for (var sb=0; sb<subs.length; sb++) {/*opens in new window because of shift:/*/
  94.                                             if (subs[sb].getAttribute("type") == "submit" && (accesskey = subs[sb].getAttribute("accesskey"))) {
  95.                                                     operaAccessKeyHack[accesskey.toLowerCase().charCodeAt(0)] = subs[sb];
  96.                                                     subs[sb].setAttribute("title", akey(accesskey));
  97.                                             }
  98.                                     }
  99.                             }
  100.                             textarea.parentNode.insertBefore(tt, textarea);
  101.                             if (form != null) {
  102.                                     var action = form.getAttribute("action");
  103.                                     var tid = action.match(/^poll\.php\?tid=([0-9]+)$/i);
  104.                                     if (tid) {
  105.                                             tid = tid[1];
  106.                                             var stuff = document.createElement("span");
  107.                                             form.setAttribute("action", "poll.php?action=post&tid="+tid);
  108.                                             form.setAttribute("id", "post");
  109.                                             var preview = stuff.appendChild(createInput("submit", "preview", "Podgląd"));
  110.                                             preview.setAttribute("accesskey", "p");
  111.                                             var lbl = stuff.appendChild(document.createElement("label"));
  112.                                             lbl.style.display = "inline";
  113.                                             lbl.appendChild(createInput("checkbox", "subscribe", "1"));
  114.                                             lbl.appendChild(document.createTextNode("Subskrybuj ten temat"));
  115.                                             var ou = form.getElementsByTagName("p");
  116.                                             if (ou.length>0) { ou = ou[ou.length-1]; }
  117.                                             else { ou = form; }
  118.                                             ou.appendChild(stuff);
  119.                                     }
  120.                                     var subs = form.getElementsByTagName("input");
  121.                                     for (var sb=0; sb<subs.length; sb++) {/*opens in new window because of shift:/*/
  122.                                             if (subs[sb].getAttribute("type") == "submit" && (accesskey = subs[sb].getAttribute("accesskey"))) {
  123.                                                     operaAccessKeyHack[accesskey.toLowerCase().charCodeAt(0)] = subs[sb];
  124.                                                     subs[sb].setAttribute("title", akey(accesskey));
  125.                                             }
  126.                                     }
  127.                             }
  128.                             break;
  129.                     }
  130.             }
  131.             function akey(k) {
  132.                     var t = "ALT+"+k.toUpperCase();
  133.                     if (navigator.userAgent.match(/firefox/i)) { t = "ALT+SHIFT+"+k.toUpperCase(); }
  134.                     else if (typeof opera != "undefined") { t = "CTRL+" + t; }
  135.                     return t;
  136.             }
  137.             function getParent(obj, tag) {
  138.                     var cur = obj;
  139.                     if (cur.parentNode) {
  140.                             cur = cur.parentNode;
  141.                             if (cur.tagName == tag.toUpperCase()) {
  142.                                     return cur;
  143.                             } else {
  144.                                     return getParent(cur, tag);
  145.                             }
  146.                     } else {
  147.                             return null;
  148.                     }
  149.             }
  150.             function createInput(type, name, value) {
  151.                     var input = document.createElement("input");
  152.                     input.setAttribute("type", type);
  153.                     input.setAttribute("name", name);
  154.                     input.setAttribute("value", value);
  155.                     return input;
  156.             }
  157.             function createBBCodeButt(text, link, title, accesskey) {
  158.                     var truc = document.createElement("span");
  159.                     truc.style.whiteSpace = "nowrap";
  160.                     truc.appendChild(document.createTextNode("\u00a0"));
  161.                     var a = truc.appendChild(document.createElement("a"));
  162.                     if (link && typeof link == "string") {
  163.                             a.setAttribute("href", link);
  164.                     }
  165.                     else {
  166.                             if (link && typeof link == "function") {
  167.                                     a.addEventListener("click", link, false);
  168.                             }
  169.                             a.style.cursor = "pointer";
  170.                     }
  171.                     if (title) { a.setAttribute("title", title); }
  172.                     if (accesskey) {
  173.                             a.setAttribute("accesskey", accesskey);
  174.                             var key = akey(accesskey);
  175.                             if (navigator.userAgent.match(/firefox/i)) { key += ":"; }
  176.                             a.setAttribute("title", key+" \n" + a.getAttribute("title"));
  177.                             if (typeof opera != "undefined" && operaAccessKeyHack) {
  178.                                     operaAccessKeyHack[accesskey.toLowerCase().charCodeAt(0)] = a;
  179.                             }
  180.                     }
  181.                     if (accesskey) {
  182.                             var kpos = text.indexOf(accesskey);
  183.                             a.appendChild(document.createTextNode(text.substr(0,kpos)));
  184.                             a.appendChild(document.createElement("strong")).appendChild(document.createTextNode(text.charAt(kpos)));
  185.                             a.appendChild(document.createTextNode(text.substr(kpos+1)));
  186.                     }
  187.                     else { a.appendChild(document.createTextNode(text)); }
  188.                     return truc;
  189.             }
  190.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement