UIPrompt.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. function UIPrompt(options){
  21. // set sensible defaults
  22. if(arguments.length > 0){
  23. // if first argument is a string, then assume it is the message
  24. if(isString(arguments[0])){
  25. options = {};
  26. options.message = arguments[0];
  27. }
  28. // if second argument is an array, then assume it is the buttons
  29. if(arguments[1] && Array.isArray(arguments[1])){
  30. options.buttons = arguments[1];
  31. }
  32. }
  33. return new Promise(async (resolve) => {
  34. // provide an 'OK' button if no buttons are provided
  35. if(!options.buttons || options.buttons.length === 0){
  36. options.buttons = [
  37. {label: i18n('cancel'), value: false, type: 'default'},
  38. {label: i18n('ok'), value: true, type: 'primary'},
  39. ]
  40. }
  41. let h = '';
  42. // message
  43. h += `<div class="window-prompt-message">${options.message}</div>`;
  44. // prompt
  45. h += `<div class="window-alert-prompt" style="margin-top: 20px;">`;
  46. h += `<input type="text" class="prompt-input" placeholder="${options.placeholder ?? ''}" value="${options.value ?? ''}">`;
  47. h += `</div>`;
  48. // buttons
  49. if(options.buttons && options.buttons.length > 0){
  50. h += `<div style="overflow:hidden; margin-top:20px; float:right;">`;
  51. h += `<button class="button button-default prompt-resp-button prompt-resp-btn-cancel" data-label="${i18n('cancel')}" style="padding: 0 20px;">${i18n('cancel')}</button>`;
  52. h += `<button class="button button-primary prompt-resp-button prompt-resp-btn-ok" data-label="${i18n('ok')}" data-value="true" autofocus>${i18n('ok')}</button>`;
  53. h += `</div>`;
  54. }
  55. const el_window = await UIWindow({
  56. title: null,
  57. icon: null,
  58. uid: null,
  59. is_dir: false,
  60. message: options.message,
  61. backdrop: options.backdrop ?? false,
  62. is_resizable: false,
  63. is_droppable: false,
  64. has_head: false,
  65. stay_on_top: options.stay_on_top ?? false,
  66. selectable_body: false,
  67. draggable_body: true,
  68. allow_context_menu: false,
  69. show_in_taskbar: false,
  70. window_class: 'window-alert',
  71. dominant: true,
  72. body_content: h,
  73. width: 450,
  74. parent_uuid: options.parent_uuid,
  75. onAppend: function(this_window){
  76. setTimeout(function(){
  77. $(this_window).find('.prompt-input').get(0).focus({preventScroll:true});
  78. }, 30);
  79. },
  80. ...options.window_options,
  81. window_css:{
  82. height: 'initial',
  83. },
  84. body_css: {
  85. width: 'initial',
  86. padding: '20px',
  87. 'background-color': 'rgba(231, 238, 245, .95)',
  88. 'backdrop-filter': 'blur(3px)',
  89. }
  90. });
  91. // focus to primary btn
  92. $(el_window).find('.button-primary').focus();
  93. // --------------------------------------------------------
  94. // Button pressed
  95. // --------------------------------------------------------
  96. $(el_window).find('.prompt-resp-button').on('click', async function(event){
  97. event.preventDefault();
  98. event.stopPropagation();
  99. if($(this).attr('data-value') === 'true'){
  100. resolve($(el_window).find('.prompt-input').val());
  101. }else{
  102. resolve(false);
  103. }
  104. $(el_window).close();
  105. return false;
  106. })
  107. $(el_window).find('.prompt-input').on('keyup', async function(e){
  108. if(e.keyCode === 13){
  109. $(el_window).find('.prompt-resp-btn-ok').click();
  110. }
  111. })
  112. })
  113. }
  114. export default UIPrompt;