gui.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. window.gui_env="prod";
  2. window.puter_gui_enabled = true;
  3. /**
  4. * Initializes and configures the GUI (Graphical User Interface) settings based on the provided options.
  5. *
  6. * The function sets global variables in the window object for various settings such as origins and domain names.
  7. * It also handles loading different resources depending on the environment (development or production).
  8. *
  9. * @param {Object} options - Configuration options to initialize the GUI.
  10. * @param {string} [options.gui_origin='https://puter.com'] - The origin URL for the GUI.
  11. * @param {string} [options.api_origin='https://api.puter.com'] - The origin URL for the API.
  12. * @param {number} [options.max_item_name_length=500] - Maximum allowed length for an item name.
  13. * @param {boolean} [options.require_email_verification_to_publish_website=true] - Flag to decide whether email verification is required to publish a website.
  14. *
  15. * @property {string} [options.app_domain] - Extracted domain name from gui_origin. It's derived automatically if not provided.
  16. * @property {string} [window.gui_env] - The environment in which the GUI is running (e.g., "dev" or "prod").
  17. *
  18. * @returns {Promise<void>} Returns a promise that resolves when initialization and resource loading are complete.
  19. *
  20. * @example
  21. * window.gui({
  22. * gui_origin: 'https://myapp.com',
  23. * api_origin: 'https://myapi.com',
  24. * max_item_name_length: 250
  25. * });
  26. */
  27. window.gui = async function(options){
  28. options = options ?? {};
  29. // app_origin is deprecated, use gui_origin instead
  30. window.gui_origin = options.gui_origin ?? options.app_origin ?? `https://puter.com`;
  31. window.app_domain = options.app_domain ?? new URL(window.gui_origin).hostname;
  32. window.hosting_domain = options.hosting_domain ?? 'puter.site';
  33. window.api_origin = options.api_origin ?? "https://api.puter.com";
  34. window.max_item_name_length = options.max_item_name_length ?? 500;
  35. window.require_email_verification_to_publish_website = options.require_email_verification_to_publish_website ?? true;
  36. // Add Puter.JS
  37. await loadScript('https://js.puter.com/v2/');
  38. // DEV: Load the initgui.js file if we are in development mode
  39. if(!window.gui_env || window.gui_env === "dev"){
  40. await loadScript('/initgui.js', {isModule: true});
  41. }
  42. // PROD: load the minified bundles if we are in production mode
  43. // note: the order of the bundles is important
  44. // note: Build script will prepend `window.gui_env="prod"` to the top of the file
  45. else if(gui_env === "prod"){
  46. // Load the minified bundles
  47. await loadCSS('/dist/bundle.min.css');
  48. await loadScript('/dist/bundle.min.js');
  49. }
  50. // 🚀 Launch the GUI 🚀
  51. initgui();
  52. }
  53. /**
  54. * Dynamically loads an external JavaScript file.
  55. * @param {string} url The URL of the external script to load.
  56. * @param {Object} [options] Optional configuration for the script.
  57. * @param {boolean} [options.isModule] Whether the script is a module.
  58. * @param {boolean} [options.defer] Whether the script should be deferred.
  59. * @param {Object} [options.dataAttributes] An object containing data attributes to add to the script element.
  60. * @returns {Promise} A promise that resolves once the script has loaded, or rejects on error.
  61. */
  62. window.loadScript = async function(url, options = {}) {
  63. return new Promise((resolve, reject) => {
  64. const script = document.createElement('script');
  65. script.src = url;
  66. // Set default script loading behavior
  67. script.async = true;
  68. // Handle if it is a module
  69. if (options.isModule) {
  70. script.type = 'module';
  71. }
  72. // Handle defer attribute
  73. if (options.defer) {
  74. script.defer = true;
  75. script.async = false; // When "defer" is true, "async" should be false as they are mutually exclusive
  76. }
  77. // Add arbitrary data attributes
  78. if (options.dataAttributes && typeof options.dataAttributes === 'object') {
  79. for (const [key, value] of Object.entries(options.dataAttributes)) {
  80. script.setAttribute(`data-${key}`, value);
  81. }
  82. }
  83. // Resolve the promise when the script is loaded
  84. script.onload = () => resolve();
  85. // Reject the promise if there's an error during load
  86. script.onerror = (error) => reject(new Error(`Failed to load script at url: ${url}`));
  87. // Append the script to the body
  88. document.body.appendChild(script);
  89. });
  90. };
  91. /**
  92. * Dynamically loads an external CSS file.
  93. * @param {string} url The URL of the external CSS to load.
  94. * @returns {Promise} A promise that resolves once the CSS has loaded, or rejects on error.
  95. */
  96. window.loadCSS = async function(url) {
  97. return new Promise((resolve, reject) => {
  98. const link = document.createElement('link');
  99. link.rel = 'stylesheet';
  100. link.href = url;
  101. link.onload = () => {
  102. resolve();
  103. };
  104. link.onerror = (error) => {
  105. reject(new Error(`Failed to load CSS at url: ${url}`));
  106. };
  107. document.head.appendChild(link);
  108. });
  109. }