snowflake/proxy/ui.coffee
Cecylia Bocovich 788f2a595f Fixed linting errors/warnings and tests
The test failure was due to pollBorker waiting the full 5 seconds before
polling for the first time.

The lint errors were some trailing whitespace and unecessary fat arrows
2019-07-02 10:01:34 -04:00

100 lines
2 KiB
CoffeeScript

###
All of Snowflake's DOM manipulation and inputs.
###
class UI
active: false
enabled: false
setStatus: (msg) ->
setActive: (connected) ->
@active = connected
log: (msg) ->
class BadgeUI extends UI
$badge: null
constructor: ->
@$badge = document.getElementById('badge')
setActive: (connected) ->
super connected
@$badge.className = if connected then 'active' else ''
class DebugUI extends UI
# DOM elements references.
$msglog: null
$status: null
constructor: ->
# Setup other DOM handlers if it's debug mode.
@$status = document.getElementById('status')
@$msglog = document.getElementById('msglog')
@$msglog.value = ''
# Status bar
setStatus: (msg) ->
txt = document.createTextNode('Status: ' + msg)
while @$status.firstChild
@$status.removeChild @$status.firstChild
@$status.appendChild txt
setActive: (connected) ->
super connected
@$msglog.className = if connected then 'active' else ''
log: (msg) ->
# Scroll to latest
@$msglog.value += msg + '\n'
@$msglog.scrollTop = @$msglog.scrollHeight
class WebExtUI extends UI
port: null
stats: null
constructor: ->
@initStats()
chrome.runtime.onConnect.addListener @onConnect
initStats: ->
@stats = [0]
setInterval (() =>
@stats.unshift 0
@stats.splice 24
@postActive()
), 60 * 60 * 1000
postActive: ->
@port?.postMessage
active: @active
total: @stats.reduce ((t, c) ->
t + c
), 0
enabled: @enabled
onConnect: (port) =>
@port = port
port.onDisconnect.addListener @onDisconnect
port.onMessage.addListener @onMessage
@postActive()
onMessage: (m) =>
@enabled = m.enabled
update()
@postActive()
onDisconnect: (port) =>
@port = null
setActive: (connected) ->
super connected
if connected then @stats[0] += 1
@postActive()
chrome.browserAction.setIcon
path:
32: "icons/status-" + (if connected then "on" else "off") + ".png"