149 lines
3.4 KiB
JavaScript
149 lines
3.4 KiB
JavaScript
|
|
// Setup some varibles
|
|
var profiles;
|
|
var connections = [];
|
|
var active_profile;
|
|
|
|
// 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;
|
|
});
|
|
|
|
function profiles_export()
|
|
{
|
|
// Write the data to the profiles file
|
|
fs.writeFileSync("profiles.json", JSON.stringify(profiles));
|
|
}
|
|
|
|
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();
|
|
|
|
// 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);
|
|
|
|
// Add the server to the active connections
|
|
connections.push(server);
|
|
}
|
|
|
|
else
|
|
{
|
|
// Push an empty connection
|
|
connections.push(undefined);
|
|
}
|
|
}
|
|
|
|
function profile_switch_to(id)
|
|
{
|
|
// Set the active profile
|
|
active_profile = id;
|
|
}
|
|
|
|
function profiles_reload()
|
|
{
|
|
// Get the chat menu id
|
|
var chat_menu = document.getElementById("chat-menu-profiles");
|
|
|
|
// 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");
|
|
|
|
// 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("+
|
|
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>\
|
|
";
|
|
}
|