diff --git a/.eslintrc.json b/.eslintrc.json index b87140cbbe..1eaae76175 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -43,6 +43,7 @@ "settings_org": false, "settings_users": false, "settings_streams": false, + "settings_filters": false, "settings": false, "resize": false, "loading": false, diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 4bb1facb60..33f9cc6b21 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -571,7 +571,7 @@ with_overrides(function (override) { // realm_filters var event = event_fixtures.realm_filters; page_params.realm_filters = []; - override('admin.populate_filters', noop); + override('settings_filters.populate_filters', noop); dispatch(event); assert_same(page_params.realm_filters, event.realm_filters); diff --git a/static/js/admin.js b/static/js/admin.js index 52fd62a2a9..0898609ad1 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -16,29 +16,6 @@ exports.show_or_hide_menu_item = function () { } }; -exports.populate_filters = function (filters_data) { - if (!meta.loaded) { - return; - } - - var filters_table = $("#admin_filters_table").expectOne(); - filters_table.find("tr.filter_row").remove(); - _.each(filters_data, function (filter) { - filters_table.append( - templates.render( - "admin_filter_list", { - filter: { - pattern: filter[0], - url_format_string: filter[1], - id: filter[2], - }, - } - ) - ); - }); - loading.destroy_indicator($('#admin_page_filters_loading_indicator')); -}; - function _setup_page() { var options = { realm_name: page_params.realm_name, @@ -89,9 +66,6 @@ function _setup_page() { $("#id_realm_default_language").val(page_params.realm_default_language); - // create loading indicators - loading.make_indicator($('#admin_page_filters_loading_indicator')); - // We set this flag before we're fully loaded so that the populate // methods don't short-circuit. meta.loaded = true; @@ -100,72 +74,7 @@ function _setup_page() { settings_emoji.set_up(); settings_users.set_up(); settings_streams.set_up(); - - // Populate filters table - exports.populate_filters(page_params.realm_filters); - - $('.admin_filters_table').on('click', '.delete', function (e) { - e.preventDefault(); - e.stopPropagation(); - var btn = $(this); - - channel.del({ - url: '/json/realm/filters/' + encodeURIComponent(btn.attr('data-filter-id')), - error: function (xhr) { - if (xhr.status.toString().charAt(0) === "4") { - btn.closest("td").html( - $("

").addClass("text-error").text($.parseJSON(xhr.responseText).msg) - ); - } else { - btn.text(i18n.t("Failed!")); - } - }, - success: function () { - var row = btn.parents('tr'); - row.remove(); - }, - }); - }); - - $(".organization").on("submit", "form.admin-filter-form", function (e) { - e.preventDefault(); - e.stopPropagation(); - var filter_status = $('#admin-filter-status'); - var pattern_status = $('#admin-filter-pattern-status'); - var format_status = $('#admin-filter-format-status'); - filter_status.hide(); - pattern_status.hide(); - format_status.hide(); - var filter = {}; - _.each($(this).serializeArray(), function (obj) { - filter[obj.name] = obj.value; - }); - - channel.post({ - url: "/json/realm/filters", - data: $(this).serialize(), - success: function (data) { - filter.id = data.id; - ui_report.success(i18n.t("Custom filter added!"), filter_status); - }, - error: function (xhr) { - var errors = $.parseJSON(xhr.responseText).errors; - 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); - } - }, - }); - }); - + settings_filters.set_up(); } diff --git a/static/js/server_events.js b/static/js/server_events.js index 7d8232294b..3858c13260 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -134,7 +134,7 @@ function dispatch_normal_event(event) { case 'realm_filters': page_params.realm_filters = event.realm_filters; echo.set_realm_filters(page_params.realm_filters); - admin.populate_filters(page_params.realm_filters); + settings_filters.populate_filters(page_params.realm_filters); break; case 'realm_domains': diff --git a/static/js/settings_filters.js b/static/js/settings_filters.js new file mode 100644 index 0000000000..1e615f7341 --- /dev/null +++ b/static/js/settings_filters.js @@ -0,0 +1,111 @@ +var settings_filters = (function () { + +var exports = {}; + +var meta = { + loaded: false, +}; + +exports.populate_filters = function (filters_data) { + if (!meta.loaded) { + return; + } + + var filters_table = $("#admin_filters_table").expectOne(); + filters_table.find("tr.filter_row").remove(); + _.each(filters_data, function (filter) { + filters_table.append( + templates.render( + "admin_filter_list", { + filter: { + pattern: filter[0], + url_format_string: filter[1], + id: filter[2], + }, + } + ) + ); + }); + loading.destroy_indicator($('#admin_page_filters_loading_indicator')); +}; + +exports.set_up = function () { + meta.loaded = true; + + // create loading indicators + loading.make_indicator($('#admin_page_filters_loading_indicator')); + + // Populate filters table + exports.populate_filters(page_params.realm_filters); + + $('.admin_filters_table').on('click', '.delete', function (e) { + e.preventDefault(); + e.stopPropagation(); + var btn = $(this); + + channel.del({ + url: '/json/realm/filters/' + encodeURIComponent(btn.attr('data-filter-id')), + error: function (xhr) { + if (xhr.status.toString().charAt(0) === "4") { + btn.closest("td").html( + $("

").addClass("text-error").text($.parseJSON(xhr.responseText).msg) + ); + } else { + btn.text(i18n.t("Failed!")); + } + }, + success: function () { + var row = btn.parents('tr'); + row.remove(); + }, + }); + }); + + $(".organization").on("submit", "form.admin-filter-form", function (e) { + e.preventDefault(); + e.stopPropagation(); + var filter_status = $('#admin-filter-status'); + var pattern_status = $('#admin-filter-pattern-status'); + var format_status = $('#admin-filter-format-status'); + filter_status.hide(); + pattern_status.hide(); + format_status.hide(); + var filter = {}; + _.each($(this).serializeArray(), function (obj) { + filter[obj.name] = obj.value; + }); + + channel.post({ + url: "/json/realm/filters", + data: $(this).serialize(), + success: function (data) { + filter.id = data.id; + ui_report.success(i18n.t("Custom filter added!"), filter_status); + }, + error: function (xhr) { + var errors = $.parseJSON(xhr.responseText).errors; + 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); + } + }, + }); + }); + + +}; + +return exports; +}()); + +if (typeof module !== 'undefined') { + module.exports = settings_filters; +} diff --git a/zproject/settings.py b/zproject/settings.py index da156e477f..c2dd65d267 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -917,6 +917,7 @@ JS_SPECS = { 'js/settings_org.js', 'js/settings_users.js', 'js/settings_streams.js', + 'js/settings_filters.js', 'js/settings.js', 'js/admin.js', 'js/tab_bar.js',