aboutsummaryrefslogtreecommitdiffstats
path: root/data/lib/flags.js
blob: ffbdb02bdbc9c6e03d210e1936ebe16f61e8fd24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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]));
  }

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