chat-client/scripts/profiles.js

129 lines
3.1 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("chat-menu-profiles").style.bottom =
document.getElementById("chat-menu-config").style.height;
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
fs.writeFileSync("profiles.json", JSON.stringify(profiles));
// Reload the profiles
profiles_reload();
// Start a connection
profile_connect(profiles.length-1);
}
function profile_connect(id)
{
// Connect to the server
var server = connect(profiles[id], connections.length);
// Add the server to the active connections
connections.push(server);
}
function profile_switch_to(id)
{
// Set the active profile
active_profile = id;
2019-04-10 15:47:50 +10:00
}
function profiles_reload()
{
// Get the chat menu id
var chat_menu = document.getElementById("chat-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++)
{
// Create a list item
var profile = document.createElement("li");
// 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();
}
// Set the html for the list item
profile.innerHTML = "<a href='javascript:void(0)' onclick='profile_switch_to("+
2019-04-10 15:47:50 +10:00
i+")'>"+profiles[i].username+"@"+profiles[i].hostname+port_display+"</a>";
// Add the profile to the menu
chat_menu.appendChild(profile);
}
}
function switch_new_profile()
{
// Set the html
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>\
";
}