summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/projects/merge_requests/creations/new/compare_autocomplete.js
blob: 65942464e2bf416539e3b1404e3cf2cb735e1c88 (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
/* eslint-disable func-names */

import $ from 'jquery';
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';
import { __ } from '~/locale';
import { fixTitle } from '~/tooltips';

export default function initCompareAutocomplete(limitTo = null, clickHandler = () => {}) {
  $('.js-compare-dropdown').each(function () {
    const $dropdown = $(this);
    const selected = $dropdown.data('selected');
    const defaultText = $dropdown.data('defaultText').trim();
    const $dropdownContainer = $dropdown.closest('.dropdown');
    const $fieldInput = $(`input[name="${$dropdown.data('fieldName')}"]`, $dropdownContainer);
    const $filterInput = $('input[type="search"]', $dropdownContainer);
    initDeprecatedJQueryDropdown($dropdown, {
      data(term, callback) {
        const params = {
          ref: $dropdown.data('ref'),
          search: term,
        };

        if (limitTo) {
          params.find = limitTo;
        }

        axios
          .get($dropdown.data('refsUrl'), {
            params,
          })
          .then(({ data }) => {
            if (limitTo) {
              callback(data[capitalizeFirstCharacter(limitTo)] || []);
            } else {
              callback(data);
            }
          })
          .catch(() =>
            createAlert({
              message: __('Error fetching refs'),
            }),
          );
      },
      selectable: true,
      filterable: true,
      filterRemote: Boolean($dropdown.data('refsUrl')),
      fieldName: $dropdown.data('fieldName'),
      filterInput: 'input[type="search"]',
      renderRow(ref) {
        const link = $('<a />')
          .attr('href', '#')
          .addClass(ref === selected ? 'is-active' : '')
          .text(ref)
          .attr('data-ref', ref);
        if (ref.header != null) {
          return $('<li />').addClass('dropdown-header').text(ref.header);
        }
        return $('<li />').append(link);
      },
      id(obj, $el) {
        return $el.attr('data-ref');
      },
      toggleLabel(obj, $el) {
        if ($el.hasClass('is-active')) {
          return $el.text().trim();
        }

        return defaultText;
      },
      clicked: () => clickHandler($dropdown),
    });
    $filterInput.on('keyup', (e) => {
      const keyCode = e.keyCode || e.which;
      if (keyCode !== 13) return;
      const text = $filterInput.val();
      $fieldInput.val(text);
      $('.dropdown-toggle-text', $dropdown).text(text);
      $dropdownContainer.removeClass('open');
    });

    $dropdownContainer.on('click', '.dropdown-content a', (e) => {
      $dropdown.prop('title', e.target.text.replace(/_+?/g, '-'));
      if ($dropdown.hasClass('has-tooltip')) {
        fixTitle($dropdown);
      }
    });
  });
}