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.");
21 var messages_per_page = 20;
24 // Placeholder for when we add i18n later
30 // Generate a random string of the specified length
31 // Useful for generating one-time-use div names
33 function randomString(length) {
34 var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'.split('');
37 length = Math.floor(Math.random() * chars.length);
41 for (var i = 0; i < length; i++) {
42 str += chars[Math.floor(Math.random() * chars.length)];
48 // string escape for html display
50 function escapeHTML(text) {
52 return text.replace(/[\"&<>]/g, function (a) {
63 // string escape for html display
65 function escapeHTMLURI(text) {
67 return text.replace(/./g, function (a) {
68 return '%' + a.charCodeAt(0).toString(16);
73 // string escape for JavaScript string
75 function escapeJS(text) {
77 return text.replace(/[\"\']/g, function (a) {
83 // This is called at the very beginning of the main page load.
85 function ctdl_startup() {
86 var request = new XMLHttpRequest();
87 request.open("GET", "/ctdl/c/info", true);
88 request.onreadystatechange = function() {
89 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
90 ctdl_startup_2(JSON.parse(this.responseText));
97 // Continuation of ctdl_startup() after serv_info is retrieved
99 function ctdl_startup_2(data) {
102 if (data.serv_rev_level < 905) {
103 alert("Citadel server is too old, some functions may not work");
108 // for now, show a room list in the main div
109 gotoroom("_BASEROOM_");
113 // Display a room list in the main div.
115 function display_room_list() {
116 document.getElementById("roomlist").innerHTML = "<img src=\"/ctdl/s/throbber.gif\" />" ; // show throbber while loading
118 var request = new XMLHttpRequest();
119 request.open("GET", "/ctdl/r/", true);
120 request.onreadystatechange = function() {
121 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
122 display_room_list_renderer(JSON.parse(this.responseText));
129 // Renderer for display_room_list()
131 function display_room_list_renderer(data) {
132 data = data.sort(function(a,b) {
133 if (a.floor != b.floor) {
134 return(a.floor - b.floor);
136 if (a.rorder != b.rorder) {
137 return(a.rorder - b.rorder);
139 return(a.name < b.name);
142 new_roomlist_text = "<ul>" ;
144 for (var i in data) {
146 if (data[i].floor != data[i-1].floor) {
147 new_roomlist_text = new_roomlist_text + "<li class=\"divider\"></li>" ;
150 new_roomlist_text = new_roomlist_text +
152 + "<a href=\"javascript:gotoroom('" + escapeJS(escapeHTML(data[i].name)) + "');\">"
153 + escapeHTML(data[i].name)
157 new_roomlist_text = new_roomlist_text + "</ul>";
158 document.getElementById("roomlist").innerHTML = new_roomlist_text ;
161 // Update the "banner" div with all relevant info.
163 function update_banner() {
166 document.getElementById("ctdl_banner_title").innerHTML = current_room;
167 document.title = current_room;
170 document.getElementById("ctdl_banner_title").innerHTML = serv_info.serv_humannode;
172 document.getElementById("current_user").innerHTML = current_user ;
174 document.getElementById("lilo").innerHTML = "<a href=\"/ctdl/a/logout\">" + _("Log off") + "</a>" ;
177 document.getElementById("lilo").innerHTML = "<a href=\"javascript:display_login_screen('')\">" + _("Log in") + "</a>" ;
184 function gotoroom(roomname) {
185 var request = new XMLHttpRequest();
186 request.open("GET", "/ctdl/r/" + escapeHTMLURI(roomname) + "/", true);
187 request.onreadystatechange = function() {
188 if ((this.readyState === 4) && ((this.status / 100) == 2)) {
189 gotoroom_2(JSON.parse(this.responseText));
195 function gotoroom_2(data) {
196 current_room = data.name;
197 new_messages = data.new_messages;
198 total_messages = data.total_messages;
199 current_view = data.current_view;
200 default_view = data.default_view;