2016-11-03 18:27:17 +08:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
2019-01-23 16:17:27 +08:00
|
|
|
function reloadRecaptchaIfExists() {
|
|
|
|
if (typeof grecaptcha !== 'undefined') {
|
|
|
|
grecaptcha.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.recaptchaCallback = function recaptchaCallback(response) {
|
|
|
|
$('#recaptcha-invite-modal').val(response);
|
|
|
|
};
|
|
|
|
|
2016-11-03 18:27:17 +08:00
|
|
|
function initializeModal(modal) {
|
|
|
|
var modalDialog = modal.find('.modal-dialog');
|
|
|
|
var type = modal.attr('data-type');
|
|
|
|
var stepForm = modal.find('[data-role=step-form]');
|
|
|
|
var stepResults = modal.find('[data-role=step-results]');
|
2016-11-06 18:29:00 +08:00
|
|
|
var stepResultsDiv = modal.find('[data-role=step-results][data-clear]');
|
2016-11-03 18:27:17 +08:00
|
|
|
var inviteBtn = modal.find('[data-role=invite-btn]');
|
2018-07-19 23:56:42 +08:00
|
|
|
var inviteWithRoleDiv = modal.find('[data-role=invite-with-role-div]');
|
|
|
|
var inviteWithRoleBtn = modal.find('[data-role=invite-with-role-btn]');
|
|
|
|
var teamSelectorCheckbox = modal.find('[data-role=team-selector-checkbox]');
|
|
|
|
var teamSelectorDropdown = modal.find('[data-role=team-selector-dropdown]');
|
2017-01-25 19:12:27 +08:00
|
|
|
var teamSelectorDropdown2 = $();
|
2016-11-06 18:29:00 +08:00
|
|
|
var tagsInput = modal.find('[data-role=tags-input]');
|
2019-01-23 16:17:27 +08:00
|
|
|
var recaptchaErrorMsgDiv = modal.find('#recaptcha-error-msg');
|
|
|
|
var recaptchaErrorText = modal.find('#recaptcha-error-msg>span');
|
2016-11-06 18:29:00 +08:00
|
|
|
|
|
|
|
// Set max tags
|
|
|
|
tagsInput.tagsinput({
|
2018-07-19 23:56:42 +08:00
|
|
|
maxTags: modal.data('max-tags')
|
2016-11-06 18:29:00 +08:00
|
|
|
});
|
2016-11-03 18:27:17 +08:00
|
|
|
|
2018-12-07 18:36:26 +08:00
|
|
|
modal.off('show.bs.modal').on('show.bs.modal', function() {
|
2016-11-03 18:27:17 +08:00
|
|
|
// This cannot be scoped outside this function
|
|
|
|
// because it is generated via JS
|
2018-07-19 23:56:42 +08:00
|
|
|
teamSelectorDropdown2 = teamSelectorDropdown.parent()
|
|
|
|
.find('button.dropdown-toggle, li');
|
2016-11-03 18:27:17 +08:00
|
|
|
|
|
|
|
// Show/hide correct step
|
|
|
|
stepForm.show();
|
|
|
|
stepResults.hide();
|
|
|
|
|
|
|
|
// Show/hide buttons & other elements
|
|
|
|
switch (type) {
|
2017-01-25 19:12:27 +08:00
|
|
|
case 'invite_to_team_with_role':
|
2016-11-03 18:27:17 +08:00
|
|
|
case 'invite':
|
2017-01-25 19:12:27 +08:00
|
|
|
case 'invite_with_team_selector':
|
|
|
|
case 'invite_with_team_selector_and_role':
|
2016-11-03 18:27:17 +08:00
|
|
|
inviteBtn.show();
|
|
|
|
inviteWithRoleDiv.hide();
|
|
|
|
break;
|
2017-01-25 19:12:27 +08:00
|
|
|
case 'invite_to_team':
|
2016-11-03 18:27:17 +08:00
|
|
|
inviteBtn.hide();
|
|
|
|
inviteWithRoleDiv.show();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checkbox toggle event
|
|
|
|
if (
|
2017-01-25 19:12:27 +08:00
|
|
|
type === 'invite_with_team_selector' ||
|
|
|
|
type === 'invite_with_team_selector_and_role'
|
2016-11-03 18:27:17 +08:00
|
|
|
) {
|
2017-01-25 19:12:27 +08:00
|
|
|
teamSelectorCheckbox.on('change', function() {
|
2016-11-03 18:27:17 +08:00
|
|
|
if ($(this).is(':checked')) {
|
2017-01-25 19:12:27 +08:00
|
|
|
teamSelectorDropdown.removeAttr('disabled');
|
|
|
|
teamSelectorDropdown2.removeClass('disabled');
|
|
|
|
if (type === 'invite_with_team_selector') {
|
2016-11-03 18:27:17 +08:00
|
|
|
inviteBtn.hide();
|
|
|
|
inviteWithRoleDiv.show();
|
|
|
|
}
|
|
|
|
} else {
|
2017-01-25 19:12:27 +08:00
|
|
|
teamSelectorDropdown.attr('disabled', 'disabled');
|
|
|
|
teamSelectorDropdown2.addClass('disabled');
|
|
|
|
if (type === 'invite_with_team_selector') {
|
2016-11-03 18:27:17 +08:00
|
|
|
inviteBtn.show();
|
|
|
|
inviteWithRoleDiv.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle depending on input tags
|
2018-07-19 23:56:42 +08:00
|
|
|
tagsInput.on('itemAdded', function() {
|
2016-11-03 18:27:17 +08:00
|
|
|
inviteBtn.removeAttr('disabled');
|
|
|
|
inviteWithRoleBtn.removeAttr('disabled');
|
2018-07-19 23:56:42 +08:00
|
|
|
}).on('itemRemoved', function() {
|
2016-11-03 18:27:17 +08:00
|
|
|
if ($(this).val() === null) {
|
|
|
|
inviteBtn.attr('disabled', 'disabled');
|
|
|
|
inviteWithRoleBtn.attr('disabled', 'disabled');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Click action
|
2018-12-07 18:36:26 +08:00
|
|
|
modal.find('[data-action=invite]').off('click').on('click', function() {
|
2016-11-03 18:27:17 +08:00
|
|
|
var data = {
|
2019-01-23 16:17:27 +08:00
|
|
|
emails: tagsInput.val(),
|
2019-02-19 20:05:33 +08:00
|
|
|
'g-recaptcha-response': $('#recaptcha-invite-modal').val()
|
2016-11-03 18:27:17 +08:00
|
|
|
};
|
2018-07-19 23:56:42 +08:00
|
|
|
|
|
|
|
animateSpinner(modalDialog);
|
|
|
|
|
2016-11-03 18:27:17 +08:00
|
|
|
switch (type) {
|
2017-01-25 19:12:27 +08:00
|
|
|
case 'invite_to_team':
|
|
|
|
data.teamId = modal.attr('data-team-id');
|
|
|
|
data.role = $(this).attr('data-team-role');
|
2016-11-03 18:27:17 +08:00
|
|
|
break;
|
2017-01-25 19:12:27 +08:00
|
|
|
case 'invite_to_team_with_role':
|
|
|
|
data.teamId = modal.attr('data-team-id');
|
|
|
|
data.role = modal.attr('data-team-role');
|
2016-11-03 18:27:17 +08:00
|
|
|
break;
|
|
|
|
case 'invite':
|
|
|
|
break;
|
2017-01-25 19:12:27 +08:00
|
|
|
case 'invite_with_team_selector':
|
|
|
|
if (teamSelectorCheckbox.is(':checked')) {
|
|
|
|
data.teamId = teamSelectorDropdown.val();
|
|
|
|
data.role = $(this).attr('data-team-role');
|
2016-11-03 18:27:17 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-01-25 19:12:27 +08:00
|
|
|
case 'invite_with_team_selector_and_role':
|
|
|
|
if (teamSelectorCheckbox.is(':checked')) {
|
|
|
|
data.teamId = teamSelectorDropdown.val();
|
|
|
|
data.role = modal.attr('data-team-role');
|
2016-11-03 18:27:17 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
method: 'POST',
|
|
|
|
url: modal.attr('data-url'),
|
|
|
|
dataType: 'json',
|
|
|
|
data: data,
|
|
|
|
success: function(data) {
|
2016-11-06 18:29:00 +08:00
|
|
|
animateSpinner(modalDialog, false);
|
|
|
|
stepForm.hide();
|
|
|
|
stepResultsDiv.html(data.html);
|
|
|
|
stepResults.show();
|
|
|
|
// Add 'data-invited="true"' status to modal element
|
|
|
|
modal.attr('data-invited', 'true');
|
|
|
|
},
|
2019-01-23 16:17:27 +08:00
|
|
|
error: function(jqXHR) {
|
|
|
|
// ReCaptcha fails
|
|
|
|
if (jqXHR.status === 422) {
|
|
|
|
recaptchaErrorMsgDiv.removeClass('hidden');
|
|
|
|
recaptchaErrorText.text(jqXHR.responseJSON.recaptcha_error);
|
|
|
|
} else {
|
|
|
|
modal.modal('hide');
|
|
|
|
alert('Error inviting users.');
|
|
|
|
}
|
|
|
|
reloadRecaptchaIfExists();
|
2016-11-06 18:29:00 +08:00
|
|
|
animateSpinner(modalDialog, false);
|
2016-11-03 18:27:17 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-07-19 23:56:42 +08:00
|
|
|
}).on('shown.bs.modal', function() {
|
2016-11-03 18:27:17 +08:00
|
|
|
tagsInput.tagsinput('focus');
|
2019-01-23 16:17:27 +08:00
|
|
|
recaptchaErrorMsgDiv.addClass('hidden');
|
2016-11-06 18:29:00 +08:00
|
|
|
// Remove 'data-invited="true"' status
|
|
|
|
modal.removeAttr('data-invited');
|
2018-07-19 23:56:42 +08:00
|
|
|
}).on('hide.bs.modal', function() {
|
2016-11-03 18:27:17 +08:00
|
|
|
// 'Reset' modal state
|
|
|
|
tagsInput.tagsinput('removeAll');
|
2017-01-25 19:12:27 +08:00
|
|
|
teamSelectorCheckbox.prop('checked', false);
|
2016-11-03 18:27:17 +08:00
|
|
|
inviteBtn.attr('disabled', 'disabled');
|
|
|
|
inviteWithRoleBtn.attr('disabled', 'disabled');
|
2017-01-25 19:12:27 +08:00
|
|
|
teamSelectorDropdown2.addClass('disabled');
|
2016-11-03 18:27:17 +08:00
|
|
|
animateSpinner(modalDialog, false);
|
2019-01-23 16:17:27 +08:00
|
|
|
recaptchaErrorMsgDiv.addClass('hidden');
|
2016-11-03 18:27:17 +08:00
|
|
|
|
|
|
|
// Unbind event listeners
|
2017-01-25 19:12:27 +08:00
|
|
|
teamSelectorCheckbox.off('change');
|
2016-11-03 18:27:17 +08:00
|
|
|
tagsInput.off('itemAdded itemRemoved');
|
2016-11-10 23:18:17 +08:00
|
|
|
modal.find('[data-action=invite]').off('click');
|
2016-11-06 18:29:00 +08:00
|
|
|
|
|
|
|
// Hide contents of the results <div>
|
|
|
|
stepResultsDiv.html('');
|
|
|
|
stepResults.hide();
|
|
|
|
stepForm.show();
|
2019-01-23 16:17:27 +08:00
|
|
|
reloadRecaptchaIfExists();
|
2019-02-19 20:05:33 +08:00
|
|
|
$('#recaptcha-invite-modal').val('');
|
2016-11-03 18:27:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeModalsToggle() {
|
2018-08-20 19:13:34 +08:00
|
|
|
$("[data-trigger='invite-users']").on('click', function(event) {
|
2016-11-03 18:27:17 +08:00
|
|
|
var id = $(this).attr('data-modal-id');
|
2018-08-20 19:13:34 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2016-11-03 18:27:17 +08:00
|
|
|
$('[data-role=invite-users-modal][data-id=' + id + ']')
|
2018-07-19 23:56:42 +08:00
|
|
|
.modal('show');
|
2016-11-03 18:27:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-24 23:11:59 +08:00
|
|
|
$(document).on('turbolinks:load', function() {
|
|
|
|
$('[data-role=invite-users-modal]').each(function() {
|
|
|
|
initializeModal($(this));
|
|
|
|
});
|
|
|
|
|
|
|
|
initializeModalsToggle();
|
2016-11-03 18:27:17 +08:00
|
|
|
});
|
2018-07-19 23:56:42 +08:00
|
|
|
}());
|