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