-
Notifications
You must be signed in to change notification settings - Fork 1
/
lyvote.ls
271 lines (241 loc) · 7.64 KB
/
lyvote.ls
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
@lyvote =
svg: null
config:
cx: 500
cy: 500
#seat-count: [12 16 18 22 24 21] now seat-count is auto-calculated
transform: ""
node: \body
colors:
"KMT": \#55f
"DPP": \#090
"NSU": \#fbb
"TSU": \#b26
"PFP": \#f90
"N/A": \#bbb
null: \#bbb
map:
qsort:
idx: 0
map: {}
order: null
limit: 0
init: (r) ->
len = r.config.seat-count.reduce (+)
@limit = 0
@order = [it for it til len]
@order = [[it,(r.seat-position it)0] for it in @order]
for it til len
[t,i] = [@order[it],it + parseInt Math.random! * (len - it)]
@order[it] = @order[i]
@order[i] = t
_sort: (r, limit, level, L, R) ->
if level>limit then return
if L>=R then return
pi = L + parseInt Math.random! * (R - L + 1 )
t = @order[L]
@order[L] = @order[pi]
@order[pi] = t
p = @order[L]1
i = L+1
j = R
while i < j
while @order[i]1<=p and i<j
i++
while @order[j]1>p
j--
if i>=j then break
t = @order[i]
@order[i] = @order[j]
@order[j] = t
t = @order[L]
@order[L] = @order[j]
@order[j] = t
@_sort(r, limit, level+1, L, j-1)
@_sort(r, limit, level+1, j+1, R)
sort: (r) ->
@_sort(r, @limit++, 0, 0, @order.length-1)
indexOf: (r, name) ->
@order[@map[name] ?= @idx++ ]0
xorder:
idx: 0
map: {}
order: null
indexOf: (r, name) ->
if !@order
@order = [[it,(r.seat-position it)0] for it til r.config.seat-count.reduce (+)].sort (a,b) -> a[1]-b[1]
return @order[@map[name] ?= @idx++ ]0
strip:
idx: 0
map: {}
order: null
indexOf: (r, name) ->
if !@order
len = r.config.seat-count.reduce (+)
@order = [0 to len by 2] +++ [1 to len by 2]
@order[@map[name] ?= @idx++ ]
circular:
idx: 0
map: {}
order: null
indexOf: (r, name) ->
if !@order
@order = [[it, (r.seat-position it)|> -> [it[0]-r.config.cx,it[1]] .reduce ((a,b)->a+b**2),0] for it til r.config.seat-count.reduce (+)].sort (a,b) -> a[1]-b[1]
return @order[@map[name] ?= @idx++ ]0
linear:
idx: 0
map: {}
order: null
indexOf: (r, name) ->
return @map[name] ?= @idx++
random:
idx: 0
map: {}
order: null
indexOf: (r, name) ->
if !@order
len = r.config.seat-count.reduce (+)
@order = [it for it til len]
for it til len
[t,i] = [@order[it],it + parseInt Math.random! * (len - it)]
@order[it] = @order[i]
@order[i] = t
return @order[@map[name] ?= @idx++ ]
_idx: 0
_map: {}
seat-mapping-default: (name) ->
@_map[name] ?= @_idx++
seat-mapping: (name) ->
if @config.seat-mapping then @config.seat-mapping.indexOf .call @config.seat-mapping,@,name
else @map.random.indexOf @,name
remap: (map-obj) ->
@config.seat-mapping = map-obj
_pt = ~> @seat-position @seat-mapping it.name
@seats.transition! .duration 750 .attr \transform ~> "translate(#{(_pt it)0},#{(_pt it)1})"
seat-position: (idx) ->
sc = @config.seat-count
sc = [sc[to i].reduce (+) for ,i in sc]
ret = (sc.map (-> it - idx) .filter (> 0))
[row, len] = [@config.seat-count.length, ret.length]
[i,j,m] = [row - len, ret[0]-1, sc[row - len] - 1 - (len<row && sc[row - len-1]) || 0 ]
v = [ Math.cos(Math.PI*j/m), Math.sin Math.PI*j/m ]
[ @config.cx + v[0]*(160+ i*60) , @config.cy - v[1]*(160 + i*60) ]
seats: null
h-name: {}
h-party: {}
generate: (error, mlys) ->
[@h-name,@h-party] = [{} {0:0}]
if !@config.seat-count
_sc = [0 til 6].map -> parseInt 2*Math.PI*(it*60+160)
_sc_total = _sc.reduce (+)
@config.seat-count = _sc.map -> Math.round it*mlys.length/_sc_total
idx = 0
for mly in mlys
@h-name[mly.name] =
name: mly.name
vote: 0
party: mly.party
idx: idx++
@h-party[mly.party] ?= @h-party[0]++
for names,i in @config.vote
for name in names
@h-name[name].vote = i+1
@svg = d3.select @config.node .append \svg
.attr \viewBox "0 0 1024 500"
.attr \preserveAspectRatio "xMinYMin meet"
defs = @svg.selectAll \defs .data mlys .enter! .append \pattern
.attr \id ~> \defs_h + @h-name[it.name].idx
.attr \patternUnits \userSpaceOnUse
.attr \x 30
.attr \y 30
.attr \width 50
.attr \height 50
imgs = defs.append \image
.attr \xlink:href -> "http://avatars.io/50a65bb26e293122b0000073/#{CryptoJS.MD5('MLY/'+it.name).toString()}?size=medium"
.attr \x 0
.attr \y 0
.attr \width 50
.attr \height 50
.attr \transform "scale(0.9)"
panel = @svg.append \g
.attr \transform ~> @config.transform
_pt = ~> @seat-position @seat-mapping it.name
@seats = panel.selectAll \g.seat
.data [@h-name[it] for it of @h-name].sort( (a,b) ~>
@h-party[a.party] - @h-party[b.party]) .enter! .append \g
.attr \transform ~> "translate(#{(_pt it)0},#{(_pt it)1}) rotate(180) scale(-1, -1)"
lockcell = null
@seats.append \circle
.attr \class \mly-seat
.attr \r 20
.attr \fill ~> @colors[it.party]
.style \opacity ~> if it.vote == 0 then 0.3 else 1
.on \click ~>
if lockcell
d3.select lockcell .attr \fill ~> @colors[it.party]
.transition! .duration 500
.attr \transform "scale(1)"
.attr \stroke \none
.style \opacity ~> if it.vote == 0 then 0.3 else 1
if lockcell == d3.event.target
return lockcell := null
lockcell := d3.event.target
d3.select d3.event.target .attr \fill -> "url(\#defs_h#{it.idx})"
.transition! .duration 500
.attr \transform "scale(2)"
.attr \stroke ~> @colors[it.party]
.attr \stroke-width \3px
.style \opacity 1
@seats.append \path
.attr \d ~> switch it.vote
|1 => "M-12 0 L0 10 L11 -11"
|2 => "M-10,-10 L10,10 L0 0 L-10 10 L10 -10"
|3 => "M-10 0 L10 00"
|9 => "M15 0 A15 15 0 1 1 -15 0 A15 15 0 1 1 15 0"
|otherwise => "M0 0"
.attr \stroke ~> switch it.vote
|1 => \#0b0
|2 => \#b00
|3 => \#999
|otherwise => \#b00
.attr \stroke-width \5px
.attr \fill \none
@seats.append \rect
.attr \class \mly-name-box
.attr \x -25
.attr \y 9
.attr \width 50
.attr \height 17
.attr \rx 10
.attr \ry 10
.attr \fill \#fff
.style \opacity 0.4
@seats.append \text
.attr \class \mly-name
.attr \y 22
.attr \text-anchor \middle
.text (.name)
factory: (config) ->
@config = config
@render = ->
d3.json @config.namelist, (error, json) ~>
@generate.call @, error, json
@
@
render: (config) ->
unless config.vote
$ config.node
..find \span.approval .each -> config.[]vote[0] = $ @ .text! / ' '
..find \span.veto .each -> config.[]vote[1] = $ @ .text! / ' '
..find \span.abstention .each -> config.[]vote[2] = $ @ .text! / ' '
..find \span .hide!
[0 to 2].map -> config.vote[it] = config.vote[it].filter ->it
new @factory {}<<<@config<<<config .render!
@lyvote.factory.prototype = @lyvote
<- $
$ \.twlyvote .each ->
[json,mapper] = [($ @ .data \mly-list ),($ @ .data \mapper ) || "linear"]
lyvote.render do
seat-mapping: lyvote.map[mapper]
namelist: json
node: "#"+@.id