2 // Copyright (c) 2016-2017 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.");
23 // Placeholder for when we add i18n later
29 // Generate a random string of the specified length
30 // Useful for generating one-time-use div names
32 function randomString(length) {
33 var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
36 length = Math.floor(Math.random() * chars.length);
40 for (var i = 0; i < length; i++) {
41 str += chars[Math.floor(Math.random() * chars.length)];
47 // string escape for html display
49 function escapeHTML(text) {
51 return text.replace(/[\"&<>]/g, function (a) {
62 // string escape for html display
64 function escapeHTMLURI(text) {
66 return text.replace(/./g, function (a) {
67 return '%' + a.charCodeAt(0).toString(16);
72 // string escape for JavaScript string
74 function escapeJS(text) {
76 return text.replace(/[\"\']/g, function (a) {
82 // This is called at the very beginning of the main page load.
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));
96 // Continuation of ctdl_startup() after serv_info is retrieved
98 function ctdl_startup_2(data) {
101 if (data.serv_rev_level < 905) {
102 alert("Citadel server is too old, some functions may not work");
107 // for now, show a room list in the main div
108 gotoroom("_BASEROOM_");
112 // Display a room list in the main div.
114 function display_room_list() {
115 document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ; // show throbber while loading
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));
128 // Renderer for display_room_list()
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);
135 if (a.rorder != b.rorder) {
136 return(a.rorder - b.rorder);
138 return(a.name < b.name);
141 new_roomlist_text = "<ul>" ;
143 for (var i in data) {
145 if (data[i].floor != data[i-1].floor) {
146 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
149 new_roomlist_text = new_roomlist_text +
151 + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
152 + escapeHTML(data[i].name)
156 new_roomlist_text = new_roomlist_text + "</ul>";
157 document.getElementById("roomlist").innerHTML = new_roomlist_text ;
160 // Update the "banner" div with all relevant info.
162 function update_banner() {
165 document.getElementById("ctdl_banner_title").innerHTML = current_room;
168 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
170 document.getElementById("current_user").innerHTML = current_user ;
172 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
175 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
182 function gotoroom(roomname) {
183 var request = new XMLHttpRequest();
184 request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
185 request.onreadystatechange = function() {
186 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
187 gotoroom_2(JSON.parse(this.responseText));
193 function gotoroom_2(data) {
194 current_room = data.name;
195 new_messages = data.new_messages;
196 total_messages = data.total_messages;
197 current_view = data.current_view;
198 default_view = data.default_view;