index.js 9.0 KB

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