123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- var detailsUrl = "details/"
- var updateTimerId = null
- var updateInterval = 5000
- var mailbox = ""
- var mailboxRegex = /^(\/m\d+)/g
- $(document).ready(function(){
- $.ajaxSetup({
- global: false,
- type: "POST"
- })
- $(window).bind('hashchange', requestDetails)
- requestDetails()
- urlPaths = mailboxRegex.exec($(location).attr('pathname'))
- if (urlPaths != null && urlPaths.length === 2) {
- mailbox = urlPaths[0]
- } else {
- mailbox = ""
- }
- loadFolders()
- loadStatusLine()
- updateMailList()
- if(mailbox != "") {
- clearInterval(updateTimerId)
- updateTimerId = setInterval(updateMailList, updateInterval)
- }
- })
- function openEmail(id) {
- window.location.hash = detailsUrl + id
- }
- function requestDetails() {
- var hashLocation = window.location.hash
- if (hashLocation.startsWith("#" + detailsUrl)) {
- var mailId = hashLocation.replace(/^#details\//, "")
- if (mailId != "") {
- $.ajax({
- url: "/mail",
- data: {mailId: mailId},
- success: function(result) {
- $("#mail"+mailId).removeClass("unread")
- $("#mail"+mailId).addClass("read")
- $("#details").html(result);
- setDetailsVisible(true);
- },
- error: function(jqXHR, textStatus, errorThrown) {
- $("#details").html(textStatus)
- setDetailsVisible(true)
- }
- })
- }
- } else {
- setDetailsVisible(false)
- }
- }
- function loadFolders() {
- if (mailbox == "") {
- $("#folders").html("Unable to load folder list")
- return
- }
- $.ajax({
- url: mailbox + "/folders",
- success: function(result) {
- $("#folders").html(result)
- },
- error: function(jqXHR, textStatus, errorThrown) {
-
- }
- })
- }
- function closeDetails() {
- window.location.hash = ""
- }
- function loadStatusLine() {
- $.ajax({
- url: mailbox + "/statusLine",
- success: function(result) {
- $("#statusLine").html(result)
- },
- error: function(jqXHR, textStatus, errorThrown) {
-
- }
- })
- }
- function localDate(timestamp) {
- var today = new Date()
- var date = new Date(timestamp*1000)
- dateString = ""
- if (today.getDay() == date.getDay()
- && today.getMonth() == date.getMonth()
- && today.getFullYear() == date.getFullYear()) {
- dateString = date.toLocaleTimeString("en-US")
- } else if (today.getFullYear() == date.getFullYear()) {
- const options = { day: 'numeric', month: 'short' }
- dateString = date.toLocaleDateString("en-US", options)
- } else {
- dateString = date.toLocaleDateString("en-US")
- }
- return dateString
- }
- function setRead(mailId, read) {
- $.ajax({
- url: "/setRead",
- data: {mailId: mailId,
- read: read},
- success: function(result) {
- if (read) {
- if ($("#readIcon"+mailId)) {
- $("#readIcon"+mailId).attr("src", "/assets/read.svg")
- }
- $("#mail"+mailId).removeClass("unread")
- $("#mail"+mailId).addClass("read")
- } else {
- if ($("#readIcon"+mailId)) {
- $("#readIcon"+mailId).attr("src", "/assets/unread.svg")
- }
- $("#mail"+mailId).removeClass("read")
- $("#mail"+mailId).addClass("unread")
- }
- },
- error: function(jqXHR, textStatus, errorThrown) {
- }
- })
- }
- function toggleRead(mailId) {
- if ($("#readIcon"+mailId)) {
- setRead(mailId, $("#readIcon"+mailId).attr("src") == "/assets/unread.svg")
- }
- }
- function removeMail(mailId) {
- $.ajax({
- url: "/remove",
- data: {mailId: mailId},
- success: function(result) {
- $("#mail"+mailId).remove();
- closeDetails()
- },
- error: function(jqXHR, textStatus, errorThrown) {
- }
- })
- }
- function setDetailsVisible(visible) {
- if (visible) {
- $("#details").show()
- $("#mailList").css({pointerEvents: "none"})
- clearInterval(updateTimerId)
- } else {
- $("#details").hide()
- $("#details").html("")
- $("#mailList").css({pointerEvents: "auto"})
- updateTimerId = setInterval(updateMailList, updateInterval)
- }
- }
- function updateMailList() {
- if (mailbox == "") {
- if($("#mailList")) {
- $("#mailList").html("Unable to load message list")
- }
- return
- }
- $.ajax({
- url: mailbox + "/mailList",
- success: function(result) {
- if($("#mailList")) {
-
- $("#mailList").html(result)
- }
- },
- error: function(jqXHR, textStatus, errorThrown) {
- if($("#mailList")) {
- $("#mailList").html(textStatus)
- }
- }
- })
- }
|