UITabLanguage.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import UIWindowThemeDialog from '../UIWindowThemeDialog.js';
  20. import changeLanguage from '../../i18n/i18nChangeLanguage.js';
  21. // About
  22. export default {
  23. id: 'language',
  24. title_i18n_key: 'language',
  25. icon: 'language.svg',
  26. html: () => {
  27. let h = `<h1>${i18n('language')}</h1>`;
  28. // search
  29. h += `<div class="search-container" style="margin-bottom: 10px;">
  30. <input type="text" class="search search-language" placeholder="Search">
  31. </div>`;
  32. // list of languages
  33. const available_languages = listSupportedLanguages();
  34. h += `<div class="language-list">`;
  35. for (let lang of available_languages) {
  36. h += `<div class="language-item ${window.locale === lang.code ? 'active': ''}" data-lang="${lang.code}" data-english-name="${html_encode(lang.english_name)}">${lang.name}</div>`;
  37. }
  38. h += `</div>`;
  39. return h;
  40. },
  41. init: ($el_window) => {
  42. $el_window.on('click', '.language-item', function(){
  43. const $this = $(this);
  44. const lang = $this.attr('data-lang');
  45. changeLanguage(lang);
  46. $this.siblings().removeClass('active');
  47. $this.addClass('active');
  48. // make sure all other language items are visible
  49. $this.closest('.language-list').find('.language-item').show();
  50. });
  51. $el_window.on('input', '.search-language', function(){
  52. const $this = $(this);
  53. const search = $this.val().toLowerCase();
  54. const $container = $this.closest('.settings').find('.settings-content-container');
  55. const $content = $container.find('.settings-content.active');
  56. const $list = $content.find('.language-list');
  57. const $items = $list.find('.language-item');
  58. $items.each(function(){
  59. const $item = $(this);
  60. const lang = $item.attr('data-lang');
  61. const name = $item.text().toLowerCase();
  62. const english_name = $item.attr('data-english-name').toLowerCase();
  63. if(name.includes(search) || lang.includes(search) || english_name.includes(search)){
  64. $item.show();
  65. }else{
  66. $item.hide();
  67. }
  68. })
  69. });
  70. },
  71. on_show: ($content) => {
  72. // Focus on search
  73. $content.find('.search').first().focus();
  74. // make sure all language items are visible
  75. $content.find('.language-item').show();
  76. // empty search
  77. $content.find('.search').val('');
  78. },
  79. };