index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 detailsUrl = "details/"
  26. var updateTimerId = null
  27. var updateInterval = 5000
  28. var mailbox = ""
  29. var mailboxRegex = /^(\/m\d+)/g
  30. $(document).ready(function(){
  31. $.ajaxSetup({
  32. global: false,
  33. type: "POST"
  34. })
  35. $(window).bind('hashchange', requestDetails)
  36. requestDetails()
  37. urlPaths = mailboxRegex.exec($(location).attr('pathname'))
  38. if (urlPaths != null && urlPaths.length === 2) {
  39. mailbox = urlPaths[0]
  40. } else {
  41. mailbox = ""
  42. }
  43. loadFolders()
  44. loadStatusLine()
  45. updateMailList()
  46. if(mailbox != "") {
  47. clearInterval(updateTimerId)
  48. updateTimerId = setInterval(updateMailList, updateInterval)
  49. }
  50. })
  51. function openEmail(id) {
  52. window.location.hash = detailsUrl + id
  53. }
  54. function requestDetails() {
  55. var hashLocation = window.location.hash
  56. if (hashLocation.startsWith("#" + detailsUrl)) {
  57. var mailId = hashLocation.replace(/^#details\//, "")
  58. if (mailId != "") {
  59. $.ajax({
  60. url: "/mail",
  61. data: {mailId: mailId},
  62. success: function(result) {
  63. $("#mail"+mailId).removeClass("unread")
  64. $("#mail"+mailId).addClass("read")
  65. $("#details").html(result);
  66. setDetailsVisible(true);
  67. },
  68. error: function(jqXHR, textStatus, errorThrown) {
  69. $("#details").html(textStatus)
  70. setDetailsVisible(true)
  71. }
  72. })
  73. }
  74. } else {
  75. setDetailsVisible(false)
  76. }
  77. }
  78. function loadFolders() {
  79. if (mailbox == "") {
  80. $("#folders").html("Unable to load folder list")
  81. return
  82. }
  83. $.ajax({
  84. url: mailbox + "/folders",
  85. success: function(result) {
  86. $("#folders").html(result)
  87. },
  88. error: function(jqXHR, textStatus, errorThrown) {
  89. //TODO: some toast message here once implemented
  90. }
  91. })
  92. }
  93. function closeDetails() {
  94. window.location.hash = ""
  95. }
  96. function loadStatusLine() {
  97. $.ajax({
  98. url: mailbox + "/statusLine",
  99. success: function(result) {
  100. $("#statusLine").html(result)
  101. },
  102. error: function(jqXHR, textStatus, errorThrown) {
  103. //TODO: some toast message here once implemented
  104. }
  105. })
  106. }
  107. function localDate(timestamp) {
  108. var today = new Date()
  109. var date = new Date(timestamp*1000)
  110. dateString = ""
  111. if (today.getDay() == date.getDay()
  112. && today.getMonth() == date.getMonth()
  113. && today.getFullYear() == date.getFullYear()) {
  114. dateString = date.toLocaleTimeString("en-US")
  115. } else if (today.getFullYear() == date.getFullYear()) {
  116. const options = { day: 'numeric', month: 'short' }
  117. dateString = date.toLocaleDateString("en-US", options)
  118. } else {
  119. dateString = date.toLocaleDateString("en-US")
  120. }
  121. return dateString
  122. }
  123. function setRead(mailId, read) {
  124. $.ajax({
  125. url: "/setRead",
  126. data: {mailId: mailId,
  127. read: read},
  128. success: function(result) {
  129. if (read) {
  130. if ($("#readIcon"+mailId)) {
  131. $("#readIcon"+mailId).attr("src", "/assets/read.svg")
  132. }
  133. $("#mail"+mailId).removeClass("unread")
  134. $("#mail"+mailId).addClass("read")
  135. } else {
  136. if ($("#readIcon"+mailId)) {
  137. $("#readIcon"+mailId).attr("src", "/assets/unread.svg")
  138. }
  139. $("#mail"+mailId).removeClass("read")
  140. $("#mail"+mailId).addClass("unread")
  141. }
  142. },
  143. error: function(jqXHR, textStatus, errorThrown) {
  144. }
  145. })
  146. }
  147. function toggleRead(mailId) {
  148. if ($("#readIcon"+mailId)) {
  149. setRead(mailId, $("#readIcon"+mailId).attr("src") == "/assets/unread.svg")
  150. }
  151. }
  152. function removeMail(mailId) {
  153. $.ajax({
  154. url: "/remove",
  155. data: {mailId: mailId},
  156. success: function(result) {
  157. $("#mail"+mailId).remove();
  158. closeDetails()
  159. },
  160. error: function(jqXHR, textStatus, errorThrown) {
  161. }
  162. })
  163. }
  164. function setDetailsVisible(visible) {
  165. if (visible) {
  166. $("#details").show()
  167. $("#mailList").css({pointerEvents: "none"})
  168. clearInterval(updateTimerId)
  169. } else {
  170. $("#details").hide()
  171. $("#details").html("")
  172. $("#mailList").css({pointerEvents: "auto"})
  173. updateTimerId = setInterval(updateMailList, updateInterval)
  174. }
  175. }
  176. function updateMailList() {
  177. if (mailbox == "") {
  178. if($("#mailList")) {
  179. $("#mailList").html("Unable to load message list")
  180. }
  181. return
  182. }
  183. $.ajax({
  184. url: mailbox + "/mailList",
  185. success: function(result) {
  186. if($("#mailList")) {
  187. // console.log("result: " + result)
  188. $("#mailList").html(result)
  189. }
  190. },
  191. error: function(jqXHR, textStatus, errorThrown) {
  192. if($("#mailList")) {
  193. $("#mailList").html(textStatus)
  194. }
  195. }
  196. })
  197. }