my_dashboard 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/my_dashboard/version.rb +1 -1
- data/vendor/assets/javascripts/my_dashboard/my_dashboard.coffee +127 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c38526e7f894d0e46e9d11c665e8b1bdbaef1719
|
4
|
+
data.tar.gz: 39d40a9513598287e1eb8075123646057790c051
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7a048cbf4d36cae5a064cd84138a6478f4aabfe342fcf6ed8923e9202c1eb1c930f9ee9451b6e0c4f1ebae218e07569225c4b24b30a303161768269d639a648
|
7
|
+
data.tar.gz: 50d183b1b22149eadf7bb55966b23033b99ca5c4689b3a687b65fd7c94058fb936cc1b9e0dd50e9a2d89cabaffbee80a6545da2ed206c215811fee27539db749
|
data/CHANGELOG.md
CHANGED
data/lib/my_dashboard/version.rb
CHANGED
@@ -0,0 +1,127 @@
|
|
1
|
+
Batman.config.pathPrefix = '/'
|
2
|
+
Batman.config.viewPrefix = '/my_dashboard/widgets/'
|
3
|
+
|
4
|
+
Batman.Filters.prettyNumber = (num) ->
|
5
|
+
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") unless isNaN(num)
|
6
|
+
|
7
|
+
Batman.Filters.dashize = (str) ->
|
8
|
+
dashes_rx1 = /([A-Z]+)([A-Z][a-z])/g;
|
9
|
+
dashes_rx2 = /([a-z\d])([A-Z])/g;
|
10
|
+
|
11
|
+
return str.replace(dashes_rx1, '$1_$2').replace(dashes_rx2, '$1_$2').replace(/_/g, '-').toLowerCase()
|
12
|
+
|
13
|
+
Batman.Filters.shortenedNumber = (num) ->
|
14
|
+
return num if isNaN(num)
|
15
|
+
if num >= 1000000000
|
16
|
+
(num / 1000000000).toFixed(1) + 'B'
|
17
|
+
else if num >= 1000000
|
18
|
+
(num / 1000000).toFixed(1) + 'M'
|
19
|
+
else if num >= 1000
|
20
|
+
(num / 1000).toFixed(1) + 'K'
|
21
|
+
else
|
22
|
+
num
|
23
|
+
|
24
|
+
class window.MyDashboard extends Batman.App
|
25
|
+
@on 'reload', (data) ->
|
26
|
+
window.location.reload(true)
|
27
|
+
|
28
|
+
@root ->
|
29
|
+
MyDashboard.params = Batman.URI.paramsFromQuery(window.location.search.slice(1));
|
30
|
+
|
31
|
+
class MyDashboard.Widget extends Batman.View
|
32
|
+
constructor: ->
|
33
|
+
# Set the view path
|
34
|
+
@constructor::source = Batman.Filters.underscore(@constructor.name)
|
35
|
+
super
|
36
|
+
|
37
|
+
@mixin($(@node).data())
|
38
|
+
MyDashboard.widgets[@id] ||= []
|
39
|
+
MyDashboard.widgets[@id].push(@)
|
40
|
+
|
41
|
+
type = Batman.Filters.dashize(@view)
|
42
|
+
$(@node).addClass("widget widget-#{type} #{@id}")
|
43
|
+
|
44
|
+
@accessor 'updatedAtMessage', ->
|
45
|
+
if updatedAt = @get('updatedAt')
|
46
|
+
timestamp = new Date(updatedAt * 1000)
|
47
|
+
hours = timestamp.getHours()
|
48
|
+
minutes = ("0" + timestamp.getMinutes()).slice(-2)
|
49
|
+
"Last updated at #{hours}:#{minutes}"
|
50
|
+
|
51
|
+
@::on 'ready', ->
|
52
|
+
MyDashboard.Widget.fire 'ready'
|
53
|
+
|
54
|
+
# In case the events from the server came before the widget was rendered
|
55
|
+
lastData = MyDashboard.lastEvents[@id]
|
56
|
+
if lastData
|
57
|
+
@mixin(lastData)
|
58
|
+
@onData(lastData)
|
59
|
+
|
60
|
+
receiveData: (data) =>
|
61
|
+
@mixin(data)
|
62
|
+
@onData(data)
|
63
|
+
|
64
|
+
onData: (data) =>
|
65
|
+
# Widgets override this to handle incoming data
|
66
|
+
|
67
|
+
MyDashboard.AnimatedValue =
|
68
|
+
get: Batman.Property.defaultAccessor.get
|
69
|
+
set: (k, to) ->
|
70
|
+
if !to? || isNaN(to)
|
71
|
+
@[k] = to
|
72
|
+
else
|
73
|
+
timer = "interval_#{k}"
|
74
|
+
num = if (!isNaN(@[k]) && @[k]?) then @[k] else 0
|
75
|
+
unless @[timer] || num == to
|
76
|
+
to = parseFloat(to)
|
77
|
+
num = parseFloat(num)
|
78
|
+
up = to > num
|
79
|
+
num_interval = Math.abs(num - to) / 90
|
80
|
+
@[timer] =
|
81
|
+
setInterval =>
|
82
|
+
num = if up then Math.ceil(num+num_interval) else Math.floor(num-num_interval)
|
83
|
+
if (up && num > to) || (!up && num < to)
|
84
|
+
num = to
|
85
|
+
clearInterval(@[timer])
|
86
|
+
@[timer] = null
|
87
|
+
delete @[timer]
|
88
|
+
@[k] = num
|
89
|
+
@set k, to
|
90
|
+
, 10
|
91
|
+
@[k] = num
|
92
|
+
|
93
|
+
MyDashboard.widgets = widgets = {}
|
94
|
+
MyDashboard.lastEvents = lastEvents = {}
|
95
|
+
MyDashboard.debugMode = false
|
96
|
+
|
97
|
+
source = new EventSource('/my_dashboard/events')
|
98
|
+
source.addEventListener 'open', (e) ->
|
99
|
+
console.log("Connection opened", e)
|
100
|
+
|
101
|
+
source.addEventListener 'error', (e)->
|
102
|
+
console.log("Connection error", e)
|
103
|
+
if (e.currentTarget.readyState == EventSource.CLOSED)
|
104
|
+
console.log("Connection closed")
|
105
|
+
setTimeout (->
|
106
|
+
window.location.reload()
|
107
|
+
), 5*60*1000
|
108
|
+
|
109
|
+
source.addEventListener 'message', (e) ->
|
110
|
+
data = JSON.parse(e.data)
|
111
|
+
if lastEvents[data.id]?.updatedAt != data.updatedAt
|
112
|
+
if MyDashboard.debugMode
|
113
|
+
console.log("Received data for #{data.id}", data)
|
114
|
+
lastEvents[data.id] = data
|
115
|
+
if widgets[data.id]?.length > 0
|
116
|
+
for widget in widgets[data.id]
|
117
|
+
widget.receiveData(data)
|
118
|
+
|
119
|
+
source.addEventListener 'dashboards', (e) ->
|
120
|
+
data = JSON.parse(e.data)
|
121
|
+
if MyDashboard.debugMode
|
122
|
+
console.log("Received data for dashboards", data)
|
123
|
+
if data.dashboard is '*' or window.location.pathname is "/#{data.dashboard}"
|
124
|
+
MyDashboard.fire data.event, data
|
125
|
+
|
126
|
+
$(document).ready ->
|
127
|
+
MyDashboard.run()
|