UIWindowSettings.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Placeholder from '../../util/Placeholder.js';
  20. import UIWindow from '../UIWindow.js'
  21. async function UIWindowSettings(options){
  22. return new Promise(async (resolve) => {
  23. options = options ?? {};
  24. const svc_settings = globalThis.services.get('settings');
  25. const tabs = svc_settings.get_tabs();
  26. const tab_placeholders = [];
  27. let h = '';
  28. h += `<div class="settings-container">`;
  29. h += `<div class="settings">`;
  30. // side bar
  31. h += `<div class="settings-sidebar disable-user-select disable-context-menu">`;
  32. tabs.forEach((tab, i) => {
  33. h += `<div class="settings-sidebar-item disable-context-menu disable-user-select ${i === 0 ? 'active' : ''}" data-settings="${tab.id}" style="background-image: url(${window.icons[tab.icon]});">${i18n(tab.title_i18n_key)}</div>`;
  34. });
  35. h += `</div>`;
  36. // content
  37. h += `<div class="settings-content-container">`;
  38. tabs.forEach((tab, i) => {
  39. h += `<div class="settings-content ${i === 0 ? 'active' : ''}" data-settings="${tab.id}">`;
  40. if ( tab.factory ) {
  41. tab_placeholders[i] = Placeholder();
  42. h += tab_placeholders[i].html;
  43. } else {
  44. h += tab.html();
  45. }
  46. h += `</div>`;
  47. });
  48. h += `</div>`;
  49. h += `</div>`;
  50. h += `</div>`;
  51. h += ``;
  52. const el_window = await UIWindow({
  53. title: 'Settings',
  54. app: 'settings',
  55. single_instance: true,
  56. icon: null,
  57. uid: null,
  58. is_dir: false,
  59. body_content: h,
  60. has_head: true,
  61. selectable_body: false,
  62. allow_context_menu: false,
  63. is_resizable: false,
  64. is_droppable: false,
  65. init_center: true,
  66. allow_native_ctxmenu: true,
  67. allow_user_select: true,
  68. backdrop: false,
  69. width: 800,
  70. height: 'auto',
  71. dominant: true,
  72. show_in_taskbar: false,
  73. draggable_body: false,
  74. onAppend: function(this_window){
  75. },
  76. window_class: 'window-settings',
  77. body_css: {
  78. width: 'initial',
  79. height: '100%',
  80. overflow: 'auto'
  81. }
  82. });
  83. const $el_window = $(el_window);
  84. tabs.forEach((tab, i) => {
  85. tab.init && tab.init($el_window);
  86. if ( tab.factory ) {
  87. const component = tab.factory();
  88. component.attach(tab_placeholders[i]);
  89. }
  90. });
  91. $(el_window).on('click', '.settings-sidebar-item', function(){
  92. const $this = $(this);
  93. const settings = $this.attr('data-settings');
  94. const $container = $this.closest('.settings').find('.settings-content-container');
  95. const $content = $container.find(`.settings-content[data-settings="${settings}"]`);
  96. // add active class to sidebar item
  97. $this.siblings().removeClass('active');
  98. $this.addClass('active');
  99. // add active class to content
  100. $container.find('.settings-content').removeClass('active');
  101. $content.addClass('active');
  102. // Run on_show handlers
  103. const tab = tabs.find((tab) => tab.id === settings);
  104. if (tab.on_show) {
  105. tab.on_show($content);
  106. }
  107. })
  108. resolve(el_window);
  109. });
  110. }
  111. export default UIWindowSettings