Blame Extras/Trac/htdocs/js/trac.js

4c79b5
// Used for dynamically updating the height of a textarea
4c79b5
function resizeTextArea(id, rows) {
4c79b5
  var textarea = document.getElementById(id);
4c79b5
  if (!textarea || (typeof(textarea.rows) == "undefined")) return;
4c79b5
  textarea.rows = rows;
4c79b5
}
4c79b5
4c79b5
// A better way than for example hardcoding foo.onload
4c79b5
function addEvent(element, type, func){
4c79b5
  if (element.addEventListener) {
4c79b5
    element.addEventListener(type, func, false);
4c79b5
    return true;
4c79b5
  } else if (element.attachEvent) {
4c79b5
    return element.attachEvent("on" + type, func);
4c79b5
  }
4c79b5
  return false;
4c79b5
}
4c79b5
4c79b5
// Convenience function for the nearest ancestor element with a specific tag
4c79b5
// name
4c79b5
function getAncestorByTagName(e, tagName) {
4c79b5
  tagName = tagName.toLowerCase();
4c79b5
  do {
4c79b5
    e = e.parentNode;
4c79b5
  } while ((e.nodeType == 1) && (e.tagName.toLowerCase() != tagName));
4c79b5
  return (e.nodeType == 1) ? e : null;
4c79b5
}
4c79b5
4c79b5
// Adapted from http://www.kryogenix.org/code/browser/searchhi/
4c79b5
function searchHighlight() {
4c79b5
  if (!document.createElement) return;
4c79b5
4c79b5
  var div = document.getElementById("searchable");
4c79b5
  if (!div) return;
4c79b5
4c79b5
  function getSearchWords(url) {
4c79b5
    if (url.indexOf('?') == -1) return [];
4c79b5
    var queryString = url.substr(url.indexOf('?') + 1);
4c79b5
    var params = queryString.split('&';;
4c79b5
    for (var p in params) {
4c79b5
      var param = params[p].split('=');
4c79b5
      if (param.length < 2) continue;
4c79b5
      if (param[0] == 'q' || param[0] == 'p') { // q= for Google, p= for Yahoo
4c79b5
        var query = decodeURIComponent(param[1].replace(/\+/g, ' '));
4c79b5
        if (query[0] == '!') query = query.slice(1);
4c79b5
        words = query.split(/(".*?")|('.*?')|(\s+)/);
4c79b5
        var words2 = new Array();
4c79b5
        for (var w in words) {
4c79b5
          words[w] = words[w].replace(/^\s+$/, '');
4c79b5
          if (words[w] != '') {
4c79b5
            words2.push(words[w].replace(/^['"]/, '').replace(/['"]$/, ''));
4c79b5
          }
4c79b5
        }
4c79b5
        return words2;
4c79b5
      }
4c79b5
    }
4c79b5
    return [];
4c79b5
  }
4c79b5
4c79b5
  function highlightWord(node, word, searchwordindex) {
4c79b5
    // If this node is a text node and contains the search word, highlight it by
4c79b5
    // surrounding it with a span element
4c79b5
    if (node.nodeType == 3) { // Node.TEXT_NODE
4c79b5
      var pos = node.nodeValue.toLowerCase().indexOf(word.toLowerCase());
4c79b5
      if (pos >= 0 && !/^searchword\d$/.test(node.parentNode.className)) {
4c79b5
        var span = document.createElement("span");
4c79b5
        span.className = "searchword" + (searchwordindex % 5);
4c79b5
        span.appendChild(document.createTextNode(
4c79b5
          node.nodeValue.substr(pos, word.length)));
4c79b5
        node.parentNode.insertBefore(span, node.parentNode.insertBefore(
4c79b5
          document.createTextNode(node.nodeValue.substr(pos + word.length)),
4c79b5
            node.nextSibling));
4c79b5
        node.nodeValue = node.nodeValue.substr(0, pos);
4c79b5
        return true;
4c79b5
      }
4c79b5
    } else if (!node.nodeName.match(/button|select|textarea/i)) {
4c79b5
      // Recurse into child nodes
4c79b5
      for (var i = 0; i < node.childNodes.length; i++) {
4c79b5
        if (highlightWord(node.childNodes[i], word, searchwordindex)) i++;
4c79b5
      }
4c79b5
    }
4c79b5
    return false;
4c79b5
  }
4c79b5
4c79b5
  var words = getSearchWords(document.URL);
4c79b5
  if (!words.length) words = getSearchWords(document.referrer);
4c79b5
  if (words.length) {
4c79b5
    for (var w in words) {
4c79b5
      if (words[w].length) highlightWord(div, words[w], w);
4c79b5
    }
4c79b5
  }
4c79b5
}
4c79b5
4c79b5
function enableControl(id, enabled) {
4c79b5
  if (typeof(enabled) == "undefined") enabled = true;
4c79b5
  var control = document.getElementById(id);
4c79b5
  if (!control) return;
4c79b5
  control.disabled = !enabled;
4c79b5
  var label = getAncestorByTagName(control, "label");
4c79b5
  if (label) {
4c79b5
    label.className = enabled ? "enabled" : "disabled";
4c79b5
  } else {
4c79b5
    var labels = document.getElementsByTagName("label");
4c79b5
    for (var i = 0; i < labels.length; i++) {
4c79b5
      if (labels[i].htmlFor == id) {
4c79b5
        labels[i].className = enabled ? "enabled" : "disabled";
4c79b5
        break;
4c79b5
      }
4c79b5
    }
4c79b5
  }
4c79b5
}
4c79b5
4c79b5
function addHeadingLinks(container, title) {
4c79b5
  var base = document.location.pathname;
4c79b5
  function addLinks(elems) {
4c79b5
    for (var i = 0; i < elems.length; i++) {
4c79b5
      var hn = elems[i];
4c79b5
      if (hn.id) {
4c79b5
        var link = document.createElement('a');
4c79b5
        link.href = base + '#' + hn.id;
4c79b5
        link.className = 'anchor';
4c79b5
        link.title = title.replace(/\$id/, hn.id);
4c79b5
        link.appendChild(document.createTextNode(" \u00B6"));
4c79b5
        hn.appendChild(link);
4c79b5
      }
4c79b5
    }
4c79b5
  }
4c79b5
  for (var lvl = 0; lvl <= 6; lvl++) {
4c79b5
    addLinks(container.getElementsByTagName('h' + lvl));
4c79b5
  }
4c79b5
}