Make unsubbing an undoable action.

(imported from commit 713fdad5aa5d138b9e95a14dccaeba0d9d612c1c)
This commit is contained in:
Luke Faraone 2012-11-07 17:25:04 -05:00
parent 055d18b484
commit 3928f763e9
1 changed files with 48 additions and 28 deletions

View File

@ -17,11 +17,21 @@ function case_insensitive_subscription_index(stream_name) {
}
function add_to_stream_list(stream_name) {
var stream_sub_row;
if (!exports.have(stream_name)) {
stream_list.push(stream_name);
stream_set[stream_name.toLowerCase()] = true;
stream_sub_row = $('#subscriptions_table').find('button[value="' + stream_name + '"]');
if (stream_sub_row.length) {
stream_sub_row.text("Unsubscribe")
.removeClass("btn-link")
.unbind('click');
} else {
$('#subscriptions_table').prepend(templates.subscription({subscription: stream_name}));
}
}
}
function remove_from_stream_list(stream_name) {
@ -77,41 +87,18 @@ exports.have = function (stream_name) {
return (stream_set[stream_name.toLowerCase()] === true);
};
$(function () {
var i;
// Populate stream_set with data handed over to client-side template.
for (i = 0; i < stream_list.length; i++) {
stream_set[stream_list[i].toLowerCase()] = true;
}
// FIXME: It would be nice to move the UI setup into ui.js.
$("#current_subscriptions").ajaxForm({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
success: function (resp, statusText, xhr, form) {
var name = $.parseJSON(xhr.responseText).data;
$('#subscriptions_table').find('button[value="' + name + '"]').parents('tr').remove();
remove_from_stream_list(name);
composebox_typeahead.update_autocomplete();
report_success("Successfully removed subscription to " + name,
$("#subscriptions-status"));
},
error: function (xhr) {
report_error("Error removing subscription", xhr, $("#subscriptions-status"));
}
});
$("#add_new_subscription").on("submit", function (e) {
e.preventDefault();
function ajaxSubscribe(streams) {
$.ajax({
type: "POST",
url: "/json/subscriptions/add",
dataType: 'json', // This seems to be ignored. We still get back an xhr.
// The next line is a total hack to format our stream as
// that simplejson will parse as a 1-element array
data: {"streams": '["' + $("#streams").val() + '"]' },
data: {"streams": '["' + streams + '"]' },
success: function (resp, statusText, xhr, form) {
if ($("#streams").val() === streams) {
$("#streams").val("");
}
var name, res = $.parseJSON(xhr.responseText);
if (res.subscribed.length === 0) {
name = res.already_subscribed[0];
@ -129,6 +116,39 @@ $(function () {
$("#streams").focus();
}
});
}
$(function () {
var i;
// Populate stream_set with data handed over to client-side template.
for (i = 0; i < stream_list.length; i++) {
stream_set[stream_list[i].toLowerCase()] = true;
}
// FIXME: It would be nice to move the UI setup into ui.js.
$("#current_subscriptions").ajaxForm({
dataType: 'json', // This seems to be ignored. We still get back an xhr.
success: function (resp, statusText, xhr, form) {
var name = $.parseJSON(xhr.responseText).data;
$('#subscriptions_table').find('button[value="' + name + '"]').text("Undo")
.addClass("btn-link")
.click(function (e) {
e.preventDefault();
ajaxSubscribe(name);
});
remove_from_stream_list(name);
composebox_typeahead.update_autocomplete();
report_success("Successfully removed subscription to " + name,
$("#subscriptions-status"));
},
error: function (xhr) {
report_error("Error removing subscription", xhr, $("#subscriptions-status"));
}
});
$("#add_new_subscription").on("submit", function (e) {
e.preventDefault();
ajaxSubscribe($("#streams").val());
});
});