summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--javascripts/dashing.coffee13
-rw-r--r--lib/dashing.rb17
2 files changed, 26 insertions, 4 deletions
diff --git a/javascripts/dashing.coffee b/javascripts/dashing.coffee
index ebf5c0a..f90c14b 100644
--- a/javascripts/dashing.coffee
+++ b/javascripts/dashing.coffee
@@ -25,6 +25,12 @@ Batman.Filters.shortenedNumber = (num) ->
num
class window.Dashing extends Batman.App
+ @on 'reload', (data) ->
+ if data.dashboard?
+ location.reload() if window.location.pathname is "/#{data.dashboard}"
+ else
+ location.reload()
+
@root ->
Dashing.params = Batman.URI.paramsFromQuery(window.location.search.slice(1));
@@ -83,7 +89,6 @@ Dashing.AnimatedValue =
@[k] = num
@set k, to
, 10
- @[k] = num
Dashing.widgets = widgets = {}
Dashing.lastEvents = lastEvents = {}
@@ -98,9 +103,13 @@ source.addEventListener 'error', (e)->
if (e.readyState == EventSource.CLOSED)
console.log("Connection closed")
-source.addEventListener 'message', (e) =>
+source.addEventListener 'message', (e) ->
data = JSON.parse(e.data)
if lastEvents[data.id]?.updatedAt != data.updatedAt
+ # /messages are internal messages, and cannot correspond to widgets.
+ # We will handle them as events on the Dashing application.
+ return Dashing.fire(data.id.slice(1), data) if data.id[0] is '/'
+
if Dashing.debugMode
console.log("Received data for #{data.id}", data)
lastEvents[data.id] = data
diff --git a/lib/dashing.rb b/lib/dashing.rb
index dc62395..4c1b44b 100644
--- a/lib/dashing.rb
+++ b/lib/dashing.rb
@@ -81,9 +81,22 @@ get '/views/:widget?.html' do
end
end
+post '/reload' do
+ request.body.rewind
+ body = JSON.parse(request.body.read)
+ auth_token = body.delete("auth_token")
+ if !settings.auth_token || settings.auth_token == auth_token
+ send_event('/reload', body)
+ 204 # response without entity body
+ else
+ status 401
+ "Invalid API key\n"
+ end
+end
+
post '/widgets/:id' do
request.body.rewind
- body = JSON.parse(request.body.read)
+ body = JSON.parse(request.body.read)
auth_token = body.delete("auth_token")
if !settings.auth_token || settings.auth_token == auth_token
send_event(params['id'], body)
@@ -110,7 +123,7 @@ def send_event(id, body)
body[:id] = id
body[:updatedAt] ||= Time.now.to_i
event = format_event(body.to_json)
- Sinatra::Application.settings.history[id] = event
+ Sinatra::Application.settings.history[id] = event unless id =~ /^\//
Sinatra::Application.settings.connections.each { |out| out << event }
end