2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const render_admin_filter_list = require("../templates/admin_filter_list.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const meta = {
|
2017-04-08 23:24:03 +02:00
|
|
|
loaded: false,
|
|
|
|
};
|
|
|
|
|
2017-04-17 16:51:27 +02:00
|
|
|
exports.reset = function () {
|
|
|
|
meta.loaded = false;
|
|
|
|
};
|
|
|
|
|
2018-12-08 17:37:57 +01:00
|
|
|
exports.maybe_disable_widgets = function () {
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-12 12:13:47 +02:00
|
|
|
function compare_by_index(a, b, i) {
|
|
|
|
if (a[i] > b[i]) {
|
|
|
|
return 1;
|
|
|
|
} else if (a[i] === b[i]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sort_pattern(a, b) {
|
|
|
|
return compare_by_index(a, b, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sort_url(a, b) {
|
|
|
|
return compare_by_index(a, b, 1);
|
|
|
|
}
|
|
|
|
|
2017-04-08 23:24:03 +02:00
|
|
|
exports.populate_filters = function (filters_data) {
|
|
|
|
if (!meta.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const filters_table = $("#admin_filters_table").expectOne();
|
2021-01-29 10:27:56 +01:00
|
|
|
ListWidget.create(filters_table, filters_data, {
|
2019-08-16 07:53:04 +02:00
|
|
|
name: "linkifiers_list",
|
2020-07-20 22:18:43 +02:00
|
|
|
modifier(filter) {
|
2019-08-16 07:53:04 +02:00
|
|
|
return render_admin_filter_list({
|
2019-07-09 21:24:00 +02:00
|
|
|
filter: {
|
|
|
|
pattern: filter[0],
|
|
|
|
url_format_string: filter[1],
|
|
|
|
id: filter[2],
|
|
|
|
},
|
|
|
|
can_modify: page_params.is_admin,
|
2019-08-16 07:53:04 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: filters_table.closest(".settings-section").find(".search"),
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, value) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
item[0].toLowerCase().includes(value) || item[1].toLowerCase().includes(value)
|
|
|
|
);
|
2019-08-16 07:53:04 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
onupdate() {
|
2019-08-16 07:53:04 +02:00
|
|
|
ui.reset_scrollbar(filters_table);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
parent_container: $("#filter-settings").expectOne(),
|
2020-04-11 16:23:29 +02:00
|
|
|
init_sort: [sort_pattern],
|
|
|
|
sort_fields: {
|
|
|
|
pattern: sort_pattern,
|
|
|
|
url: sort_url,
|
|
|
|
},
|
2020-07-15 01:29:15 +02:00
|
|
|
simplebar_container: $("#filter-settings .progressive-table-wrapper"),
|
2020-04-11 16:23:29 +02:00
|
|
|
});
|
2019-08-16 08:03:43 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#admin_page_filters_loading_indicator"));
|
2017-04-08 23:24:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.set_up = function () {
|
2018-12-08 18:16:37 +01:00
|
|
|
exports.build_page();
|
|
|
|
exports.maybe_disable_widgets();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.build_page = function () {
|
2017-04-08 23:24:03 +02:00
|
|
|
meta.loaded = true;
|
|
|
|
|
|
|
|
// create loading indicators
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.make_indicator($("#admin_page_filters_loading_indicator"));
|
2017-04-08 23:24:03 +02:00
|
|
|
|
|
|
|
// Populate filters table
|
|
|
|
exports.populate_filters(page_params.realm_filters);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".admin_filters_table").on("click", ".delete", function (e) {
|
2017-04-08 23:24:03 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2019-11-02 00:06:25 +01:00
|
|
|
const btn = $(this);
|
2017-04-08 23:24:03 +02:00
|
|
|
|
|
|
|
channel.del({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/realm/filters/" + encodeURIComponent(btn.attr("data-filter-id")),
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2018-03-25 11:12:06 +02:00
|
|
|
ui_report.generic_row_button_error(xhr, btn);
|
2017-04-08 23:24:03 +02:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2020-07-15 01:29:15 +02:00
|
|
|
const row = btn.parents("tr");
|
2017-04-08 23:24:03 +02:00
|
|
|
row.remove();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
$(".organization form.admin-filter-form")
|
|
|
|
.off("submit")
|
|
|
|
.on("submit", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
const filter_status = $("#admin-filter-status");
|
|
|
|
const pattern_status = $("#admin-filter-pattern-status");
|
|
|
|
const format_status = $("#admin-filter-format-status");
|
|
|
|
const add_filter_button = $(".new-filter-form button");
|
2020-07-22 02:59:06 +02:00
|
|
|
add_filter_button.prop("disabled", true);
|
2020-07-15 00:34:28 +02:00
|
|
|
filter_status.hide();
|
|
|
|
pattern_status.hide();
|
|
|
|
format_status.hide();
|
|
|
|
const filter = {};
|
|
|
|
|
|
|
|
for (const obj of $(this).serializeArray()) {
|
|
|
|
filter[obj.name] = obj.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: "/json/realm/filters",
|
|
|
|
data: $(this).serialize(),
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#filter_pattern").val("");
|
|
|
|
$("#filter_format_string").val("");
|
2020-07-22 02:59:06 +02:00
|
|
|
add_filter_button.prop("disabled", false);
|
2020-07-15 00:34:28 +02:00
|
|
|
filter.id = data.id;
|
|
|
|
ui_report.success(i18n.t("Custom filter added!"), filter_status);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-07-15 00:34:28 +02:00
|
|
|
const errors = JSON.parse(xhr.responseText).errors;
|
2020-07-22 02:59:06 +02:00
|
|
|
add_filter_button.prop("disabled", false);
|
2020-07-15 00:34:28 +02:00
|
|
|
if (errors.pattern !== undefined) {
|
|
|
|
xhr.responseText = JSON.stringify({msg: errors.pattern});
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, pattern_status);
|
|
|
|
}
|
|
|
|
if (errors.url_format_string !== undefined) {
|
|
|
|
xhr.responseText = JSON.stringify({msg: errors.url_format_string});
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, format_status);
|
|
|
|
}
|
|
|
|
if (errors.__all__ !== undefined) {
|
|
|
|
xhr.responseText = JSON.stringify({msg: errors.__all__});
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, filter_status);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2017-04-08 23:24:03 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.settings_linkifiers = exports;
|