summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/projects/merge_requests/edit/index.js
blob: 406959c80eacece2215967708f9b0e86e4d97edb (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
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';

import { GitLabDropdown } from '~/deprecated_jquery_dropdown/gl_dropdown';

import initMergeRequest from '~/pages/projects/merge_requests/init_merge_request';
import initCheckFormState from './check_form_state';
import initFormUpdate from './update_form';

function initTargetBranchSelector() {
  const targetBranch = document.querySelector('.js-target-branch');
  const { selected, fieldName, refsUrl } = targetBranch?.dataset ?? {};
  const formField = document.querySelector(`input[name="${fieldName}"]`);

  if (targetBranch && refsUrl && formField) {
    /* eslint-disable-next-line no-new */
    new GitLabDropdown(targetBranch, {
      selectable: true,
      filterable: true,
      filterRemote: Boolean(refsUrl),
      filterInput: 'input[type="search"]',
      data(term, callback) {
        const params = {
          search: term,
        };

        axios
          .get(refsUrl, {
            params,
          })
          .then(({ data }) => {
            callback(data);
          })
          .catch(() =>
            createAlert({
              message: __('Error fetching branches'),
            }),
          );
      },
      renderRow(branch) {
        const item = document.createElement('li');
        const link = document.createElement('a');

        link.setAttribute('href', '#');
        link.dataset.branch = branch;
        link.classList.toggle('is-active', branch === selected);
        link.textContent = branch;

        item.appendChild(link);

        return item;
      },
      id(obj, $el) {
        return $el.data('id');
      },
      toggleLabel(obj, $el) {
        return $el.text().trim();
      },
      clicked({ $el, e }) {
        e.preventDefault();

        const branchName = $el[0].dataset.branch;

        formField.setAttribute('value', branchName);
      },
    });
  }
}

initMergeRequest();
initFormUpdate();
initCheckFormState();
initTargetBranchSelector();