zulip/static/js/settings_streams.js

151 lines
4.7 KiB
JavaScript
Raw Normal View History

import $ from "jquery";
import render_admin_default_streams_list from "../templates/admin_default_streams_list.hbs";
import * as channel from "./channel";
import {$t_html} from "./i18n";
import * as ListWidget from "./list_widget";
import * as loading from "./loading";
import {page_params} from "./page_params";
import * as stream_data from "./stream_data";
import * as sub_store from "./sub_store";
import * as typeahead_helper from "./typeahead_helper";
import * as ui from "./ui";
import * as ui_report from "./ui_report";
const meta = {
2017-04-08 21:02:08 +02:00
loaded: false,
};
export function reset() {
meta.loaded = false;
}
export function maybe_disable_widgets() {
if (page_params.is_admin) {
return;
}
$(".organization-box [data-name='default-streams-list']")
.find("input:not(.search), button, select")
.prop("disabled", true);
}
export function build_default_stream_table() {
const table = $("#admin_default_streams_table").expectOne();
const stream_ids = stream_data.get_default_stream_ids();
const subs = stream_ids.map((stream_id) => sub_store.get(stream_id));
ListWidget.create(table, subs, {
name: "default_streams_list",
modifier(item) {
return render_admin_default_streams_list({
stream: item,
can_modify: page_params.is_admin,
});
},
filter: {
element: table.closest(".settings-section").find(".search"),
predicate(item, query) {
return item.name.toLowerCase().includes(query.toLowerCase());
},
onupdate() {
ui.reset_scrollbar(table);
},
},
parent_container: $("#admin-default-streams-list").expectOne(),
init_sort: ["alphabetic", "name"],
simplebar_container: $("#admin-default-streams-list .progressive-table-wrapper"),
});
loading.destroy_indicator($("#admin_page_default_streams_loading_indicator"));
}
2017-04-08 21:02:08 +02:00
export function update_default_streams_table() {
if (/#*organization/.test(window.location.hash) || /#*settings/.test(window.location.hash)) {
2017-04-08 21:02:08 +02:00
$("#admin_default_streams_table").expectOne().find("tr.default_stream_row").remove();
build_default_stream_table();
2017-04-08 21:02:08 +02:00
}
}
2017-04-08 21:02:08 +02:00
function make_stream_default(stream_id) {
const data = {
stream_id,
2017-04-08 21:02:08 +02:00
};
const default_stream_status = $("#admin-default-stream-status");
default_stream_status.hide();
2017-04-08 21:02:08 +02:00
channel.post({
url: "/json/default_streams",
data,
error(xhr) {
2017-04-08 21:02:08 +02:00
if (xhr.status.toString().charAt(0) === "4") {
ui_report.error($t_html({defaultMessage: "Failed"}), xhr, default_stream_status);
2017-04-08 21:02:08 +02:00
} else {
ui_report.error($t_html({defaultMessage: "Failed"}), default_stream_status);
2017-04-08 21:02:08 +02:00
}
default_stream_status.show();
2017-04-08 21:02:08 +02:00
},
});
}
export function delete_default_stream(stream_id, default_stream_row, alert_element) {
channel.del({
url: "/json/default_streams?" + $.param({stream_id}),
error(xhr) {
ui_report.generic_row_button_error(xhr, alert_element);
},
success() {
default_stream_row.remove();
},
});
}
export function set_up() {
build_page();
maybe_disable_widgets();
}
export function build_page() {
2017-04-08 21:02:08 +02:00
meta.loaded = true;
update_default_streams_table();
2017-04-08 21:02:08 +02:00
$(".create_default_stream").on("keypress", (e) => {
2017-04-08 21:02:08 +02:00
if (e.which === 13) {
e.preventDefault();
e.stopPropagation();
const default_stream_input = $(".create_default_stream");
make_stream_default(stream_data.get_stream_id(default_stream_input.val()));
default_stream_input[0].value = "";
2017-04-08 21:02:08 +02:00
}
});
$(".create_default_stream").typeahead({
2017-04-08 21:02:08 +02:00
items: 5,
fixed: true,
source() {
return stream_data.get_non_default_stream_names();
2017-04-08 21:02:08 +02:00
},
highlighter(item) {
return typeahead_helper.render_typeahead_item({primary: item});
},
2017-04-08 21:02:08 +02:00
});
$(".default-stream-form").on("click", "#do_submit_stream", (e) => {
e.preventDefault();
e.stopPropagation();
const default_stream_input = $(".create_default_stream");
make_stream_default(stream_data.get_stream_id(default_stream_input.val()));
// Clear value inside input box
default_stream_input[0].value = "";
});
$("body").on("click", ".default_stream_row .remove-default-stream", function (e) {
const row = $(this).closest(".default_stream_row");
const stream_id = Number.parseInt(row.attr("data-stream-id"), 10);
delete_default_stream(stream_id, row, $(e.target));
});
}