All files / app/assets/javascripts/groups groups_filterable_list.js

0% Statements 0/51
0% Branches 0/24
0% Functions 0/8
0% Lines 0/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                         
import $ from 'jquery';
import FilterableList from '~/filterable_list';
import { normalizeHeaders } from '../lib/utils/common_utils';
import { getParameterByName } from '../lib/utils/url_utility';
import eventHub from './event_hub';
 
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');
    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.search : window.location.search,
    );
    const archivedParam = getParameterByName(
      'archived',
      isOptionFilterByArchivedProjects ? e.currentTarget.search : window.location.search,
    );
 
    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));
  }
}