chat-client/scripts/profiles.js

243 lines
5.4 KiB
JavaScript
Raw Normal View History

2019-04-10 15:47:50 +10:00
// Setup some varibles
2019-04-10 15:47:50 +10:00
var profiles;
var connections = [];
var active_profile;
2019-04-10 15:47:50 +10:00
// Load the profiles
fs.readFile("profiles.json", "utf-8", function(err, data)
{
try
{
// Throw an error if there is one
if(err) throw err;
// Try to load it from the data
profiles = JSON.parse(data);
}
catch(e)
{
// Set the profiles varible to its default value
profiles = [];
}
// Reload the profiles
profiles_reload();
// Connect to all the profiles
for(var i=0;i<profiles.length;i++)
{
// Connect to the server
profile_connect(i);
}
document.getElementById("server-menu-profiles").style.bottom =
document.getElementById("server-menu-config").style.height;
2019-04-10 15:47:50 +10:00
});
function profiles_export()
{
// Write the data to the profiles file
fs.writeFileSync("profiles.json", JSON.stringify(profiles));
}
2019-04-10 15:47:50 +10:00
function profile_save()
{
// Get the values from the form
var hostname = document.getElementById("edit_profile_hostname").value;
var port = document.getElementById("edit_profile_port").value;
var username = document.getElementById("edit_profile_username").value;
var password = document.getElementById("edit_profile_password").value;
// Push the profiles data
profiles.push({
hostname: hostname,
port: port,
username: username,
password: password
});
// Write the data to the profiles file
profiles_export();
2019-04-10 15:47:50 +10:00
// Reload the profiles
profiles_reload();
// Start a connection
profile_connect(profiles.length-1);
}
function profile_connect(id)
{
// Is the profile set
if(profiles[id])
{
// Connect to the server
var server = connect(profiles[id], connections.length);
2019-04-10 15:47:50 +10:00
// Add the server to the active connections
connections.push(server);
}
else
{
// Push an empty connection
connections.push(undefined);
}
2019-04-10 15:47:50 +10:00
}
2019-04-13 10:10:18 +10:00
function port_display(i)
{
// Should the port be displayed
var port_display = "";
// Is the port not the default port
if(profiles[i].port != PORT_DEFAULT)
{
// Set the port display varible
port_display = ":"+profiles[i].port.toString();
}
return port_display;
}
function title(id) {
return profiles[id].username+"@"+profiles[id].hostname+port_display(id);
}
2019-04-10 15:47:50 +10:00
function profile_switch_to(id)
{
// Set the active profile
active_profile = id;
// Create a chats menu
var chats_menu = "";
// Loop over the chats
for(var i=0;i<connections[id].chats.length;i++)
{
// Add to the chats menu
chats_menu += "<li><a href='#' onclick=chat_switch_to("+i+")>"
+connections[id].chats[i].name+"</a></li>";
}
// Create a users menu
var users_menu = "";
// Loop over the users
var users_array = Object.keys(connections[id].users);
for(var i=0;i<users_array.length;i++)
{
// Add to the chats menu
users_menu += "<li><a href='#'>"+users_array[i]+"</a></li>";
}
// Set the html data
document.getElementById("chat-main").innerHTML = "\
<div id='chats-menu' class='menu-vertical'>\
2019-04-13 10:10:18 +10:00
<ul>\
<li><a class='add-chat' href='#' onclick='add_chat_menu()'><b>Add</b></a></li>\
"+chats_menu+"\
</ul>\
</div>\
<div id='chat-area'>\
</div>\
<div id='users-menu' class='menu-vertical'>\
2019-04-13 10:10:18 +10:00
<ul>"+users_menu+"</ul>\
</div>\
";
2019-04-13 10:10:18 +10:00
// Set the title
document.title = title(id);
}
function chat_switch_to(id)
{
// Set the HTML
document.getElementById("chat-area").innerHTML = "\
<div class='chat-content'></div>\
2019-04-13 14:40:31 +10:00
<div class='chat-content-send'>\
<textarea id='chat-content-send-textbox'></textarea>\
<button id='chat-content-send-button'>Send</button>\
</div>\
";
// Set the title
document.title = title(active_profile)+" - "
+connections[active_profile].chats[id].name;
2019-04-13 10:10:18 +10:00
}
function add_chat_menu()
{
// Set the html data
document.getElementById("chat-area").innerHTML = "\
<p>\
<table>\
<tr><td>Name</td><td><input type='text' id='add-chat-name'></td></tr>\
</table>\
<button onclick='add_chat()'>Add</button>\
</p>\
";
}
function add_chat()
{
// Send the data to the server
socket_write(
connections[active_profile].client,
connections[active_profile].encryption,
{
mode: "new_chat",
name: document.getElementById('add-chat-name').value
}
);
2019-04-10 15:47:50 +10:00
}
function profiles_reload()
{
// Get the chat menu id
var chat_menu = document.getElementById("server-menu-profiles");
2019-04-10 15:47:50 +10:00
// Reset it
chat_menu.innerHTML = "";
// Loop over the profiles
for(var i=0;i<profiles.length;i++)
{
// Is the profile defined
if(profiles[i])
{
// Create a list item
var profile = document.createElement("li");
2019-04-10 15:47:50 +10:00
// Set the html for the list item
profile.innerHTML = "<a href='javascript:void(0)' onclick='profile_switch_to("+
i+")'>"+title(i)+"</a>";
2019-04-10 15:47:50 +10:00
// Add the profile to the menu
chat_menu.appendChild(profile);
}
2019-04-10 15:47:50 +10:00
}
}
function switch_new_profile()
{
// Set the html
2019-04-13 10:10:18 +10:00
document.title = "New Profile";
document.getElementById("chat-main").innerHTML = "\
<p>New Profile:</p>\
<p>\
<table>\
<tr><td>Hostname</td><td><input type='text' id='edit_profile_hostname'></td></tr>\
<tr><td>Port</td><td><input type='text' id='edit_profile_port' value='22068'></td></tr>\
<tr><td>Username</td><td><input type='text', id='edit_profile_username'></td></tr>\
<tr><td>Password</td><td><input type='password' id='edit_profile_password'></td></tr>\
</table>\
<button onclick='profile_save()'>Save Profile</button>\
</p>\
";
}