summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/branches/branches_delete_modal.js
blob: af8bcdc1794e326eb98129ce66d36dc2eacb1425 (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
const MODAL_SELECTOR = '#modal-delete-branch';

class DeleteModal {
  constructor() {
    this.$modal = $(MODAL_SELECTOR);
    this.$toggleBtns = $(`[data-target="${MODAL_SELECTOR}"]`);
    this.$branchName = $('.js-branch-name', this.$modal);
    this.$confirmInput = $('.js-delete-branch-input', this.$modal);
    this.$deleteBtn = $('.js-delete-branch', this.$modal);
    this.bindEvents();
  }

  bindEvents() {
    this.$toggleBtns.on('click', this.setModalData.bind(this));
    this.$confirmInput.on('input', this.setDeleteDisabled.bind(this));
  }

  setModalData(e) {
    this.branchName = e.currentTarget.dataset.branchName || '';
    this.deletePath = e.currentTarget.dataset.deletePath || '';
    this.updateModal();
  }

  setDeleteDisabled(e) {
    this.$deleteBtn.attr('disabled', e.currentTarget.value !== this.branchName);
  }

  updateModal() {
    this.$branchName.text(this.branchName);
    this.$confirmInput.val('');
    this.$deleteBtn.attr('href', this.deletePath);
    this.$deleteBtn.attr('disabled', true);
  }
}

export default DeleteModal;