zulip/static/js/settings_streams.js

144 lines
4.4 KiB
JavaScript
Raw Normal View History

const render_admin_default_streams_list = require("../templates/admin_default_streams_list.hbs");
const meta = {
2017-04-08 21:02:08 +02:00
loaded: false,
};
exports.reset = function () {
meta.loaded = false;
};
exports.maybe_disable_widgets = function () {
if (page_params.is_admin) {
return;
}
$(".organization-box [data-name='default-streams-list']")
.find("input:not(.search), button, select").attr("disabled", true);
};
exports.build_default_stream_table = function () {
const table = $("#admin_default_streams_table").expectOne();
const stream_ids = stream_data.get_default_stream_ids();
const subs = stream_ids.map(stream_data.get_sub_by_id);
const streams_list = list_render.create(table, subs, {
name: "default_streams_list",
modifier: function (item) {
const row = $(render_admin_default_streams_list({
stream: item,
can_modify: page_params.is_admin,
}));
return row;
},
filter: {
element: table.closest(".settings-section").find(".search"),
predicate: function (item, value) {
js: Convert a.indexOf(…) !== -1 to a.includes(…). Babel polyfills this for us for Internet Explorer. import * as babelParser from "recast/parsers/babel"; import * as recast from "recast"; import * as tsParser from "recast/parsers/typescript"; import { builders as b, namedTypes as n } from "ast-types"; import K from "ast-types/gen/kinds"; import fs from "fs"; import path from "path"; import process from "process"; const checkExpression = (node: n.Node): node is K.ExpressionKind => n.Expression.check(node); for (const file of process.argv.slice(2)) { console.log("Parsing", file); const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), { parser: path.extname(file) === ".ts" ? tsParser : babelParser, }); let changed = false; recast.visit(ast, { visitBinaryExpression(path) { const { operator, left, right } = path.node; if ( n.CallExpression.check(left) && n.MemberExpression.check(left.callee) && !left.callee.computed && n.Identifier.check(left.callee.property) && left.callee.property.name === "indexOf" && left.arguments.length === 1 && checkExpression(left.arguments[0]) && ((["===", "!==", "==", "!=", ">", "<="].includes(operator) && n.UnaryExpression.check(right) && right.operator == "-" && n.Literal.check(right.argument) && right.argument.value === 1) || ([">=", "<"].includes(operator) && n.Literal.check(right) && right.value === 0)) ) { const test = b.callExpression( b.memberExpression(left.callee.object, b.identifier("includes")), [left.arguments[0]] ); path.replace( ["!==", "!=", ">", ">="].includes(operator) ? test : b.unaryExpression("!", test) ); changed = true; } this.traverse(path); }, }); if (changed) { console.log("Writing", file); fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" }); } } Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
return item.name.toLowerCase().includes(value);
},
onupdate: function () {
ui.reset_scrollbar(table);
},
},
parent_container: $("#admin-default-streams-list").expectOne(),
}).init();
streams_list.sort("alphabetic", "name");
loading.destroy_indicator($('#admin_page_default_streams_loading_indicator'));
2017-04-08 21:02:08 +02:00
};
exports.update_default_streams_table = function () {
if (/#*organization/.test(window.location.hash) ||
/#*settings/.test(window.location.hash)) {
$("#admin_default_streams_table").expectOne().find("tr.default_stream_row").remove();
exports.build_default_stream_table();
2017-04-08 21:02:08 +02:00
}
};
function make_stream_default(stream_name) {
const data = {
2017-04-08 21:02:08 +02:00
stream_name: stream_name,
};
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: data,
error: function (xhr) {
if (xhr.status.toString().charAt(0) === "4") {
ui_report.error(i18n.t("Failed"), xhr, default_stream_status);
2017-04-08 21:02:08 +02:00
} else {
ui_report.error(i18n.t("Failed"), default_stream_status);
2017-04-08 21:02:08 +02:00
}
default_stream_status.show();
2017-04-08 21:02:08 +02:00
},
});
}
exports.delete_default_stream = function (stream_name, default_stream_row, alert_element) {
channel.del({
url: "/json/default_streams" + "?" + $.param({ stream_name: stream_name }),
error: function (xhr) {
ui_report.generic_row_button_error(xhr, alert_element);
},
success: function () {
default_stream_row.remove();
},
});
};
2017-04-08 21:02:08 +02:00
exports.set_up = function () {
exports.build_page();
exports.maybe_disable_widgets();
};
exports.build_page = function () {
2017-04-08 21:02:08 +02:00
meta.loaded = true;
exports.update_default_streams_table();
$('.create_default_stream').keypress(function (e) {
if (e.which === 13) {
e.preventDefault();
e.stopPropagation();
const default_stream_input = $(".create_default_stream");
make_stream_default(default_stream_input.val());
default_stream_input[0].value = "";
2017-04-08 21:02:08 +02:00
}
});
$('.create_default_stream').typeahead({
items: 5,
fixed: true,
source: function () {
return stream_data.get_non_default_stream_names();
2017-04-08 21:02:08 +02:00
},
highlighter: function (item) {
return typeahead_helper.render_typeahead_item({ primary: item });
},
2017-04-08 21:02:08 +02:00
updater: function (stream_name) {
make_stream_default(stream_name);
},
});
$(".default-stream-form").on("click", "#do_submit_stream", function (e) {
e.preventDefault();
e.stopPropagation();
const default_stream_input = $(".create_default_stream");
make_stream_default(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_name = row.attr("id");
exports.delete_default_stream(stream_name, row, $(e.target));
});
};
2017-04-08 21:02:08 +02:00
window.settings_streams = exports;