Adding timestamps

resolves #66
This commit is contained in:
joeyak 2019-03-28 20:54:29 -04:00
parent 10092e1dd5
commit ee2e92a9a5
3 changed files with 45 additions and 11 deletions

View File

@ -193,6 +193,11 @@ function changeColor() {
} }
} }
function setTimestamp(v) {
showTimestamp(v)
document.cookie = "timestamp=" + v
}
// Get the websocket setup in a function so it can be recalled // Get the websocket setup in a function so it can be recalled
function setupWebSocket() { function setupWebSocket() {
ws = new WebSocket(getWsUri()); ws = new WebSocket(getWsUri());
@ -246,6 +251,11 @@ function defaultValues() {
$("#colorRed").val(0).trigger("input"); $("#colorRed").val(0).trigger("input");
$("#colorGreen").val(0).trigger("input"); $("#colorGreen").val(0).trigger("input");
$("#colorBlue").val(0).trigger("input"); $("#colorBlue").val(0).trigger("input");
let timestamp = getCookie("timestamp")
if (timestamp !== "") {
showTimestamp(timestamp)
}
} }
window.addEventListener("onresize", updateSuggestionCss); window.addEventListener("onresize", updateSuggestionCss);

View File

@ -57,6 +57,11 @@
{{end}} {{end}}
</div> </div>
<hr /> <hr />
<label class="contrast">
<input type="checkbox" checked="false" onchange="setTimestamp(this.checked);" />
Show Timestamp
</label>
<hr />
<div id="hiddencolor" class="hiddendiv"> <div id="hiddencolor" class="hiddendiv">
<div class="range-div" style="background-image: linear-gradient(to right, transparent, red);"> <div class="range-div" style="background-image: linear-gradient(to right, transparent, red);">
<input id="colorRed" type="range" min="0" max="255" value="0" oninput="updateColor();" /> <input id="colorRed" type="range" min="0" max="255" value="0" oninput="updateColor();" />

View File

@ -11,8 +11,9 @@ import (
) )
var ( var (
auth common.CommandLevel timestamp bool
color string color string
auth common.CommandLevel
) )
func recieve(v []js.Value) { func recieve(v []js.Value) {
@ -24,7 +25,7 @@ func recieve(v []js.Value) {
chatJSON, err := common.DecodeData(v[0].String()) chatJSON, err := common.DecodeData(v[0].String())
if err != nil { if err != nil {
fmt.Printf("Error decoding data: %s\n", err) fmt.Printf("Error decoding data: %s\n", err)
js.Call("appendMessages", v) js.Call("appendMessages", fmt.Sprintf("<div>%v</div>", v))
return return
} }
@ -57,7 +58,7 @@ func recieve(v []js.Value) {
// on join or leave, update list of possible user names // on join or leave, update list of possible user names
fallthrough fallthrough
case common.DTChat: case common.DTChat:
js.Call("appendMessages", chat.Data.HTML()) appendMessage(chat.Data.HTML())
case common.DTCommand: case common.DTCommand:
d := chat.Data.(common.DataCommand) d := chat.Data.(common.DataCommand)
@ -76,18 +77,26 @@ func recieve(v []js.Value) {
js.Call("initPlayer", nil) js.Call("initPlayer", nil)
case common.CmdPurgeChat: case common.CmdPurgeChat:
js.Call("purgeChat", nil) js.Call("purgeChat", nil)
js.Call("appendMessages", d.HTML()) appendMessage(d.HTML())
case common.CmdHelp: case common.CmdHelp:
url := "/help" url := "/help"
if d.Arguments != nil && len(d.Arguments) > 0 { if d.Arguments != nil && len(d.Arguments) > 0 {
url = d.Arguments[0] url = d.Arguments[0]
} }
js.Call("appendMessages", d.HTML()) appendMessage(d.HTML())
js.Get("window").Call("open", url, "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600") js.Get("window").Call("open", url, "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600")
} }
} }
} }
func appendMessage(msg string) {
if timestamp {
h, m, _ := time.Now().Clock()
msg = fmt.Sprintf(`<span class="time">%d:%d</span> %s`, h, m, msg)
}
js.Call("appendMessages", "<div>"+msg+"</div>")
}
func websocketSend(msg string, dataType common.ClientDataType) error { func websocketSend(msg string, dataType common.ClientDataType) error {
if strings.TrimSpace(msg) == "" && dataType == common.CdMessage { if strings.TrimSpace(msg) == "" && dataType == common.CdMessage {
return nil return nil
@ -126,6 +135,14 @@ func showChatError(err error) {
} }
} }
func showTimestamp(v []js.Value) {
if len(v) != 1 {
// Don't bother with returning a value
return
}
timestamp = v[0].Bool()
}
func isValidColor(this js.Value, v []js.Value) interface{} { func isValidColor(this js.Value, v []js.Value) interface{} {
if len(v) != 1 { if len(v) != 1 {
return false return false
@ -141,11 +158,12 @@ func isValidName(this js.Value, v []js.Value) interface{} {
} }
func debugValues(v []js.Value) { func debugValues(v []js.Value) {
fmt.Printf("auth %#v\n", auth) fmt.Printf("timestamp: %#v\n", timestamp)
fmt.Printf("color %#v\n", color) fmt.Printf("auth: %#v\n", auth)
fmt.Printf("currentName %#v\n", currentName) fmt.Printf("color: %#v\n", color)
fmt.Printf("names %#v\n", names) fmt.Printf("currentName: %#v\n", currentName)
fmt.Printf("filteredNames %#v\n", filteredNames) fmt.Printf("names: %#v\n", names)
fmt.Printf("filteredNames: %#v\n", filteredNames)
} }
func main() { func main() {
@ -157,6 +175,7 @@ func main() {
js.Set("recieveMessage", js.CallbackOf(recieve)) js.Set("recieveMessage", js.CallbackOf(recieve))
js.Set("processMessage", js.CallbackOf(processMessage)) js.Set("processMessage", js.CallbackOf(processMessage))
js.Set("debugValues", js.CallbackOf(debugValues)) js.Set("debugValues", js.CallbackOf(debugValues))
js.Set("showTimestamp", js.CallbackOf(showTimestamp))
// This is needed so the goroutine does not end // This is needed so the goroutine does not end
for { for {