summaryrefslogtreecommitdiffstats
path: root/js/instant-search.js
blob: 6ce3516a749d10ac9de9dbaff72a8f29756d18db (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0. */

var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;

Event.onDOMReady(function() {
  YAHOO.bugzilla.instantSearch.onInit();
  if (YAHOO.bugzilla.instantSearch.getContent().length >= 4) {
    YAHOO.bugzilla.instantSearch.doSearch(YAHOO.bugzilla.instantSearch.getContent());
  } else {
    YAHOO.bugzilla.instantSearch.reset();
  }
});

YAHOO.bugzilla.instantSearch = {
  counter: 0,
  dataTable: null,
  dataTableColumns: null,
  elContent: null,
  elList: null,
  currentSearchQuery: '',
  currentSearchProduct: '',

  onInit: function() {
    YAHOO.util.Connect.setDefaultPostHeader('application/json; charset=UTF-8');

    this.elContent = Dom.get('content');
    this.elList = Dom.get('results');

    Event.addListener(this.elContent, 'keyup', this.onContentKeyUp);
    Event.addListener(Dom.get('product'), 'change', this.onProductChange);
  },

  setLabels: function(labels) {
    this.dataTableColumns = [
      { key: "id", label: labels.id, formatter: this.formatId },
      { key: "summary", label: labels.summary, formatter: "text" },
      { key: "component", label: labels.component, formatter: "text" },
      { key: "status", label: labels.status, formatter: this.formatStatus },
    ];
  },

  initDataTable: function() {
    var dataSource = new YAHOO.util.XHRDataSource("jsonrpc.cgi");
    dataSource.connTimeout = 15000;
    dataSource.connMethodPost = true;
    dataSource.connXhrMode = "cancelStaleRequests";
    dataSource.maxCacheEntries = 3;
    dataSource.responseSchema = {
      resultsList : "result.bugs",
      metaFields : { error: "error", jsonRpcId: "id" }
    };
    // DataSource can't understand a JSON-RPC error response, so
    // we have to modify the result data if we get one.
    dataSource.doBeforeParseData =
      function(oRequest, oFullResponse, oCallback) {
        if (oFullResponse.error) {
          oFullResponse.result = {};
          oFullResponse.result.bugs = [];
          if (console)
            console.error("JSON-RPC error:", oFullResponse.error);
        }
        return oFullResponse;
      };
    dataSource.subscribe('dataErrorEvent',
      function() {
        YAHOO.bugzilla.instantSearch.currentSearchQuery = '';
      }
    );

    this.dataTable = new YAHOO.widget.DataTable(
      'results',
      this.dataTableColumns,
      dataSource,
      {
        initialLoad: false,
        MSG_EMPTY: 'No matching bugs found.',
        MSG_ERROR: 'An error occurred while searching for bugs, please try again.'
      }
    );
  },

  formatId: function(el, oRecord, oColumn, oData) {
    el.innerHTML = '<a href="show_bug.cgi?id=' + oData + '" target="_blank">' + oData + '</a>';
  },

  formatStatus: function(el, oRecord, oColumn, oData) {
    var resolution = oRecord.getData('resolution');
    var bugStatus = display_value('bug_status', oData);
    if (resolution) {
      el.innerHTML = bugStatus + ' ' + display_value('resolution', resolution);
    } else {
      el.innerHTML = bugStatus;
    }
  },

  reset: function() {
    Dom.addClass(this.elList, 'hidden');
    this.elList.innerHTML = '';
    this.currentSearchQuery = '';
    this.currentSearchProduct = '';
  },

  onContentKeyUp: function(e) {
    clearTimeout(YAHOO.bugzilla.instantSearch.lastTimeout);
    YAHOO.bugzilla.instantSearch.lastTimeout = setTimeout(function() {
      YAHOO.bugzilla.instantSearch.doSearch(YAHOO.bugzilla.instantSearch.getContent()) },
      600);
  },

  onProductChange: function(e) {
    YAHOO.bugzilla.instantSearch.doSearch(YAHOO.bugzilla.instantSearch.getContent());
  },

  doSearch: function(query) {
    if (query.length < 4)
      return;

    // don't query if we already have the results (or they are pending)
    var product = Dom.get('product').value;
    if (YAHOO.bugzilla.instantSearch.currentSearchQuery == query &&
        YAHOO.bugzilla.instantSearch.currentSearchProduct == product)
      return;
    YAHOO.bugzilla.instantSearch.currentSearchQuery = query;
    YAHOO.bugzilla.instantSearch.currentSearchProduct = product;

    // initialise the datatable as late as possible
    YAHOO.bugzilla.instantSearch.initDataTable();

    try {
      // run the search
      Dom.removeClass(YAHOO.bugzilla.instantSearch.elList, 'hidden');

      YAHOO.bugzilla.instantSearch.dataTable.showTableMessage(
        'Searching...&nbsp;&nbsp;&nbsp;' +
        '<img src="extensions/GuidedBugEntry/web/images/throbber.gif"' +
        ' width="16" height="11">',
        YAHOO.widget.DataTable.CLASS_LOADING
      );
      var jsonObject = {
        version: "1.1",
        method: "Bug.possible_duplicates",
        id: ++YAHOO.bugzilla.instantSearch.counter,
        params: {
          product: YAHOO.bugzilla.instantSearch.getProduct(),
          summary: query,
          limit: 20,
          include_fields: [ "id", "summary", "status", "resolution", "component" ]
        }
      };
      if (BUGZILLA.api_token) {
        jsonObject.params.Bugzilla_api_token = BUGZILLA.api_token;
      }

      YAHOO.bugzilla.instantSearch.dataTable.getDataSource().sendRequest(
        YAHOO.lang.JSON.stringify(jsonObject),
        {
          success: YAHOO.bugzilla.instantSearch.onSearchResults,
          failure: YAHOO.bugzilla.instantSearch.onSearchResults,
          scope: YAHOO.bugzilla.instantSearch.dataTable,
          argument: YAHOO.bugzilla.instantSearch.dataTable.getState()
        }
      );

    } catch(err) {
      if (console)
        console.error(err.message);
    }
  },

  onSearchResults: function(sRequest, oResponse, oPayload) {
    YAHOO.bugzilla.instantSearch.dataTable.onDataReturnInitializeTable(sRequest, oResponse, oPayload);
  },

  getContent: function() {
    var content = YAHOO.lang.trim(this.elContent.value);
    // work around chrome bug
    if (content == YAHOO.bugzilla.instantSearch.elContent.getAttribute('placeholder')) {
      return '';
    } else {
      return content;
    }
  },

  getProduct: function() {
    var result = [];
    var name = Dom.get('product').value;
    result.push(name);
    if (products[name] && products[name].related) {
      for (var i = 0, n = products[name].related.length; i < n; i++) {
        result.push(products[name].related[i]);
      }
    }
    return result;
  }

};