Move all DOM related stuff into ui.coffee

This commit is contained in:
Serene Han 2016-02-03 20:59:13 -08:00
parent 7677707249
commit caba2cc8f8
4 changed files with 77 additions and 62 deletions

61
proxy/ui.coffee Normal file
View file

@ -0,0 +1,61 @@
###
All of Snowflake's DOM manipulation and inputs.
###
class UI
debug = false # True when there's no badge
# DOM elements references.
$msglog = null
$send = null
$input = null
$status = null
constructor: ->
@$badge = document.getElementById('badge')
debug = !@$badge
return if !debug
# Setup other DOM handlers if it's debug mode.
@$status = document.getElementById('status')
@$msglog = document.getElementById('msglog')
@$msglog.value = ''
@$send = document.getElementById('send')
@$send.onclick = => { @acceptInput }
@$input = document.getElementById('input')
@$input.focus()
@$input.onkeydown = (e) -> @$send.onclick() if 13 == e.keyCode # enter
# Status bar
setStatus: (msg) =>
return if !debug
@$status.innerHTML = 'Status: ' + msg
setActive: (connected) =>
if debug
@$msglog.className = if connected then 'active' else ''
else
# magic
# Local input from keyboard into message window.
acceptInput: =>
msg = @$input.value
if !COPY_PASTE_ENABLED
@log 'No input expected - Copy Paste Signalling disabled.'
else switch snowflake.state
when MODE.WEBRTC_CONNECTING
Signalling.receive msg
when MODE.WEBRTC_READY
@log 'No input expected - WebRTC connected.'
else
@log 'ERROR: ' + msg
@$input.value = ''
@$input.focus()
log: (msg) =>
return if !debug
# Scroll to latest
@$msglog.value += msg + '\n'
@$msglog.scrollTop = @$msglog.scrollHeight