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

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