2012-12-05 22:48:15 +01:00
|
|
|
var invite = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
function update_subscription_checkboxes() {
|
|
|
|
// TODO: If we were more clever, we would only do this if the
|
|
|
|
// stream list has actually changed; that way, the settings of the
|
|
|
|
// checkboxes are saved from invocation to invocation (which is
|
|
|
|
// nice if I want to invite a bunch of people at once)
|
2013-02-07 02:15:12 +01:00
|
|
|
var streams = [];
|
2017-03-21 21:10:20 +01:00
|
|
|
|
|
|
|
_.each(stream_data.invite_streams(), function (value) {
|
2014-01-15 22:21:43 +01:00
|
|
|
var is_notifications_stream = value === page_params.notifications_stream;
|
|
|
|
if ((stream_data.subscribed_streams().length === 1) ||
|
|
|
|
!is_notifications_stream ||
|
|
|
|
(is_notifications_stream && stream_data.get_invite_only(value))) {
|
|
|
|
// You can't actually elect not 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.
|
2017-03-21 21:10:20 +01:00
|
|
|
var default_status = stream_data.get_default_status(value);
|
|
|
|
var invite_status = stream_data.get_invite_only(value);
|
|
|
|
streams.push({name: value, invite_only: invite_status, default_stream: default_status});
|
|
|
|
// Sort by default status.
|
|
|
|
streams.sort(function (a, b) {
|
|
|
|
return b.default_stream - a.default_stream;
|
|
|
|
});
|
2014-01-15 22:21:43 +01:00
|
|
|
}
|
2013-02-07 02:15:12 +01:00
|
|
|
});
|
2013-02-16 10:45:32 +01:00
|
|
|
$('#streams_to_add').html(templates.render('invite_subscription', {streams: streams}));
|
2012-12-05 22:48:15 +01:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:09:18 +01:00
|
|
|
function reset_error_messages() {
|
|
|
|
var invite_status = $('#invite_status');
|
|
|
|
var invitee_emails = $("#invitee_emails");
|
|
|
|
var invitee_emails_group = invitee_emails.closest('.control-group');
|
|
|
|
|
|
|
|
invite_status.hide().text('').removeClass('alert-error alert-warning alert-success');
|
|
|
|
invitee_emails_group.removeClass('warning error');
|
2016-06-20 21:58:59 +02:00
|
|
|
if (page_params.development_environment) {
|
|
|
|
$('#dev_env_msg').hide().text('').removeClass('alert-error alert-warning alert-success');
|
|
|
|
}
|
2013-01-31 17:09:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepare_form_to_be_shown() {
|
|
|
|
update_subscription_checkboxes();
|
|
|
|
reset_error_messages();
|
|
|
|
}
|
|
|
|
|
2012-12-05 22:48:15 +01:00
|
|
|
exports.initialize = 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
|
|
|
var invite_status = $('#invite_status');
|
|
|
|
var invitee_emails = $("#invitee_emails");
|
|
|
|
var invitee_emails_group = invitee_emails.closest('.control-group');
|
|
|
|
|
2013-01-31 17:23:34 +01:00
|
|
|
$('#submit-invitation').button();
|
2017-03-14 23:22:46 +01:00
|
|
|
prepare_form_to_be_shown();
|
|
|
|
invitee_emails.focus().autosize();
|
2013-04-09 19:11:58 +02:00
|
|
|
|
2012-12-05 22:48:15 +01:00
|
|
|
$("#invite_user_form").ajaxForm({
|
|
|
|
dataType: 'json',
|
2016-12-02 14:06:06 +01:00
|
|
|
beforeSubmit: function () {
|
2013-01-31 17:09:18 +01:00
|
|
|
reset_error_messages();
|
2012-12-05 22:48:15 +01:00
|
|
|
// 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.
|
2013-01-31 17:23:34 +01:00
|
|
|
$('#submit-invitation').button('loading');
|
2012-12-05 22:48:15 +01:00
|
|
|
return true;
|
|
|
|
},
|
2016-12-02 14:06:06 +01:00
|
|
|
success: function () {
|
2013-01-31 17:23:34 +01:00
|
|
|
$('#submit-invitation').button('reset');
|
2016-12-03 03:08:47 +01:00
|
|
|
invite_status.text(i18n.t('User invited successfully.', {count: (invitee_emails.val().match(/@/g) || [] ).length}))
|
2012-12-05 22:48:15 +01:00
|
|
|
.addClass('alert-success')
|
|
|
|
.show();
|
2016-07-26 23:17:53 +02:00
|
|
|
invitee_emails.val('');
|
2016-06-20 21:58:59 +02:00
|
|
|
|
|
|
|
if (page_params.development_environment) {
|
2017-10-02 23:26:20 +02:00
|
|
|
var email_msg = templates.render('dev_env_email_access');
|
|
|
|
$('#dev_env_msg').html(email_msg).addClass('alert-info').show();
|
2016-06-20 21:58:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-05 22:48:15 +01:00
|
|
|
},
|
2016-12-02 14:06:06 +01:00
|
|
|
error: function (xhr) {
|
2013-01-31 17:23:34 +01:00
|
|
|
$('#submit-invitation').button('reset');
|
2016-08-24 23:56:23 +02:00
|
|
|
var arr = JSON.parse(xhr.responseText);
|
2012-12-05 22:48:15 +01:00
|
|
|
if (arr.errors === undefined) {
|
|
|
|
// There was a fatal error, no partial processing occurred.
|
|
|
|
invite_status.text(arr.msg)
|
|
|
|
.addClass('alert-error')
|
|
|
|
.show();
|
|
|
|
} else {
|
|
|
|
// Some users were not invited.
|
2016-12-13 20:03:23 +01:00
|
|
|
var invitee_emails_errored = [];
|
2012-12-05 22:48:15 +01:00
|
|
|
var error_list = $('<ul>');
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(arr.errors, function (value) {
|
2012-12-05 22:48:15 +01:00
|
|
|
error_list.append($('<li>').text(value.join(': ')));
|
2016-12-13 20:03:23 +01:00
|
|
|
invitee_emails_errored.push(value[0]);
|
2012-12-05 22:48:15 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
invite_status.addClass('alert-warning')
|
|
|
|
.empty()
|
|
|
|
.append($('<p>').text(arr.msg))
|
|
|
|
.append(error_list)
|
|
|
|
.show();
|
|
|
|
invitee_emails_group.addClass('warning');
|
|
|
|
|
2016-12-13 20:03:23 +01:00
|
|
|
if (arr.sent_invitations) {
|
|
|
|
invitee_emails.val(invitee_emails_errored.join('\n'));
|
|
|
|
} else { // Invitations not sent -- keep all emails in the list
|
|
|
|
var current_emails = invitee_emails.val().split(/\n|,/);
|
|
|
|
invitee_emails.val(util.move_array_elements_to_front(current_emails, invitee_emails_errored).join('\n'));
|
|
|
|
}
|
|
|
|
|
2012-12-05 22:48:15 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
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
|
|
|
};
|
|
|
|
|
|
|
|
$(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();
|
|
|
|
});
|
2017-03-14 23:22:46 +01: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;
|
|
|
|
}
|