diff --git a/app/assets/javascripts/preferences.js b/app/assets/javascripts/preferences.js new file mode 100644 index 0000000000..977a586e13 --- /dev/null +++ b/app/assets/javascripts/preferences.js @@ -0,0 +1,32 @@ +$(document).ready(function () { + $("#available_languages_menu").prop("hidden", false); + $("#user_languages, #available_languages").on("input", updateButtons); + updateButtons(); + + $("#add_preferred_language").click(function () { + var preferredLanguagesValue = $("#user_languages").val().trim(); + var selectedLanguage = $("#available_languages").val(); + $("#user_languages").val(selectedLanguage + " " + preferredLanguagesValue); + updateButtons(); + $("#remove_preferred_language").trigger("focus"); + }); + + $("#remove_preferred_language").click(function () { + var preferredLanguages = $("#user_languages").val().trim().split(/\s+/); + var selectedLanguage = $("#available_languages").val(); + var updatedPreferredLanguages = preferredLanguages.filter(function (language) { + return language !== selectedLanguage; + }); + $("#user_languages").val(updatedPreferredLanguages.join(" ")); + updateButtons(); + $("#add_preferred_language").trigger("focus"); + }); + + function updateButtons() { + var preferredLanguages = $("#user_languages").val().trim().split(/\s+/); + var selectedLanguage = $("#available_languages").val(); + var languageAlreadyAdded = preferredLanguages.includes(selectedLanguage); + $("#add_preferred_language").prop("hidden", languageAlreadyAdded); + $("#remove_preferred_language").prop("hidden", !languageAlreadyAdded); + } +}); diff --git a/app/views/preferences/edit.html.erb b/app/views/preferences/edit.html.erb index 07d89fbb5b..7051e7acaa 100644 --- a/app/views/preferences/edit.html.erb +++ b/app/views/preferences/edit.html.erb @@ -1,3 +1,7 @@ +<% content_for :head do %> + <%= javascript_include_tag "preferences" %> +<% end %> + <% content_for :heading do %>