[tor-commits] [snowflake/master] Move all DOM related stuff into ui.coffee
serene at torproject.org
serene at torproject.org
Thu Feb 4 05:43:27 UTC 2016
commit caba2cc8f8600ce43c12822549c0e80b16688f6c
Author: Serene Han <keroserene+git at gmail.com>
Date: Wed Feb 3 20:59:13 2016 -0800
Move all DOM related stuff into ui.coffee
---
proxy/Cakefile | 9 ++------
proxy/proxypair.coffee | 8 +++----
proxy/snowflake.coffee | 61 +++++++++-----------------------------------------
proxy/ui.coffee | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 77 insertions(+), 62 deletions(-)
diff --git a/proxy/Cakefile b/proxy/Cakefile
index fa37ce1..fc92934 100644
--- a/proxy/Cakefile
+++ b/proxy/Cakefile
@@ -8,6 +8,7 @@ FILES = [
'proxypair.coffee'
'websocket.coffee'
'broker.coffee'
+ 'ui.coffee'
'snowflake.coffee'
]
FILES_TEST = [
@@ -33,12 +34,6 @@ task 'test', 'snowflake unit tests', ->
throw err if err
console.log stdout + stderr
-# task 'build:embed', 'build the snowflake badge', ->
- # exec 'mkdir -p build'
- # concatCoffeeFiles()
- # copyStaticFiles()
- # compileCoffee()
-
task 'build', 'build the snowflake proxy', ->
exec 'mkdir -p build'
concatCoffeeFiles()
@@ -54,4 +49,4 @@ task 'lint', 'ensure idiomatic coffeescript', ->
task 'clean', 'remove all built files', ->
exec 'rm -r build'
- exec 'rm -r test'
+ exec 'rm -r test/snowflake.bundle.coffee'
diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee
index ac65ac5..3c62d13 100644
--- a/proxy/proxypair.coffee
+++ b/proxy/proxypair.coffee
@@ -59,15 +59,15 @@ class ProxyPair
channel.onopen = =>
log 'Data channel opened!'
snowflake.state = MODE.WEBRTC_READY
- $msglog.className = 'active' if $msglog
+ ui.setActive true
# This is the point when the WebRTC datachannel is done, so the next step
# is to establish websocket to the server.
@connectRelay()
channel.onclose = ->
log 'Data channel closed.'
- Status.set 'disconnected.'
+ ui.setStatus 'disconnected.'
snowflake.state = MODE.INIT
- $msglog.className = '' if $msglog
+ ui.setActive false
# Change this for multiplexing.
snowflake.reset()
channel.onerror = -> log 'Data channel error!'
@@ -80,7 +80,7 @@ class ProxyPair
@relay.label = 'websocket-relay'
@relay.onopen = =>
log '\nRelay ' + @relay.label + ' connected!'
- Status.set 'connected'
+ ui.setStatus 'connected'
@relay.onclose = @onClose
@relay.onerror = @onError
@relay.onmessage = @onRelayToClientMessage
diff --git a/proxy/snowflake.coffee b/proxy/snowflake.coffee
index b59c35a..60c2751 100644
--- a/proxy/snowflake.coffee
+++ b/proxy/snowflake.coffee
@@ -102,7 +102,7 @@ class Snowflake
timer = null
# Temporary countdown.
countdown = (msg, sec) ->
- Status.set msg + ' (Retrying in ' + sec + ' seconds...)'
+ ui.setStatus msg + ' (Retrying in ' + sec + ' seconds...)'
sec--
if sec >= 0
setTimeout((-> countdown(msg, sec)), 1000)
@@ -113,7 +113,7 @@ class Snowflake
clearTimeout timer
msg = 'polling for client... '
msg += '[retries: ' + @retries + ']' if @retries > 0
- Status.set msg
+ ui.setStatus msg
recv = broker.getClientOffer()
@retries++
recv.then (desc) =>
@@ -176,32 +176,8 @@ class Snowflake
snowflake = null
broker = null
+ui = null
-#
-## -- DOM & Inputs -- #
-#
-
-# DOM elements references.
-$msglog = null
-$send = null
-$input = null
-$status = null
-
-Interface =
- # 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()
# Signalling channel - just tells user to copy paste to the peer.
# Eventually this should go over the broker.
@@ -224,37 +200,20 @@ Signalling =
return false
snowflake.receiveOffer recv if desc
-log = (msg) -> # Log to the message window.
+# Log to both console and UI if applicable.
+log = (msg) ->
console.log msg
- if $msglog # Scroll to latest
- $msglog.value += msg + '\n'
- $msglog.scrollTop = $msglog.scrollHeight
-
-# Status bar
-Status =
- set: (msg) ->
- $status.innerHTML = 'Status: ' + msg if $status
+ ui.log msg
init = ->
- $badge = document.getElementById('badge')
- if !badge
- $status = document.getElementById('status')
- $msglog = document.getElementById('msglog')
- $msglog.value = ''
-
- $send = document.getElementById('send')
- $send.onclick = Interface.acceptInput
-
- $input = document.getElementById('input')
- $input.focus()
- $input.onkeydown = (e) -> $send.onclick() if 13 == e.keyCode # enter
-
- log '== snowflake browser proxy =='
+ ui = new UI()
+ log '== snowflake proxy =='
log 'Copy-Paste mode detected.' if COPY_PASTE_ENABLED
brokerUrl = Params.getString(query, 'broker', DEFAULT_BROKER)
broker = new Broker brokerUrl
snowflake = new Snowflake(broker)
- window.snowflake = snowflake
+ # window.snowflake = snowflake
+ # window.ui = ui
relayAddr = Params.getAddress(query, 'relay', DEFAULT_RELAY)
snowflake.setRelayAddr relayAddr
diff --git a/proxy/ui.coffee b/proxy/ui.coffee
new file mode 100644
index 0000000..eb54854
--- /dev/null
+++ b/proxy/ui.coffee
@@ -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
More information about the tor-commits
mailing list