forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvite.js
159 lines (137 loc) · 5.92 KB
/
invite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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)
var streams = [];
_.each(stream_data.invite_streams(), function (value) {
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.
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;
});
}
});
$('#streams_to_add').html(templates.render('invite_subscription', {streams: streams}));
}
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');
if (page_params.development_environment) {
$('#dev_env_msg').hide().text('').removeClass('alert-error alert-warning alert-success');
}
}
function prepare_form_to_be_shown() {
update_subscription_checkboxes();
reset_error_messages();
}
exports.launch = function () {
ui.set_up_scrollbar($("#invite_user_form .modal-body"));
var invite_status = $('#invite_status');
var invitee_emails = $("#invitee_emails");
var invitee_emails_group = invitee_emails.closest('.control-group');
$('#submit-invitation').button();
prepare_form_to_be_shown();
invitee_emails.focus().autosize();
$("#submit-invitation").on("click", function () {
var data = {
invitee_emails: $("#invitee_emails").val(),
invite_as_admin: $("#invite_as_admin input:checked").val(),
csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').attr('value'),
};
var streams = [];
$.each($("#invite-stream-checkboxes input:checked"), function () {
streams.push($(this).val());
});
data.stream = streams;
channel.post({
url: "/json/invites",
data: data,
traditional: true,
beforeSubmit: function () {
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;
},
success: function () {
$('#submit-invitation').button('reset');
invite_status.text(i18n.t('User(s) invited successfully.'))
.addClass('alert-success')
.show();
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) {
$('#submit-invitation').button('reset');
var arr = JSON.parse(xhr.responseText);
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.
var invitee_emails_errored = [];
var error_list = $('<ul>');
_.each(arr.errors, function (value) {
error_list.append($('<li>').text(value.join(': ')));
invitee_emails_errored.push(value[0]);
});
invite_status.addClass('alert-warning')
.empty()
.append($('<p>').text(arr.msg))
.append(error_list)
.show();
invitee_emails_group.addClass('warning');
if (arr.sent_invitations) {
invitee_emails.val(invitee_emails_errored.join('\n'));
}
}
},
});
});
overlays.open_overlay({
name: 'invite',
overlay: $('#invite-user'),
on_close: function () {
hashchange.exit_overlay();
},
});
};
exports.initialize = function () {
$(document).on('click', '.invite_check_all_button', function (e) {
$('#streams_to_add :checkbox').prop('checked', true);
e.preventDefault();
});
$(document).on('click', '.invite_uncheck_all_button', function (e) {
$('#streams_to_add :checkbox').prop('checked', false);
e.preventDefault();
});
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = invite;
}