index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 updateTimerId = null
  29. var updateInterval = 50000
  30. var mailbox = ""
  31. var pageMax = 10
  32. const mailboxRegex = /^(\/m\d+)/g
  33. var folders = new Array()
  34. $(window).click(function(e){
  35. var target = $(e.target)
  36. var isDropDown = false
  37. for (var i = 0; i < target.parents().length; i++) {
  38. isDropDown = target.parents()[i].classList.contains("dropbtn")
  39. if (isDropDown) {
  40. break
  41. }
  42. }
  43. if (!e.target.matches('.dropbtn') && !isDropDown) {
  44. $(".dropdown-content").hide()
  45. }
  46. })
  47. $(document).ready(function(){
  48. $.ajaxSetup({
  49. global: false,
  50. type: "POST"
  51. })
  52. urlPaths = mailboxRegex.exec($(location).attr('pathname'))
  53. if (urlPaths != null && urlPaths.length === 2) {
  54. mailbox = urlPaths[0]
  55. } else {
  56. mailbox = ""
  57. }
  58. $(window).bind('hashchange', onHashChanged)
  59. onHashChanged()
  60. loadFolders()
  61. loadStatusLine()
  62. if (mailbox != "") {
  63. clearInterval(updateTimerId)
  64. }
  65. })
  66. function openEmail(id) {
  67. window.location.hash = currentFolder + currentPage + "/" + id
  68. }
  69. function openFolder(folder) {
  70. window.location.hash = folder
  71. }
  72. function onHashChanged() {
  73. var hashLocation = window.location.hash
  74. if (hashLocation == "") {
  75. setDetailsVisible(false)
  76. openFolder("Inbox")
  77. return
  78. }
  79. hashRegex = /^#([a-zA-Z]+)(\d*)\/?([A-Fa-f\d]*)/g
  80. hashParts = hashRegex.exec(hashLocation)
  81. page = 0
  82. if (hashParts.length >= 3 && hashParts[2] != "") {
  83. page = parseInt(hashParts[2])
  84. if (typeof page != "number" || page > pageMax || page < 0) {
  85. page = 0
  86. }
  87. }
  88. if (hashParts.length >= 2 && (hashParts[1] != currentFolder || currentPage != page) && hashParts[1] != "") {
  89. updateMailList(hashParts[1], page)
  90. }
  91. if (hashParts.length >= 4 && hashParts[3] != "") {
  92. if (currentMail != hashParts[3]) {
  93. requestMail(hashParts[3])
  94. }
  95. } else {
  96. setDetailsVisible(false)
  97. }
  98. }
  99. function requestMail(mailId) {
  100. if (mailId != "") {
  101. $.ajax({
  102. url: "/mail",
  103. data: {
  104. mailId: mailId
  105. },
  106. success: function(result) {
  107. currentMail = mailId
  108. $("#mail"+mailId).removeClass("unread")
  109. $("#mail"+mailId).addClass("read")
  110. $("#mailDetails").html(result);
  111. setDetailsVisible(true);
  112. folderStat(currentFolder);//TODO: receive statistic from websocket
  113. },
  114. error: function(jqXHR, textStatus, errorThrown) {
  115. $("#mailDetails").html(textStatus)
  116. setDetailsVisible(true)
  117. }
  118. })
  119. }
  120. }
  121. function loadFolders() {
  122. if (mailbox == "") {
  123. $("#folders").html("Unable to load folder list")
  124. return
  125. }
  126. $.ajax({
  127. url: mailbox + "/folders",
  128. success: function(result) {
  129. folderList = jQuery.parseJSON(result)
  130. for(var i = 0; i < folderList.folders.length; i++) {
  131. folders.push(folderList.folders[i].name)
  132. folderStat(folderList.folders[i].name)
  133. }
  134. $("#folders").html(folderList.html)
  135. },
  136. error: function(jqXHR, textStatus, errorThrown) {
  137. //TODO: some toast message here once implemented
  138. }
  139. })
  140. }
  141. function folderStat(folder) {
  142. $.ajax({
  143. url: mailbox + "/folderStat",
  144. data: {
  145. folder: folder
  146. },
  147. success: function(result) {
  148. var stats = jQuery.parseJSON(result)
  149. if (stats.unread > 0) {
  150. $("#folderStats"+folder).text(stats.unread)
  151. $("#folder"+folder).addClass("unread")
  152. } else {
  153. $("#folder"+folder).removeClass("unread")
  154. $("#folderStats"+folder).text("")
  155. }
  156. },
  157. error: function(jqXHR, textStatus, errorThrown) {
  158. //TODO: some toast message here once implemented
  159. }
  160. })
  161. }
  162. function closeDetails() {
  163. window.location.hash = currentFolder + currentPage
  164. }
  165. function loadStatusLine() {
  166. $.ajax({
  167. url: mailbox + "/statusLine",
  168. success: function(result) {
  169. $("#statusLine").html(result)
  170. },
  171. error: function(jqXHR, textStatus, errorThrown) {
  172. //TODO: some toast message here once implemented
  173. }
  174. })
  175. }
  176. function localDate(timestamp) {
  177. var today = new Date()
  178. var date = new Date(timestamp*1000)
  179. dateString = ""
  180. if (today.getDay() == date.getDay()
  181. && today.getMonth() == date.getMonth()
  182. && today.getFullYear() == date.getFullYear()) {
  183. dateString = date.toLocaleTimeString("en-US")
  184. } else if (today.getFullYear() == date.getFullYear()) {
  185. const options = { day: 'numeric', month: 'short' }
  186. dateString = date.toLocaleDateString("en-US", options)
  187. } else {
  188. dateString = date.toLocaleDateString("en-US")
  189. }
  190. return dateString
  191. }
  192. function setRead(mailId, read) {
  193. $.ajax({
  194. url: "/setRead",
  195. data: {mailId: mailId,
  196. read: read},
  197. success: function(result) {
  198. if (read) {
  199. if ($("#readIcon"+mailId)) {
  200. $("#readIcon"+mailId).attr("src", "/assets/read.svg")
  201. }
  202. if ($("#readListIcon"+mailId)) {
  203. $("#readListIcon"+mailId).attr("src", "/assets/read.svg")
  204. }
  205. $("#mail"+mailId).removeClass("unread")
  206. $("#mail"+mailId).addClass("read")
  207. } else {
  208. if ($("#readIcon"+mailId)) {
  209. $("#readIcon"+mailId).attr("src", "/assets/unread.svg")
  210. }
  211. if ($("#readListIcon"+mailId)) {
  212. $("#readListIcon"+mailId).attr("src", "/assets/unread.svg")
  213. }
  214. $("#mail"+mailId).removeClass("read")
  215. $("#mail"+mailId).addClass("unread")
  216. }
  217. folderStat(currentFolder);//TODO: receive statistic from websocket
  218. },
  219. error: function(jqXHR, textStatus, errorThrown) {
  220. }
  221. })
  222. }
  223. function toggleRead(mailId, iconId) {
  224. if ($("#"+iconId+mailId)) {
  225. setRead(mailId, $("#"+iconId+mailId).attr("src") == "/assets/unread.svg")
  226. }
  227. }
  228. function removeMail(mailId, callback) {
  229. var url = currentFolder != "Trash" ? "/remove" : "/delete"
  230. $.ajax({
  231. url: url,
  232. data: {mailId: mailId},
  233. success: function(result) {
  234. $("#mail"+mailId).remove();
  235. if (callback) {
  236. callback();
  237. }
  238. folderStat(currentFolder);//TODO: receive statistic from websocket
  239. folderStat("Trash");//TODO: receive statistic from websocket
  240. },
  241. error: function(jqXHR, textStatus, errorThrown) {
  242. }
  243. })
  244. }
  245. function restoreMail(mailId, callback) {
  246. var url = "/restore"
  247. $.ajax({
  248. url: url,
  249. data: {mailId: mailId},
  250. success: function(result) {
  251. if (currentFolder == "Trash") {
  252. $("#mail"+mailId).remove();
  253. }
  254. if (callback) {
  255. callback();
  256. }
  257. for (var i = 0; i < folders.length; i++) {
  258. folderStat(folders[i])
  259. }
  260. },
  261. error: function(jqXHR, textStatus, errorThrown) {
  262. }
  263. })
  264. }
  265. function setDetailsVisible(visible) {
  266. if (visible) {
  267. $("#mailDetails").show()
  268. $("#mailList").css({pointerEvents: "none"})
  269. clearInterval(updateTimerId)
  270. } else {
  271. currentMail = ""
  272. $("#mailDetails").hide()
  273. $("#mailDetails").html("")
  274. $("#mailList").css({pointerEvents: "auto"})
  275. }
  276. }
  277. function updateMailList(folder, page) {
  278. if (mailbox == "" || folder == "") {
  279. if ($("#mailList")) {
  280. $("#mailList").html("Unable to load message list")
  281. }
  282. return
  283. }
  284. $.ajax({
  285. url: mailbox + "/mailList",
  286. data: {
  287. folder: folder,
  288. page: page
  289. },
  290. success: function(result) {
  291. var data = jQuery.parseJSON(result)
  292. pageMax = Math.floor(data.total/50)
  293. if ($("#mailList")) {
  294. $("#mailList").html(data.html)
  295. }
  296. currentFolder = folder
  297. currentPage = page
  298. if($("#currentPageIndex")) {
  299. $("#currentPageIndex").text(currentPage + 1)
  300. }
  301. if($("#totalPageCount")) {
  302. $("#totalPageCount").text(pageMax + 1)
  303. }
  304. },
  305. error: function(jqXHR, textStatus, errorThrown) {
  306. if ($("#mailList")) {
  307. $("#mailList").html("Unable to load message list")
  308. }
  309. }
  310. })
  311. }
  312. function nextPage() {
  313. var newPage = currentPage < (pageMax - 1) ? currentPage + 1 : pageMax
  314. window.location.hash = currentFolder + newPage
  315. }
  316. function prevPage() {
  317. var newPage = currentPage > 0 ? currentPage - 1 : 0
  318. window.location.hash = currentFolder + newPage
  319. }
  320. function toggleDropDown(dd) {
  321. $("#"+dd).toggle()
  322. }