aboutsummaryrefslogblamecommitdiffstats
path: root/data/lib/flags.js
blob: ffbdb02bdbc9c6e03d210e1936ebe16f61e8fd24 (plain) (tree)
































































































































































                                                                                    
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";

/*
var d = Date.prototype;
d.__defineGetter__("year", function() { return this.getFullYear(); });
d.__defineSetter__("year", function(y) { this.setFullYear(y); });
 */

/**
 *
 */
function FlagElement(element,column) {
  this.element = element;
  this.id = element.id;
  var label = column.querySelector("label[for='" + this.id + "']");
  this.key = label.textContent.trim().
    replace(/\s*:?$/, "").replace('\u2011', '-', 'g');
}

FlagElement.prototype.__defineSetter__("value", function () {
    selectOption(this.id, what);
});

FlagElement.prototype.__defineGetter__("value", function () {
    return document.getElementById(id).value;
});

FlagElement.prototype.set = function set() {
    this.value = "+";
};

FlagElement.prototype.reject = function reject() {
    this.value = "-";
};

FlagElement.prototype.ask = function ask() {
    this.value = "?";
};

FlagElement.prototype.unset = function unset() {
    this.value = "--";
};

FlagElement.prototype.toString = function toString() {
    return this.value;
};

/**
 * Abstract model of flags found on BMO (and later hopefully not
 * only there)
 *
 * @return Object with function properties:
 *   - set Approve flag (currently "+"),
 *   - reject Reject flag (currently "-"),
 *   - ask ask for decision (currently "?"),
 *   - unset clear the flag to the initial state (currently "--"), and
 *   - dump dump internal variable to console 
 */
function FlagList() {
  this.flags = {};
  var tdColumn2 = document.getElementById("custom_flags");
  var flag_selects = tdColumn2.getElementsByTagName("select");
  Array.forEach(flag_selects, function(sel) {
    var object = new FlagElement(sel, tdColumn2);
    this.flags[object.id] = object;
  });
}

FlagList.prototype.set = function set(label) {
    this.flags[label].set();
};

FlagList.prototype.reject = function reject(label) {
    this.flags[label].reject();
};

FlagList.prototype.ask = function ask(label) {
    this.flags[label].ask();
};

FlagList.prototype.unset = function unset(label) {
    this.flags[label].unset();
};

FlagList.prototype.toString = function toString() {
    var out = "flags:\n";
    for (var key in this.flags) {
      out += this.flags[key] + ",\n";
    }
    return out;
};

// =================================================

/*
 flagNames ... list of all tag names
 flags ... dictionary of td elements keyed by flag names

 function trimContent(el) {
  return el.textContent.trim();
}
 */
  /**
   * Finds history item in the collected flags, returns WHAT??? FIXME
   *
   * @param item a History Item
   *            (which I should rewrite into proper objects as well? FIXME)
   * @return FIXME
   */
  function findFlag(item) {
    // This does actually makes sense because "added"/"removed" property of
    // changes item can contain multiple flags FIXME
    /**
     * Is name among flagNames? If yes, return array with one element ...
     * found name as a key to flags dict.
     *
     * @param name String to be looked up
     * @return Array with one element String with flagName corresponding to
     *          the key of flags dict.
     */
    function lookup(name) {
      name = name.replace('\u2011', '-', 'g');
      for (var i = 0; i < flagNames.length; ++i) {
        var quotedFlagName = flagNames[i].replace('.', '\\.', 'g').
          replace('\u2011', '-', 'g');
        if ((new RegExp('^' + quotedFlagName)).test(name)) {
          return flagNames[i];
        }
      }
      return null;
    }

    var base = item[4] ? 2 : 0; // Is this strange way how to avoid following
    // condition to be satisified, or it is possible that item[0] can actually be
    // "Flag"?
    // handle normal flags
    if (trimContent(item[base]) == 'Flag') { // FIXME This is acutally bug, it could
    // be "Flags" as well in HTML
      var result = [];
      // This could contain actual value of the flag (or flags) as one string
      // separated by commas with (optional) values in parenthesis
      var tmp = lookup(trimContent(item[base + 1]));
      if (tmp) {
        result.push(tmp);
      }
      // Or it could be also elsewhere? Seems weird.... hopefully not needed
      // with JSON
      tmp = lookup(trimContent(item[base + 2]));
      if (tmp) {
        result.push(tmp);
      }
      
      return result;
    }
    // handle special pseudo-flags
    return lookup(trimContent(item[base]));
  }

  // ========================================================