BigW Consortium Gitlab

ref_select_dropdown.js 1.31 KB
Newer Older
1 2
class RefSelectDropdown {
  constructor($dropdownButton, availableRefs) {
3
    const availableRefsValue = availableRefs || JSON.parse(document.getElementById('availableRefs').innerHTML);
4
    $dropdownButton.glDropdown({
5
      data: availableRefsValue,
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
      filterable: true,
      filterByText: true,
      remote: false,
      fieldName: $dropdownButton.data('field-name'),
      filterInput: 'input[type="search"]',
      selectable: true,
      isSelectable(branch, $el) {
        return !$el.hasClass('is-active');
      },
      text(branch) {
        return branch;
      },
      id(branch) {
        return branch;
      },
      toggleLabel(branch) {
        return branch;
      },
    });

    const $dropdownContainer = $dropdownButton.closest('.dropdown');
    const $fieldInput = $(`input[name="${$dropdownButton.data('field-name')}"]`, $dropdownContainer);
    const $filterInput = $('input[type="search"]', $dropdownContainer);

    $filterInput.on('keyup', (e) => {
      const keyCode = e.keyCode || e.which;
      if (keyCode !== 13) return;

      const ref = $filterInput.val().trim();
      if (ref === '') {
        return;
      }

      $fieldInput.val(ref);
      $('.dropdown-toggle-text', $dropdownButton).text(ref);

      $dropdownContainer.removeClass('open');
    });
  }
}

export default RefSelectDropdown;