index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. var currentFolder = ""
  26. var currentPage = 0
  27. var currentMail = ""
  28. var mailbox = ""
  29. var pageMax = 10
  30. const mailboxRegex = /^(\/m\d+)/g
  31. var folders = new Array()
  32. var notifierSocket = null
  33. $(window).click(function(e){
  34. var target = $(e.target)
  35. var isDropDown = false
  36. for (var i = 0; i < target.parents().length; i++) {
  37. isDropDown = target.parents()[i].classList.contains("dropbtn")
  38. if (isDropDown) {
  39. break
  40. }
  41. }
  42. if (!e.target.matches('.dropbtn') && !isDropDown) {
  43. $(".dropdown-content").hide()
  44. }
  45. })
  46. $(document).ready(function(){
  47. $.ajaxSetup({
  48. global: false,
  49. type: "POST"
  50. })
  51. urlPaths = mailboxRegex.exec($(location).attr('pathname'))
  52. if (urlPaths != null && urlPaths.length === 2) {
  53. mailbox = urlPaths[0]
  54. } else {
  55. mailbox = ""
  56. }
  57. $(window).bind('hashchange', onHashChanged)
  58. onHashChanged()
  59. loadFolders()
  60. loadStatusLine()
  61. $("#mailNewButton").click(mailNew)
  62. connectNotifier()
  63. })
  64. function mailNew(e) {
  65. window.location.hash = currentFolder + currentPage + "/mailNew"
  66. }
  67. function mailOpen(id) {
  68. window.location.hash = currentFolder + currentPage + "/" + id
  69. }
  70. function openFolder(folder) {
  71. window.location.hash = folder
  72. }
  73. function onHashChanged() {
  74. var hashLocation = window.location.hash
  75. if (hashLocation == "") {
  76. setDetailsVisible(false)
  77. openFolder("Inbox")
  78. return
  79. }
  80. hashRegex = /^#([a-zA-Z]+)(\d*)\/?([A-Fa-f\d]*)/g
  81. hashParts = hashRegex.exec(hashLocation)
  82. page = 0
  83. if (hashParts.length >= 3 && hashParts[2] != "") {
  84. page = parseInt(hashParts[2])
  85. if (typeof page != "number" || page > pageMax || page < 0) {
  86. page = 0
  87. }
  88. }
  89. if (hashParts.length >= 2 && (hashParts[1] != currentFolder || currentPage != page) && hashParts[1] != "") {
  90. updateMailList(hashParts[1], page)
  91. }
  92. if (hashParts.length >= 4 && hashParts[3] != "" && hashParts[3] != "/mailNew") {
  93. if (currentMail != hashParts[3]) {
  94. requestMail(hashParts[3])
  95. }
  96. } else {
  97. setDetailsVisible(false)
  98. }
  99. hashParts = hashLocation.split("/")
  100. if (hashParts.length == 2 && hashParts[1] == "mailNew") {
  101. setMailNewVisible(true)
  102. } else {
  103. setMailNewVisible(false)
  104. }
  105. }
  106. function requestMail(mailId) {
  107. if (mailId != "") {
  108. $.ajax({
  109. url: "/mail",
  110. data: {
  111. mailId: mailId
  112. },
  113. success: function(result) {
  114. currentMail = mailId
  115. $("#mail"+mailId).removeClass("unread")
  116. $("#mail"+mailId).addClass("read")
  117. $("#mailDetails").html(result);
  118. setDetailsVisible(true);
  119. folderStat(currentFolder);//TODO: receive statistic from websocket
  120. },
  121. error: function(jqXHR, textStatus, errorThrown) {
  122. $("#mailDetails").html(textStatus)
  123. setDetailsVisible(true)
  124. }
  125. })
  126. }
  127. }
  128. function loadFolders() {
  129. if (mailbox == "") {
  130. $("#folders").html("Unable to load folder list")
  131. return
  132. }
  133. $.ajax({
  134. url: mailbox + "/folders",
  135. success: function(result) {
  136. folderList = jQuery.parseJSON(result)
  137. for(var i = 0; i < folderList.folders.length; i++) {
  138. folders.push(folderList.folders[i].name)
  139. folderStat(folderList.folders[i].name)
  140. }
  141. $("#folders").html(folderList.html)
  142. },
  143. error: function(jqXHR, textStatus, errorThrown) {
  144. //TODO: some toast message here once implemented
  145. }
  146. })
  147. }
  148. function folderStat(folder) {
  149. $.ajax({
  150. url: mailbox + "/folderStat",
  151. data: {
  152. folder: folder
  153. },
  154. success: function(result) {
  155. var stats = jQuery.parseJSON(result)
  156. if (stats.unread > 0) {
  157. $("#folderStats"+folder).text(stats.unread)
  158. $("#folder"+folder).addClass("unread")
  159. } else {
  160. $("#folder"+folder).removeClass("unread")
  161. $("#folderStats"+folder).text("")
  162. }
  163. },
  164. error: function(jqXHR, textStatus, errorThrown) {
  165. //TODO: some toast message here once implemented
  166. }
  167. })
  168. }
  169. function closeDetails() {
  170. window.location.hash = currentFolder + currentPage
  171. }
  172. function closeMailNew() {
  173. window.location.hash = currentFolder + currentPage
  174. }
  175. function loadStatusLine() {
  176. $.ajax({
  177. url: mailbox + "/statusLine",
  178. success: function(result) {
  179. $("#statusLine").html(result)
  180. },
  181. error: function(jqXHR, textStatus, errorThrown) {
  182. //TODO: some toast message here once implemented
  183. }
  184. })
  185. }
  186. function localDate(timestamp) {
  187. var today = new Date()
  188. var date = new Date(timestamp*1000)
  189. dateString = ""
  190. if (today.getDay() == date.getDay()
  191. && today.getMonth() == date.getMonth()
  192. && today.getFullYear() == date.getFullYear()) {
  193. dateString = date.toLocaleTimeString("en-US")
  194. } else if (today.getFullYear() == date.getFullYear()) {
  195. const options = { day: 'numeric', month: 'short' }
  196. dateString = date.toLocaleDateString("en-US", options)
  197. } else {
  198. dateString = date.toLocaleDateString("en-US")
  199. }
  200. return dateString
  201. }
  202. function setRead(mailId, read) {
  203. $.ajax({
  204. url: "/setRead",
  205. data: {mailId: mailId,
  206. read: read},
  207. success: function(result) {
  208. if (read) {
  209. if ($("#readIcon"+mailId)) {
  210. $("#readIcon"+mailId).attr("src", "/assets/read.svg")
  211. }
  212. if ($("#readListIcon"+mailId)) {
  213. $("#readListIcon"+mailId).attr("src", "/assets/read.svg")
  214. }
  215. $("#mail"+mailId).removeClass("unread")
  216. $("#mail"+mailId).addClass("read")
  217. } else {
  218. if ($("#readIcon"+mailId)) {
  219. $("#readIcon"+mailId).attr("src", "/assets/unread.svg")
  220. }
  221. if ($("#readListIcon"+mailId)) {
  222. $("#readListIcon"+mailId).attr("src", "/assets/unread.svg")
  223. }
  224. $("#mail"+mailId).removeClass("read")
  225. $("#mail"+mailId).addClass("unread")
  226. }
  227. folderStat(currentFolder);//TODO: receive statistic from websocket
  228. },
  229. error: function(jqXHR, textStatus, errorThrown) {
  230. }
  231. })
  232. }
  233. function toggleRead(mailId, iconId) {
  234. if ($("#"+iconId+mailId)) {
  235. setRead(mailId, $("#"+iconId+mailId).attr("src") == "/assets/unread.svg")
  236. }
  237. }
  238. function removeMail(mailId, callback) {
  239. var url = currentFolder != "Trash" ? "/remove" : "/delete"
  240. $.ajax({
  241. url: url,
  242. data: {mailId: mailId},
  243. success: function(result) {
  244. $("#mail"+mailId).remove();
  245. if (callback) {
  246. callback();
  247. }
  248. folderStat(currentFolder);//TODO: receive statistic from websocket
  249. folderStat("Trash");//TODO: receive statistic from websocket
  250. },
  251. error: function(jqXHR, textStatus, errorThrown) {
  252. }
  253. })
  254. }
  255. function restoreMail(mailId, callback) {
  256. var url = "/restore"
  257. $.ajax({
  258. url: url,
  259. data: {mailId: mailId},
  260. success: function(result) {
  261. if (currentFolder == "Trash") {
  262. $("#mail"+mailId).remove();
  263. }
  264. if (callback) {
  265. callback();
  266. }
  267. for (var i = 0; i < folders.length; i++) {
  268. folderStat(folders[i])
  269. }
  270. },
  271. error: function(jqXHR, textStatus, errorThrown) {
  272. }
  273. })
  274. }
  275. function setDetailsVisible(visible) {
  276. if (visible) {
  277. $("#mailDetails").show()
  278. $("#mailList").css({pointerEvents: "none"})
  279. } else {
  280. currentMail = ""
  281. $("#mailDetails").hide()
  282. $("#mailDetails").html("")
  283. $("#mailList").css({pointerEvents: "auto"})
  284. }
  285. }
  286. function setMailNewVisible(visible) {
  287. if (visible) {
  288. $("#mailNew").show()
  289. $("#mailList").css({pointerEvents: "none"})
  290. } else {
  291. currentMail = ""
  292. $("#mailNew").hide()
  293. $("#mailList").css({pointerEvents: "auto"})
  294. }
  295. }
  296. function updateMailList(folder, page) {
  297. if (mailbox == "" || folder == "") {
  298. if ($("#mailList")) {
  299. $("#mailList").html("Unable to load message list")
  300. }
  301. return
  302. }
  303. $.ajax({
  304. url: mailbox + "/mailList",
  305. data: {
  306. folder: folder,
  307. page: page
  308. },
  309. success: function(result) {
  310. var data = jQuery.parseJSON(result)
  311. pageMax = Math.floor(data.total/50)
  312. if ($("#mailList")) {
  313. $("#mailList").html(data.html)
  314. }
  315. currentFolder = folder
  316. currentPage = page
  317. if($("#currentPageIndex")) {
  318. $("#currentPageIndex").text(currentPage + 1)
  319. }
  320. if($("#totalPageCount")) {
  321. $("#totalPageCount").text(pageMax + 1)
  322. }
  323. },
  324. error: function(jqXHR, textStatus, errorThrown) {
  325. if ($("#mailList")) {
  326. $("#mailList").html("Unable to load message list")
  327. }
  328. }
  329. })
  330. }
  331. function nextPage() {
  332. var newPage = currentPage < (pageMax - 1) ? currentPage + 1 : pageMax
  333. window.location.hash = currentFolder + newPage
  334. }
  335. function prevPage() {
  336. var newPage = currentPage > 0 ? currentPage - 1 : 0
  337. window.location.hash = currentFolder + newPage
  338. }
  339. function toggleDropDown(dd) {
  340. $("#"+dd).toggle()
  341. }
  342. function sendNewMail() {
  343. var formValue = $("#mailNewForm").serialize()
  344. $.ajax({
  345. url: mailbox + "/sendNewMail",
  346. data: formValue,
  347. success: function(result) {
  348. $("#newMailEditor").val("")
  349. $("#newMailSubject").val("")
  350. $("#newMailTo").val("")
  351. closeMailNew()
  352. },
  353. error: function(jqXHR, textStatus, errorThrown) {
  354. //TODO: some toast message here once implemented
  355. }
  356. })
  357. }
  358. function logout() {
  359. window.location.href = "/logout"
  360. }
  361. function connectNotifier() {
  362. if (notifierSocket != null) {
  363. return
  364. }
  365. var protocol = "wss://"
  366. if(window.location.protocol !== "https:") {
  367. protocol = "ws://"
  368. }
  369. notifierSocket = new WebSocket(protocol + window.location.host + mailbox + "/notifierSubscribe")
  370. notifierSocket.onopen = function() {
  371. };
  372. notifierSocket.onmessage = function (e) {
  373. for (var i = 0; i < folders.length; i++) {
  374. folderStat(folders[i])
  375. }
  376. updateMailList(currentFolder, currentPage)
  377. }
  378. notifierSocket.onclose = function () {
  379. }
  380. }
  381. window.onbeforeunload = function() {
  382. if (notifierSocket != null) {
  383. notifierSocket.onclose = function () {}; // disable onclose handler first
  384. notifierSocket.close();
  385. }
  386. };