debian-mirror-gitlab/app/assets/javascripts/confirm_danger_modal.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2021-01-03 14:25:43 +05:30
import { Rails } from '~/lib/utils/rails_ujs';
2018-03-17 18:26:18 +05:30
import { rstrip } from './lib/utils/common_utils';
2016-09-13 17:45:13 +05:30
2020-01-01 13:55:28 +05:30
function openConfirmDangerModal($form, $modal, text) {
const $input = $('.js-confirm-danger-input', $modal);
2018-11-18 11:00:15 +05:30
$input.val('');
2020-01-01 13:55:28 +05:30
$('.js-confirm-text', $modal).text(text || '');
$modal.modal('show');
2016-09-13 17:45:13 +05:30
2020-01-01 13:55:28 +05:30
const confirmTextMatch = $('.js-confirm-danger-match', $modal).text();
const $submit = $('.js-confirm-danger-submit', $modal);
2018-05-09 12:01:36 +05:30
$submit.disable();
2018-11-18 11:00:15 +05:30
$input.focus();
2018-05-09 12:01:36 +05:30
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2020-01-01 13:55:28 +05:30
$input.off('input').on('input', function handleInput() {
const confirmText = rstrip($(this).val());
if (confirmText === confirmTextMatch) {
$submit.enable();
} else {
$submit.disable();
}
});
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2020-01-01 13:55:28 +05:30
$('.js-confirm-danger-submit', $modal)
2018-12-13 13:39:08 +05:30
.off('click')
2021-01-03 14:25:43 +05:30
.on('click', () => {
if ($form.data('remote')) {
Rails.fire($form[0], 'submit');
} else {
$form.submit();
}
});
2018-05-09 12:01:36 +05:30
}
2020-01-01 13:55:28 +05:30
function getModal($btn) {
const $modal = $btn.prev('.modal');
if ($modal.length) {
return $modal;
}
return $('#modal-confirm-danger');
}
2018-05-09 12:01:36 +05:30
export default function initConfirmDangerModal() {
2018-12-13 13:39:08 +05:30
$(document).on('click', '.js-confirm-danger', e => {
2018-05-09 12:01:36 +05:30
const $btn = $(e.target);
2020-01-01 13:55:28 +05:30
const checkFieldName = $btn.data('checkFieldName');
const checkFieldCompareValue = $btn.data('checkCompareValue');
const checkFieldVal = parseInt($(`[name="${checkFieldName}"]`).val(), 10);
if (!checkFieldName || checkFieldVal < checkFieldCompareValue) {
e.preventDefault();
const $form = $btn.closest('form');
const $modal = getModal($btn);
const text = $btn.data('confirmDangerMessage');
openConfirmDangerModal($form, $modal, text);
}
2018-05-09 12:01:36 +05:30
});
}