UIWindowPublishWebsite.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 UIWindowMyWebsites from './UIWindowMyWebsites.js'
  21. async function UIWindowPublishWebsite(target_dir_uid, target_dir_name, target_dir_path){
  22. let h = '';
  23. h += `<div class="window-publishWebsite-content" style="padding: 20px; border-bottom: 1px solid #ced7e1;">`;
  24. // success
  25. h += `<div class="window-publishWebsite-success">`;
  26. h += `<img src="${html_encode(window.icons['c-check.svg'])}" style="width:80px; height:80px; display: block; margin:10px auto;">`;
  27. h += `<p style="text-align:center;">${i18n('dir_published_as_website', `<strong>${target_dir_name}</strong>`)}<p>`;
  28. h += `<p style="text-align:center;"><a class="publishWebsite-published-link" target="_blank"></a><img class="publishWebsite-published-link-icon" src="${html_encode(window.icons['launch.svg'])}"></p>`;
  29. h += `<button class="button button-normal button-block button-primary publish-window-ok-btn" style="margin-top:20px;">OK</button>`;
  30. h+= `</div>`;
  31. // form
  32. h += `<form class="window-publishWebsite-form">`;
  33. // error msg
  34. h += `<div class="publish-website-error-msg"></div>`;
  35. // subdomain
  36. h += `<div style="overflow: hidden;">`;
  37. h += `<label style="margin-bottom: 10px;">${i18n('pick_name_for_website')}</label>`;
  38. h += `<div style="font-family: monospace;">https://<input class="publish-website-subdomain" style="width:235px;" type="text" autocomplete="subdomain" spellcheck="false" autocorrect="off" autocapitalize="off" data-gramm_editor="false"/>.${window.hosting_domain}</div>`;
  39. h += `</div>`;
  40. // uid
  41. h += `<input class="publishWebsiteTargetDirUID" type="hidden" value="${target_dir_uid}"/>`;
  42. // Publish
  43. h += `<button class="publish-btn button button-action button-block button-normal">${i18n('publish')}</button>`
  44. h += `</form>`;
  45. h += `</div>`;
  46. const el_window = await UIWindow({
  47. title: 'Publish Website',
  48. icon: null,
  49. uid: null,
  50. is_dir: false,
  51. body_content: h,
  52. has_head: true,
  53. selectable_body: false,
  54. draggable_body: false,
  55. allow_context_menu: false,
  56. is_resizable: false,
  57. is_droppable: false,
  58. init_center: true,
  59. allow_native_ctxmenu: true,
  60. allow_user_select: true,
  61. width: 450,
  62. dominant: true,
  63. onAppend: function(this_window){
  64. $(this_window).find(`.publish-website-subdomain`).val(generate_identifier());
  65. $(this_window).find(`.publish-website-subdomain`).get(0).focus({preventScroll:true});
  66. },
  67. window_class: 'window-publishWebsite',
  68. window_css:{
  69. height: 'initial'
  70. },
  71. body_css: {
  72. width: 'initial',
  73. height: '100%',
  74. 'background-color': 'rgb(245 247 249)',
  75. 'backdrop-filter': 'blur(3px)',
  76. }
  77. })
  78. $(el_window).find('.publish-btn').on('click', function(e){
  79. // todo do some basic validation client-side
  80. //Subdomain
  81. let subdomain = $(el_window).find('.publish-website-subdomain').val();
  82. // disable 'Publish' button
  83. $(el_window).find('.publish-btn').prop('disabled', true);
  84. puter.hosting.create(
  85. subdomain,
  86. target_dir_path).then((res)=>{
  87. $(el_window).find('.window-publishWebsite-form').hide(100, function(){
  88. let url = 'https://' + subdomain + '.' + window.hosting_domain + '/';
  89. $(el_window).find('.publishWebsite-published-link').attr('href', url);
  90. $(el_window).find('.publishWebsite-published-link').text(url);
  91. $(el_window).find('.window-publishWebsite-success').show(100)
  92. $(`.item[data-uid="${target_dir_uid}"] .item-has-website-badge`).show();
  93. });
  94. // find all items whose path starts with target_dir_path
  95. $(`.item[data-path^="${target_dir_path}"]`).each(function(){
  96. // show the link badge
  97. $(this).find('.item-has-website-url-badge').show();
  98. // update item's website_url attribute
  99. $(this).attr('data-website_url', url + $(this).attr('data-path').substring(target_dir_path.length));
  100. })
  101. update_sites_cache();
  102. }).catch((err)=>{
  103. $(el_window).find('.publish-website-error-msg').html(
  104. err.message + (
  105. err.code === 'subdomain_limit_reached' ?
  106. ' <span class="manage-your-websites-link">Manage Your Subdomains</span>' : ''
  107. )
  108. );
  109. $(el_window).find('.publish-website-error-msg').fadeIn();
  110. // re-enable 'Publish' button
  111. $(el_window).find('.publish-btn').prop('disabled', false);
  112. })
  113. })
  114. $(el_window).find('.publish-window-ok-btn').on('click', function(){
  115. $(el_window).close();
  116. })
  117. }
  118. $(document).on('click', '.manage-your-websites-link', async function(e){
  119. UIWindowMyWebsites();
  120. })
  121. export default UIWindowPublishWebsite