branches_delete_modal.js 1.5 KB
Newer Older
1 2
import $ from 'jquery';

3 4 5 6 7 8 9 10 11
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);
12
    this.$notMerged = $('.js-not-merged', this.$modal);
13 14 15 16 17 18
    this.bindEvents();
  }

  bindEvents() {
    this.$toggleBtns.on('click', this.setModalData.bind(this));
    this.$confirmInput.on('input', this.setDeleteDisabled.bind(this));
19
    this.$deleteBtn.on('click', this.setDisableDeleteButton.bind(this));
20 21 22
  }

  setModalData(e) {
23 24 25 26
    const branchData = e.currentTarget.dataset;
    this.branchName = branchData.branchName || '';
    this.deletePath = branchData.deletePath || '';
    this.isMerged = !!branchData.isMerged;
27 28 29 30 31 32 33
    this.updateModal();
  }

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

34 35 36 37 38 39 40 41 42 43
  setDisableDeleteButton(e) {
    if (this.$deleteBtn.is('[disabled]')) {
      e.preventDefault();
      e.stopPropagation();
      return false;
    }

    return true;
  }

44 45 46 47 48
  updateModal() {
    this.$branchName.text(this.branchName);
    this.$confirmInput.val('');
    this.$deleteBtn.attr('href', this.deletePath);
    this.$deleteBtn.attr('disabled', true);
49
    this.$notMerged.toggleClass('hidden', this.isMerged);
50 51 52 53
  }
}

export default DeleteModal;