aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/background-tasks/search.js
blob: a1624f02aaea70d7a0fb95a4b7d3ee81f92b4be5 (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
import $ from 'jquery';
import moment from 'moment';
import React from 'react';
import RadioToggle from '../../components/shared/radio-toggle';
import { STATUSES, CURRENTS, DATE, DATE_FORMAT } from './constants';

export default React.createClass({
  componentDidUpdate() {
    this.attachDatePicker();
  },

  componentDidMount() {
    this.attachDatePicker();
  },

  getCurrentsOptions() {
    return [
      { value: CURRENTS.ALL, label: window.t('background_tasks.currents_filter.ALL') },
      { value: CURRENTS.ONLY_CURRENTS, label: window.t('background_tasks.currents_filter.ONLY_CURRENTS') }
    ];
  },

  getStatusOptions() {
    return [
      { value: STATUSES.ALL, label: window.t('background_task.status.ALL') },
      { value: STATUSES.SUCCESS, label: window.t('background_task.status.SUCCESS') },
      { value: STATUSES.FAILED, label: window.t('background_task.status.FAILED') },
      { value: STATUSES.CANCELED, label: window.t('background_task.status.CANCELED') }
    ];
  },

  getDateOptions() {
    return [
      { value: DATE.ANY, label: window.t('background_tasks.date_filter.ALL') },
      { value: DATE.TODAY, label: window.t('background_tasks.date_filter.TODAY') },
      { value: DATE.CUSTOM, label: window.t('background_tasks.date_filter.CUSTOM') }
    ];
  },

  onDateChange(newDate) {
    if (newDate === DATE.CUSTOM) {
      let minDateRaw = this.refs.minDate.value,
          maxDateRaw = this.refs.maxDate.value,
          minDate = moment(minDateRaw, DATE_FORMAT, true),
          maxDate = moment(maxDateRaw, DATE_FORMAT, true);
      this.props.onDateChange(newDate,
          minDate.isValid() ? minDate : null,
          maxDate.isValid() ? maxDate : null);
    } else {
      this.props.onDateChange(newDate);
    }
  },

  onDateInputChange() {
    this.onDateChange(DATE.CUSTOM);
  },

  attachDatePicker() {
    let opts = {
      dateFormat: 'yy-mm-dd',
      changeMonth: true,
      changeYear: true,
      onSelect: this.onDateInputChange
    };
    if ($.fn.datepicker) {
      $(this.refs.minDate).datepicker(opts);
      $(this.refs.maxDate).datepicker(opts);
    }
  },

  renderCustomDateInput() {
    let shouldBeVisible = this.props.dateFilter === DATE.CUSTOM,
        className = shouldBeVisible ? 'spacer-top' : 'spacer-top hidden';
    return (
        <div className={className}>
          from&nbsp;
          <input onChange={this.onDateInputChange} ref="minDate" type="text"/>
          &nbsp;to&nbsp;
          <input onChange={this.onDateInputChange} ref="maxDate" type="text"/>
        </div>
    );
  },

  onSearchFormSubmit(e) {
    e.preventDefault();
    this.onSearch();
  },

  onSearch() {
    let searchInput = this.refs.searchInput,
        query = searchInput.value;
    this.props.onSearch(query);
  },

  renderSearchBox() {
    if (this.props.options && this.props.options.component) {
      // do not render search form on the project-level page
      return null;
    }
    return (
        <form onSubmit={this.onSearchFormSubmit} className="search-box">
          <button className="search-box-submit button-clean">
            <i className="icon-search"></i>
          </button>
          <input onChange={this.onSearch} value={this.props.searchQuery} ref="searchInput" className="search-box-input"
                 type="search" placeholder="Search"/>
        </form>
    );
  },

  refresh(e) {
    e.preventDefault();
    this.props.refresh();
    let btn = e.target;
    btn.disabled = true;
    setTimeout(() => btn.disabled = false, 500);
  },

  render() {
    return (
        <section className="big-spacer-top big-spacer-bottom">
          <ul className="list-inline">
            <li>
              <RadioToggle options={this.getStatusOptions()} value={this.props.statusFilter}
                           name="background-task-status" onCheck={this.props.onStatusChange}/>
            </li>
            <li>
              <RadioToggle options={this.getCurrentsOptions()} value={this.props.currentsFilter}
                           name="background-task-currents" onCheck={this.props.onCurrentsChange}/>
            </li>
            <li>
              <RadioToggle options={this.getDateOptions()} value={this.props.dateFilter}
                           name="background-task-date" onCheck={this.onDateChange}/>
              {this.renderCustomDateInput()}
            </li>
            <li>{this.renderSearchBox()}</li>
            <li className="pull-right">
              <button onClick={this.refresh} ref="reloadButton">{window.t('reload')}</button>
            </li>
          </ul>
        </section>
    );
  }
});