aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/groups/search-view.js
blob: 442c5533904f90c908ae13f8ba1fa61f660f4142 (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
import _ from 'underscore';
import Marionette from 'backbone.marionette';
import Template from './templates/groups-search.hbs';

export default Marionette.ItemView.extend({
  template: Template,

  events: {
    'submit #groups-search-form': 'onFormSubmit',
    'search #groups-search-query': 'debouncedOnKeyUp',
    'keyup #groups-search-query': 'debouncedOnKeyUp'
  },

  initialize: function () {
    this._bufferedValue = null;
    this.debouncedOnKeyUp = _.debounce(this.onKeyUp, 400);
  },

  onRender: function () {
    this.delegateEvents();
  },

  onFormSubmit: function (e) {
    e.preventDefault();
    this.debouncedOnKeyUp();
  },

  onKeyUp: function () {
    var q = this.getQuery();
    if (q === this._bufferedValue) {
      return;
    }
    this._bufferedValue = this.getQuery();
    if (this.searchRequest != null) {
      this.searchRequest.abort();
    }
    this.searchRequest = this.search(q);
  },

  getQuery: function () {
    return this.$('#groups-search-query').val();
  },

  search: function (q) {
    return this.collection.fetch({ reset: true, data: { q: q } });
  }
});