forked from antin/bchirometer.oknesset.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmeter.coffee
More file actions
184 lines (180 loc) · 6.68 KB
/
bmeter.coffee
File metadata and controls
184 lines (180 loc) · 6.68 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
$ ->
# Some local routines.
parse_score=(e)->parseFloat $(e).data().score or 0
update_link_results=->
voted=$('#agendas button.selected:not(.indifferent)').length
total=$('#agendas-list>li').length
# Hide/disable nav to results (entire footer?) if no votes.
$ '.to-results'
.toggleClass 'disabled',voted is 0
# Modify URL link to results page to contain votes so can bookmark, share…
results_link='#!results'+votes_serialize()
$ '.to-results'
.attr 'href',results_link
# Show progress (count votes).
$ '#progress'
.text "(הצבעת ל-#{voted} מתוך #{total} אג'נדות)"
# Update FB share link.
$ '#results footer a[title="Facebook"]'
.attr href:"https://www.facebook.com/sharer/sharer.php?app_id=102113249964274&u=#{location.origin+location.pathname+results_link}&display=popup&ref=plugin"
disable_category=->
# Mark category to indicate whether votes done in it.
#???... Don't use visible, parse ID instead, so results URL works too.
voted=$('#agendas button.selected:visible:not(.indifferent)').length # Find any dis/agree votes in current category.
$("#categories a[href=\"#{location.hash}\"]").toggleClass 'has-votes',voted isnt 0
votes_serialize=->
vs={}
$ '#agendas button.selected:not(.indifferent)'
.each ->
b=$ @
aid=b.parents('li').attr('id').slice 'agenda-'.length
vs[aid]=if b.hasClass 'agree' then 'y' else 'n'
JSON.stringify vs
# Dynamically generated content. (Run before paging, because need buttons in DOM.)
$ '#agendas-list>li'
.append $ """<div>
<button type="button" class="agree">בעד</button>
<button type="button" class="indifferent selected">לא אכפת</button>
<button type="button" class="disagree">נגד</button>
<p><small>(* לחצו "לא אכפת" כדי לבטל הצבעה.)</small></p></div>
"""
# SPA with virtual pages.
show_page=(p)->
$('section').hide().filter(p).show()
$('body').attr 'data-page',p # Mark current page on body so can style with CSS selectors.
window.scrollTo 0,0
page_turner=(p)->return ->return show_page p # Factory.
# Routing.
page.base location.pathname #???
page 'splash:ref?',page_turner '#splash'
page 'about',page_turner '#about'
page 'qna',page_turner '#qna'
page 'categories',page_turner '#categories'
page 'agendas:cid',(context)->
# Identify category from URL and fetch details; eg "#!agendas[92,113,101]" means category includes agendas 92, 113, and 101.
# (Apparently, window.location is updated after this handler, so can't get URL hash from it. Page.js provides details in a "context" parameter.)
cid=context.path.slice 'agendas'.length
ids=$.parseJSON cid
c=$ "#categories-list a[href=\"#!#{context.path}\"]"
icon=c.attr('class').match(/fa-\S+/)[0]
# Set heading to chosen category.
$ '#agendas h2'
.empty()
.text c.text()
.prepend $ "<i class=\"fa #{icon}\"></i>"
# Filter agendas list for selected category.
$ '#agendas-list li'
.each ->
a=$ @ # Wrap DOM node with jQuery? Yeah, easier to work with.
aid=a.attr 'id' # Each li has an id attribute in the form "agenda-113".
.slice 'agenda-'.length
a.toggle parseInt(aid,10) in ids
show_page '#agendas'
page 'results:votes?',(context)-> # Nav to results page.
# Guard against missing votes: redirect home.
unless context.params.votes then console.log 'Missing votes; redirecting.'; page '#!splash'
# Extract votes from URL. Set voting buttons.
try
votes=$.parseJSON context.params.votes
catch e
console.log 'parseJSON failed:',e
return page '#!splash'
for own agenda,vote of votes
do (agenda,vote)->
v=if vote is 'y' then 'agree' else 'disagree'
$ "#agenda-#{agenda} button.#{v}"
.addClass 'selected'
.siblings().removeClass 'selected'
# Guard against invalid votes.
unless $('#agendas button.selected:not(.indifferent)').length isnt 0 then console.log 'No votes; redirecting.'; page '#!splash'
#???... Mark category buttons too!
# Wipe old final scores: reset to zeroes.
final={}
$ '#parties-list li'
.data 'score',0
.find 'h3 span'
.text '—'
###???
.find 'h3 img'
.attr 'src','meter/zero.png'
.siblings 'span'
###
# Recalculate final scores.
$ '#agendas-list>li'
.each ->
a=$ @
# Voted?
if a.find('button.selected:not(.indifferent)').length isnt 0
dis_agree=switch
when a.find('button.selected.agree').length isnt 0 then 1.0
else -1.0
ps=$.parseJSON a.attr 'data-parties-scores' #??? Optimize by parsing once and setting a.data!
for own party,score of ps
do (party,score)->
final[party] or=[] # Initialize to empty array, once.
final[party].push score*dis_agree # Add score.
# Sort parties by score.
for own party,scores of final
do (party,scores)->
sum=scores.reduce (x,y)->x+y
average=sum/scores.length
#??? meter=switch
$ "#party-#{party}"
.data 'score',average
.find 'h3 span'
.text average.toFixed 1
.attr 'title','ממוצע של \u202d'+scores # Just for debugging?
###???
.find 'h3 img'
.attr 'src','meter/zero.png'
.siblings 'span'
###
ol=$ '#parties-list' # Parent <ol>.
ch=ol.children().get() # Children <li>.
.sort (x,y)->if (parse_score x)<(parse_score y) then 1 else -1
ol.append ch
# Count votes.
$ '#results>p span'
.text $('#agendas button.selected:not(.indifferent)').length
# Only show best result's logo.
$('#parties-list li>img').hide().first().show()
show_page '#results'
page '*',->console.log '404; redirecting.'; page '#!splash' # Anything else — redirect to home page.
page.start hashbang:yes # Begin listening to location changes.
# Voting buttons handler.
$ '#agendas-list'
.on 'click','button',(ev)->
b=$ ev.target # Which button?
v=switch # Vote?
when b.hasClass 'agree' then 1
when b.hasClass 'indifferent' then 0
when b.hasClass 'disagree' then -1
b.addClass 'selected'
.siblings().removeClass 'selected'
# Update stuff depending on number of votes.
disable_category()
update_link_results()
# Disable results button.
$ '.to-results'
.click (ev)->
if $(@).hasClass 'disabled' then ev.preventDefault()
ga 'send','event','survey','show-results','votes-for-agendas:'+votes_serialize()
# Expansion clicks.
$ 'a.expand'
.click (ev)->
ev.preventDefault() # Not a real (nav) link.
id=$ ev.target
.attr 'href'
#??? .next 'span'
$ id
.toggle()
# Next category.
$ '#next'
.click (ev)->
ev.preventDefault() # Not a real (nav) link.
# Find current category, and decide which next: from last cycle back to first.
n=$("#categories-list a[href=\"#{location.hash}\"]").parent().next().find 'a'
if n.length is 0 then n=$("#categories-list a").first()
page n.attr 'href'
# Stuff that needs to be initially hidden. #??? Use new [hidden] attribute? Polyfill it?
update_link_results()