compose.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html>
  2. <html lang="en" data-theme="light">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>New message</title>
  7. <link rel="icon" href="img/icon.svg">
  8. <link rel="stylesheet" href="../script/semantic/components/icon.min.css">
  9. <link rel="stylesheet" href="style/mail.css">
  10. <script src="../script/jquery.min.js"></script>
  11. <script src="../script/ao_module.js"></script>
  12. </head>
  13. <body class="window-page">
  14. <div class="compose-host" id="composeHost">
  15. <div class="reader-loading"><div class="spinner large"></div></div>
  16. </div>
  17. <script src="script/util.js"></script>
  18. <script src="script/api.js"></script>
  19. <script src="script/ui.js"></script>
  20. <script src="script/shared.js"></script>
  21. <script src="script/compose.js"></script>
  22. <script>
  23. /*
  24. Mail — composer window
  25. Every new message, reply, forward and reopened draft gets a float
  26. window of its own. The message to start with arrives through a
  27. storage hand-off (see Mail.shared.openComposeWindow), because a
  28. quoted message is often too large for a URL.
  29. Closing the window saves the message to Drafts first. A browser tab
  30. that is closed without that chance sends what it has as a beacon.
  31. */
  32. (function () {
  33. "use strict";
  34. var util = Mail.util;
  35. var ui = Mail.ui;
  36. var api = Mail.api;
  37. var shared = Mail.shared;
  38. var el = util.el;
  39. var icon = util.icon;
  40. var host = document.getElementById("composeHost");
  41. var accounts = [];
  42. var settings = {};
  43. var closing = false;
  44. var finished = false;
  45. var applyTheme = shared.followTheme(function () { return settings.theme; });
  46. function setTitle(title) {
  47. if (typeof ao_module_setWindowTitle === "function") {
  48. ao_module_setWindowTitle(title || "New message");
  49. } else {
  50. document.title = title || "New message";
  51. }
  52. }
  53. //closeWindow closes the window once the composer is done with it
  54. function closeWindow() {
  55. finished = true;
  56. ao_module_closeHandler();
  57. }
  58. //The title bar's close button lands here: save the draft, then close
  59. window.ao_module_close = function () {
  60. var composer = Mail.compose.current();
  61. if (!composer || finished) {
  62. closeWindow();
  63. return;
  64. }
  65. if (closing) {
  66. return;
  67. }
  68. closing = true;
  69. composer.close().then(function (ok) {
  70. closing = false;
  71. if (ok) {
  72. closeWindow();
  73. }
  74. }, function () {
  75. closing = false;
  76. });
  77. };
  78. //Last resort when the window goes away without asking (a browser
  79. //tab, a reload): hand the unsaved message to the server
  80. window.addEventListener("pagehide", function () {
  81. var composer = Mail.compose.current();
  82. var draft = composer && !finished ? composer.unsavedDraft() : null;
  83. if (!draft || !navigator.sendBeacon) {
  84. return;
  85. }
  86. var root = (typeof ao_root === "string" && ao_root) ? ao_root : "../";
  87. navigator.sendBeacon(root + "system/ajgi/interface?script=Mail/backend/compose.agi", new URLSearchParams({
  88. opr: "draft",
  89. data: JSON.stringify({ message: draft })
  90. }));
  91. });
  92. function showProblem(title, text, withMailButton) {
  93. util.clear(host);
  94. host.appendChild(el("div", { class: "reader-empty" }, [
  95. icon("exclamation triangle"),
  96. el("div", { class: "title", text: title }),
  97. el("div", { text: text }),
  98. withMailButton ? el("button", {
  99. class: "btn primary", on: {
  100. click: function () {
  101. ao_module_newfw({ url: "Mail/index.html", width: 1280, height: 800, appicon: "Mail/img/icon.svg", title: "Mail" });
  102. closeWindow();
  103. }
  104. }
  105. }, [icon("envelope outline"), "Open Mail"]) : null
  106. ]));
  107. }
  108. //startOptions reads what to write from the launch hash
  109. function startOptions() {
  110. var hash = (window.location.hash || "").replace(/^#/, "");
  111. var options = null;
  112. if (hash.indexOf("handoff=") === 0) {
  113. options = shared.takeHandOff(hash.slice(8));
  114. } else if (/^mailto:/i.test(decodeURIComponent(hash))) {
  115. options = Mail.compose.mailtoOptions(decodeURIComponent(hash), "");
  116. } else if (hash.indexOf("compose=") === 0) {
  117. try {
  118. options = JSON.parse(decodeURIComponent(hash.slice(8)));
  119. } catch (e) { /* malformed */ }
  120. }
  121. //A reload must not start the same message twice
  122. try {
  123. history.replaceState(null, "", window.location.pathname + window.location.search);
  124. } catch (e) { /* ignore */ }
  125. return options || {};
  126. }
  127. var options = startOptions();
  128. if (!api.available) {
  129. showProblem("Not connected", "Open Mail from the ArozOS desktop.", false);
  130. return;
  131. }
  132. Promise.all([
  133. api.accounts("list", {}),
  134. api.settings("get", {}).catch(function () { return {}; })
  135. ]).then(function (results) {
  136. accounts = results[0] || [];
  137. settings = results[1] || {};
  138. applyTheme();
  139. if (accounts.length === 0) {
  140. showProblem("No mail account", "Add a mail account in Mail before writing a message.", true);
  141. return;
  142. }
  143. util.clear(host);
  144. Mail.compose.init({
  145. host: host,
  146. context: {
  147. windowMode: true,
  148. accounts: function () { return accounts; },
  149. settings: function () { return settings; },
  150. defaultAccountId: function () {
  151. if (settings.defaultAccount && accounts.some(function (item) { return item.id === settings.defaultAccount; })) {
  152. return settings.defaultAccount;
  153. }
  154. return accounts[0].id;
  155. },
  156. onTitle: setTitle,
  157. closeWindow: closeWindow,
  158. onAuthFailed: function (accountId) {
  159. var item = accounts.filter(function (account) { return account.id === accountId; })[0];
  160. ui.toast("Sign in to " + (item ? item.email : "this account") + " again from the Mail window, then send the message.", { error: true, duration: 9000 });
  161. }
  162. }
  163. });
  164. setTitle(options.subject || "New message");
  165. Mail.compose.openHere(options);
  166. }).catch(function (error) {
  167. showProblem("Cannot write a message", error.message, false);
  168. });
  169. })();
  170. </script>
  171. </body>
  172. </html>