2 // Copyright (c) 2016-2018 by the citadel.org team
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.
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.
13 var current_room = "_BASEROOM_";
15 var total_messages = 0;
19 var current_user = _("Not logged in.");
22 var messages_per_page = 20;
25 // Placeholder for when we add i18n later
31 // Generate a random string of the specified length
32 // Useful for generating one-time-use div names
34 function randomString(length) {
35 var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'.split('');
38 length = Math.floor(Math.random() * chars.length);
42 for (var i = 0; i < length; i++) {
43 str += chars[Math.floor(Math.random() * chars.length)];
49 // string escape for html display
51 function escapeHTML(text) {
53 return text.replace(/[\"&<>]/g, function (a) {
64 // string escape for html display
66 function escapeHTMLURI(text) {
68 return text.replace(/./g, function (a) {
69 return '%' + a.charCodeAt(0).toString(16);
74 // string escape for JavaScript string
76 function escapeJS(text) {
78 return text.replace(/[\"\']/g, function (a) {
84 // This is called at the very beginning of the main page load.
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));
98 // Continuation of ctdl_startup() after serv_info is retrieved
100 function ctdl_startup_2(data) {
103 if (data.serv_rev_level < 905) {
104 alert("Citadel server is too old, some functions may not work");
109 // for now, show a room list in the main div
110 gotoroom("_BASEROOM_");
114 // Display a room list in the main div.
116 function display_room_list() {
117 document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ; // show throbber while loading
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));
130 // Renderer for display_room_list()
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);
137 if (a.rorder != b.rorder) {
138 return(a.rorder - b.rorder);
140 return(a.name < b.name);
143 new_roomlist_text = "<ul>" ;
145 for (var i in data) {
147 if (data[i].floor != data[i-1].floor) {
148 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
151 new_roomlist_text = new_roomlist_text +
153 + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
154 + escapeHTML(data[i].name)
158 new_roomlist_text = new_roomlist_text + "</ul>";
159 document.getElementById("roomlist").innerHTML = new_roomlist_text ;
162 // Update the "banner" div with all relevant info.
164 function update_banner() {
167 document.getElementById("ctdl_banner_title").innerHTML = current_room;
168 document.title = current_room;
171 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
173 document.getElementById("current_user").innerHTML = current_user ;
175 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
178 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
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));
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;
204 render_room_view(0, 9999999999);