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