track c_lastseen so we can use it in read operations
[citadel.git] / webcit-ng / static / js / main.js
1 //
2 // Copyright (c) 2016-2017 by the citadel.org team
3 //
4 // This program is open source software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 3.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License for more details.
11
12
13 var current_room = "_BASEROOM_";
14 var new_messages = 0;
15 var total_messages = 0;
16 var default_view = 0;
17 var current_view = 0;
18 var logged_in = 0;
19 var current_user = _("Not logged in.");
20 var serv_info;
21 var last_seen = 0;
22 var messages_per_page = 20;
23
24
25 // Placeholder for when we add i18n later
26 function _(x) {
27         return x;
28 }
29
30
31 // Generate a random string of the specified length
32 // Useful for generating one-time-use div names
33 //
34 function randomString(length) {
35         var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'.split('');
36
37         if (! length) {
38                 length = Math.floor(Math.random() * chars.length);
39         }
40
41         var str = '';
42         for (var i = 0; i < length; i++) {
43                 str += chars[Math.floor(Math.random() * chars.length)];
44         }
45         return str;
46 }
47
48
49 // string escape for html display
50 //
51 function escapeHTML(text) {
52     'use strict';
53     return text.replace(/[\"&<>]/g, function (a) {
54         return {
55                 '"': '&quot;',
56                 '&': '&amp;',
57                 '<': '&lt;',
58                 '>': '&gt;'
59         }[a];
60     });
61 }
62
63
64 // string escape for html display
65 //
66 function escapeHTMLURI(text) {
67     'use strict';
68     return text.replace(/./g, function (a) {
69         return '%' + a.charCodeAt(0).toString(16);
70     });
71 }
72
73
74 // string escape for JavaScript string
75 //
76 function escapeJS(text) {
77     'use strict';
78     return text.replace(/[\"\']/g, function (a) {
79         return '\\' + a ;
80     });
81 }
82
83
84 // This is called at the very beginning of the main page load.
85 //
86 function ctdl_startup() {
87         var request = new XMLHttpRequest();
88         request.open("GET", "/ctdl/c/info", true);
89         request.onreadystatechange = function() {
90                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
91                         ctdl_startup_2(JSON.parse(this.responseText));
92                 }
93         };
94         request.send();
95         request = null;
96 }
97
98 // Continuation of ctdl_startup() after serv_info is retrieved
99 //
100 function ctdl_startup_2(data) {
101         serv_info = data;
102
103         if (data.serv_rev_level < 905) {
104                 alert("Citadel server is too old, some functions may not work");
105         }
106
107         update_banner();
108
109         // for now, show a room list in the main div
110         gotoroom("_BASEROOM_");
111         display_room_list();
112 }
113
114 // Display a room list in the main div.
115 //
116 function display_room_list() {
117         document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ;                // show throbber while loading
118
119         var request = new XMLHttpRequest();
120         request.open("GET", "/ctdl/r/", true);
121         request.onreadystatechange = function() {
122                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
123                         display_room_list_renderer(JSON.parse(this.responseText));
124                 }
125         };
126         request.send();
127         request = null;
128 }
129
130 // Renderer for display_room_list()
131 //
132 function display_room_list_renderer(data) {
133         data = data.sort(function(a,b) {
134                 if (a.floor != b.floor) {
135                         return(a.floor - b.floor);
136                 }
137                 if (a.rorder != b.rorder) {
138                         return(a.rorder - b.rorder);
139                 }
140                 return(a.name < b.name);
141         });
142
143         new_roomlist_text = "<ul>" ;
144
145         for (var i in data) {
146                 if (i > 0) {
147                         if (data[i].floor != data[i-1].floor) {
148                                 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
149                         }
150                 }
151                 new_roomlist_text = new_roomlist_text +
152                         "<li>"
153                         + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
154                         + escapeHTML(data[i].name)
155                         + "</a></li>"
156                 ;
157         }
158         new_roomlist_text = new_roomlist_text + "</ul>";
159         document.getElementById("roomlist").innerHTML = new_roomlist_text ;
160 }
161
162 // Update the "banner" div with all relevant info.
163 //
164 function update_banner() {
165         detect_logged_in();
166         if (current_room) {
167                 document.getElementById("ctdl_banner_title").innerHTML = current_room;
168                 document.title = current_room;
169         }
170         else {
171                 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
172         }
173         document.getElementById("current_user").innerHTML = current_user ;
174         if (logged_in) {
175                 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
176         }
177         else {
178                 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
179         }
180 }
181
182
183 // goto room
184 //
185 function gotoroom(roomname) {
186         var request = new XMLHttpRequest();
187         request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
188         request.onreadystatechange = function() {
189                 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
190                         gotoroom_2(JSON.parse(this.responseText));
191                 }
192         };
193         request.send();
194         request = null;
195 }
196 function gotoroom_2(data) {
197         current_room = data.name;
198         new_messages = data.new_messages;
199         total_messages = data.total_messages;
200         current_view = data.current_view;
201         default_view = data.default_view;
202         last_seen = data.last_seen;
203         update_banner();
204         render_room_view();
205 }