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