UIWindowSessionList.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 UIWindow from './UIWindow.js'
  20. import UIWindowLogin from './UIWindowLogin.js'
  21. import UIWindowSignup from './UIWindowSignup.js'
  22. async function UIWindowSessionList(options){
  23. options = options ?? {};
  24. options.reload_on_success = options.reload_on_success ?? true;
  25. return new Promise(async (resolve) => {
  26. let h = '';
  27. h += `<div style="margin:10px;">`;
  28. h += `<div class="loading">${i18n('signing_in')}</div>`
  29. h += `<div style="overflow-y: scroll; max-width: 400px; margin: 0 auto;">`;
  30. h += `<h1 style="text-align: center; font-size: 18px; font-weight: normal; color: #757575;"><img src="${icons['logo-white.svg']}" style="padding: 4px; background-color: blue; border-radius: 5px; width: 25px; box-sizing: border-box; margin-bottom: -6px; margin-right: 6px;">${i18n('sign_in_with_puter')}</h1>`
  31. for (let index = 0; index < logged_in_users.length; index++) {
  32. const l_user = logged_in_users[index];
  33. h += `<div data-uuid="${l_user.uuid}" class="session-entry">${l_user.username}</div>`;
  34. }
  35. h += `</div>`;
  36. h += `<div style="margin-top: 20px; margin-bottom: 20px; text-align:center;"><span class="login-c2a-session-list">Log Into Another Account</span> &bull; <span class="signup-c2a-session-list">${i18n('create_account')}</span></div>`;
  37. h += `</div>`;
  38. const el_window = await UIWindow({
  39. title: 'Session List!',
  40. app: 'session-list',
  41. single_instance: true,
  42. icon: null,
  43. uid: null,
  44. is_dir: false,
  45. body_content: h,
  46. has_head: false,
  47. selectable_body: false,
  48. draggable_body: options.draggable_body ?? true,
  49. allow_context_menu: false,
  50. is_resizable: false,
  51. is_droppable: false,
  52. init_center: true,
  53. allow_native_ctxmenu: false,
  54. allow_user_select: false,
  55. width: 350,
  56. height: 'auto',
  57. dominant: true,
  58. show_in_taskbar: false,
  59. update_window_url: false,
  60. cover_page: options.cover_page ?? false,
  61. onAppend: function(this_window){
  62. },
  63. window_class: 'window-session-list',
  64. body_css: {
  65. width: 'initial',
  66. height: '100%',
  67. 'background-color': 'rgb(245 247 249)',
  68. 'backdrop-filter': 'blur(3px)',
  69. 'display': 'flex',
  70. 'flex-direction': 'column',
  71. 'justify-content': 'center',
  72. }
  73. })
  74. $(el_window).find('.login-c2a-session-list').on('click', async function(e){
  75. const login = await UIWindowLogin({
  76. referrer: options.referrer,
  77. reload_on_success: options.reload_on_success,
  78. cover_page: options.cover_page ?? false,
  79. has_head: options.has_head,
  80. send_confirmation_code: options.send_confirmation_code,
  81. window_options: {
  82. has_head: false,
  83. cover_page: options.cover_page ?? false,
  84. }
  85. });
  86. if(login){
  87. if(options.reload_on_success){
  88. // disable native browser exit confirmation
  89. window.onbeforeunload = null;
  90. // refresh
  91. location.reload();
  92. }else{
  93. resolve(login);
  94. }
  95. }
  96. })
  97. $(el_window).find('.signup-c2a-session-list').on('click', async function(e){
  98. $('.signup-c2a-clickable').parents('.window').close();
  99. // create Signup window
  100. const signup = await UIWindowSignup({
  101. referrer: options.referrer,
  102. reload_on_success: options.reload_on_success,
  103. send_confirmation_code: options.send_confirmation_code,
  104. window_options: {
  105. has_head: false,
  106. cover_page: options.cover_page ?? false,
  107. }
  108. });
  109. if(signup){
  110. if(options.reload_on_success){
  111. // disable native browser exit confirmation
  112. window.onbeforeunload = null;
  113. // refresh
  114. location.reload();
  115. }else{
  116. resolve(signup);
  117. }
  118. }
  119. })
  120. $(el_window).find('.session-entry').on('click', function(e){
  121. $(el_window).find('.loading').css({display: 'flex'});
  122. setTimeout(() => {
  123. let selected_uuid = $(this).attr('data-uuid');
  124. let selected_user;
  125. for (let index = 0; index < window.logged_in_users.length; index++) {
  126. const l_user = logged_in_users[index];
  127. if(l_user.uuid === selected_uuid){
  128. selected_user = l_user;
  129. }
  130. }
  131. // new logged in user
  132. update_auth_data(selected_user.auth_token, selected_user);
  133. if(options.reload_on_success){
  134. // disable native browser exit confirmation
  135. window.onbeforeunload = null;
  136. // refresh
  137. location.reload();
  138. }else{
  139. resolve(true);
  140. }
  141. }, 500);
  142. })
  143. })
  144. }
  145. export default UIWindowSessionList