compose: split check_stream_for_send into into request and UI response functions.

In preparation for re-using the /json/subscriptions/exists request on
the subscriptions page.

(imported from commit 76eca95b952c4b60e583a050be711023ee5fedac)
This commit is contained in:
Jessica McKellar 2013-01-31 15:03:45 -05:00
parent 018f45d151
commit ce0c43329b
1 changed files with 30 additions and 12 deletions

View File

@ -248,9 +248,9 @@ function compose_error(error_text, bad_input) {
} }
// *Synchronously* check if a stream exists. // *Synchronously* check if a stream exists.
// If not, displays an error and returns false. exports.check_stream_existence = function (stream_name) {
function check_stream_for_send(stream_name) {
var result = "error"; var result = "error";
var error_xhr = "";
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "/json/subscriptions/exists", url: "/json/subscriptions/exists",
@ -260,26 +260,44 @@ function check_stream_for_send(stream_name) {
if (!data.exists) { if (!data.exists) {
// The stream doesn't exist // The stream doesn't exist
result = "does-not-exist"; result = "does-not-exist";
} else if (data.subscribed) {
result = "subscribed";
} else {
result = "not-subscribed";
}
},
error: function (xhr) {
result = "error";
error_xhr = xhr;
}
});
return [result, error_xhr];
};
// Checks if a stream exists. If not, displays an error and returns
// false.
function check_stream_for_send(stream_name) {
var stream_check = exports.check_stream_existence(stream_name);
var result = stream_check[0];
var xhr = stream_check[1];
if (result === "error") {
ui.report_error("Error checking subscription", xhr, $("#home-error"));
$("#stream").focus();
$("#compose-send-button").removeAttr('disabled');
} else {
if (result === "does-not-exist") {
$('#send-status').removeClass(status_classes); $('#send-status').removeClass(status_classes);
$('#stream-dne-name').text(stream_name); $('#stream-dne-name').text(stream_name);
$('#stream-dne').show(); $('#stream-dne').show();
$("#compose-send-button").removeAttr('disabled'); $("#compose-send-button").removeAttr('disabled');
exports.hide(); exports.hide();
$('#create-it').focus(); $('#create-it').focus();
} else if (data.subscribed) {
result = "subscribed";
} else {
result = "not-subscribed";
} }
$("#home-error").hide(); $("#home-error").hide();
},
error: function (xhr) {
result = "error";
ui.report_error("Error checking subscription", xhr, $("#home-error"));
$("#stream").focus();
$("#compose-send-button").removeAttr('disabled');
} }
});
return result; return result;
} }