From 0fc7b9907f3b3e93cb4a88571b1defb56168bc7d Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 8 Apr 2017 09:13:39 -0700 Subject: [PATCH] Extract settings_org.js (and fix live updates). This is mostly moving code, but we do add short-circuit logic for some live-updating methods here. Note that this affects two different sections of the admin app: * Organization settings * Authentication methods We really want to move to one module per section, but there is some legacy coupling that makes this difficult for now. --- .eslintrc.json | 1 + static/js/admin.js | 431 +-------------------------------- static/js/server_events.js | 14 +- static/js/settings_org.js | 475 +++++++++++++++++++++++++++++++++++++ zproject/settings.py | 1 + 5 files changed, 485 insertions(+), 437 deletions(-) create mode 100644 static/js/settings_org.js diff --git a/.eslintrc.json b/.eslintrc.json index 88f50af19a..e809c4ebed 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -40,6 +40,7 @@ "settings_bots": false, "settings_sections": false, "settings_emoji": false, + "settings_org": false, "settings": false, "resize": false, "loading": false, diff --git a/static/js/admin.js b/static/js/admin.js index 8daacafe91..2dcc8e2851 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -172,24 +172,6 @@ function populate_streams(streams_data) { loading.destroy_indicator($('#admin_page_streams_loading_indicator')); } -exports.toggle_name_change_display = function () { - if ($('#full_name').attr('disabled')) { - $('#full_name').removeAttr('disabled'); - } else { - $('#full_name').attr('disabled', 'disabled'); - } - $(".change_name_tooltip").toggle(); -}; - -exports.toggle_email_change_display = function () { - $("#change_email").toggle(); - $(".change_email_tooltip").toggle(); -}; - -exports.update_realm_description = function (description) { - $('#id_realm_description').val(description); -}; - exports.build_default_stream_table = function (streams_data) { var self = {}; @@ -314,60 +296,6 @@ exports.populate_filters = function (filters_data) { loading.destroy_indicator($('#admin_page_filters_loading_indicator')); }; -exports.populate_realm_domains = function (realm_domains) { - if (!meta.loaded) { - return; - } - - var domains_list = _.map(realm_domains, function (realm_domain) { - return (realm_domain.allow_subdomains ? "*." + realm_domain.domain : realm_domain.domain); - }); - var domains = domains_list.join(', '); - - $("#id_realm_restricted_to_domain").prop("checked", page_params.realm_restricted_to_domain); - if (domains.length === 0) { - domains = i18n.t("None"); - $("#id_realm_restricted_to_domain").prop("disabled", true); - } - $("#realm_restricted_to_domains_label").text(i18n.t("New users restricted to the following domains: __domains__", {domains: domains})); - - var realm_domains_table_body = $("#realm_domains_table tbody").expectOne(); - realm_domains_table_body.find("tr").remove(); - _.each(realm_domains, function (realm_domain) { - realm_domains_table_body.append(templates.render("admin-realm-domains-list", {realm_domain: realm_domain})); - }); -}; - -exports.reset_realm_default_language = function () { - if (!meta.loaded) { - return; - } - - $("#id_realm_default_language").val(page_params.realm_default_language); -}; - -exports.populate_auth_methods = function (auth_methods) { - if (!meta.loaded) { - return; - } - - var auth_methods_table = $("#admin_auth_methods_table").expectOne(); - auth_methods_table.find('tr.method_row').remove(); - _.each(_.keys(auth_methods).sort(), function (key) { - auth_methods_table.append(templates.render('admin_auth_methods_list', { - method: { - method: key, - enabled: auth_methods[key], - }, - })); - }); - loading.destroy_indicator($('#admin_page_auth_methods_loading_indicator')); -}; - -exports.update_message_retention_days = function () { - $("#id_realm_message_retention_days").val(page_params.message_retention_days); -}; - function _setup_page() { var options = { realm_name: page_params.realm_name, @@ -423,7 +351,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_auth_methods_loading_indicator')); loading.make_indicator($('#admin_page_filters_loading_indicator')); // Populate users and bots tables @@ -448,19 +375,14 @@ function _setup_page() { // methods don't short-circuit. meta.loaded = true; + settings_org.set_up(); settings_emoji.set_up(); - // Populate authentication methods table - exports.populate_auth_methods(page_params.realm_authentication_methods); - exports.update_default_streams_table(); // Populate filters table exports.populate_filters(page_params.realm_filters); - // Populate realm domains - exports.populate_realm_domains(page_params.domains); - // Setup click handlers $(".admin_user_table").on("click", ".deactivate", function (e) { e.preventDefault(); @@ -595,252 +517,6 @@ function _setup_page() { }); }); - $("#id_realm_invite_required").change(function () { - if (this.checked) { - $("#id_realm_invite_by_admins_only").removeAttr("disabled"); - $("#id_realm_invite_by_admins_only_label").parent().removeClass("control-label-disabled"); - } else { - $("#id_realm_invite_by_admins_only").attr("disabled", true); - $("#id_realm_invite_by_admins_only_label").parent().addClass("control-label-disabled"); - } - }); - - $("#id_realm_allow_message_editing").change(function () { - if (this.checked) { - $("#id_realm_message_content_edit_limit_minutes").removeAttr("disabled"); - $("#id_realm_message_content_edit_limit_minutes_label").parent().removeClass("control-label-disabled"); - } else { - $("#id_realm_message_content_edit_limit_minutes").attr("disabled", true); - $("#id_realm_message_content_edit_limit_minutes_label").parent().addClass("control-label-disabled"); - } - }); - - $(".organization").on("submit", "form.admin-realm-form", function (e) { - var name_status = $("#admin-realm-name-status").expectOne(); - var description_status = $("#admin-realm-description-status").expectOne(); - var restricted_to_domain_status = $("#admin-realm-restricted-to-domain-status").expectOne(); - var invite_required_status = $("#admin-realm-invite-required-status").expectOne(); - var invite_by_admins_only_status = $("#admin-realm-invite-by-admins-only-status").expectOne(); - var inline_image_preview_status = $("#admin-realm-inline-image-preview-status").expectOne(); - var inline_url_embed_preview_status = $("#admin-realm-inline-url-embed-preview-status").expectOne(); - var authentication_methods_status = $("#admin-realm-authentication-methods-status").expectOne(); - var create_stream_by_admins_only_status = $("#admin-realm-create-stream-by-admins-only-status").expectOne(); - var name_changes_disabled_status = $("#admin-realm-name-changes-disabled-status").expectOne(); - var email_changes_disabled_status = $("#admin-realm-email-changes-disabled-status").expectOne(); - var add_emoji_by_admins_only_status = $("#admin-realm-add-emoji-by-admins-only-status").expectOne(); - var message_editing_status = $("#admin-realm-message-editing-status").expectOne(); - var default_language_status = $("#admin-realm-default-language-status").expectOne(); - var waiting_period_threshold_status = $("#admin-realm-waiting_period_threshold_status").expectOne(); - name_status.hide(); - description_status.hide(); - restricted_to_domain_status.hide(); - invite_required_status.hide(); - invite_by_admins_only_status.hide(); - inline_image_preview_status.hide(); - inline_url_embed_preview_status.hide(); - authentication_methods_status.hide(); - create_stream_by_admins_only_status.hide(); - name_changes_disabled_status.hide(); - email_changes_disabled_status.hide(); - add_emoji_by_admins_only_status.hide(); - message_editing_status.hide(); - default_language_status.hide(); - waiting_period_threshold_status.hide(); - - e.preventDefault(); - e.stopPropagation(); - - var new_name = $("#id_realm_name").val(); - var new_description = $("#id_realm_description").val(); - var new_restricted = $("#id_realm_restricted_to_domain").prop("checked"); - var new_invite = $("#id_realm_invite_required").prop("checked"); - var new_invite_by_admins_only = $("#id_realm_invite_by_admins_only").prop("checked"); - var new_inline_image_preview = $("#id_realm_inline_image_preview").prop("checked"); - var new_inline_url_embed_preview = $("#id_realm_inline_url_embed_preview").prop("checked"); - var new_create_stream_by_admins_only = $("#id_realm_create_stream_by_admins_only").prop("checked"); - var new_name_changes_disabled = $("#id_realm_name_changes_disabled").prop("checked"); - var new_email_changes_disabled = $("#id_realm_email_changes_disabled").prop("checked"); - var new_add_emoji_by_admins_only = $("#id_realm_add_emoji_by_admins_only").prop("checked"); - var new_allow_message_editing = $("#id_realm_allow_message_editing").prop("checked"); - var new_message_content_edit_limit_minutes = $("#id_realm_message_content_edit_limit_minutes").val(); - var new_message_retention_days = $("#id_realm_message_retention_days").val(); - var new_default_language = $("#id_realm_default_language").val(); - var new_waiting_period_threshold = $("#id_realm_waiting_period_threshold").val(); - var new_auth_methods = {}; - _.each($("#admin_auth_methods_table").find('tr.method_row'), function (method_row) { - new_auth_methods[$(method_row).data('method')] = $(method_row).find('input').prop('checked'); - }); - // If allow_message_editing is unchecked, message_content_edit_limit_minutes - // is irrelevant. Hence if allow_message_editing is unchecked, and - // message_content_edit_limit_minutes is poorly formed, we set the latter to - // a default value to prevent the server from returning an error. - if (!new_allow_message_editing) { - if ((parseInt(new_message_content_edit_limit_minutes, 10).toString() !== - new_message_content_edit_limit_minutes) || - new_message_content_edit_limit_minutes < 0) { - // Realm.DEFAULT_MESSAGE_CONTENT_EDIT_LIMIT_SECONDS / 60 - new_message_content_edit_limit_minutes = 10; - } - } - if (parseInt(new_message_retention_days, 10).toString() !== - new_message_retention_days && new_message_retention_days !== "") { - new_message_retention_days = ""; - } - - var url = "/json/realm"; - var data = { - name: JSON.stringify(new_name), - description: JSON.stringify(new_description), - restricted_to_domain: JSON.stringify(new_restricted), - invite_required: JSON.stringify(new_invite), - invite_by_admins_only: JSON.stringify(new_invite_by_admins_only), - inline_image_preview: JSON.stringify(new_inline_image_preview), - inline_url_embed_preview: JSON.stringify(new_inline_url_embed_preview), - authentication_methods: JSON.stringify(new_auth_methods), - create_stream_by_admins_only: JSON.stringify(new_create_stream_by_admins_only), - name_changes_disabled: JSON.stringify(new_name_changes_disabled), - email_changes_disabled: JSON.stringify(new_email_changes_disabled), - add_emoji_by_admins_only: JSON.stringify(new_add_emoji_by_admins_only), - allow_message_editing: JSON.stringify(new_allow_message_editing), - message_content_edit_limit_seconds: - JSON.stringify(parseInt(new_message_content_edit_limit_minutes, 10) * 60), - message_retention_days: new_message_retention_days !== "" ? JSON.stringify(parseInt(new_message_retention_days, 10)) : null, - default_language: JSON.stringify(new_default_language), - waiting_period_threshold: JSON.stringify(parseInt(new_waiting_period_threshold, 10)), - }; - - channel.patch({ - url: url, - data: data, - success: function (response_data) { - if (response_data.name !== undefined) { - ui_report.success(i18n.t("Name changed!"), name_status); - } - if (response_data.description !== undefined) { - ui_report.success(i18n.t("Description changed!"), description_status); - } - if (response_data.restricted_to_domain !== undefined) { - if (response_data.restricted_to_domain) { - ui_report.success(i18n.t("New user e-mails now restricted to certain domains!"), restricted_to_domain_status); - } else { - ui_report.success(i18n.t("New users may have arbitrary e-mails!"), restricted_to_domain_status); - } - } - if (response_data.invite_required !== undefined) { - if (response_data.invite_required) { - ui_report.success(i18n.t("New users must be invited by e-mail!"), invite_required_status); - } else { - ui_report.success(i18n.t("New users may sign up online!"), invite_required_status); - } - } - if (response_data.invite_by_admins_only !== undefined) { - if (response_data.invite_by_admins_only) { - ui_report.success(i18n.t("New users must be invited by an admin!"), invite_by_admins_only_status); - } else { - ui_report.success(i18n.t("Any user may now invite new users!"), invite_by_admins_only_status); - } - } - if (response_data.inline_image_preview !== undefined) { - if (response_data.inline_image_preview) { - ui_report.success(i18n.t("Previews of uploaded and linked images will be shown!"), inline_image_preview_status); - } else { - ui_report.success(i18n.t("Previews of uploaded and linked images will not be shown!"), inline_image_preview_status); - } - } - if (response_data.inline_url_embed_preview !== undefined) { - if (response_data.inline_url_embed_preview) { - ui_report.success(i18n.t("Previews for linked websites will be shown!"), inline_url_embed_preview_status); - } else { - ui_report.success(i18n.t("Previews for linked websites will not be shown!"), inline_url_embed_preview_status); - } - } - if (response_data.create_stream_by_admins_only !== undefined) { - if (response_data.create_stream_by_admins_only) { - ui_report.success(i18n.t("Only administrators may now create new streams!"), create_stream_by_admins_only_status); - } else { - ui_report.success(i18n.t("Any user may now create new streams!"), create_stream_by_admins_only_status); - } - } - if (response_data.name_changes_disabled !== undefined) { - if (response_data.name_changes_disabled) { - ui_report.success(i18n.t("Users cannot change their name!"), name_changes_disabled_status); - } else { - ui_report.success(i18n.t("Users may now change their name!"), name_changes_disabled_status); - } - } - if (response_data.email_changes_disabled !== undefined) { - if (response_data.email_changes_disabled) { - ui_report.success(i18n.t("Users cannot change their email!"), email_changes_disabled_status); - } else { - ui_report.success(i18n.t("Users may now change their email!"), email_changes_disabled_status); - } - } - if (response_data.add_emoji_by_admins_only !== undefined) { - if (response_data.add_emoji_by_admins_only) { - ui_report.success(i18n.t("Only administrators may now add new emoji!"), add_emoji_by_admins_only_status); - } else { - ui_report.success(i18n.t("Any user may now add new emoji!"), add_emoji_by_admins_only_status); - } - } - if (response_data.authentication_methods !== undefined) { - if (response_data.authentication_methods) { - ui_report.success(i18n.t("Authentication methods saved!"), authentication_methods_status); - } - } - if (response_data.allow_message_editing !== undefined) { - // We expect message_content_edit_limit_seconds was sent in the - // response as well - var data_message_content_edit_limit_minutes = - Math.ceil(response_data.message_content_edit_limit_seconds / 60); - if (response_data.allow_message_editing) { - if (response_data.message_content_edit_limit_seconds > 0) { - ui_report.success(i18n.t("Users can now edit topics for all their messages," - +" and the content of messages which are less than __num_minutes__ minutes old.", - {num_minutes : - data_message_content_edit_limit_minutes}), - message_editing_status); - } else { - ui_report.success(i18n.t("Users can now edit the content and topics of all their past messages!"), message_editing_status); - } - } else { - ui_report.success(i18n.t("Users can no longer edit their past messages!"), message_editing_status); - } - // message_content_edit_limit_seconds could have been changed earlier - // in this function, so update the field just in case - $("#id_realm_message_content_edit_limit_minutes").val(data_message_content_edit_limit_minutes); - } - if (response_data.default_language !== undefined) { - if (response_data.default_language) { - ui_report.success(i18n.t("Default language changed!"), default_language_status); - } - } - if (response_data.waiting_period_threshold !== undefined) { - if (response_data.waiting_period_threshold > 0) { - ui_report.success(i18n.t("Waiting period threshold changed!"), waiting_period_threshold_status); - } - } - // Check if no changes made - var no_changes_made = true; - for (var key in response_data) { - if (['msg', 'result'].indexOf(key) < 0) { - no_changes_made = false; - } - } - if (no_changes_made) { - ui_report.success(i18n.t("No changes to save!"), name_status); - } - }, - error: function (xhr) { - var reason = $.parseJSON(xhr.responseText).reason; - if (reason === "no authentication") { - ui_report.error(i18n.t("Failed!"), xhr, authentication_methods_status); - } else { - ui_report.error(i18n.t("Failed!"), xhr, name_status); - } - }, - }); - }); - $(".admin_user_table").on("click", ".make-admin", function (e) { e.preventDefault(); e.stopPropagation(); @@ -1052,111 +728,6 @@ function _setup_page() { }); }); - $("#realm_domains_table").on("click", ".delete_realm_domain", function () { - var domain = $(this).parents("tr").find(".domain").text(); - var url = "/json/realm/domains/" + domain; - var realm_domains_info = $("#realm_domains_modal").find(".realm_domains_info"); - - channel.del({ - url: url, - success: function () { - realm_domains_info.removeClass("text-error"); - realm_domains_info.addClass("text-success"); - realm_domains_info.text(i18n.t("Deleted successfully!")); - }, - error: function (xhr) { - realm_domains_info.removeClass("text-success"); - realm_domains_info.addClass("text-error"); - realm_domains_info.text(JSON.parse(xhr.responseText).msg); - }, - }); - }); - - $("#submit-add-realm-domain").click(function () { - var realm_domains_info = $("#realm_domains_modal").find(".realm_domains_info"); - var widget = $("#add-realm-domain-widget"); - var domain = widget.find(".new-realm-domain").val(); - var allow_subdomains = widget.find(".new-realm-domain-allow-subdomains").prop("checked"); - var data = { - domain: JSON.stringify(domain), - allow_subdomains: JSON.stringify(allow_subdomains), - }; - - channel.post({ - url: "/json/realm/domains", - data: data, - success: function () { - $("#add-realm-domain-widget .new-realm-domain").val(""); - $("#add-realm-domain-widget .new-realm-domain-allow-subdomains").prop("checked", false); - $("#id_realm_restricted_to_domain").prop("disabled", false); - realm_domains_info.removeClass("text-error"); - realm_domains_info.addClass("text-success"); - realm_domains_info.text(i18n.t("Added successfully!")); - }, - error: function (xhr) { - realm_domains_info.removeClass("text-success"); - realm_domains_info.addClass("text-error"); - realm_domains_info.text(JSON.parse(xhr.responseText).msg); - }, - }); - }); - - $("#realm_domains_table").on("change", ".allow-subdomains", function (e) { - e.stopPropagation(); - var realm_domains_info = $("#realm_domains_modal").find(".realm_domains_info"); - var domain = $(this).parents("tr").find(".domain").text(); - var allow_subdomains = $(this).prop('checked'); - var url = '/json/realm/domains/' + domain; - var data = { - allow_subdomains: JSON.stringify(allow_subdomains), - }; - - channel.patch({ - url: url, - data: data, - success: function () { - realm_domains_info.removeClass("text-error"); - realm_domains_info.addClass("text-success"); - if (allow_subdomains) { - realm_domains_info.text(i18n.t("Update successful: Subdomains allowed for __domain__", - {domain: domain})); - } else { - realm_domains_info.text(i18n.t("Update successful: Subdomains no longer allowed for __domain__", - {domain: domain})); - } - }, - error: function (xhr) { - realm_domains_info.removeClass("text-success"); - realm_domains_info.addClass("text-error"); - realm_domains_info.text(JSON.parse(xhr.responseText).msg); - }, - }); - }); - - function upload_realm_icon(file_input) { - var form_data = new FormData(); - - form_data.append('csrfmiddlewaretoken', csrf_token); - jQuery.each(file_input[0].files, function (i, file) { - form_data.append('file-'+i, file); - }); - - var spinner = $("#upload_icon_spinner").expectOne(); - loading.make_indicator(spinner, {text: i18n.t("Uploading icon.")}); - - channel.put({ - url: '/json/realm/icon', - data: form_data, - cache: false, - processData: false, - contentType: false, - success: function () { - loading.destroy_indicator($("#upload_icon_spinner")); - }, - }); - - } - realm_icon.build_realm_icon_widget(upload_realm_icon); } diff --git a/static/js/server_events.js b/static/js/server_events.js index b7f324e8e0..0719b66b01 100644 --- a/static/js/server_events.js +++ b/static/js/server_events.js @@ -62,7 +62,7 @@ function dispatch_normal_event(event) { notifications.redraw_title(); } else if (event.op === 'update' && event.property === 'description') { page_params.realm_description = event.value; - admin.update_realm_description(event.value); + settings_org.update_realm_description(event.value); } else if (event.op === 'update' && event.property === 'invite_required') { page_params.realm_invite_required = event.value; } else if (event.op === 'update' && event.property === 'invite_by_admins_only') { @@ -78,27 +78,27 @@ function dispatch_normal_event(event) { } } else if (event.op === 'update' && event.property === 'name_changes_disabled') { page_params.realm_name_changes_disabled = event.value; - admin.toggle_name_change_display(); + settings_org.toggle_name_change_display(); } else if (event.op === 'update' && event.property === 'email_changes_disabled') { page_params.realm_email_changes_disabled = event.value; - admin.toggle_email_change_display(); + settings_org.toggle_email_change_display(); } else if (event.op === 'update' && event.property === 'add_emoji_by_admins_only') { page_params.add_emoji_by_admins_only = event.value; } else if (event.op === 'update' && event.property === 'restricted_to_domain') { page_params.realm_restricted_to_domain = event.value; } else if (event.op === 'update' && event.property === 'message_retention_days') { page_params.message_retention_days = event.value; - admin.update_message_retention_days(); + settings_org.update_message_retention_days(); } else if (event.op === 'update_dict' && event.property === 'default') { $.each(event.data, function (key, value) { page_params['realm_' + key] = value; }); if (event.data.authentication_methods !== undefined) { - admin.populate_auth_methods(event.data.authentication_methods); + settings_org.populate_auth_methods(event.data.authentication_methods); } } else if (event.op === 'update' && event.property === 'default_language') { page_params.realm_default_language = event.value; - admin.reset_realm_default_language(); + settings_org.reset_realm_default_language(); } else if (event.op === 'update' && event.property === 'waiting_period_threshold') { page_params.realm_waiting_period_threshold = event.value; } else if (event.op === 'update_dict' && event.property === 'icon') { @@ -156,7 +156,7 @@ function dispatch_normal_event(event) { } } } - admin.populate_realm_domains(page_params.domains); + settings_org.populate_realm_domains(page_params.domains); break; case 'realm_user': diff --git a/static/js/settings_org.js b/static/js/settings_org.js new file mode 100644 index 0000000000..2ba33ac15c --- /dev/null +++ b/static/js/settings_org.js @@ -0,0 +1,475 @@ +var settings_org = (function () { + +var exports = {}; + +var meta = { + loaded: false, +}; + +exports.populate_realm_domains = function (realm_domains) { + if (!meta.loaded) { + return; + } + + var domains_list = _.map(realm_domains, function (realm_domain) { + return (realm_domain.allow_subdomains ? "*." + realm_domain.domain : realm_domain.domain); + }); + var domains = domains_list.join(', '); + + $("#id_realm_restricted_to_domain").prop("checked", page_params.realm_restricted_to_domain); + if (domains.length === 0) { + domains = i18n.t("None"); + $("#id_realm_restricted_to_domain").prop("disabled", true); + } + $("#realm_restricted_to_domains_label").text(i18n.t("New users restricted to the following domains: __domains__", {domains: domains})); + + var realm_domains_table_body = $("#realm_domains_table tbody").expectOne(); + realm_domains_table_body.find("tr").remove(); + _.each(realm_domains, function (realm_domain) { + realm_domains_table_body.append(templates.render("admin-realm-domains-list", {realm_domain: realm_domain})); + }); +}; + +exports.reset_realm_default_language = function () { + if (!meta.loaded) { + return; + } + + $("#id_realm_default_language").val(page_params.realm_default_language); +}; + + +exports.toggle_name_change_display = function () { + if (!meta.loaded) { + return; + } + + if ($('#full_name').attr('disabled')) { + $('#full_name').removeAttr('disabled'); + } else { + $('#full_name').attr('disabled', 'disabled'); + } + $(".change_name_tooltip").toggle(); +}; + +exports.toggle_email_change_display = function () { + if (!meta.loaded) { + return; + } + + $("#change_email").toggle(); + $(".change_email_tooltip").toggle(); +}; + +exports.update_realm_description = function (description) { + if (!meta.loaded) { + return; + } + + $('#id_realm_description').val(description); +}; + +exports.update_message_retention_days = function () { + if (!meta.loaded) { + return; + } + + $("#id_realm_message_retention_days").val(page_params.message_retention_days); +}; + +exports.populate_auth_methods = function (auth_methods) { + if (!meta.loaded) { + return; + } + + var auth_methods_table = $("#admin_auth_methods_table").expectOne(); + auth_methods_table.find('tr.method_row').remove(); + _.each(_.keys(auth_methods).sort(), function (key) { + auth_methods_table.append(templates.render('admin_auth_methods_list', { + method: { + method: key, + enabled: auth_methods[key], + }, + })); + }); + loading.destroy_indicator($('#admin_page_auth_methods_loading_indicator')); +}; + +exports.set_up = function () { + meta.loaded = true; + + loading.make_indicator($('#admin_page_auth_methods_loading_indicator')); + + // Populate realm domains + exports.populate_realm_domains(page_params.domains); + + // Populate authentication methods table + exports.populate_auth_methods(page_params.realm_authentication_methods); + + + $("#id_realm_invite_required").change(function () { + if (this.checked) { + $("#id_realm_invite_by_admins_only").removeAttr("disabled"); + $("#id_realm_invite_by_admins_only_label").parent().removeClass("control-label-disabled"); + } else { + $("#id_realm_invite_by_admins_only").attr("disabled", true); + $("#id_realm_invite_by_admins_only_label").parent().addClass("control-label-disabled"); + } + }); + + $("#id_realm_allow_message_editing").change(function () { + if (this.checked) { + $("#id_realm_message_content_edit_limit_minutes").removeAttr("disabled"); + $("#id_realm_message_content_edit_limit_minutes_label").parent().removeClass("control-label-disabled"); + } else { + $("#id_realm_message_content_edit_limit_minutes").attr("disabled", true); + $("#id_realm_message_content_edit_limit_minutes_label").parent().addClass("control-label-disabled"); + } + }); + + $(".organization").on("submit", "form.admin-realm-form", function (e) { + // TODO: We actually have two forms named admin-realm-form. We really + // should break out a separate endpoint to update the realm's + // auth backends, and then we can make our code more modular by + // having two different form classes, two different click handlers, + // and a separate JS module for handling auth backends. + + var name_status = $("#admin-realm-name-status").expectOne(); + var description_status = $("#admin-realm-description-status").expectOne(); + var restricted_to_domain_status = $("#admin-realm-restricted-to-domain-status").expectOne(); + var invite_required_status = $("#admin-realm-invite-required-status").expectOne(); + var invite_by_admins_only_status = $("#admin-realm-invite-by-admins-only-status").expectOne(); + var inline_image_preview_status = $("#admin-realm-inline-image-preview-status").expectOne(); + var inline_url_embed_preview_status = $("#admin-realm-inline-url-embed-preview-status").expectOne(); + var authentication_methods_status = $("#admin-realm-authentication-methods-status").expectOne(); + var create_stream_by_admins_only_status = $("#admin-realm-create-stream-by-admins-only-status").expectOne(); + var name_changes_disabled_status = $("#admin-realm-name-changes-disabled-status").expectOne(); + var email_changes_disabled_status = $("#admin-realm-email-changes-disabled-status").expectOne(); + var add_emoji_by_admins_only_status = $("#admin-realm-add-emoji-by-admins-only-status").expectOne(); + var message_editing_status = $("#admin-realm-message-editing-status").expectOne(); + var default_language_status = $("#admin-realm-default-language-status").expectOne(); + var waiting_period_threshold_status = $("#admin-realm-waiting_period_threshold_status").expectOne(); + name_status.hide(); + description_status.hide(); + restricted_to_domain_status.hide(); + invite_required_status.hide(); + invite_by_admins_only_status.hide(); + inline_image_preview_status.hide(); + inline_url_embed_preview_status.hide(); + authentication_methods_status.hide(); + create_stream_by_admins_only_status.hide(); + name_changes_disabled_status.hide(); + email_changes_disabled_status.hide(); + add_emoji_by_admins_only_status.hide(); + message_editing_status.hide(); + default_language_status.hide(); + waiting_period_threshold_status.hide(); + + e.preventDefault(); + e.stopPropagation(); + + var new_name = $("#id_realm_name").val(); + var new_description = $("#id_realm_description").val(); + var new_restricted = $("#id_realm_restricted_to_domain").prop("checked"); + var new_invite = $("#id_realm_invite_required").prop("checked"); + var new_invite_by_admins_only = $("#id_realm_invite_by_admins_only").prop("checked"); + var new_inline_image_preview = $("#id_realm_inline_image_preview").prop("checked"); + var new_inline_url_embed_preview = $("#id_realm_inline_url_embed_preview").prop("checked"); + var new_create_stream_by_admins_only = $("#id_realm_create_stream_by_admins_only").prop("checked"); + var new_name_changes_disabled = $("#id_realm_name_changes_disabled").prop("checked"); + var new_email_changes_disabled = $("#id_realm_email_changes_disabled").prop("checked"); + var new_add_emoji_by_admins_only = $("#id_realm_add_emoji_by_admins_only").prop("checked"); + var new_allow_message_editing = $("#id_realm_allow_message_editing").prop("checked"); + var new_message_content_edit_limit_minutes = $("#id_realm_message_content_edit_limit_minutes").val(); + var new_message_retention_days = $("#id_realm_message_retention_days").val(); + var new_default_language = $("#id_realm_default_language").val(); + var new_waiting_period_threshold = $("#id_realm_waiting_period_threshold").val(); + var new_auth_methods = {}; + _.each($("#admin_auth_methods_table").find('tr.method_row'), function (method_row) { + new_auth_methods[$(method_row).data('method')] = $(method_row).find('input').prop('checked'); + }); + // If allow_message_editing is unchecked, message_content_edit_limit_minutes + // is irrelevant. Hence if allow_message_editing is unchecked, and + // message_content_edit_limit_minutes is poorly formed, we set the latter to + // a default value to prevent the server from returning an error. + if (!new_allow_message_editing) { + if ((parseInt(new_message_content_edit_limit_minutes, 10).toString() !== + new_message_content_edit_limit_minutes) || + new_message_content_edit_limit_minutes < 0) { + // Realm.DEFAULT_MESSAGE_CONTENT_EDIT_LIMIT_SECONDS / 60 + new_message_content_edit_limit_minutes = 10; + } + } + if (parseInt(new_message_retention_days, 10).toString() !== + new_message_retention_days && new_message_retention_days !== "") { + new_message_retention_days = ""; + } + + var url = "/json/realm"; + var data = { + name: JSON.stringify(new_name), + description: JSON.stringify(new_description), + restricted_to_domain: JSON.stringify(new_restricted), + invite_required: JSON.stringify(new_invite), + invite_by_admins_only: JSON.stringify(new_invite_by_admins_only), + inline_image_preview: JSON.stringify(new_inline_image_preview), + inline_url_embed_preview: JSON.stringify(new_inline_url_embed_preview), + authentication_methods: JSON.stringify(new_auth_methods), + create_stream_by_admins_only: JSON.stringify(new_create_stream_by_admins_only), + name_changes_disabled: JSON.stringify(new_name_changes_disabled), + email_changes_disabled: JSON.stringify(new_email_changes_disabled), + add_emoji_by_admins_only: JSON.stringify(new_add_emoji_by_admins_only), + allow_message_editing: JSON.stringify(new_allow_message_editing), + message_content_edit_limit_seconds: + JSON.stringify(parseInt(new_message_content_edit_limit_minutes, 10) * 60), + message_retention_days: new_message_retention_days !== "" ? JSON.stringify(parseInt(new_message_retention_days, 10)) : null, + default_language: JSON.stringify(new_default_language), + waiting_period_threshold: JSON.stringify(parseInt(new_waiting_period_threshold, 10)), + }; + + channel.patch({ + url: url, + data: data, + success: function (response_data) { + if (response_data.name !== undefined) { + ui_report.success(i18n.t("Name changed!"), name_status); + } + if (response_data.description !== undefined) { + ui_report.success(i18n.t("Description changed!"), description_status); + } + if (response_data.restricted_to_domain !== undefined) { + if (response_data.restricted_to_domain) { + ui_report.success(i18n.t("New user e-mails now restricted to certain domains!"), restricted_to_domain_status); + } else { + ui_report.success(i18n.t("New users may have arbitrary e-mails!"), restricted_to_domain_status); + } + } + if (response_data.invite_required !== undefined) { + if (response_data.invite_required) { + ui_report.success(i18n.t("New users must be invited by e-mail!"), invite_required_status); + } else { + ui_report.success(i18n.t("New users may sign up online!"), invite_required_status); + } + } + if (response_data.invite_by_admins_only !== undefined) { + if (response_data.invite_by_admins_only) { + ui_report.success(i18n.t("New users must be invited by an admin!"), invite_by_admins_only_status); + } else { + ui_report.success(i18n.t("Any user may now invite new users!"), invite_by_admins_only_status); + } + } + if (response_data.inline_image_preview !== undefined) { + if (response_data.inline_image_preview) { + ui_report.success(i18n.t("Previews of uploaded and linked images will be shown!"), inline_image_preview_status); + } else { + ui_report.success(i18n.t("Previews of uploaded and linked images will not be shown!"), inline_image_preview_status); + } + } + if (response_data.inline_url_embed_preview !== undefined) { + if (response_data.inline_url_embed_preview) { + ui_report.success(i18n.t("Previews for linked websites will be shown!"), inline_url_embed_preview_status); + } else { + ui_report.success(i18n.t("Previews for linked websites will not be shown!"), inline_url_embed_preview_status); + } + } + if (response_data.create_stream_by_admins_only !== undefined) { + if (response_data.create_stream_by_admins_only) { + ui_report.success(i18n.t("Only administrators may now create new streams!"), create_stream_by_admins_only_status); + } else { + ui_report.success(i18n.t("Any user may now create new streams!"), create_stream_by_admins_only_status); + } + } + if (response_data.name_changes_disabled !== undefined) { + if (response_data.name_changes_disabled) { + ui_report.success(i18n.t("Users cannot change their name!"), name_changes_disabled_status); + } else { + ui_report.success(i18n.t("Users may now change their name!"), name_changes_disabled_status); + } + } + if (response_data.email_changes_disabled !== undefined) { + if (response_data.email_changes_disabled) { + ui_report.success(i18n.t("Users cannot change their email!"), email_changes_disabled_status); + } else { + ui_report.success(i18n.t("Users may now change their email!"), email_changes_disabled_status); + } + } + if (response_data.add_emoji_by_admins_only !== undefined) { + if (response_data.add_emoji_by_admins_only) { + ui_report.success(i18n.t("Only administrators may now add new emoji!"), add_emoji_by_admins_only_status); + } else { + ui_report.success(i18n.t("Any user may now add new emoji!"), add_emoji_by_admins_only_status); + } + } + if (response_data.authentication_methods !== undefined) { + if (response_data.authentication_methods) { + ui_report.success(i18n.t("Authentication methods saved!"), authentication_methods_status); + } + } + if (response_data.allow_message_editing !== undefined) { + // We expect message_content_edit_limit_seconds was sent in the + // response as well + var data_message_content_edit_limit_minutes = + Math.ceil(response_data.message_content_edit_limit_seconds / 60); + if (response_data.allow_message_editing) { + if (response_data.message_content_edit_limit_seconds > 0) { + ui_report.success(i18n.t("Users can now edit topics for all their messages," + +" and the content of messages which are less than __num_minutes__ minutes old.", + {num_minutes : + data_message_content_edit_limit_minutes}), + message_editing_status); + } else { + ui_report.success(i18n.t("Users can now edit the content and topics of all their past messages!"), message_editing_status); + } + } else { + ui_report.success(i18n.t("Users can no longer edit their past messages!"), message_editing_status); + } + // message_content_edit_limit_seconds could have been changed earlier + // in this function, so update the field just in case + $("#id_realm_message_content_edit_limit_minutes").val(data_message_content_edit_limit_minutes); + } + if (response_data.default_language !== undefined) { + if (response_data.default_language) { + ui_report.success(i18n.t("Default language changed!"), default_language_status); + } + } + if (response_data.waiting_period_threshold !== undefined) { + if (response_data.waiting_period_threshold > 0) { + ui_report.success(i18n.t("Waiting period threshold changed!"), waiting_period_threshold_status); + } + } + // Check if no changes made + var no_changes_made = true; + for (var key in response_data) { + if (['msg', 'result'].indexOf(key) < 0) { + no_changes_made = false; + } + } + if (no_changes_made) { + ui_report.success(i18n.t("No changes to save!"), name_status); + } + }, + error: function (xhr) { + var reason = $.parseJSON(xhr.responseText).reason; + if (reason === "no authentication") { + ui_report.error(i18n.t("Failed!"), xhr, authentication_methods_status); + } else { + ui_report.error(i18n.t("Failed!"), xhr, name_status); + } + }, + }); + }); + + $("#realm_domains_table").on("click", ".delete_realm_domain", function () { + var domain = $(this).parents("tr").find(".domain").text(); + var url = "/json/realm/domains/" + domain; + var realm_domains_info = $("#realm_domains_modal").find(".realm_domains_info"); + + channel.del({ + url: url, + success: function () { + realm_domains_info.removeClass("text-error"); + realm_domains_info.addClass("text-success"); + realm_domains_info.text(i18n.t("Deleted successfully!")); + }, + error: function (xhr) { + realm_domains_info.removeClass("text-success"); + realm_domains_info.addClass("text-error"); + realm_domains_info.text(JSON.parse(xhr.responseText).msg); + }, + }); + }); + + $("#submit-add-realm-domain").click(function () { + var realm_domains_info = $("#realm_domains_modal").find(".realm_domains_info"); + var widget = $("#add-realm-domain-widget"); + var domain = widget.find(".new-realm-domain").val(); + var allow_subdomains = widget.find(".new-realm-domain-allow-subdomains").prop("checked"); + var data = { + domain: JSON.stringify(domain), + allow_subdomains: JSON.stringify(allow_subdomains), + }; + + channel.post({ + url: "/json/realm/domains", + data: data, + success: function () { + $("#add-realm-domain-widget .new-realm-domain").val(""); + $("#add-realm-domain-widget .new-realm-domain-allow-subdomains").prop("checked", false); + $("#id_realm_restricted_to_domain").prop("disabled", false); + realm_domains_info.removeClass("text-error"); + realm_domains_info.addClass("text-success"); + realm_domains_info.text(i18n.t("Added successfully!")); + }, + error: function (xhr) { + realm_domains_info.removeClass("text-success"); + realm_domains_info.addClass("text-error"); + realm_domains_info.text(JSON.parse(xhr.responseText).msg); + }, + }); + }); + + $("#realm_domains_table").on("change", ".allow-subdomains", function (e) { + e.stopPropagation(); + var realm_domains_info = $("#realm_domains_modal").find(".realm_domains_info"); + var domain = $(this).parents("tr").find(".domain").text(); + var allow_subdomains = $(this).prop('checked'); + var url = '/json/realm/domains/' + domain; + var data = { + allow_subdomains: JSON.stringify(allow_subdomains), + }; + + channel.patch({ + url: url, + data: data, + success: function () { + realm_domains_info.removeClass("text-error"); + realm_domains_info.addClass("text-success"); + if (allow_subdomains) { + realm_domains_info.text(i18n.t("Update successful: Subdomains allowed for __domain__", + {domain: domain})); + } else { + realm_domains_info.text(i18n.t("Update successful: Subdomains no longer allowed for __domain__", + {domain: domain})); + } + }, + error: function (xhr) { + realm_domains_info.removeClass("text-success"); + realm_domains_info.addClass("text-error"); + realm_domains_info.text(JSON.parse(xhr.responseText).msg); + }, + }); + }); + + function upload_realm_icon(file_input) { + var form_data = new FormData(); + + form_data.append('csrfmiddlewaretoken', csrf_token); + jQuery.each(file_input[0].files, function (i, file) { + form_data.append('file-'+i, file); + }); + + var spinner = $("#upload_icon_spinner").expectOne(); + loading.make_indicator(spinner, {text: i18n.t("Uploading icon.")}); + + channel.put({ + url: '/json/realm/icon', + data: form_data, + cache: false, + processData: false, + contentType: false, + success: function () { + loading.destroy_indicator($("#upload_icon_spinner")); + }, + }); + + } + realm_icon.build_realm_icon_widget(upload_realm_icon); + +}; + +return exports; +}()); + +if (typeof module !== 'undefined') { + module.exports = settings_org; +} diff --git a/zproject/settings.py b/zproject/settings.py index 3a814c77eb..21d9c5bebf 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -914,6 +914,7 @@ JS_SPECS = { 'js/settings_lab.js', 'js/settings_sections.js', 'js/settings_emoji.js', + 'js/settings_org.js', 'js/settings.js', 'js/admin.js', 'js/tab_bar.js',