-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add theming, persist options on test page, separate out test page js.
- Loading branch information
Showing
4 changed files
with
345 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
var login = document.querySelector(".login"); | ||
var stat = document.querySelector(".status"); | ||
var chat = document.querySelector(".chat"); | ||
var menu = document.querySelector(".menu"); | ||
var client = new LichatClient(); | ||
var ui = new LichatUI(chat, client); | ||
|
||
var fail = function(reason){ | ||
chat.style.display = "none"; | ||
stat.style.display = "block"; | ||
login.style.display = "block"; | ||
stat.innerHTML = reason; | ||
window.onbeforeunload = ()=>{ | ||
return true; | ||
}; | ||
ui.reset(); | ||
}; | ||
|
||
var save = (name, value)=>{ | ||
if(window.localStorage){ | ||
window.localStorage.setItem(name, JSON.stringify(value)); | ||
} | ||
return value; | ||
}; | ||
|
||
var load = (name, def)=>{ | ||
if(window.localStorage){ | ||
var value = window.localStorage.getItem(name); | ||
return (value)? JSON.parse(value) : def; | ||
}else{ | ||
return def; | ||
} | ||
}; | ||
|
||
var setup = ()=>{ | ||
login.querySelector("input[name=username]").value = load("username", "Lion"); | ||
login.querySelector("input[name=password]").value = load("password", ""); | ||
login.querySelector("input[name=hostname]").value = load("hostname", "localhost"); | ||
login.querySelector("input[name=port]").value = load("port", "1113"); | ||
login.querySelector("input[name=channel]").value = load("channel", ""); | ||
login.querySelector("select[name=theme]").value = load("theme", "light"); | ||
document.querySelector("body").setAttribute("class", load("theme", "light")); | ||
}; | ||
|
||
login.querySelector("[name=theme]").addEventListener("change", (ev)=>{ | ||
document.querySelector("body").setAttribute("class", ev.target.value); | ||
save("theme", ev.target.value); | ||
}); | ||
|
||
client.handleFailure = (e)=>{ | ||
console.log("Failure:",e); | ||
fail(e+""); | ||
}; | ||
|
||
client.addHandler("DISCONNECT", (client,update)=>{ | ||
fail("Disconnected"); | ||
}); | ||
|
||
client.addHandler("CONNECT", (client,update)=>{ | ||
stat.style.display = "none"; | ||
chat.style.display = ""; | ||
|
||
var channel = login.querySelector("input[name=channel]").value; | ||
if(channel){ | ||
setTimeout(()=> ui.invokeCommand("join", channel), 500); | ||
} | ||
|
||
window.onbeforeunload = ()=>{ | ||
return "Are you sure you want to leave?"; | ||
}; | ||
}); | ||
|
||
login.addEventListener("submit", (ev)=>{ | ||
ev.preventDefault() | ||
login.style.display = "none"; | ||
client.username = login.querySelector("input[name=username]").value; | ||
client.password = login.querySelector("input[name=password]").value; | ||
client.hostname = login.querySelector("input[name=hostname]").value; | ||
client.port = parseInt(login.querySelector("input[name=port]").value); | ||
save("username", client.username); | ||
save("password", client.password); | ||
save("hostname", client.hostname); | ||
save("port", client.port); | ||
save("channel", login.querySelector("input[name=channel]").value); | ||
stat.style.display = "block"; | ||
stat.innerHTML = "Connecting..."; | ||
client.openConnection(); | ||
return false; | ||
}, false); | ||
|
||
chat.querySelector("[type=submit]").addEventListener("click", (ev)=>{ | ||
ui.processInput(); | ||
}, false); | ||
|
||
chat.querySelector("[type=file]").addEventListener("change", (ev)=>{ | ||
ui.sendFile(chat.querySelector("[type=file]").files[0]); | ||
chat.querySelector("[type=file]").value = null; | ||
}, false); | ||
|
||
menu.querySelector("[data-action=create]").addEventListener("click", (ev)=>{ | ||
var name = prompt("Please enter a channel name:"); | ||
if(name) ui.invokeCommand("create", name); | ||
}, false); | ||
|
||
menu.querySelector("[data-action=join]").addEventListener("click", (ev)=>{ | ||
var name = prompt("Please enter a channel name:"); | ||
if(name) ui.invokeCommand("join", name); | ||
}, false); | ||
|
||
menu.querySelector("[data-action=leave]").addEventListener("click", (ev)=>{ | ||
if(name) ui.invokeCommand("leave"); | ||
}, false); | ||
|
||
setup(); |
Oops, something went wrong.