aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/issues/facets/creation-date-facet.js
blob: 831cead64240ce989c195d203e6469b8ea72375c (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
define([
  './base-facet',
  '../templates'
], function (BaseFacet) {

  var $ = jQuery;

  return BaseFacet.extend({
    template: Templates['issues-creation-date-facet'],

    events: function () {
      return _.extend(BaseFacet.prototype.events.apply(this, arguments), {
        'change input': 'applyFacet',
        'click .js-select-period-start': 'selectPeriodStart',
        'click .js-select-period-end': 'selectPeriodEnd',
        'click .sonar-d3 rect': 'selectBar',
        'click .js-all': 'onAllClick',
        'click .js-last-week': 'onLastWeekClick',
        'click .js-last-month': 'onLastMonthClick',
        'click .js-last-year': 'onLastYearClick'
      });
    },

    onRender: function () {
      var that = this;
      this.$el.toggleClass('search-navigator-facet-box-collapsed', !this.model.get('enabled'));
      this.$('input').datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true
      });
      var props = ['createdAfter', 'createdBefore', 'createdAt'],
          query = this.options.app.state.get('query');
      props.forEach(function (prop) {
        var value = query[prop];
        if (value != null) {
          return that.$('input[name=' + prop + ']').val(value);
        }
      });
      var values = this.model.getValues();
      if (!(_.isArray(values) && values.length > 0)) {
        var date = moment();
        values = [];
        _.times(10, function () {
            values.push({ count: 0, val: date.toDate().toString() });
            date = date.subtract(1, 'days');
        });
        values.reverse();
      }
      return this.$('.js-barchart').barchart(values);
    },

    selectPeriodStart: function () {
      return this.$('.js-period-start').datepicker('show');
    },

    selectPeriodEnd: function () {
      return this.$('.js-period-end').datepicker('show');
    },

    applyFacet: function () {
      var obj = { createdAt: null, createdInLast: null };
      this.$('input').each(function () {
        var property, value;
        property = $(this).prop('name');
        value = $(this).val();
        obj[property] = value;
      });
      return this.options.app.state.updateFilter(obj);
    },

    disable: function () {
      return this.options.app.state.updateFilter({
        createdAfter: null,
        createdBefore: null,
        createdAt: null,
        createdInLast: null
      });
    },

    selectBar: function (e) {
      var periodStart = $(e.currentTarget).data('period-start'),
          periodEnd = $(e.currentTarget).data('period-end');
      return this.options.app.state.updateFilter({
        createdAfter: periodStart,
        createdBefore: periodEnd,
        createdAt: null,
        createdInLast: null
      });
    },

    selectPeriod: function (period) {
      return this.options.app.state.updateFilter({
        createdAfter: null,
        createdBefore: null,
        createdAt: null,
        createdInLast: period
      });
    },

    onAllClick: function () {
      return this.disable();
    },

    onLastWeekClick: function (e) {
      e.preventDefault();
      return this.selectPeriod('1w');
    },

    onLastMonthClick: function (e) {
      e.preventDefault();
      return this.selectPeriod('1m');
    },

    onLastYearClick: function (e) {
      e.preventDefault();
      return this.selectPeriod('1y');
    },

    serializeData: function () {
      return _.extend(BaseFacet.prototype.serializeData.apply(this, arguments), {
        periodStart: this.options.app.state.get('query').createdAfter,
        periodEnd: this.options.app.state.get('query').createdBefore,
        createdAt: this.options.app.state.get('query').createdAt,
        createdInLast: this.options.app.state.get('query').createdInLast
      });
    }
  });

});