2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
import render_admin_default_streams_list from "../templates/admin_default_streams_list.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as channel from "./channel";
|
2021-04-13 05:18:25 +02:00
|
|
|
import {$t_html} from "./i18n";
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
|
|
|
import * as loading from "./loading";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-04-15 17:02:54 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as typeahead_helper from "./typeahead_helper";
|
2021-02-28 21:33:10 +01:00
|
|
|
import * as ui from "./ui";
|
2021-02-28 01:21:35 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2021-02-28 00:36:14 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-04-08 21:02:08 +02:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function reset() {
|
2017-04-17 16:51:27 +02:00
|
|
|
meta.loaded = false;
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2017-04-17 16:51:27 +02:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function maybe_disable_widgets() {
|
2018-12-08 17:37:57 +01:00
|
|
|
if (page_params.is_admin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(".organization-box [data-name='default-streams-list']")
|
2020-07-15 00:34:28 +02:00
|
|
|
.find("input:not(.search), button, select")
|
2020-07-22 02:59:06 +02:00
|
|
|
.prop("disabled", true);
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2018-12-08 17:37:57 +01:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function build_default_stream_table() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const table = $("#admin_default_streams_table").expectOne();
|
2017-04-20 21:49:12 +02:00
|
|
|
|
2020-03-22 13:04:39 +01:00
|
|
|
const stream_ids = stream_data.get_default_stream_ids();
|
2021-04-15 17:02:54 +02:00
|
|
|
const subs = stream_ids.map((stream_id) => sub_store.get(stream_id));
|
2020-03-22 13:04:39 +01:00
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
ListWidget.create(table, subs, {
|
2017-04-20 21:49:12 +02:00
|
|
|
name: "default_streams_list",
|
2020-07-20 22:18:43 +02:00
|
|
|
modifier(item) {
|
2020-04-14 14:43:27 +02:00
|
|
|
return render_admin_default_streams_list({
|
2019-07-09 21:24:00 +02:00
|
|
|
stream: item,
|
|
|
|
can_modify: page_params.is_admin,
|
2020-04-14 14:43:27 +02:00
|
|
|
});
|
2017-04-20 21:49:12 +02:00
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: table.closest(".settings-section").find(".search"),
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, query) {
|
2020-03-22 16:39:23 +01:00
|
|
|
return item.name.toLowerCase().includes(query.toLowerCase());
|
2017-04-20 21:49:12 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
onupdate() {
|
2019-01-09 14:30:35 +01:00
|
|
|
ui.reset_scrollbar(table);
|
2017-09-15 23:01:36 +02:00
|
|
|
},
|
2017-04-20 21:49:12 +02:00
|
|
|
},
|
2019-08-16 06:37:19 +02:00
|
|
|
parent_container: $("#admin-default-streams-list").expectOne(),
|
2020-07-15 01:29:15 +02:00
|
|
|
init_sort: ["alphabetic", "name"],
|
|
|
|
simplebar_container: $("#admin-default-streams-list .progressive-table-wrapper"),
|
2020-04-11 16:23:29 +02:00
|
|
|
});
|
2019-08-16 06:37:19 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#admin_page_default_streams_loading_indicator"));
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2017-04-08 21:02:08 +02:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function update_default_streams_table() {
|
2020-07-15 00:34:28 +02:00
|
|
|
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();
|
2021-02-28 01:21:35 +01:00
|
|
|
build_default_stream_table();
|
2017-04-08 21:02:08 +02:00
|
|
|
}
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2017-04-08 21:02:08 +02:00
|
|
|
|
2020-04-13 13:57:07 +02:00
|
|
|
function make_stream_default(stream_id) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2017-04-08 21:02:08 +02:00
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const default_stream_status = $("#admin-default-stream-status");
|
2017-03-28 06:44:02 +02:00
|
|
|
default_stream_status.hide();
|
2017-04-08 21:02:08 +02:00
|
|
|
|
|
|
|
channel.post({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/default_streams",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
|
|
|
error(xhr) {
|
2017-04-08 21:02:08 +02:00
|
|
|
if (xhr.status.toString().charAt(0) === "4") {
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error($t_html({defaultMessage: "Failed"}), xhr, default_stream_status);
|
2017-04-08 21:02:08 +02:00
|
|
|
} else {
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error($t_html({defaultMessage: "Failed"}), default_stream_status);
|
2017-04-08 21:02:08 +02:00
|
|
|
}
|
2017-03-28 06:44:02 +02:00
|
|
|
default_stream_status.show();
|
2017-04-08 21:02:08 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function delete_default_stream(stream_id, default_stream_row, alert_element) {
|
2018-03-07 13:31:23 +01:00
|
|
|
channel.del({
|
2020-10-07 13:17:55 +02:00
|
|
|
url: "/json/default_streams?" + $.param({stream_id}),
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2018-03-07 13:31:23 +01:00
|
|
|
ui_report.generic_row_button_error(xhr, alert_element);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2018-03-07 13:31:23 +01:00
|
|
|
default_stream_row.remove();
|
|
|
|
},
|
|
|
|
});
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|
2018-03-07 13:31:23 +01:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function set_up() {
|
|
|
|
build_page();
|
|
|
|
maybe_disable_widgets();
|
|
|
|
}
|
2018-12-08 18:16:37 +01:00
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
export function build_page() {
|
2017-04-08 21:02:08 +02:00
|
|
|
meta.loaded = true;
|
|
|
|
|
2021-02-28 01:21:35 +01:00
|
|
|
update_default_streams_table();
|
2017-04-08 21:02:08 +02:00
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
$(".create_default_stream").on("keypress", (e) => {
|
2017-04-08 21:02:08 +02:00
|
|
|
if (e.which === 13) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-11-02 00:06:25 +01:00
|
|
|
const default_stream_input = $(".create_default_stream");
|
2020-04-13 13:57:07 +02:00
|
|
|
make_stream_default(stream_data.get_stream_id(default_stream_input.val()));
|
2017-04-26 22:32:37 +02:00
|
|
|
default_stream_input[0].value = "";
|
2017-04-08 21:02:08 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".create_default_stream").typeahead({
|
2017-04-08 21:02:08 +02:00
|
|
|
items: 5,
|
|
|
|
fixed: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
source() {
|
2017-08-22 20:00:09 +02:00
|
|
|
return stream_data.get_non_default_stream_names();
|
2017-04-08 21:02:08 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
highlighter(item) {
|
2020-07-16 22:40:18 +02:00
|
|
|
return typeahead_helper.render_typeahead_item({primary: item});
|
2017-06-12 19:15:57 +02:00
|
|
|
},
|
2017-04-08 21:02:08 +02:00
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
$(".default-stream-form").on("click", "#do_submit_stream", (e) => {
|
2017-03-28 06:41:26 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-11-02 00:06:25 +01:00
|
|
|
const default_stream_input = $(".create_default_stream");
|
2020-04-13 13:57:07 +02:00
|
|
|
make_stream_default(stream_data.get_stream_id(default_stream_input.val()));
|
2017-03-28 06:41:26 +02:00
|
|
|
// Clear value inside input box
|
|
|
|
default_stream_input[0].value = "";
|
|
|
|
});
|
|
|
|
|
2018-03-25 19:57:38 +02:00
|
|
|
$("body").on("click", ".default_stream_row .remove-default-stream", function (e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = $(this).closest(".default_stream_row");
|
2020-10-07 09:17:30 +02:00
|
|
|
const stream_id = Number.parseInt(row.attr("data-stream-id"), 10);
|
2021-02-28 01:21:35 +01:00
|
|
|
delete_default_stream(stream_id, row, $(e.target));
|
2018-03-25 19:57:38 +02:00
|
|
|
});
|
2021-02-28 01:21:35 +01:00
|
|
|
}
|