index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. $("#mailNewButton").click(mailNew)
  66. })
  67. function mailNew(e) {
  68. window.location.hash = currentFolder + currentPage + "/mailNew"
  69. }
  70. function mailOpen(id) {
  71. window.location.hash = currentFolder + currentPage + "/" + id
  72. }
  73. function openFolder(folder) {
  74. window.location.hash = folder
  75. }
  76. function onHashChanged() {
  77. var hashLocation = window.location.hash
  78. if (hashLocation == "") {
  79. setDetailsVisible(false)
  80. openFolder("Inbox")
  81. return
  82. }
  83. hashRegex = /^#([a-zA-Z]+)(\d*)\/?([A-Fa-f\d]*)/g
  84. hashParts = hashRegex.exec(hashLocation)
  85. page = 0
  86. if (hashParts.length >= 3 && hashParts[2] != "") {
  87. page = parseInt(hashParts[2])
  88. if (typeof page != "number" || page > pageMax || page < 0) {
  89. page = 0
  90. }
  91. }
  92. if (hashParts.length >= 2 && (hashParts[1] != currentFolder || currentPage != page) && hashParts[1] != "") {
  93. updateMailList(hashParts[1], page)
  94. }
  95. if (hashParts.length >= 4 && hashParts[3] != "" && hashParts[3] != "/mailNew") {
  96. if (currentMail != hashParts[3]) {
  97. requestMail(hashParts[3])
  98. }
  99. } else {
  100. setDetailsVisible(false)
  101. }
  102. hashParts = hashLocation.split("/")
  103. if (hashParts.length == 2 && hashParts[1] == "mailNew") {
  104. console.log("hashParts: " + hashParts + " length" + hashParts.length + " hashParts[1] " + hashParts[1])
  105. setMailNewVisible(true)
  106. } else {
  107. console.log("!hashParts: " + hashParts)
  108. setMailNewVisible(false)
  109. }
  110. }
  111. function requestMail(mailId) {
  112. if (mailId != "") {
  113. $.ajax({
  114. url: "/mail",
  115. data: {
  116. mailId: mailId
  117. },
  118. success: function(result) {
  119. currentMail = mailId
  120. $("#mail"+mailId).removeClass("unread")
  121. $("#mail"+mailId).addClass("read")
  122. $("#mailDetails").html(result);
  123. setDetailsVisible(true);
  124. folderStat(currentFolder);//TODO: receive statistic from websocket
  125. },
  126. error: function(jqXHR, textStatus, errorThrown) {
  127. $("#mailDetails").html(textStatus)
  128. setDetailsVisible(true)
  129. }
  130. })
  131. }
  132. }
  133. function loadFolders() {
  134. if (mailbox == "") {
  135. $("#folders").html("Unable to load folder list")
  136. return
  137. }
  138. $.ajax({
  139. url: mailbox + "/folders",
  140. success: function(result) {
  141. folderList = jQuery.parseJSON(result)
  142. for(var i = 0; i < folderList.folders.length; i++) {
  143. folders.push(folderList.folders[i].name)
  144. folderStat(folderList.folders[i].name)
  145. }
  146. $("#folders").html(folderList.html)
  147. },
  148. error: function(jqXHR, textStatus, errorThrown) {
  149. //TODO: some toast message here once implemented
  150. }
  151. })
  152. }
  153. function folderStat(folder) {
  154. $.ajax({
  155. url: mailbox + "/folderStat",
  156. data: {
  157. folder: folder
  158. },
  159. success: function(result) {
  160. var stats = jQuery.parseJSON(result)
  161. if (stats.unread > 0) {
  162. $("#folderStats"+folder).text(stats.unread)
  163. $("#folder"+folder).addClass("unread")
  164. } else {
  165. $("#folder"+folder).removeClass("unread")
  166. $("#folderStats"+folder).text("")
  167. }
  168. },
  169. error: function(jqXHR, textStatus, errorThrown) {
  170. //TODO: some toast message here once implemented
  171. }
  172. })
  173. }
  174. function closeDetails() {
  175. window.location.hash = currentFolder + currentPage
  176. }
  177. function closeMailNew() {
  178. window.location.hash = currentFolder + currentPage
  179. }
  180. function loadStatusLine() {
  181. $.ajax({
  182. url: mailbox + "/statusLine",
  183. success: function(result) {
  184. $("#statusLine").html(result)
  185. },
  186. error: function(jqXHR, textStatus, errorThrown) {
  187. //TODO: some toast message here once implemented
  188. }
  189. })
  190. }
  191. function localDate(timestamp) {
  192. var today = new Date()
  193. var date = new Date(timestamp*1000)
  194. dateString = ""
  195. if (today.getDay() == date.getDay()
  196. && today.getMonth() == date.getMonth()
  197. && today.getFullYear() == date.getFullYear()) {
  198. dateString = date.toLocaleTimeString("en-US")
  199. } else if (today.getFullYear() == date.getFullYear()) {
  200. const options = { day: 'numeric', month: 'short' }
  201. dateString = date.toLocaleDateString("en-US", options)
  202. } else {
  203. dateString = date.toLocaleDateString("en-US")
  204. }
  205. return dateString
  206. }
  207. function setRead(mailId, read) {
  208. $.ajax({
  209. url: "/setRead",
  210. data: {mailId: mailId,
  211. read: read},
  212. success: function(result) {
  213. if (read) {
  214. if ($("#readIcon"+mailId)) {
  215. $("#readIcon"+mailId).attr("src", "/assets/read.svg")
  216. }
  217. if ($("#readListIcon"+mailId)) {
  218. $("#readListIcon"+mailId).attr("src", "/assets/read.svg")
  219. }
  220. $("#mail"+mailId).removeClass("unread")
  221. $("#mail"+mailId).addClass("read")
  222. } else {
  223. if ($("#readIcon"+mailId)) {
  224. $("#readIcon"+mailId).attr("src", "/assets/unread.svg")
  225. }
  226. if ($("#readListIcon"+mailId)) {
  227. $("#readListIcon"+mailId).attr("src", "/assets/unread.svg")
  228. }
  229. $("#mail"+mailId).removeClass("read")
  230. $("#mail"+mailId).addClass("unread")
  231. }
  232. folderStat(currentFolder);//TODO: receive statistic from websocket
  233. },
  234. error: function(jqXHR, textStatus, errorThrown) {
  235. }
  236. })
  237. }
  238. function toggleRead(mailId, iconId) {
  239. if ($("#"+iconId+mailId)) {
  240. setRead(mailId, $("#"+iconId+mailId).attr("src") == "/assets/unread.svg")
  241. }
  242. }
  243. function removeMail(mailId, callback) {
  244. var url = currentFolder != "Trash" ? "/remove" : "/delete"
  245. $.ajax({
  246. url: url,
  247. data: {mailId: mailId},
  248. success: function(result) {
  249. $("#mail"+mailId).remove();
  250. if (callback) {
  251. callback();
  252. }
  253. folderStat(currentFolder);//TODO: receive statistic from websocket
  254. folderStat("Trash");//TODO: receive statistic from websocket
  255. },
  256. error: function(jqXHR, textStatus, errorThrown) {
  257. }
  258. })
  259. }
  260. function restoreMail(mailId, callback) {
  261. var url = "/restore"
  262. $.ajax({
  263. url: url,
  264. data: {mailId: mailId},
  265. success: function(result) {
  266. if (currentFolder == "Trash") {
  267. $("#mail"+mailId).remove();
  268. }
  269. if (callback) {
  270. callback();
  271. }
  272. for (var i = 0; i < folders.length; i++) {
  273. folderStat(folders[i])
  274. }
  275. },
  276. error: function(jqXHR, textStatus, errorThrown) {
  277. }
  278. })
  279. }
  280. function setDetailsVisible(visible) {
  281. if (visible) {
  282. $("#mailDetails").show()
  283. $("#mailList").css({pointerEvents: "none"})
  284. clearInterval(updateTimerId)
  285. } else {
  286. currentMail = ""
  287. $("#mailDetails").hide()
  288. $("#mailDetails").html("")
  289. $("#mailList").css({pointerEvents: "auto"})
  290. }
  291. }
  292. function setMailNewVisible(visible) {
  293. if (visible) {
  294. $("#mailNew").show()
  295. $("#mailList").css({pointerEvents: "none"})
  296. clearInterval(updateTimerId)
  297. } else {
  298. currentMail = ""
  299. $("#mailNew").hide()
  300. $("#mailList").css({pointerEvents: "auto"})
  301. }
  302. }
  303. function updateMailList(folder, page) {
  304. if (mailbox == "" || folder == "") {
  305. if ($("#mailList")) {
  306. $("#mailList").html("Unable to load message list")
  307. }
  308. return
  309. }
  310. $.ajax({
  311. url: mailbox + "/mailList",
  312. data: {
  313. folder: folder,
  314. page: page
  315. },
  316. success: function(result) {
  317. var data = jQuery.parseJSON(result)
  318. pageMax = Math.floor(data.total/50)
  319. if ($("#mailList")) {
  320. $("#mailList").html(data.html)
  321. }
  322. currentFolder = folder
  323. currentPage = page
  324. if($("#currentPageIndex")) {
  325. $("#currentPageIndex").text(currentPage + 1)
  326. }
  327. if($("#totalPageCount")) {
  328. $("#totalPageCount").text(pageMax + 1)
  329. }
  330. },
  331. error: function(jqXHR, textStatus, errorThrown) {
  332. if ($("#mailList")) {
  333. $("#mailList").html("Unable to load message list")
  334. }
  335. }
  336. })
  337. }
  338. function nextPage() {
  339. var newPage = currentPage < (pageMax - 1) ? currentPage + 1 : pageMax
  340. window.location.hash = currentFolder + newPage
  341. }
  342. function prevPage() {
  343. var newPage = currentPage > 0 ? currentPage - 1 : 0
  344. window.location.hash = currentFolder + newPage
  345. }
  346. function toggleDropDown(dd) {
  347. $("#"+dd).toggle()
  348. }
  349. function sendNewMail() {
  350. var formValue = $("#mailNewForm").serialize()
  351. $.ajax({
  352. url: mailbox + "/sendNewMail",
  353. data: formValue,
  354. success: function(result) {
  355. $("#newMailEditor").val("")
  356. $("#newMailSubject").val("")
  357. $("#newMailTo").val("")
  358. closeMailNew()
  359. },
  360. error: function(jqXHR, textStatus, errorThrown) {
  361. //TODO: some toast message here once implemented
  362. }
  363. })
  364. }
  365. function logout() {
  366. window.location.href = "/logout"
  367. }