-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.coffee
75 lines (66 loc) · 2.6 KB
/
data.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
###
* Federated Wiki : Data Plugin
*
* Licensed under the MIT license.
* https://github.com/fedwiki/wiki-plugin-data/blob/master/LICENSE.txt
###
# lots of cases, ward will try these
# http://nmsi.localhost:1111/view/cotton-in-the-field/view/tier-1-material-summary/cotton.localhost:1111/talk-about-wool/view/cotton-fabric
window.plugins.data =
emit: (div, item) ->
$('<p />').addClass('readout').appendTo(div).text(summary(item))
$('<p />').addClass('label').appendTo(div).html(wiki.resolveLinks(item.text||'data'))
bind: (div, item) ->
lastThumb = null
div.find('.readout')
.on 'mousemove', (e) ->
if (e.offsetX == undefined)
offset = e.pageX - $(this).offset().left
else
offset = e.offsetX
thumb = thumbs(item)[Math.floor(thumbs(item).length * offset / e.target.offsetWidth)]
return if thumb == lastThumb || null == (lastThumb = thumb)
refresh thumb
$(div).trigger('thumb', thumb)
.on 'dblclick', (e) ->
wiki.dialog "JSON for #{item.text}", $('<pre/>').text(JSON.stringify(item, null, 2))
div.find('.label')
.on 'dblclick', () ->
wiki.textEditor div, item
$(".main").on 'thumb', (evt, thumb) ->
refresh thumb unless thumb == lastThumb || -1 == (thumbs(item).indexOf thumb)
value = (obj) ->
return NaN unless obj?
switch obj.constructor
when Number then obj
when String then +obj
when Array then value(obj[0])
when Object then value(obj.value)
when Function then obj()
else NaN
average = (thumb) ->
values = _.map(item.data, (obj) -> value(obj[thumb]))
values = _.reject(values, (obj) -> isNaN obj)
result = _.reduce(values, ((m,n) -> m+n), 0) / values.length
result.toFixed 2
readout = (thumb) ->
return average(thumb) if item.columns?
return summary(item) unless item.data.object?
field = item.data[thumb]
return "#{field.value}" if field.value?
return "#{field.toFixed 2}" if field.constructor == Number
NaN
label = (thumb) ->
return "Averaged:<br>#{thumb}" if item.columns? && item.data.length > 1
thumb
refresh = (thumb) ->
div.find('.readout').text readout(thumb)
div.find('.label').html label(thumb)
summary = (item) ->
return "#{item.data.length}x#{item.columns.length}" if item.columns?
return "#{item.data.nodes.length}/#{item.data.links.length}" if item.data? && item.data.nodes? && item.data.links?
return "1x#{thumbs(item).length}"
"data"
thumbs = (item) ->
return item.columns if item.columns?
(key for own key of item.data)