-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaderboard.js
92 lines (82 loc) · 2.53 KB
/
leaderboard.js
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
const _ = window._;
const calculateMovement = x => {
const change = x.position - x.lastPosition;
switch (true) {
case (change < 0): return 'moving-down'
case (change > 0): return 'moving-up'
default: return 'steady'
}
}
const toggleHighlight = (state) => {
return (event, leader) => {
const constituency = window.constituencies.find(leader.name)
if (constituency) {
constituency.classList[state]('highlight')
}
const row = event.srcElement.closest('tr')
if (row) {
row.classList[state]('highlight')
}
}
}
const leaderBoardView = new Vue({
el: '#votes',
data: {
leaders: [],
pageSize: 10,
currentPage: 1,
constituencyFilter: window.petitionPinger.getValueFromURL('filter') || ''
},
methods: {
mouseOver: toggleHighlight('add'),
mouseLeave: toggleHighlight('remove'),
nextPage: function () {
if ((this.currentPage * this.pageSize) < this.filteredLeaders.length) this.currentPage++;
},
prevPage: function () {
if (this.currentPage > 1) this.currentPage--;
}
},
computed: {
filteredLeaders: function() {
return this.leaders.filter(l => l.name.toLowerCase().indexOf(this.constituencyFilter) >= 0)
},
leaderboardPage: function () {
const firstItem = (this.currentPage - 1) * this.pageSize
const lastItem = this.currentPage * this.pageSize
return this.filteredLeaders.slice(firstItem, lastItem);
},
totalPages: function () {
return Math.ceil(this.filteredLeaders.length / this.pageSize)
}
}
})
let lastLeaders = null;
function update(data) {
const leaders = _.chain(data)
.orderBy('signature_count', 'desc')
.map(function (x, index) {
return {
name: x.name,
signatureCount: x.signature_count,
position: index
}
})
.map(function (x) {
if (lastLeaders) {
x.lastPosition = _.findIndex(lastLeaders, {name: x.name})
x.isNewEntry = x.lastPosition === -1
}
return x
})
.map(function (x) {
if (lastLeaders) {
x.movement = calculateMovement(x)
}
return x
})
.value();
lastLeaders = leaders
leaderBoardView.leaders = leaders
}
window.petitionPinger.signatures$.subscribe(update)