Blame Extras/Trac/htdocs/js/query.js

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