2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-09-21 11:09:44 -04:00
|
|
|
import { Rails } from '~/lib/utils/rails_ujs';
|
2017-09-06 04:01:01 -04:00
|
|
|
import { rstrip } from './lib/utils/common_utils';
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2019-12-06 07:06:21 -05:00
|
|
|
function openConfirmDangerModal($form, $modal, text) {
|
|
|
|
const $input = $('.js-confirm-danger-input', $modal);
|
2018-07-06 10:14:31 -04:00
|
|
|
$input.val('');
|
|
|
|
|
2019-12-06 07:06:21 -05:00
|
|
|
$('.js-confirm-text', $modal).text(text || '');
|
|
|
|
$modal.modal('show');
|
2018-03-20 01:37:16 -04:00
|
|
|
|
2019-12-06 07:06:21 -05:00
|
|
|
const confirmTextMatch = $('.js-confirm-danger-match', $modal).text();
|
|
|
|
const $submit = $('.js-confirm-danger-submit', $modal);
|
2018-03-20 01:37:16 -04:00
|
|
|
$submit.disable();
|
2018-07-06 10:14:31 -04:00
|
|
|
$input.focus();
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2019-12-06 07:06:21 -05:00
|
|
|
$input.off('input').on('input', function handleInput() {
|
|
|
|
const confirmText = rstrip($(this).val());
|
|
|
|
if (confirmText === confirmTextMatch) {
|
|
|
|
$submit.enable();
|
|
|
|
} else {
|
|
|
|
$submit.disable();
|
|
|
|
}
|
|
|
|
});
|
2020-09-21 11:09:44 -04:00
|
|
|
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2019-12-06 07:06:21 -05:00
|
|
|
$('.js-confirm-danger-submit', $modal)
|
2018-10-24 15:17:03 -04:00
|
|
|
.off('click')
|
2020-09-21 11:09:44 -04:00
|
|
|
.on('click', () => {
|
|
|
|
if ($form.data('remote')) {
|
|
|
|
Rails.fire($form[0], 'submit');
|
|
|
|
} else {
|
|
|
|
$form.submit();
|
|
|
|
}
|
|
|
|
});
|
2018-03-20 01:37:16 -04:00
|
|
|
}
|
2018-03-20 11:50:26 -04:00
|
|
|
|
2019-12-06 07:06:21 -05:00
|
|
|
function getModal($btn) {
|
|
|
|
const $modal = $btn.prev('.modal');
|
|
|
|
|
|
|
|
if ($modal.length) {
|
|
|
|
return $modal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $('#modal-confirm-danger');
|
|
|
|
}
|
|
|
|
|
2018-03-20 11:50:26 -04:00
|
|
|
export default function initConfirmDangerModal() {
|
2020-12-23 16:10:24 -05:00
|
|
|
$(document).on('click', '.js-confirm-danger', (e) => {
|
2018-03-20 11:50:26 -04:00
|
|
|
const $btn = $(e.target);
|
2019-12-06 07:06:21 -05:00
|
|
|
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-03-20 11:50:26 -04:00
|
|
|
});
|
|
|
|
}
|