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

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

    events: {
      'change [name="update-center-filter"]': 'onFilterChange',

      'submit #update-center-search-form': 'onFormSubmit',
      'search #update-center-search-query': 'debouncedOnKeyUp',
      'keyup #update-center-search-query': 'debouncedOnKeyUp',
      'change #update-center-search-query': 'debouncedOnKeyUp'
    },

    collectionEvents: {
      'filter': 'onFilter'
    },

    initialize: function () {
      this._bufferedValue = null;
      this.debouncedOnKeyUp = _.debounce(this.onKeyUp, 50);
      this.listenTo(this.options.state, 'change', this.render);
    },

    onRender: function () {
      this.$('[data-toggle="tooltip"]').tooltip({ container: 'body', placement: 'bottom' });
    },

    onDestroy: function () {
      this.$('[data-toggle="tooltip"]').tooltip('destroy');
    },

    onFilterChange: function () {
      var value = this.$('[name="update-center-filter"]:checked').val();
      this.filter(value);
    },

    filter: function (value) {
      this.options.router.navigate(value, { trigger: true });
    },

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

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

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

    search: function (q) {
      this.collection.search(q);
    },

    focusSearch: function () {
      var that = this;
      setTimeout(function () {
        that.$('#update-center-search-query').focus();
      }, 0);
    },

    onFilter: function (model) {
      var q = model.get('category');
      this.$('#update-center-search-query').val(q);
      this.search(q);
    },

    serializeData: function () {
      return _.extend(this._super(), { state: this.options.state.toJSON() });
    }
  });

});