summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/groups/groups_filterable_list.js
blob: 693519729acfaf478126dc44a3ca3d52e153cad4 (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
import $ from 'jquery';
import FilterableList from '~/filterable_list';
import eventHub from './event_hub';
import { normalizeHeaders, getParameterByName } from '../lib/utils/common_utils';

export default class GroupFilterableList extends FilterableList {
  constructor({
    form,
    filter,
    holder,
    filterEndpoint,
    pagePath,
    dropdownSel,
    filterInputField,
    action,
  }) {
    super(form, filter, holder, filterInputField);
    this.form = form;
    this.filterEndpoint = filterEndpoint;
    this.pagePath = pagePath;
    this.filterInputField = filterInputField;
    this.$dropdown = $(dropdownSel);
    this.action = action;
  }

  getFilterEndpoint() {
    return this.filterEndpoint;
  }

  getPagePath(queryData) {
    const params = queryData ? $.param(queryData) : '';
    const queryString = params ? `?${params}` : '';
    const path = this.pagePath || window.location.pathname;
    return `${path}${queryString}`;
  }

  bindEvents() {
    super.bindEvents();

    this.onFilterOptionClickWrapper = this.onOptionClick.bind(this);

    this.$dropdown.on('click', 'a', this.onFilterOptionClickWrapper);
  }

  onFilterInput() {
    const queryData = {};
    const $form = $(this.form);
    const archivedParam = getParameterByName('archived', window.location.href);
    const filterGroupsParam = $form.find(`[name="${this.filterInputField}"]`).val();

    if (filterGroupsParam) {
      queryData[this.filterInputField] = filterGroupsParam;
    }

    if (archivedParam) {
      queryData.archived = archivedParam;
    }

    this.filterResults(queryData);

    if (this.setDefaultFilterOption) {
      this.setDefaultFilterOption();
    }
  }

  setDefaultFilterOption() {
    const defaultOption = $.trim(
      this.$dropdown
        .find('.dropdown-menu li.js-filter-sort-order a')
        .first()
        .text(),
    );
    this.$dropdown.find('.dropdown-label').text(defaultOption);
  }

  onOptionClick(e) {
    e.preventDefault();

    const queryData = {};

    // Get type of option selected from dropdown
    const currentTargetClassList = e.currentTarget.parentElement.classList;
    const isOptionFilterBySort = currentTargetClassList.contains('js-filter-sort-order');
    const isOptionFilterByArchivedProjects = currentTargetClassList.contains(
      'js-filter-archived-projects',
    );

    // Get option query param, also preserve currently applied query param
    const sortParam = getParameterByName(
      'sort',
      isOptionFilterBySort ? e.currentTarget.href : window.location.href,
    );
    const archivedParam = getParameterByName(
      'archived',
      isOptionFilterByArchivedProjects ? e.currentTarget.href : window.location.href,
    );

    if (sortParam) {
      queryData.sort = sortParam;
    }

    if (archivedParam) {
      queryData.archived = archivedParam;
    }

    this.filterResults(queryData);

    // Active selected option
    if (isOptionFilterBySort) {
      this.$dropdown.find('.dropdown-label').text($.trim(e.currentTarget.text));
      this.$dropdown.find('.dropdown-menu li.js-filter-sort-order a').removeClass('is-active');
    } else if (isOptionFilterByArchivedProjects) {
      this.$dropdown
        .find('.dropdown-menu li.js-filter-archived-projects a')
        .removeClass('is-active');
    }

    $(e.target).addClass('is-active');

    // Clear current value on search form
    this.form.querySelector(`[name="${this.filterInputField}"]`).value = '';
  }

  onFilterSuccess(res, queryData) {
    const currentPath = this.getPagePath(queryData);

    window.history.replaceState(
      {
        page: currentPath,
      },
      document.title,
      currentPath,
    );

    eventHub.$emit(
      `${this.action}updateGroups`,
      res.data,
      Object.prototype.hasOwnProperty.call(queryData, this.filterInputField),
    );
    eventHub.$emit(`${this.action}updatePagination`, normalizeHeaders(res.headers));
  }
}