Clean up stream subscribe/unsubscribe button code

(imported from commit ac8fc7058534aaa3fdcb161c2aead4cc397980cf)
This commit is contained in:
Zev Benjamin
2013-01-10 16:15:55 -05:00
parent 044fc61be2
commit 1d4a34aeba
3 changed files with 24 additions and 30 deletions

View File

@@ -6,8 +6,7 @@
<div class="subscription_header" data-toggle="collapse" data-target="#subscription_settings_{{id}}"> <div class="subscription_header" data-toggle="collapse" data-target="#subscription_settings_{{id}}">
<span class="color_swatch" style="background-color: {{color}}"></span> <span class="color_swatch" style="background-color: {{color}}"></span>
<span class="subscription_name">{{name}}</span> <span class="subscription_name">{{name}}</span>
<button class="btn btn-block unsubscribe_button" type="button" name="subscription" <button class="btn btn-block sub_unsub_button" type="button" name="subscription">Unsubscribe</button>
onclick="subs.unsubscribe_button_click(event);">Unsubscribe</button>
</div> </div>
<div id="subscription_settings_{{id}}" class="collapse subscription_settings"> <div id="subscription_settings_{{id}}" class="collapse subscription_settings">

View File

@@ -77,10 +77,6 @@ var colorpicker_options = {
} }
}; };
function get_button_for_stream(stream_name) {
return $('#subscription_' + stream_info[stream_name.toLowerCase()].id).find('.unsubscribe_button');
}
function add_to_stream_list(stream_name) { function add_to_stream_list(stream_name) {
var stream_sub_row; var stream_sub_row;
var sub; var sub;
@@ -97,14 +93,6 @@ function add_to_stream_list(stream_name) {
sub = removed_streams[lstream_name]; sub = removed_streams[lstream_name];
delete removed_streams[lstream_name]; delete removed_streams[lstream_name];
stream_info[lstream_name] = sub; stream_info[lstream_name] = sub;
stream_sub_row = get_button_for_stream(stream_name);
stream_sub_row.text("Unsubscribe")
.removeClass("btn-primary")
.unbind("click")
.removeAttr("onclick")
.click(function (event) {exports.unsubscribe_button_click(event);});
} else { } else {
sub = {name: stream_name, id: next_sub_id++, color: default_color, sub = {name: stream_name, id: next_sub_id++, color: default_color,
render_subscribers: render_subscribers()}; render_subscribers: render_subscribers()};
@@ -207,7 +195,7 @@ exports.have = function (stream_name) {
return (stream_info[stream_name.toLowerCase()] !== undefined); return (stream_info[stream_name.toLowerCase()] !== undefined);
}; };
function ajaxSubscribe(stream) { function ajaxSubscribe(stream, button) {
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "/json/subscriptions/add", url: "/json/subscriptions/add",
@@ -227,6 +215,10 @@ function ajaxSubscribe(stream) {
$("#subscriptions-status")); $("#subscriptions-status"));
} }
add_to_stream_list(name); add_to_stream_list(name);
if (button !== undefined) {
button.text("Unsubscribe").removeClass("btn-primary");
}
typeahead_helper.update_autocomplete();
}, },
error: function (xhr) { error: function (xhr) {
ui.report_error("Error adding subscription", xhr, $("#subscriptions-status")); ui.report_error("Error adding subscription", xhr, $("#subscriptions-status"));
@@ -235,10 +227,7 @@ function ajaxSubscribe(stream) {
}); });
} }
exports.unsubscribe_button_click = function (e) { function ajaxUnsubscribe(stream, button) {
e.preventDefault();
e.stopPropagation();
var stream = $(e.target).closest('.subscription_row').find('.subscription_name').text();
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: "/json/subscriptions/remove", url: "/json/subscriptions/remove",
@@ -255,16 +244,8 @@ exports.unsubscribe_button_click = function (e) {
ui.report_success("Successfully removed subscription to " + name, ui.report_success("Successfully removed subscription to " + name,
$("#subscriptions-status")); $("#subscriptions-status"));
} }
get_button_for_stream(name).text("Subscribe")
.addClass("btn-primary")
.unbind("click")
.removeAttr("onclick")
.click(function (e) {
e.preventDefault();
e.stopPropagation();
ajaxSubscribe(name);
});
remove_from_stream_list(name); remove_from_stream_list(name);
button.text("Subscribe").addClass("btn-primary");
typeahead_helper.update_autocomplete(); typeahead_helper.update_autocomplete();
}, },
error: function (xhr) { error: function (xhr) {
@@ -272,7 +253,7 @@ exports.unsubscribe_button_click = function (e) {
$("#streams").focus(); $("#streams").focus();
} }
}); });
}; }
$(function () { $(function () {
var i; var i;
@@ -290,6 +271,20 @@ $(function () {
return; return;
} }
$("#subscriptions_table").on("click", ".sub_unsub_button", function (e) {
e.preventDefault();
e.stopPropagation();
var sub_row = $(e.target).closest('.subscription_row');
var stream_name = sub_row.find('.subscription_name').text();
var sub = stream_info[stream_name.toLowerCase()];
if (sub) {
ajaxUnsubscribe(stream_name, $(e.target));
} else {
ajaxSubscribe(stream_name, $(e.target));
}
});
// From here down is only stuff that happens when we're rendering // From here down is only stuff that happens when we're rendering
// the subscriber settings // the subscriber settings

View File

@@ -631,7 +631,7 @@ table.floating_recipient {
margin-right: 10px; margin-right: 10px;
} }
.unsubscribe_button { .sub_unsub_button {
width: 140px; width: 140px;
float: right; float: right;
} }