Work-in-progress Web Interface

This commit is contained in:
Carlos
2019-08-12 02:53:46 +02:00
parent 5d1a193a39
commit 49674f7ffe
6 changed files with 338 additions and 0 deletions

View File

@ -0,0 +1,27 @@
/**
Main JS file for WebConsole.
Version v1.0.0
https://github.com/mesacarlos
2019 Carlos Mesa under MIT License.
*/
/**
* Update dropdown with saved server list
*/
function updateServerList(){
//Delete all servers in dropdown
$('.servermenuitem').remove();
//Add all servers
var servers = new WebConsolePersistenceManager().getAllServers();
for(var i = 0; i < servers.length; i++){
$('#ServerListDropDown').append('<a class="dropdown-item servermenuitem" href="#" onclick=openServer("' + servers[i].serverName + '")>' + servers[i].serverName + '</a>');
}
}
/**
* Prepare and show server to user
*/
function openServer(serverName){
}

View File

@ -0,0 +1,96 @@
/**
WebConsole Connector for WebConsole v1.0.0
Used to connect to WebSocketsServer
https://github.com/mesacarlos
2019 Carlos Mesa under MIT License.
*/
/*
USAGE
1 Create needed GUI
2 Create a object of this class
3 subscribe a function to receive the login required message
4 show password modal
5 subscribe console output list etc...
*/
class WebConsoleConnector {
constructor(serverName, serverURI) {
this.serverName = serverName;
this.serverURI = serverURI;
this.subscribers = []; //List of functions called when a new message arrive
this.messages = []; //All messages retrieved since connection start
}
/**
* Connect to WebSocket
*/
connect(){
var connector = this;
this.websocket = new WebSocket(this.serverURI);
this.websocket.onopen = function(evt) { connector.onOpen(evt) };
this.websocket.onclose = function(evt) { connector.onClose(evt) };
this.websocket.onmessage = function(evt) { connector.onMessage(evt) };
this.websocket.onerror = function(evt) { connector.onError(evt) };
}
/**
* Internal function
*/
onOpen(evt){
//TODO Check que la version es correcta, y que es un WebSocket del plugin y no de otra cosa
//No es necesario notificar al usuario porque ya se recibe un console output de ello
}
/**
* Internal function
*/
onClose(evt){
//TODO
}
/**
* Internal function
*/
onMessage(evt){
var obj = JSON.parse(evt.data);
this.notify(obj); //Notify all subscribers
this.messages.push(obj);
}
/**
* Internal function
*/
onError(evt){
//TODO
}
/**
* Sends a WebSocket command to Server
*/
sendToServer(message){
this.websocket.send(message);
}
/**
* Notifies a new message to all subscribers
*/
notify(obj){
this.subscribers.forEach(function(fun) {
fun(obj); //Calls function with this object
});
}
/**
* Adds a function to subscriber list
*/
subscribe(func){
this.subscribers.push(func);
}
/**
* Unsubscribe all subscribers
*/
removeSubscribers(){
this.subscribers = [];
}
}

View File

@ -0,0 +1,30 @@
/**
WebConsole Manager for WebConsole v1.0.0
Used to manage active connections
https://github.com/mesacarlos
2019 Carlos Mesa under MIT License.
*/
class WebConsoleManager {
constructor(){
this.activeConnections = [];
}
/**
* Adds connection to list
*/
addConnection(connection){
this.activeConnections.push(connection);
}
/**
* Retrieve server by name
*/
getConnection(serverName){
for (i = 0; i < this.activeConnections.length; i++) {
if(this.activeConnections[i].serverName == serverName){
return this.activeConnections[i];
}
}
}
}

View File

@ -0,0 +1,89 @@
/**
WebConsole Persistence Manager for WebConsole v1.0.0
Used to save your servers into your browser
https://github.com/mesacarlos
2019 Carlos Mesa under MIT License.
*/
class WebConsolePersistenceManager{
/**
* Saves server into WebStorage
*/
saveServer(serverName, serverURI, serverPassword){
this.createListIfUndefined();
//Create anonymous object
var server = new Object();
server.serverName = serverName;
server.serverURI = serverURI;
server.serverPassword = serverPassword;
//Save to WebStorage
var servers = this.getAllServers();
servers.push(server);
this.replaceAllServers(servers);
}
/**
* Delete server from saved servers
*/
deleteServer(serverName){
this.createListIfUndefined();
//Find server
var index = -1;
var servers = this.getAllServers();
for (i = 0; i < servers.length; i++) {
if(servers[i].serverName == serverName){
index = i;
}
}
//Delete it
if(index > -1){
servers.splice(index, 1);
}
//Save to WebStorage
this.replaceAllServers(servers);
}
/**
* Get server details as object
*/
getServer(serverName){
this.createListIfUndefined();
var servers = this.getAllServers();
for (i = 0; i < servers.length; i++) {
if(servers[i].serverName == serverName){
return servers[i];
}
}
}
/**
* Get all servers
*/
getAllServers(){
this.createListIfUndefined();
return JSON.parse(window.localStorage.servers);
}
/**
* Create server list if not defined
*/
createListIfUndefined(){
if (typeof window.localStorage.servers === 'undefined') {
window.localStorage.servers = JSON.stringify(new Array());
}
}
/**
* Replaces all server list with provided list
*/
replaceAllServers(newServerList){
window.localStorage.servers = JSON.stringify(newServerList);
}
}