Blame Identity/Webenv/Trac/htdocs/js/query.js

ef5584
function initializeFilters() {
ef5584
ef5584
  // Bail early for Konqueror and IE5.2/Mac, which don't fully support dynamic
ef5584
  // creation of form controls
ef5584
  try {
ef5584
    var test = document.createElement("input");
ef5584
    test.type = "button";
ef5584
    if (test.type != "button") throw Error();
ef5584
  } catch (e) {
ef5584
    return;
ef5584
  }
ef5584
ef5584
  // Removes an existing row from the filters table
ef5584
  function removeRow(button, propertyName) {
ef5584
    var tr = getAncestorByTagName(button, "tr");
ef5584
ef5584
    var mode = null;
ef5584
    var selects = tr.getElementsByTagName("select");
ef5584
    for (var i = 0; i < selects.length; i++) {
ef5584
      if (selects[i].name == propertyName + "_mode") {
ef5584
        mode = selects[i];
ef5584
        break;
ef5584
      }
ef5584
    }
ef5584
    if (mode && (getAncestorByTagName(mode, "tr") == tr)) {
ef5584
      // Check whether there are more 'or' rows for this filter
ef5584
      var next = tr.nextSibling;
ef5584
      if (next && (next.className == propertyName)) {
ef5584
        function getChildElementAt(e, idx) {
ef5584
          e = e.firstChild;
ef5584
          var cur = 0;
ef5584
          while (cur <= idx) {
ef5584
            while (e && e.nodeType != 1) e = e.nextSibling;
ef5584
            if (cur++ == idx) break;
ef5584
            e = e.nextSibling;
ef5584
          }
ef5584
          return e;
ef5584
        }
ef5584
ef5584
        var thisTh = getChildElementAt(tr, 0);
ef5584
        var nextTh = getChildElementAt(next, 0);
ef5584
        next.insertBefore(thisTh, nextTh);
ef5584
        nextTh.colSpan = 1;
ef5584
ef5584
        thisTd = getChildElementAt(tr, 0);
ef5584
        nextTd = getChildElementAt(next, 1);
ef5584
        next.replaceChild(thisTd, nextTd);
ef5584
      }
ef5584
    }
ef5584
ef5584
    var tBody = tr.parentNode;
ef5584
    tBody.deleteRow(tr.sectionRowIndex);
ef5584
    if (!tBody.rows.length) {
ef5584
        tBody.parentNode.removeChild(tBody);
ef5584
    }
ef5584
    
ef5584
    if (propertyName) {
ef5584
      var select = document.forms["query"].elements["add_filter"];
ef5584
      for (var i = 0; i < select.options.length; i++) {
ef5584
        var option = select.options[i];
ef5584
        if (option.value == propertyName) option.disabled = false;
ef5584
      }
ef5584
    }
ef5584
  }
ef5584
ef5584
  // Initializes a filter row, the 'input' parameter is the submit
ef5584
  // button for removing the filter
ef5584
  function initializeFilter(input) {
ef5584
    var removeButton = document.createElement("input");
ef5584
    removeButton.type = "button";
ef5584
    removeButton.value = input.value;
ef5584
    if (input.name.substr(0, 10) == "rm_filter_") {
ef5584
      removeButton.onclick = function() {
ef5584
        var endIndex = input.name.search(/_\d+$/);
ef5584
        if (endIndex < 0) endIndex = input.name.length;
ef5584
        removeRow(removeButton, input.name.substring(10, endIndex));
ef5584
        return false;
ef5584
      }
ef5584
    } else {
ef5584
      removeButton.onclick = function() {
ef5584
        removeRow(removeButton);
ef5584
        return false;
ef5584
      }
ef5584
    }
ef5584
    input.parentNode.replaceChild(removeButton, input);
ef5584
  }
ef5584
ef5584
  // Make the submit buttons for removing filters client-side triggers
ef5584
  var filters = document.getElementById("filters");
ef5584
  var inputs = filters.getElementsByTagName("input");
ef5584
  for (var i = 0; i < inputs.length; i++) {
ef5584
    var input = inputs[i];
ef5584
    if (input.type == "submit" && input.name
ef5584
     && input.name.match(/^rm_filter_/)) {
ef5584
      initializeFilter(input);
ef5584
    }
ef5584
  }
ef5584
ef5584
  // Make the drop-down menu for adding a filter a client-side trigger
ef5584
  var addButton = document.forms["query"].elements["add"];
ef5584
  addButton.parentNode.removeChild(addButton);
ef5584
  var select = document.getElementById("add_filter");
ef5584
  select.onchange = function() {
ef5584
    if (select.selectedIndex < 1) return;
ef5584
ef5584
    if (select.options[select.selectedIndex].disabled) {
ef5584
      // Neither IE nor Safari supported disabled options at the time this was
ef5584
      // written, so alert the user
ef5584
      alert("A filter already exists for that property");
ef5584
      return;
ef5584
    }
ef5584
ef5584
    // Convenience function for creating a <label>
ef5584
    function createLabel(text, htmlFor) {
ef5584
      var label = document.createElement("label");
ef5584
      if (text) label.appendChild(document.createTextNode(text));
ef5584
      if (htmlFor) label.htmlFor = htmlFor;
ef5584
      return label;
ef5584
    }
ef5584
ef5584
    // Convenience function for creating an <input type="checkbox">
ef5584
    function createCheckbox(name, value, id) {
ef5584
      var input = document.createElement("input");
ef5584
      input.type = "checkbox";
ef5584
      if (name) input.name = name;
ef5584
      if (value) input.value = value;
ef5584
      if (id) input.id = id;
ef5584
      return input;
ef5584
    }
ef5584
ef5584
    // Convenience function for creating an <input type="radio">
ef5584
    function createRadio(name, value, id) {
ef5584
      var input = document.createElement("input");
ef5584
      input.type = "radio";
ef5584
      if (name) input.name = name;
ef5584
      if (value) input.value = value;
ef5584
      if (id) input.id = id;
ef5584
      return input;
ef5584
    }
ef5584
ef5584
    // Convenience function for creating a <select>
ef5584
    function createSelect(name, options, optional) {
ef5584
      var e = document.createElement("select");
ef5584
      if (name) e.name = name;
ef5584
      if (optional) e.options[0] = new Option();
ef5584
      if (options) {
ef5584
        for (var i = 0; i < options.length; i++) {
ef5584
          var option;
ef5584
          if (typeof(options[i]) == "object") {
ef5584
            option = new Option(options[i].text, options[i].value);
ef5584
          } else {
ef5584
            option = new Option(options[i], options[i]);
ef5584
          }
ef5584
          e.options[e.options.length] = option;
ef5584
        }
ef5584
      }
ef5584
      return e;
ef5584
    }
ef5584
ef5584
    var propertyName = select.options[select.selectedIndex].value;
ef5584
    var property = properties[propertyName];
ef5584
    var table = document.getElementById("filters").getElementsByTagName("table")[0];
ef5584
    var tr = document.createElement("tr");
ef5584
    tr.className = propertyName;
ef5584
ef5584
    var alreadyPresent = false;
ef5584
    for (var i = 0; i < table.rows.length; i++) {
ef5584
      if (table.rows[i].className == propertyName) {
ef5584
        var existingTBody = table.rows[i].parentNode;
ef5584
        alreadyPresent = true;
ef5584
        break;
ef5584
      }
ef5584
    }
ef5584
ef5584
    // Add the row header
ef5584
    var th = document.createElement("th");
ef5584
    th.scope = "row";
ef5584
    if (!alreadyPresent) {
ef5584
      th.appendChild(createLabel(property.label));
ef5584
    } else {
ef5584
      th.colSpan = 2;
ef5584
      th.appendChild(createLabel("or"));
ef5584
    }
ef5584
    tr.appendChild(th);
ef5584
ef5584
    var td = document.createElement("td");
ef5584
    if (property.type == "radio" || property.type == "checkbox") {
ef5584
      td.colSpan = 2;
ef5584
      td.className = "filter";
ef5584
      if (property.type == "radio") {
ef5584
        for (var i = 0; i < property.options.length; i++) {
ef5584
          var option = property.options[i];
ef5584
          td.appendChild(createCheckbox(propertyName, option,
ef5584
            propertyName + "_" + option));
ef5584
          td.appendChild(document.createTextNode(" "));
ef5584
          td.appendChild(createLabel(option ? option : "none",
ef5584
            propertyName + "_" + option));
ef5584
        }
ef5584
      } else {
ef5584
        td.appendChild(createRadio(propertyName, "1", propertyName + "_on"));
ef5584
        td.appendChild(document.createTextNode(" "));
ef5584
        td.appendChild(createLabel("yes", propertyName + "_on"));
ef5584
        td.appendChild(createRadio(propertyName, "!1", propertyName + "_off"));
ef5584
        td.appendChild(document.createTextNode(" "));
ef5584
        td.appendChild(createLabel("no", propertyName + "_off"));
ef5584
      }
ef5584
      tr.appendChild(td);
ef5584
    } else {
ef5584
      if (!alreadyPresent) {
ef5584
        // Add the mode selector
ef5584
        td.className = "mode";
ef5584
        var modeSelect = createSelect(propertyName + "_mode",
ef5584
                                      modes[property.type]);
ef5584
        td.appendChild(modeSelect);
ef5584
        tr.appendChild(td);
ef5584
      }
ef5584
ef5584
      // Add the selector or text input for the actual filter value
ef5584
      td = document.createElement("td");
ef5584
      td.className = "filter";
ef5584
      if (property.type == "select") {
ef5584
        var element = createSelect(propertyName, property.options, true);
ef5584
      } else if (property.type == "text") {
ef5584
        var element = document.createElement("input");
ef5584
        element.type = "text";
ef5584
        element.name = propertyName;
ef5584
        element.size = 42;
ef5584
      }
ef5584
      td.appendChild(element);
ef5584
      element.focus();
ef5584
      tr.appendChild(td);
ef5584
    }
ef5584
ef5584
    // Add the add and remove buttons
ef5584
    td = document.createElement("td");
ef5584
    td.className = "actions";
ef5584
    var removeButton = document.createElement("input");
ef5584
    removeButton.type = "button";
ef5584
    removeButton.value = "-";
ef5584
    removeButton.onclick = function() { removeRow(removeButton, propertyName) };
ef5584
    td.appendChild(removeButton);
ef5584
    tr.appendChild(td);
ef5584
ef5584
    if (alreadyPresent) {
ef5584
      existingTBody.appendChild(tr);
ef5584
    } else {
ef5584
      // Find the insertion point for the new row. We try to keep the filter rows
ef5584
      // in the same order as the options in the 'Add filter' drop-down, because
ef5584
      // that's the order they'll appear in when submitted.
ef5584
      var insertionPoint = getAncestorByTagName(select, "tbody");
ef5584
      outer: for (var i = select.selectedIndex + 1; i < select.options.length; i++) {
ef5584
        for (var j = 0; j < table.tBodies.length; j++) {
ef5584
          if (table.tBodies[j].rows[0].className == select.options[i].value) {
ef5584
            insertionPoint = table.tBodies[j];
ef5584
            break outer;
ef5584
          }
ef5584
        }
ef5584
      }
ef5584
      // Finally add the new row to the table
ef5584
      var tbody = document.createElement("tbody");
ef5584
      tbody.appendChild(tr);
ef5584
      insertionPoint.parentNode.insertBefore(tbody, insertionPoint);
ef5584
    }
ef5584
ef5584
    // Disable the add filter in the drop-down list
ef5584
    if (property.type == "radio" || property.type == "checkbox") {
ef5584
      select.options[select.selectedIndex].disabled = true;
ef5584
    }
ef5584
    select.selectedIndex = 0;
ef5584
  }
ef5584
}