chat-client/scripts/profiles.js

109 lines
2.3 KiB
JavaScript

// Create a profiles varible
var profiles;
// Create a connections varible
var connections = [];
// 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);
}
});
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)
{
}
function profiles_reload()
{
// Get the chat menu id
var chat_menu = document.getElementById("chat-menu");
// 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_connect("+
i+")'>"+profiles[i].username+"@"+profiles[i].hostname+port_display+"</a>";
// Add the profile to the menu
chat_menu.appendChild(profile);
}
}