summaryrefslogtreecommitdiff
path: root/assets/js/mumi.js
blob: a44a386486ac748f1150c7625db53f7a1c519b36 (about) (plain)
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
162
163
164
165
166
167
168
169
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-v3.0-or-later
var mumi = (function () {
  var initTokenInput = function () {
    var inputElement = document.querySelector (".tokenInput input#query");
    if (inputElement == null) {
      return;
    }
    const queryTokenizer = {
      [Symbol.split](str) {
        let phrase = false;
        let phrasePos = 0;
        let pos = 0;
        const result = [];
        while (pos < str.length) {
          let matchPos = str.substring(pos).search(/(\s|\")/);
          if (matchPos === -1) {
            result.push(str.substring(pos));
            break;
          }

          matchPos += pos;
          let char = str.charAt(matchPos);
          let begin = pos;

          if (char === '"') {
            if (phrase) {
              /* finished phrase, record from beginning */
              phrase = false;
              begin = phrasePos;
              matchPos += 1;
            } else {
              /* beginning phrase, remember position */
              phrase = true;
              phrasePos = matchPos;
            }
          }

          /* don't push anything while inside a phrase */
          if (!phrase) {
            result.push(str.substring(begin, matchPos));
          }
          pos = matchPos + 1;
        }
        return result;
      },
    };
    var possibleTokens = [
      { text: 'is:open' },
      { text: 'is:pending' },
      { text: 'is:done' },
      { text: 'is:closed' },

      { text: 'tag:confirmed' },
      { text: 'tag:easy' },
      { text: 'tag:fixed' },
      { text: 'tag:help' },
      { text: 'tag:moreinfo' },
      { text: 'tag:notabug' },
      { text: 'tag:patch' },
      { text: 'tag:pending' },
      { text: 'tag:security' },
      { text: 'tag:unreproducible' },
      { text: 'tag:wontfix' },

      { text: 'severity:critical' },
      { text: 'severity:grave' },
      { text: 'severity:important' },
      { text: 'severity:minor' },
      { text: 'severity:normal' },
      { text: 'severity:serious' },
      { text: 'severity:wishlist' },
    ];
    var completionsForTextWithSuggestions = function (suggestions) {
      return function (text) {
        var completions = [];
        text = text.toLowerCase();
        if (text.length) {
          suggestions.forEach(function (suggestion) {
            var suggestionText = suggestion.text.toLowerCase();
            if (suggestionText.substr (0, text.length) == text) {
              completions.push (suggestion);
            }
          }, this);
        }
        return completions;
      };
    };
    var options = {
      freeTextEnabled: true,
      freeTextToken : function (text) {
        return {
          text: text,
          value: text,
          freeText: true,
          group: 'freeText'
        };
      },
      tokenFormatter: function (datum, element) {
        if (datum.freeText) {
          element.classList += " free-text";
        } else {
          element.classList += " completed";
        }
      },
      completionsForText: completionsForTextWithSuggestions (possibleTokens),
      positionFloatingElement: function (element) {
        element.style.position = 'absolute';
        element.style.display = 'inline-block';
        element.style.left = '0px';
        element.style.top = inputElement.offsetTop + inputElement.offsetHeight + 'px';
      }
    };

    tokenInput = new TokenInput (inputElement, options);
    window.tokenInputs = window.tokenInputs || [];
    window.tokenInputs.push (tokenInput);

    const form = document.querySelector ("form#search");
    form.addEventListener ("submit", function (event) {
      event.preventDefault ();
      for (token of tokenInput.getTokens ()) {
        inputElement.value += token.text + " ";
      }
      inputElement.style.visibility = 'hidden';
      form.submit ();
    });

    /* tokenize existing input text */
    if (inputElement.value.length > 0) {
      let items = inputElement.value.split(queryTokenizer).filter(entry => entry.trim() != '');
      let tokens = [];
      for (item of items) {
        if (possibleTokens.find(element => element.text == item)) {
          tokens.push({text: item});
        } else {
          tokens.push(options.freeTextToken(item));
        }
      }
      inputElement.value = "";
      tokenInput.setTokens(tokens);
    }
    inputElement.style.visibility = 'visible';
  };

  var setupLineHandler = function () {
    let lineClickHandler = (evt) => {
      if ((evt.target.classList.contains("line")) &&
          (evt.x < evt.target.offsetLeft)) {
        window.location.hash = evt.target.id;
        return;
      }
    };
    var root = document.querySelector ("div.conversation");
    if (root === null) { return; }
    root.addEventListener ("click", lineClickHandler);
  };

  var init = function () {
    initTokenInput ();
  };
  return({
    'init': init,
    'lines': setupLineHandler,
  });
})();

window.addEventListener ("load", mumi.init);
window.addEventListener ("DOMContentLoaded", mumi.lines);
// @license-end