2023-04-13 21:34:36 +08:00
|
|
|
/* global HelperModule I18n */
|
2023-03-27 18:38:01 +08:00
|
|
|
|
2021-04-30 19:26:06 +08:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function initNewUserAssignmentFormListener() {
|
2023-01-23 05:23:18 +08:00
|
|
|
$(document).on('change', 'form#new-user-assignment-form', function() {
|
2021-04-30 19:26:06 +08:00
|
|
|
let values = [];
|
|
|
|
let count = 0;
|
2021-05-02 17:36:10 +08:00
|
|
|
let submitBtn = $(this).find('input[type="submit"]');
|
2021-04-30 19:26:06 +08:00
|
|
|
|
|
|
|
$(this).find('input:checked').each((_, el) => {
|
2023-03-06 23:43:46 +08:00
|
|
|
let select = $(el).closest('.new-member-item').find('select');
|
2021-04-30 19:26:06 +08:00
|
|
|
let selectValue = parseInt(select.val(), 10);
|
|
|
|
values.push(selectValue);
|
|
|
|
count += 1;
|
|
|
|
|
|
|
|
if (isNaN(selectValue)) {
|
|
|
|
select.closest('.form-group ')
|
|
|
|
.addClass('has-error');
|
|
|
|
} else {
|
|
|
|
select.closest('.form-group ')
|
|
|
|
.removeClass('has-error');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (values.includes(NaN) || values.length === 0) {
|
|
|
|
submitBtn.attr('disabled', 'disabled');
|
|
|
|
} else {
|
|
|
|
submitBtn.attr('disabled', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (count) {
|
|
|
|
case 0:
|
|
|
|
submitBtn.val(submitBtn.data('label-default'));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
submitBtn.val(submitBtn.data('label-singular'));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
submitBtn.val(submitBtn.data('label-plural').replace('{num}', count));
|
|
|
|
}
|
|
|
|
});
|
2023-01-23 05:23:18 +08:00
|
|
|
|
|
|
|
$(document).on('ajax:success', 'form#new-user-assignment-form', function(_e, data) {
|
2023-03-28 22:22:38 +08:00
|
|
|
$('#user_assignments_modal').replaceWith($(data.html).find('#user_assignments_modal'));
|
|
|
|
HelperModule.flashAlertMsg(data.flash, 'success');
|
2023-01-23 05:23:18 +08:00
|
|
|
});
|
2023-01-23 23:01:40 +08:00
|
|
|
|
2023-04-13 21:34:36 +08:00
|
|
|
$(document).on('ajax:error', 'form#new-user-assignment-form', function(_e, data) {
|
|
|
|
HelperModule.flashAlertMsg(data.responseJSON.flash
|
|
|
|
? data.responseJSON.flash : I18n.t('errors.general'), 'danger');
|
|
|
|
});
|
|
|
|
|
2023-03-27 18:38:01 +08:00
|
|
|
$(document).on('ajax:error', 'form.member-item', function(_e, data) {
|
|
|
|
HelperModule.flashAlertMsg(data.responseJSON.flash, 'danger');
|
|
|
|
});
|
|
|
|
|
2023-03-31 18:14:26 +08:00
|
|
|
$(document).on('ajax:success', 'form.member-item', function(_e, data) {
|
|
|
|
if (data.flash) {
|
|
|
|
HelperModule.flashAlertMsg(data.flash, 'success');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-01-23 23:01:40 +08:00
|
|
|
$(document).on('click', '.user-assignment-dropdown .user-role-selector', function() {
|
|
|
|
let roleId = $(this).data('role-id');
|
2023-03-06 23:43:46 +08:00
|
|
|
$(this).closest('.dropdown').find('#user_assignment_user_role_id, .default-public-user-role-id').val(roleId);
|
2023-01-23 23:01:40 +08:00
|
|
|
$(this).closest('form').trigger('submit');
|
|
|
|
});
|
2021-04-30 19:26:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$(document).one('turbolinks:load', initNewUserAssignmentFormListener);
|
2021-05-02 17:36:10 +08:00
|
|
|
}());
|