viewer.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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>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>
  14. <div class="viewer-app" id="viewer">
  15. <div class="viewer-bar">
  16. <div class="file"><i class="envelope outline icon"></i><span id="fileName">Message</span></div>
  17. <button class="btn small" id="replyButton" disabled><i class="reply icon"></i>Reply</button>
  18. <button class="btn small" id="replyAllButton" disabled><i class="reply all icon"></i>Reply all</button>
  19. <button class="btn small" id="forwardButton" disabled><i class="share icon"></i>Forward</button>
  20. <button class="iconbtn" id="moreButton" title="More" disabled><i class="ellipsis horizontal icon"></i></button>
  21. </div>
  22. <div class="reader-scroll scroll" id="content"></div>
  23. </div>
  24. <script src="script/util.js"></script>
  25. <script src="script/api.js"></script>
  26. <script src="script/ui.js"></script>
  27. <script src="script/render.js"></script>
  28. <script src="script/shared.js"></script>
  29. <script src="script/compose.js"></script>
  30. <script>
  31. /*
  32. Mail — .eml viewer
  33. Opened by ArozOS for .eml files. Shows the message with the same
  34. sanitiser and sandbox as the mailbox view; replying and forwarding
  35. open a composer window.
  36. */
  37. (function () {
  38. "use strict";
  39. var util = Mail.util;
  40. var ui = Mail.ui;
  41. var api = Mail.api;
  42. var render = Mail.render;
  43. var el = util.el;
  44. var icon = util.icon;
  45. var content = document.getElementById("content");
  46. var file = null;
  47. var message = null;
  48. var accounts = [];
  49. var settings = {};
  50. var applyTheme = Mail.shared.followTheme(function () { return settings.theme; });
  51. Mail.compose.init({
  52. host: document.body,
  53. context: {
  54. accounts: function () { return accounts; },
  55. settings: function () { return settings; },
  56. defaultAccountId: function () { return accounts.length > 0 ? accounts[0].id : ""; }
  57. }
  58. });
  59. function showError(text) {
  60. util.clear(content);
  61. content.appendChild(el("div", { class: "reader-empty" }, [icon("exclamation triangle"), el("div", { class: "title", text: "Cannot open this message" }), el("div", { text: text })]));
  62. }
  63. //handOver opens a composer window with a prepared message
  64. function handOver(options) {
  65. if (accounts.length === 0) {
  66. ui.toast("Add a mail account in Mail first", { error: true });
  67. return;
  68. }
  69. Mail.shared.openComposeWindow(options);
  70. }
  71. function withAttachmentsAsFiles(options) {
  72. if (!message.attachments || message.attachments.length === 0) {
  73. return Promise.resolve(options);
  74. }
  75. return api.files("tempFolder", { purpose: "uploads" }).then(function (folder) {
  76. return Promise.all(message.attachments.map(function (attachment) {
  77. return api.files("saveEmlAttachment", { path: file.filepath, partId: attachment.id, dest: folder }).then(function (saved) {
  78. return { kind: "file", path: saved.path, name: attachment.filename, size: attachment.size };
  79. });
  80. }));
  81. }).then(function (files) {
  82. options.attachments = files;
  83. return options;
  84. });
  85. }
  86. function reply(all) {
  87. var options = Mail.compose.replyOptions(message, all);
  88. options.originalFolder = "";
  89. options.originalUid = 0;
  90. handOver(options);
  91. }
  92. function forward() {
  93. var options = Mail.compose.forwardOptions(message, false);
  94. options.originalFolder = "";
  95. options.originalUid = 0;
  96. withAttachmentsAsFiles(options).then(handOver).catch(function (error) { ui.errorToast(error); });
  97. }
  98. function importTo(accountId) {
  99. api.files("importEml", { path: file.filepath, accountId: accountId, folder: "INBOX" }).then(function () {
  100. var target = accounts.filter(function (item) { return item.id === accountId; })[0];
  101. ui.toast("Imported into the inbox of " + (target ? target.email : "the account"));
  102. }).catch(function (error) { ui.errorToast(error, "Import failed"); });
  103. }
  104. function saveAttachment(attachment, dest) {
  105. return api.files("saveEmlAttachment", { path: file.filepath, partId: attachment.id, dest: dest });
  106. }
  107. function show(loaded, allowRemote) {
  108. message = loaded;
  109. message.accountId = accounts.length > 0 ? accounts[0].id : "";
  110. util.clear(content);
  111. content.appendChild(el("div", { class: "reader-subject" }, el("h1", { text: message.subject || "(no subject)" })));
  112. content.appendChild(render.header(message, {}));
  113. var notices = render.notices(message, {
  114. loadRemote: function () { load(true); }
  115. });
  116. if (notices) {
  117. content.appendChild(notices);
  118. }
  119. var body = el("div", { class: "reader-body" });
  120. content.appendChild(body);
  121. render.body(body, message, {
  122. onMailto: function (href) {
  123. handOver(Mail.compose.mailtoOptions(href, accounts.length > 0 ? accounts[0].id : ""));
  124. }
  125. });
  126. var attachments = render.attachments(message.attachments, {
  127. save: function (attachment) {
  128. ao_module_openFileSelector(function (files) {
  129. if (!files || files.length === 0) {
  130. return;
  131. }
  132. saveAttachment(attachment, files[0].filepath).then(function (saved) {
  133. ui.toast("Saved to " + saved.path);
  134. }).catch(function (error) { ui.errorToast(error); });
  135. }, "user:/Desktop", "folder", false, { path_memory_key: "mail-save" });
  136. },
  137. download: function (attachment) {
  138. api.files("tempFolder", { purpose: "downloads" }).then(function (folder) {
  139. return saveAttachment(attachment, folder);
  140. }).then(function (saved) {
  141. var link = el("a", { href: (ao_root || "../") + "media/?file=" + encodeURIComponent(saved.path) + "&download=true", download: saved.filename || "" });
  142. document.body.appendChild(link);
  143. link.click();
  144. setTimeout(function () { link.remove(); }, 1000);
  145. }).catch(function (error) { ui.errorToast(error); });
  146. }
  147. });
  148. if (attachments) {
  149. content.appendChild(attachments);
  150. }
  151. ["replyButton", "replyAllButton", "forwardButton", "moreButton"].forEach(function (id) {
  152. document.getElementById(id).disabled = false;
  153. });
  154. if (typeof ao_module_setWindowTitle === "function") {
  155. ao_module_setWindowTitle(message.subject || file.filename);
  156. }
  157. }
  158. function load(allowRemote) {
  159. content.appendChild(el("div", { class: "reader-loading" }, el("div", { class: "spinner large" })));
  160. api.files("openEml", { path: file.filepath, allowRemote: allowRemote === true }).then(function (result) {
  161. show(result.message, allowRemote);
  162. }).catch(function (error) {
  163. showError(error.message);
  164. });
  165. }
  166. document.getElementById("replyButton").addEventListener("click", function () { reply(false); });
  167. document.getElementById("replyAllButton").addEventListener("click", function () { reply(true); });
  168. document.getElementById("forwardButton").addEventListener("click", forward);
  169. document.getElementById("moreButton").addEventListener("click", function (event) {
  170. var items = [{ title: "Import into" }];
  171. accounts.forEach(function (item) {
  172. items.push({ label: item.email + " · Inbox", dot: item.color, onClick: function () { importTo(item.id); } });
  173. });
  174. if (accounts.length === 0) {
  175. items.push({ label: "No mail accounts", disabled: true });
  176. }
  177. items.push("-");
  178. items.push({ label: "Show in File Manager", icon: "folder open outline", onClick: function () {
  179. var dir = file.filepath.substring(0, file.filepath.lastIndexOf("/"));
  180. ao_module_openPath(dir, file.filename);
  181. } });
  182. ui.menu(event.currentTarget, items, { alignRight: true });
  183. });
  184. var files = (typeof ao_module_loadInputFiles === "function") ? ao_module_loadInputFiles() : null;
  185. if (!files || files.length === 0) {
  186. showError("No file was given. Open an .eml file from the File Manager.");
  187. return;
  188. }
  189. file = files[0];
  190. document.getElementById("fileName").textContent = file.filename;
  191. api.accounts("list", {}).then(function (list) { accounts = list || []; }).catch(function () { });
  192. api.settings("get", {}).then(function (loaded) {
  193. settings = loaded || {};
  194. applyTheme();
  195. }).catch(function () { });
  196. load(false);
  197. })();
  198. </script>
  199. </body>
  200. </html>