2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-02-17 01:08:55 -05:00
|
|
|
import { isEmpty } from 'lodash';
|
2017-03-26 21:33:47 -04:00
|
|
|
import '../commons/bootstrap';
|
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Requires Input behavior
|
|
|
|
//
|
|
|
|
// When called on a form with input fields with the `required` attribute, the
|
|
|
|
// form's submit button will be disabled until all required fields have values.
|
|
|
|
//
|
|
|
|
// ### Example Markup
|
|
|
|
//
|
|
|
|
// <form class="js-requires-input">
|
|
|
|
// <input type="text" required="required">
|
|
|
|
// <input type="submit" value="Submit">
|
|
|
|
// </form>
|
|
|
|
//
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-03-26 21:33:47 -04:00
|
|
|
$.fn.requiresInput = function requiresInput() {
|
|
|
|
const $form = $(this);
|
|
|
|
const $button = $('button[type=submit], input[type=submit]', $form);
|
2018-10-10 02:09:55 -04:00
|
|
|
const fieldSelector =
|
|
|
|
'input[required=required], select[required=required], textarea[required=required]';
|
2017-03-26 21:33:47 -04:00
|
|
|
|
|
|
|
function requireInput() {
|
|
|
|
// Collect the input values of *all* required fields
|
2020-12-23 16:10:24 -05:00
|
|
|
const values = Array.from($(fieldSelector, $form)).map((field) => field.value);
|
2017-03-26 21:33:47 -04:00
|
|
|
|
|
|
|
// Disable the button if any required fields are empty
|
2020-02-17 01:08:55 -05:00
|
|
|
if (values.length && values.some(isEmpty)) {
|
2017-03-26 21:33:47 -04:00
|
|
|
$button.disable();
|
|
|
|
} else {
|
|
|
|
$button.enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set initial button state
|
|
|
|
requireInput();
|
|
|
|
$form.on('change input', fieldSelector, requireInput);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Hide or Show the help block when creating a new project
|
|
|
|
// based on the option selected
|
|
|
|
function hideOrShowHelpBlock(form) {
|
|
|
|
const selected = $('.js-select-namespace option:selected');
|
2018-02-20 17:20:48 -05:00
|
|
|
if (selected.length && selected.data('optionsParent') === 'groups') {
|
2018-04-11 14:26:37 -04:00
|
|
|
form.find('.form-text.text-muted').hide();
|
2017-03-26 21:33:47 -04:00
|
|
|
} else if (selected.length) {
|
2018-04-11 14:26:37 -04:00
|
|
|
form.find('.form-text.text-muted').show();
|
2017-03-26 21:33:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$(() => {
|
2018-11-19 10:03:58 -05:00
|
|
|
$('form.js-requires-input').each((i, el) => {
|
|
|
|
const $form = $(el);
|
|
|
|
|
2017-08-03 16:31:53 -04:00
|
|
|
$form.requiresInput();
|
|
|
|
hideOrShowHelpBlock($form);
|
|
|
|
$('.select2.js-select-namespace').change(() => hideOrShowHelpBlock($form));
|
2018-11-19 10:03:58 -05:00
|
|
|
});
|
2017-03-26 21:33:47 -04:00
|
|
|
});
|