admin screens: Encapsulate default streams table.

We add a default_streams_table() function that builds an
object encapsulating the defaults streams table in the admin
system.

This function allows us to simplify the click handler code by
closing on row/stream_name rather than picking those values out
of the DOM.
This commit is contained in:
Steve Howell 2017-02-18 10:21:50 -08:00 committed by Tim Abbott
parent 96fc6f2372
commit 6af83e31f6
1 changed files with 44 additions and 34 deletions

View File

@ -113,13 +113,48 @@ function populate_streams(streams_data) {
loading.destroy_indicator($('#admin_page_streams_loading_indicator'));
}
function populate_default_streams(streams_data) {
var default_streams_table = $("#admin_default_streams_table").expectOne();
_.each(streams_data, function (stream) {
default_streams_table.append(templates.render("admin_default_streams_list", {stream: stream}));
});
loading.destroy_indicator($('#admin_page_default_streams_loading_indicator'));
}
exports.build_default_stream_table = function (streams_data) {
var self = {};
self.row_dict = new Dict();
function set_up_remove_click_hander(row, stream_name) {
row.on("click", ".remove-default-stream", function (e) {
e.preventDefault();
e.stopPropagation();
channel.del({
url: '/json/default_streams'+ '?' + $.param({stream_name: stream_name}),
error: function (xhr) {
var button = row.find("button");
if (xhr.status.toString().charAt(0) === "4") {
button.closest("td").html(
$("<p>").addClass("text-error").text(JSON.parse(xhr.responseText).msg)
);
} else {
button.text(i18n.t("Failed!"));
}
},
success: function () {
row.remove();
},
});
});
}
(function () {
var table = $("#admin_default_streams_table").expectOne();
_.each(streams_data, function (stream) {
var row = $(templates.render("admin_default_streams_list", {stream: stream}));
set_up_remove_click_hander(row, stream.name);
self.row_dict.set(stream.stream_id, row);
table.append(row);
});
loading.destroy_indicator($('#admin_page_default_streams_loading_indicator'));
}());
return self;
};
function get_non_default_streams_names(streams_data) {
var non_default_streams_names = [];
@ -141,7 +176,8 @@ exports.update_default_streams_table = function () {
if (/#*administration/.test(window.location.hash) ||
/#*settings/.test(window.location.hash)) {
$("#admin_default_streams_table").expectOne().find("tr.default_stream_row").remove();
populate_default_streams(page_params.realm_default_streams);
exports.build_default_stream_table(
page_params.realm_default_streams);
}
};
@ -382,32 +418,6 @@ function _setup_page() {
$("#deactivation_stream_modal").modal("show");
});
$(".admin_default_stream_table").on("click", ".remove-default-stream", function (e) {
e.preventDefault();
e.stopPropagation();
$(".active_default_stream_row").removeClass("active_default_stream_row");
var row = $(e.target).closest(".default_stream_row");
row.addClass("active_default_stream_row");
var stream_name = row.find('.default_stream_name').text();
channel.del({
url: '/json/default_streams'+ '?' + $.param({stream_name: stream_name}),
error: function (xhr) {
if (xhr.status.toString().charAt(0) === "4") {
$(".active_default_stream_row button").closest("td").html(
$("<p>").addClass("text-error").text(JSON.parse(xhr.responseText).msg));
} else {
$(".active_default_stream_row button").text("Failed!");
}
},
success: function () {
var row = $(".active_default_stream_row");
row.remove();
},
});
});
$('.create_default_stream').keypress(function (e) {
if (e.which === 13) {
e.preventDefault();