aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/users/search-view.js
blob: 5129bf5a2536d7ddb19d4270a19483493f9644a6 (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
define([
  './templates'
], function () {

  return Marionette.ItemView.extend({
    template: Templates['users-search'],

    events: {
      'submit #users-search-form': 'onFormSubmit',
      'search #users-search-query': 'debouncedOnKeyUp',
      'keyup #users-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.$('#users-search-query').val();
    },

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

});