summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/file_pickers.js
blob: 956a4954afb45e783201cf26c746a5e32c9fe84e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
export default function initFilePickers() {
  const filePickers = document.querySelectorAll('.js-filepicker');

  filePickers.forEach(filePicker => {
    const button = filePicker.querySelector('.js-filepicker-button');

    button.addEventListener('click', () => {
      const form = button.closest('form');
      form.querySelector('.js-filepicker-input').click();
    });

    const input = filePicker.querySelector('.js-filepicker-input');

    input.addEventListener('change', () => {
      const form = input.closest('form');
      const filename = input.value.replace(/^.*[\\\/]/, ''); // eslint-disable-line no-useless-escape

      form.querySelector('.js-filepicker-filename').textContent = filename;
    });
  });
}