zulip/static/js/stream_create.js
Angelika Serwa 9930cb43cd stream_settings: Add loading spinner when creating a new stream.
Add a spinner for when a stream is being created to show that
an operation is being performed, while also disallowing users to
modify the form in the meanwhile.

Commit modified by Brock Whittaker <[email protected]>.

Fixes: #5268.
2017-11-07 12:26:47 -08:00

377 lines
13 KiB
JavaScript

var stream_create = (function () {
var exports = {};
var created_stream;
exports.reset_created_stream = function () {
created_stream = undefined;
};
exports.set_name = function (stream) {
created_stream = stream;
};
exports.get_name = function () {
return created_stream;
};
var stream_name_error = (function () {
var self = {};
self.report_already_exists = function () {
$("#stream_name_error").text(i18n.t("A stream with this name already exists"));
$("#stream_name_error").show();
};
self.clear_errors = function () {
$("#stream_name_error").hide();
};
self.report_empty_stream = function () {
$("#stream_name_error").text(i18n.t("A stream needs to have a name"));
$("#stream_name_error").show();
};
self.select = function () {
$("#create_stream_name").focus().select();
};
self.pre_validate = function (stream_name) {
// Don't worry about empty strings...we just want to call this
// to warn users early before they start doing too much work
// after they make the effort to type in a stream name. (The
// use case here is that I go to create a stream, only to find
// out it already exists, and I was just too lazy to look at
// the public streams that I'm not subscribed to yet. Once I
// realize the stream already exists, I may want to cancel.)
if (stream_name && stream_data.get_sub(stream_name)) {
self.report_already_exists();
return;
}
self.clear_errors();
};
self.validate_for_submit = function (stream_name) {
if (!stream_name) {
self.report_empty_stream();
self.select();
return false;
}
if (stream_data.get_sub(stream_name)) {
self.report_already_exists();
self.select();
return false;
}
// If we got this far, then we think we have a new unique stream
// name, so we'll submit to the server. (It's still plausible,
// however, that there's some invite-only stream that we don't
// know about locally that will cause a name collision.)
return true;
};
return self;
}());
function ajaxSubscribeForCreation(stream_name, description, principals, invite_only, announce) {
// Subscribe yourself and possible other people to a new stream.
return channel.post({
url: "/json/users/me/subscriptions",
data: {subscriptions: JSON.stringify([{name: stream_name,
description: description}]),
principals: JSON.stringify(principals),
invite_only: JSON.stringify(invite_only),
announce: JSON.stringify(announce),
},
success: function () {
$("#create_stream_name").val("");
$("#create_stream_description").val("");
$("#subscriptions-status").hide();
loading.destroy_indicator($('#stream_creating_indicator'));
// The rest of the work is done via the subscribe event we will get
},
error: function (xhr) {
var msg = JSON.parse(xhr.responseText).msg;
if (msg.indexOf('access') >= 0) {
// If we can't access the stream, we can safely assume it's
// a duplicate stream that we are not invited to.
stream_name_error.report_already_exists(stream_name);
stream_name_error.select();
}
// TODO: This next line does nothing. See #4647.
ui_report.error(i18n.t("Error creating stream"), xhr,
$("#subscriptions-status"), 'subscriptions-status');
loading.destroy_indicator($('#stream_creating_indicator'));
},
});
}
// Within the new stream modal...
function update_announce_stream_state() {
// If there is no notifications_stream, we simply hide the widget.
if (!page_params.notifications_stream) {
$('#announce-new-stream').hide();
return;
}
// If the stream is invite only, or everyone's added, disable
// the "Announce stream" option. Otherwise enable it.
var announce_stream_checkbox = $('#announce-new-stream input');
var disable_it = false;
var is_invite_only = $('input:radio[name=privacy]:checked').val() === 'invite-only';
if (is_invite_only) {
disable_it = true;
announce_stream_checkbox.prop('checked', false);
} else {
disable_it = $('#user-checkboxes input').length
=== $('#user-checkboxes input:checked').length;
}
announce_stream_checkbox.prop('disabled', disable_it);
$('#announce-new-stream').show();
}
function get_principals() {
return _.map(
$("#stream_creation_form input:checkbox[name=user]:checked"),
function (elem) {
return $(elem).val();
}
);
}
function create_stream() {
var stream_name = $.trim($("#create_stream_name").val());
var description = $.trim($("#create_stream_description").val());
var is_invite_only = $('#stream_creation_form input[name=privacy]:checked').val() === "invite-only";
var principals = get_principals();
// You are always subscribed to streams you create.
principals.push(people.my_current_email());
created_stream = stream_name;
var announce = (!!page_params.notifications_stream &&
$('#announce-new-stream input').prop('checked'));
loading.make_indicator($('#stream_creating_indicator'), {text: i18n.t('Creating stream...')});
ajaxSubscribeForCreation(stream_name,
description,
principals,
is_invite_only,
announce
);
}
exports.new_stream_clicked = function (stream_name) {
// this changes the tab switcher (settings/preview) which isn't necessary
// to a add new stream title.
$(".display-type #add_new_stream_title").show();
$(".display-type #stream_settings_title").hide();
$(".stream-row.active").removeClass("active");