2012-12-05 22:48:15 +01:00
|
|
|
var invite = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2018-08-23 19:24:37 +02:00
|
|
|
function reset_error_messages() {
|
|
|
|
var invite_status = $('#invite_status');
|
2019-02-02 08:12:38 +01:00
|
|
|
var invitee_emails_group = $("#invitee_emails").closest('.control-group');
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
invite_status.hide().text('').removeClass(common.status_classes);
|
2018-08-23 19:24:37 +02:00
|
|
|
invitee_emails_group.removeClass('warning error');
|
|
|
|
if (page_params.development_environment) {
|
2019-02-02 08:12:38 +01:00
|
|
|
$('#dev_env_msg').hide().text('').removeClass(common.status_classes);
|
2018-08-23 19:24:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:20:13 +01:00
|
|
|
function get_common_invitation_data() {
|
2019-01-17 13:07:40 +01:00
|
|
|
var invite_as = parseInt($('#invite_as').val(), 10);
|
2018-12-22 05:41:54 +01:00
|
|
|
var stream_ids = [];
|
2019-02-01 12:20:13 +01:00
|
|
|
$("#invite-stream-checkboxes input:checked").each(function () {
|
2018-12-22 05:41:54 +01:00
|
|
|
var stream_id = parseInt($(this).val(), 10);
|
|
|
|
stream_ids.push(stream_id);
|
2018-08-23 19:24:37 +02:00
|
|
|
});
|
2019-02-01 12:20:13 +01:00
|
|
|
var data = {
|
|
|
|
csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').attr('value'),
|
|
|
|
invite_as: invite_as,
|
2018-12-22 05:41:54 +01:00
|
|
|
stream_ids: JSON.stringify(stream_ids),
|
2019-02-01 12:20:13 +01:00
|
|
|
};
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
function beforeSend() {
|
|
|
|
reset_error_messages();
|
|
|
|
// TODO: You could alternatively parse the textarea here, and return errors to
|
|
|
|
// the user if they don't match certain constraints (i.e. not real email addresses,
|
|
|
|
// aren't in the right domain, etc.)
|
|
|
|
//
|
|
|
|
// OR, you could just let the server do it. Probably my temptation.
|
|
|
|
$('#submit-invitation').button('loading');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-01 12:20:13 +01:00
|
|
|
function submit_invitation_form() {
|
|
|
|
var invite_status = $('#invite_status');
|
|
|
|
var invitee_emails = $("#invitee_emails");
|
|
|
|
var invitee_emails_group = invitee_emails.closest('.control-group');
|
|
|
|
var data = get_common_invitation_data();
|
|
|
|
data.invitee_emails = $("#invitee_emails").val();
|
2018-08-23 19:24:37 +02:00
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: "/json/invites",
|
|
|
|
data: data,
|
2019-02-02 08:12:38 +01:00
|
|
|
beforeSend: beforeSend,
|
2018-08-23 19:24:37 +02:00
|
|
|
success: function () {
|
2019-01-17 16:55:25 +01:00
|
|
|
ui_report.success(i18n.t('User(s) invited successfully.'), invite_status);
|
|
|
|
invitee_emails_group.removeClass('warning');
|
2018-08-23 19:24:37 +02:00
|
|
|
invitee_emails.val('');
|
|
|
|
|
|
|
|
if (page_params.development_environment) {
|
|
|
|
var rendered_email_msg = templates.render('dev_env_email_access');
|
|
|
|
$('#dev_env_msg').html(rendered_email_msg).addClass('alert-info').show();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
var arr = JSON.parse(xhr.responseText);
|
|
|
|
if (arr.errors === undefined) {
|
|
|
|
// There was a fatal error, no partial processing occurred.
|
2019-01-17 16:55:25 +01:00
|
|
|
ui_report.error("", xhr, invite_status);
|
2018-08-23 19:24:37 +02:00
|
|
|
} else {
|
|
|
|
// Some users were not invited.
|
|
|
|
var invitee_emails_errored = [];
|
2019-01-17 16:55:25 +01:00
|
|
|
var error_list = [];
|
|
|
|
arr.errors.forEach(function (value) {
|
|
|
|
error_list.push(value.join(': '));
|
2018-08-23 19:24:37 +02:00
|
|
|
invitee_emails_errored.push(value[0]);
|
|
|
|
});
|
|
|
|
|
2019-01-17 16:55:25 +01:00
|
|
|
var error_response = templates.render("invitation_failed_error", {
|
|
|
|
error_message: arr.msg,
|
|
|
|
error_list: error_list,
|
|
|
|
});
|
|
|
|
ui_report.message(error_response, invite_status, "alert-warning");
|
2018-08-23 19:24:37 +02:00
|
|
|
invitee_emails_group.addClass('warning');
|
|
|
|
|
|
|
|
if (arr.sent_invitations) {
|
|
|
|
invitee_emails.val(invitee_emails_errored.join('\n'));
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 08:12:38 +01:00
|
|
|
},
|
|
|
|
complete: function () {
|
|
|
|
$('#submit-invitation').button('reset');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2018-08-23 19:24:37 +02:00
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
function generate_multiuse_invite() {
|
|
|
|
var invite_status = $('#invite_status');
|
|
|
|
var data = get_common_invitation_data();
|
|
|
|
channel.post({
|
|
|
|
url: "/json/invites/multiuse",
|
|
|
|
data: data,
|
|
|
|
beforeSend: beforeSend,
|
|
|
|
success: function (data) {
|
|
|
|
ui_report.success(i18n.t('Invitation link: <a href="__link__">__link__</a>',
|
|
|
|
{link: data.invite_link}), invite_status);
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.error("", xhr, invite_status);
|
|
|
|
},
|
|
|
|
complete: function () {
|
|
|
|
$('#submit-invitation').button('reset');
|
2018-08-23 19:24:37 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-20 09:43:32 +02:00
|
|
|
exports.get_invite_streams = function () {
|
2019-01-10 18:00:53 +01:00
|
|
|
var streams = _.filter(stream_data.get_invite_stream_data(), function (stream) {
|
|
|
|
var is_notifications_stream = stream.name === page_params.notifications_stream;
|
2018-07-20 09:43:32 +02:00
|
|
|
// You can't actually elect to invite someone to the
|
|
|
|
// notifications stream. We won't even show it as a choice unless
|
|
|
|
// it's the only stream you have, or if you've made it private.
|
2019-01-10 18:00:53 +01:00
|
|
|
return stream_data.subscribed_streams().length === 1 ||
|
2014-01-15 22:21:43 +01:00
|
|
|
!is_notifications_stream ||
|
2019-01-10 18:00:53 +01:00
|
|
|
is_notifications_stream && stream.is_invite_only;
|
2013-02-07 02:15:12 +01:00
|
|
|
});
|
2018-07-20 09:43:32 +02:00
|
|
|
return streams;
|
|
|
|
};
|
|
|
|
|
|
|
|
function update_subscription_checkboxes() {
|
|
|
|
var data = {streams: exports.get_invite_streams()};
|
|
|
|
var html = templates.render('invite_subscription', data);
|
|
|
|
$('#streams_to_add').html(html);
|
2012-12-05 22:48:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:09:18 +01:00
|
|
|
function prepare_form_to_be_shown() {
|
|
|
|
update_subscription_checkboxes();
|
|
|
|
reset_error_messages();
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:24:26 +02:00
|
|
|
exports.launch = function () {
|
2017-08-10 15:52:21 +02:00
|
|
|
ui.set_up_scrollbar($("#invite_user_form .modal-body"));
|
2012-12-05 22:48:15 +01:00
|
|
|
|
2013-01-31 17:23:34 +01:00
|
|
|
$('#submit-invitation').button();
|
2017-03-14 23:22:46 +01:00
|
|
|
prepare_form_to_be_shown();
|
2018-08-23 19:24:37 +02:00
|
|
|
$("#invitee_emails").focus().autosize();
|
2012-12-05 22:48:15 +01:00
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
overlays.open_overlay({
|
2017-05-06 00:41:47 +02:00
|
|
|
name: 'invite',
|
|
|
|
overlay: $('#invite-user'),
|
|
|
|
on_close: function () {
|
2017-05-27 18:55:41 +02:00
|
|
|
hashchange.exit_overlay();
|
2017-05-06 00:41:47 +02:00
|
|
|
},
|
|
|
|
});
|
2017-03-14 23:22:46 +01:00
|
|
|
};
|
|
|
|
|
2018-05-15 22:03:14 +02:00
|
|
|
exports.initialize = function () {
|
2013-04-02 20:47:18 +02:00
|
|
|
$(document).on('click', '.invite_check_all_button', function (e) {
|
2016-08-24 22:24:45 +02:00
|
|
|
$('#streams_to_add :checkbox').prop('checked', true);
|
2013-04-02 20:47:18 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', '.invite_uncheck_all_button', function (e) {
|
2016-08-24 22:24:45 +02:00
|
|
|
$('#streams_to_add :checkbox').prop('checked', false);
|
2013-04-02 20:47:18 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
2018-12-22 05:41:54 +01:00
|
|
|
|
2019-02-02 08:12:38 +01:00
|
|
|
$("#submit-invitation").on("click", function () {
|
|
|
|
var is_generate_invite_link = $('#generate_multiuse_invite').prop('checked');
|
|
|
|
if (is_generate_invite_link) {
|
|
|
|
generate_multiuse_invite();
|
|
|
|
} else {
|
|
|
|
submit_invitation_form();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#invite-user').on('change', '#generate_multiuse_invite', function () {
|
|
|
|
var generate_multiuse_invite_link = $(this).prop('checked');
|
|
|
|
$('#invitee_emails').prop('disabled', generate_multiuse_invite_link);
|
|
|
|
var submit_invitation_button = $('#submit-invitation');
|
|
|
|
if (generate_multiuse_invite_link) {
|
|
|
|
submit_invitation_button.text(i18n.t('Generate invite link'));
|
|
|
|
submit_invitation_button.data('loading-text', i18n.t('Generating link...'));
|
|
|
|
} else {
|
|
|
|
submit_invitation_button.text(i18n.t('Invite'));
|
|
|
|
submit_invitation_button.data('loading-text', i18n.t('Inviting...'));
|
|
|
|
}
|
|
|
|
});
|
2018-05-15 22:03:14 +02:00
|
|
|
};
|
2012-12-05 22:48:15 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
2016-12-04 08:59:56 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = invite;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.invite = invite;
|