Blame Identity/Webenv/App/Trac/htdocs/js/wikitoolbar.js

f2e824
function addWikiFormattingToolbar(textarea) {
f2e824
  if ((typeof(document["selection"]) == "undefined")
f2e824
   && (typeof(textarea["setSelectionRange"]) == "undefined")) {
f2e824
    return;
f2e824
  }
f2e824
  
f2e824
  var toolbar = document.createElement("div");
f2e824
  toolbar.className = "wikitoolbar";
f2e824
f2e824
  function addButton(id, title, fn) {
f2e824
    var a = document.createElement("a");
f2e824
    a.href = "#";
f2e824
    a.id = id;
f2e824
    a.title = title;
f2e824
    a.onclick = function() { try { fn() } catch (e) { } return false };
f2e824
    a.tabIndex = 400;
f2e824
    toolbar.appendChild(a);
f2e824
  }
f2e824
f2e824
  function encloseSelection(prefix, suffix) {
f2e824
    textarea.focus();
f2e824
    var start, end, sel, scrollPos, subst;
f2e824
    if (typeof(document["selection"]) != "undefined") {
f2e824
      sel = document.selection.createRange().text;
f2e824
    } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
f2e824
      start = textarea.selectionStart;
f2e824
      end = textarea.selectionEnd;
f2e824
      scrollPos = textarea.scrollTop;
f2e824
      sel = textarea.value.substring(start, end);
f2e824
    }
f2e824
    if (sel.match(/ $/)) { // exclude ending space char, if any
f2e824
      sel = sel.substring(0, sel.length - 1);
f2e824
      suffix = suffix + " ";
f2e824
    }
f2e824
    subst = prefix + sel + suffix;
f2e824
    if (typeof(document["selection"]) != "undefined") {
f2e824
      var range = document.selection.createRange().text = subst;
f2e824
      textarea.caretPos -= suffix.length;
f2e824
    } else if (typeof(textarea["setSelectionRange"]) != "undefined") {
f2e824
      textarea.value = textarea.value.substring(0, start) + subst +
f2e824
                       textarea.value.substring(end);
f2e824
      if (sel) {
f2e824
        textarea.setSelectionRange(start + subst.length, start + subst.length);
f2e824
      } else {
f2e824
        textarea.setSelectionRange(start + prefix.length, start + prefix.length);
f2e824
      }
f2e824
      textarea.scrollTop = scrollPos;
f2e824
    }
f2e824
  }
f2e824
f2e824
  addButton("strong", "Bold text: '''Example'''", function() {
f2e824
    encloseSelection("'''", "'''");
f2e824
  });
f2e824
  addButton("em", "Italic text: ''Example''", function() {
f2e824
    encloseSelection("''", "''");
f2e824
  });
f2e824
  addButton("heading", "Heading: == Example ==", function() {
f2e824
    encloseSelection("\n== ", " ==\n", "Heading");
f2e824
  });
f2e824
  addButton("link", "Link: [http://www.example.com/ Example]", function() {
f2e824
    encloseSelection("[", "]");
f2e824
  });
f2e824
  addButton("code", "Code block: {{{ example }}}", function() {
f2e824
    encloseSelection("\n{{{\n", "\n}}}\n");
f2e824
  });
f2e824
  addButton("hr", "Horizontal rule: ----", function() {
f2e824
    encloseSelection("\n----\n", "");
f2e824
  });
f2e824
  addButton("np", "New paragraph", function() {
f2e824
    encloseSelection("\n\n", "");
f2e824
  });
f2e824
  addButton("br", "Line break: [[BR]]", function() {
f2e824
    encloseSelection("[[BR]]\n", "");
f2e824
  });
f2e824
f2e824
  textarea.parentNode.insertBefore(toolbar, textarea);
f2e824
}
f2e824
f2e824
// Add the toolbar to all <textarea> elements on the page with the class
f2e824
// 'wikitext'.
f2e824
var re = /\bwikitext\b/;
f2e824
var textareas = document.getElementsByTagName("textarea");
f2e824
for (var i = 0; i < textareas.length; i++) {
f2e824
  var textarea = textareas[i];
f2e824
  if (textarea.className && re.test(textarea.className)) {
f2e824
    addWikiFormattingToolbar(textarea);
f2e824
  }
f2e824
}