index.js 5.6 KB

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