]> code.citadel.org Git - citadel.git/blob - webcit-ng/static/js/view_mail.js
Added the dispatch function for entering a new mail message
[citadel.git] / webcit-ng / static / js / view_mail.js
1 // This module handles the view for "mailbox" rooms.
2 //
3 // Copyright (c) 2016-2022 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8
9 var selected_message = 0;                                                       // Remember the last message that was selected
10 var RefreshMailboxInterval;                                                     // We store our refresh timer here
11
12
13 // Render a message into the mailbox view
14 function mail_render_one(msg, target_div) {
15         let div = "FIXME";
16         try {
17                 outmsg =
18                   "<div class=\"ctdl-mmsg-wrapper\">"                           // begin message wrapper
19                 + "<div class=\"ctdl-avatar\" onClick=\"javascript:user_profile('" + msg.from + "');\">"
20                 + "<img src=\"/ctdl/u/" + msg.from + "/userpic\" width=\"32\" "
21                 + "onerror=\"this.parentNode.innerHTML='&lt;i class=&quot;fa fa-user-circle fa-2x&quot;&gt;&lt;/i&gt; '\">"
22                 + "</div>"                                                      // end avatar
23                 + "<div class=\"ctdl-mmsg-content\">"                           // begin content
24                 + "<div class=\"ctdl-msg-header\">"                             // begin header
25                 + "<span class=\"ctdl-msg-header-info\">"                       // begin header info on left side
26                 + "<span class=\"ctdl-username\" onClick=\"javascript:user_profile('" + msg.from + "');\">"
27                 + msg.from
28                 + "</a></span>"                                                 // end username
29                 + "<span class=\"ctdl-msgdate\">"
30                 + convertTimestamp(msg.time)
31                 + "</span>"                                                     // end msgdate
32                 + "</span>"                                                     // end header info on left side
33                 + "<span class=\"ctdl-msg-header-buttons\">"                    // begin buttons on right side
34         
35                 + "<span class=\"ctdl-msg-button\">"                            // Reply
36                 + "<a href=\"javascript:open_reply_box('"+div+"',false,'"+msg.wefw+"','"+msg.msgn+"');\">"
37                 + "<i class=\"fa fa-reply\"></i> " 
38                 + _("Reply")
39                 + "</a></span>"
40         
41                 + "<span class=\"ctdl-msg-button\">"                            // ReplyQuoted
42                 + "<a href=\"javascript:open_reply_box('"+div+"',true,'"+msg.wefw+"','"+msg.msgn+"');\">"
43                 + "<i class=\"fa fa-comment\"></i> " 
44                 + _("ReplyQuoted")
45                 + "</a></span>";
46         
47                 if (can_delete_messages) {
48                         outmsg +=
49                         "<span class=\"ctdl-msg-button\">"
50                         + "<a href=\"javascript:forum_delete_message('"+div+"','"+msg.msgnum+"');\">"
51                         + "<i class=\"fa fa-trash\"></i> " 
52                         + _("Delete")
53                         + "</a></span>";
54                 }
55         
56                 outmsg +=
57                   "</span>";                                                    // end buttons on right side
58                 if (msg.subj) {
59                         outmsg +=
60                         "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
61                 }
62                 outmsg +=
63                   "</div>"                                                      // end header
64                 + "<div class=\"ctdl-msg-body\" id=\"" + div + "_body\">"       // begin body
65                 + msg.text
66                 + "</div>"                                                      // end body
67                 + "</div>"                                                      // end content
68                 + "</div>"                                                      // end wrapper
69                 ;
70         }
71         catch(err) {
72                 outmsg = "<div class=\"ctdl-mmsg-wrapper\">" + err.message + "</div>";
73         }
74
75         target_div.innerHTML = outmsg;
76 }
77
78
79 // display an individual message
80 function mail_display_message(msgnum, target_div) {
81         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgnum + "/json";
82         mail_fetch_msg = async() => {
83                 response = await fetch(url);
84                 msg = await(response.json());
85                 if (response.ok) {
86                         mail_render_one(msg, target_div);
87                 }
88         }
89         mail_fetch_msg();
90 }
91
92
93 // A message has been selected...
94 function select_message(msgnum) {
95         // unhighlight any previously selected message
96         try {
97                 document.getElementById("ctdl-msgsum-" + selected_message).classList.remove("w3-blue");
98         }
99         catch {
100         }
101
102         // highlight the newly selected message
103         document.getElementById("ctdl-msgsum-" + msgnum).classList.add("w3-blue");
104         //document.getElementById("ctdl-msgsum-" + msgnum).scrollIntoView();
105
106         // display the message if it isn't already displayed
107         if (selected_message != msgnum) {
108                 selected_message = msgnum;
109                 mail_display_message(msgnum, document.getElementById("ctdl-reading-pane"));
110         }
111 }
112
113
114 // render one row in the mailbox table (this could be called from one of several places)
115 function mail_render_row(msg) {
116         row     = "<tr "
117                 + "id=\"ctdl-msgsum-" + msg["msgnum"] + "\" "
118                 + "onClick=\"select_message(" + msg["msgnum"] + ");\" "
119                 //+ "onmouseenter=\"console.log('mouse in');\" "
120                 //+ "onmouseleave=\"console.log('mouse out');\""
121                 + ">"
122                 + "<td>" + msg["subject"] + "</td>"
123                 + "<td>" + msg["author"] + " &lt;" + msg["addr"] + "&gt;</td>"
124                 + "<td>" + convertTimestamp(msg["time"]) + "</td>"
125                 + "<td class=\"w3-right-align\">" + msg["msgnum"] + "</td>"
126                 + "</tr>";
127         return(row);
128 }
129
130
131 // Set up the mailbox view
132 function mail_display() {
133
134         // Put the "enter new message" button into the navbar
135         document.getElementById("ctdl-newmsg-button").innerHTML = "<i class=\"fa fa-edit\"></i>" + _("Write mail");
136         document.getElementById("ctdl-newmsg-button").style.display = "block";
137
138         // Put the mailbox message list into the stuffbar
139         document.getElementById("ctdl-stuffbar").style.display = "block";
140
141         document.getElementById("ctdl-stuffbar").innerHTML
142                 = "<div id=\"ctdl-mailbox-pane\" class=\"ctdl-mailbox-pane\"></div>"
143
144         document.getElementById("ctdl-main").innerHTML
145                 = "<div id=\"ctdl-reading-pane\" class=\"ctdl-reading-pane\"></div>"
146         ;
147
148         render_mailbox_display();
149         try {                                                   // if this was already set up, clear it so there aren't multiple
150                 clearInterval(RefreshMailboxInterval);
151         }
152         catch {
153         }
154         RefreshMailboxInterval = setInterval(refresh_mail_display, 10000);
155 }
156
157
158 // Refresh the mailbox, either for the first time or whenever needed
159 function refresh_mail_display() {
160
161         // If the "ctdl-mailbox-pane" no longer exists, the user has navigated to a different part of the site,
162         // so cancel the refresh.
163         try {
164                 document.getElementById("ctdl-mailbox-pane").innerHTML;
165         }
166         catch {
167                 console.log("ending refresh_mail_display()");
168                 clearInterval(RefreshMailboxInterval);
169                 document.getElementById("ctdl-stuffbar").style.display = "none";
170                 return;
171         }
172
173         // Ask the server if the room has been written to since our last look at it.
174         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/stat";
175         fetch_stat = async() => {
176                 response = await fetch(url);
177                 stat = await(response.json());
178                 if (stat.room_mtime > room_mtime) {
179                         room_mtime = stat.room_mtime;
180                         render_mailbox_display();
181                 }
182         }
183         fetch_stat();
184 }
185
186
187 // This is where the rendering of the message list in the mailbox view is performed.
188 function render_mailbox_display() {
189
190         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/mailbox";
191         fetch_mailbox = async() => {
192                 response = await fetch(url);
193                 msgs = await(response.json());
194                 if (response.ok) {
195
196                         box =   "<table class=\"w3-table-all w3-hoverable\" width=100%>"
197                                 + "<tr class=\"ctdl-mailbox-heading w3-blue\">"
198                                 + "<th>" + _("Subject") + "</th>"
199                                 + "<th>" + _("Sender") + "</th>"
200                                 + "<th>" + _("Date") + "</th>"
201                                 + "<th class=\"w3-right-align\">#</th>"
202                                 + "</tr>";
203
204                         for (var i=0; i<msgs.length; ++i) {
205                                 box += mail_render_row(msgs[i]);
206                         }
207
208                         box +=  "</table>";
209                         document.getElementById("ctdl-mailbox-pane").innerHTML = box;
210
211                         if (selected_message > 0) {                     // if we had a message selected, keep it selected
212                                 select_message(selected_message);
213                         }
214                 }
215         }
216         fetch_mailbox();
217 }
218
219
220 // Enter a new mail message (called by the dispatcher in views.js)
221 function mail_entmsg() {
222         alert("no handler for entering new mail yet");
223 }