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