index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. $("#mail"+mailId).removeClass("unread")
  203. $("#mail"+mailId).addClass("read")
  204. } else {
  205. if ($("#readIcon"+mailId)) {
  206. $("#readIcon"+mailId).attr("src", "/assets/unread.svg")
  207. }
  208. $("#mail"+mailId).removeClass("read")
  209. $("#mail"+mailId).addClass("unread")
  210. }
  211. folderStat(currentFolder);//TODO: receive statistic from websocket
  212. },
  213. error: function(jqXHR, textStatus, errorThrown) {
  214. }
  215. })
  216. }
  217. function toggleRead(mailId) {
  218. if ($("#readIcon"+mailId)) {
  219. setRead(mailId, $("#readIcon"+mailId).attr("src") == "/assets/unread.svg")
  220. }
  221. }
  222. function removeMail(mailId, callback) {
  223. var url = currentFolder != "Trash" ? "/remove" : "/delete"
  224. $.ajax({
  225. url: url,
  226. data: {mailId: mailId},
  227. success: function(result) {
  228. $("#mail"+mailId).remove();
  229. if (callback) {
  230. callback();
  231. }
  232. folderStat(currentFolder);//TODO: receive statistic from websocket
  233. folderStat("Trash");//TODO: receive statistic from websocket
  234. },
  235. error: function(jqXHR, textStatus, errorThrown) {
  236. }
  237. })
  238. }
  239. function setDetailsVisible(visible) {
  240. if (visible) {
  241. $("#mailDetails").show()
  242. $("#mailList").css({pointerEvents: "none"})
  243. clearInterval(updateTimerId)
  244. } else {
  245. currentMail = ""
  246. $("#mailDetails").hide()
  247. $("#mailDetails").html("")
  248. $("#mailList").css({pointerEvents: "auto"})
  249. }
  250. }
  251. function updateMailList(folder, page) {
  252. if (mailbox == "" || folder == "") {
  253. if ($("#mailList")) {
  254. $("#mailList").html("Unable to load message list")
  255. }
  256. return
  257. }
  258. $.ajax({
  259. url: mailbox + "/mailList",
  260. data: {
  261. folder: folder,
  262. page: page
  263. },
  264. success: function(result) {
  265. var data = jQuery.parseJSON(result)
  266. pageMax = Math.floor(data.total/50)
  267. if ($("#mailList")) {
  268. $("#mailList").html(data.html)
  269. }
  270. currentFolder = folder
  271. currentPage = page
  272. if($("#currentPageIndex")) {
  273. $("#currentPageIndex").text(currentPage + 1)
  274. }
  275. if($("#totalPageCount")) {
  276. $("#totalPageCount").text(pageMax + 1)
  277. }
  278. },
  279. error: function(jqXHR, textStatus, errorThrown) {
  280. if ($("#mailList")) {
  281. $("#mailList").html("Unable to load message list")
  282. }
  283. }
  284. })
  285. }
  286. function nextPage() {
  287. var newPage = currentPage < (pageMax - 1) ? currentPage + 1 : pageMax
  288. window.location.hash = currentFolder + newPage
  289. }
  290. function prevPage() {
  291. var newPage = currentPage > 0 ? currentPage - 1 : 0
  292. window.location.hash = currentFolder + newPage
  293. }
  294. function toggleDropDown(dd) {
  295. $("#"+dd).toggle()
  296. }