diff --git a/.eslintrc.json b/.eslintrc.json index d70a755a9f..88f50af19a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -39,6 +39,7 @@ "settings_lab": false, "settings_bots": false, "settings_sections": false, + "settings_emoji": false, "settings": false, "resize": false, "loading": false, diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 3b98b81d95..df4b78e839 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -560,7 +560,7 @@ with_overrides(function (override) { global.with_stub(function (stub) { override('emoji.update_emojis', stub.f); - override('admin.populate_emoji', noop); + override('settings_emoji.populate_emoji', noop); dispatch(event); var args = stub.get_args('realm_emoji'); assert_same(args.realm_emoji, event.realm_emoji); diff --git a/static/js/admin.js b/static/js/admin.js index a5d3877d94..8daacafe91 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -291,26 +291,6 @@ function make_stream_default(stream_name) { }); } -exports.populate_emoji = function (emoji_data) { - if (!meta.loaded) { - return; - } - - var emoji_table = $('#admin_emoji_table').expectOne(); - emoji_table.find('tr.emoji_row').remove(); - _.each(emoji_data, function (data, name) { - emoji_table.append(templates.render('admin_emoji_list', { - emoji: { - name: name, source_url: data.source_url, - display_url: data.display_url, - author: data.author, - is_admin: page_params.is_admin, - }, - })); - }); - loading.destroy_indicator($('#admin_page_emoji_loading_indicator')); -}; - exports.populate_filters = function (filters_data) { if (!meta.loaded) { return; @@ -443,7 +423,6 @@ function _setup_page() { loading.make_indicator($('#admin_page_bots_loading_indicator')); loading.make_indicator($('#admin_page_streams_loading_indicator')); loading.make_indicator($('#admin_page_deactivated_users_loading_indicator')); - loading.make_indicator($('#admin_page_emoji_loading_indicator')); loading.make_indicator($('#admin_page_auth_methods_loading_indicator')); loading.make_indicator($('#admin_page_filters_loading_indicator')); @@ -469,11 +448,11 @@ function _setup_page() { // methods don't short-circuit. meta.loaded = true; + settings_emoji.set_up(); + // Populate authentication methods table exports.populate_auth_methods(page_params.realm_authentication_methods); - // Populate emoji table - exports.populate_emoji(page_params.realm_emoji); exports.update_default_streams_table(); // Populate filters table @@ -1011,55 +990,6 @@ function _setup_page() { }); }); - $('.admin_emoji_table').on('click', '.delete', function (e) { - e.preventDefault(); - e.stopPropagation(); - var btn = $(this); - - channel.del({ - url: '/json/realm/emoji/' + encodeURIComponent(btn.attr('data-emoji-name')), - error: function (xhr) { - if (xhr.status.toString().charAt(0) === "4") { - btn.closest("td").html( - $("

").addClass("text-error").text(JSON.parse(xhr.responseText).msg) - ); - } else { - btn.text(i18n.t("Failed!")); - } - }, - success: function () { - var row = btn.parents('tr'); - row.remove(); - }, - }); - }); - - $(".organization").on("submit", "form.admin-emoji-form", function (e) { - e.preventDefault(); - e.stopPropagation(); - var emoji_status = $('#admin-emoji-status'); - var emoji = {}; - _.each($(this).serializeArray(), function (obj) { - emoji[obj.name] = obj.value; - }); - - channel.put({ - url: "/json/realm/emoji/" + encodeURIComponent(emoji.name), - data: $(this).serialize(), - success: function () { - $('#admin-emoji-status').hide(); - ui_report.success(i18n.t("Custom emoji added!"), emoji_status); - $("form.admin-emoji-form input[type='text']").val(""); - }, - error: function (xhr) { - $('#admin-emoji-status').hide(); - var errors = JSON.parse(xhr.responseText).msg; - xhr.responseText = JSON.stringify({msg: errors}); - ui_report.error(i18n.t("Failed!"), xhr, emoji_status); - }, - }); - }); - $('.admin_filters_table').on('click', '.delete', function (e) { e.preventDefault(); e.stopPropagation(); diff --git a/static/js/server_events.js b/static/js/server_events.js index 5cd982ad9c..b7f324e8e0 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -128,7 +128,7 @@ function dispatch_normal_event(event) { case 'realm_emoji': emoji.update_emojis(event.realm_emoji); - admin.populate_emoji(event.realm_emoji); + settings_emoji.populate_emoji(event.realm_emoji); break; case 'realm_filters': diff --git a/static/js/settings_emoji.js b/static/js/settings_emoji.js new file mode 100644 index 0000000000..980add247f --- /dev/null +++ b/static/js/settings_emoji.js @@ -0,0 +1,92 @@ +var settings_emoji = (function () { + +var exports = {}; + +var meta = { + loaded: false, +}; + +exports.populate_emoji = function (emoji_data) { + if (!meta.loaded) { + return; + } + + var emoji_table = $('#admin_emoji_table').expectOne(); + emoji_table.find('tr.emoji_row').remove(); + _.each(emoji_data, function (data, name) { + emoji_table.append(templates.render('admin_emoji_list', { + emoji: { + name: name, source_url: data.source_url, + display_url: data.display_url, + author: data.author, + is_admin: page_params.is_admin, + }, + })); + }); + loading.destroy_indicator($('#admin_page_emoji_loading_indicator')); +}; + +exports.set_up = function () { + meta.loaded = true; + + loading.make_indicator($('#admin_page_emoji_loading_indicator')); + + // Populate emoji table + exports.populate_emoji(page_params.realm_emoji); + + $('.admin_emoji_table').on('click', '.delete', function (e) { + e.preventDefault(); + e.stopPropagation(); + var btn = $(this); + + channel.del({ + url: '/json/realm/emoji/' + encodeURIComponent(btn.attr('data-emoji-name')), + error: function (xhr) { + if (xhr.status.toString().charAt(0) === "4") { + btn.closest("td").html( + $("

").addClass("text-error").text(JSON.parse(xhr.responseText).msg) + ); + } else { + btn.text(i18n.t("Failed!")); + } + }, + success: function () { + var row = btn.parents('tr'); + row.remove(); + }, + }); + }); + + $(".organization").on("submit", "form.admin-emoji-form", function (e) { + e.preventDefault(); + e.stopPropagation(); + var emoji_status = $('#admin-emoji-status'); + var emoji = {}; + _.each($(this).serializeArray(), function (obj) { + emoji[obj.name] = obj.value; + }); + + channel.put({ + url: "/json/realm/emoji/" + encodeURIComponent(emoji.name), + data: $(this).serialize(), + success: function () { + $('#admin-emoji-status').hide(); + ui_report.success(i18n.t("Custom emoji added!"), emoji_status); + $("form.admin-emoji-form input[type='text']").val(""); + }, + error: function (xhr) { + $('#admin-emoji-status').hide(); + var errors = JSON.parse(xhr.responseText).msg; + xhr.responseText = JSON.stringify({msg: errors}); + ui_report.error(i18n.t("Failed!"), xhr, emoji_status); + }, + }); + }); +}; + +return exports; +}()); + +if (typeof module !== 'undefined') { + module.exports = settings_emoji; +} diff --git a/zproject/settings.py b/zproject/settings.py index f357c34850..3a814c77eb 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -913,6 +913,7 @@ JS_SPECS = { 'js/settings_muting.js', 'js/settings_lab.js', 'js/settings_sections.js', + 'js/settings_emoji.js', 'js/settings.js', 'js/admin.js', 'js/tab_bar.js',