-
Notifications
You must be signed in to change notification settings - Fork 0
/
democlub-show-other-candidates.user.js
106 lines (86 loc) · 3.77 KB
/
democlub-show-other-candidates.user.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// ==UserScript==
// @name Democracy Club show other candidates
// @namespace [email protected]
// @include https://candidates.democracyclub.org.uk/person/*/update
// @include https://candidates.democracyclub.org.uk/election/*/person/create/*
// @version 2021.03.19.0
// @grant none
// @require https://raw.githubusercontent.com/sjorford/democlub-userscripts/master/lib/utils.js
// ==/UserScript==
// TODO: also handle inline form on https://candidates.democracyclub.org.uk/election/*/post/*
// TODO: does this handle multiple current elections?
// temporary fix due to c.dc script errors
// $(onready);
window.setTimeout(onready, 0);
function onready() {
$(`<style class="sjo-styles">
.sjo-post-candidates {background-color: #ff9;}
.sjo-post-candidates p {margin-bottom: 0.25em;}
.sjo-is-current {font-weight: bold;}
.sjo-ballot-link {
position: absolute;
margin-left: 0.5em;
padding: 0.2em 0.5em;
border-radius: 0.5em;
background-color: steelblue;
color: white;
font-size: small;
font-weight: bold;
}
</style>`).appendTo('head');
$('select.post-select').each((index, element) => getPostCandidates(element));
$('body').on('change', 'select.post-select', event => getPostCandidates(event.target));
function getPostCandidates(input) {
// Get election and post
var electionID = input.id.match(/id_constituency_(.*)/)[1];
$('#sjo-post-candidates-' + electionID.replace(/\./g, '_')).empty();
var selected = $(':checked', input);
if (selected.length === 0) return;
var postName = selected.text();
var postID = selected.val();
if (!postID) return;
// Call API
// FIXME: post ID in dropdown no longer matches API
//$.getJSON(`/api/v0.9/posts/${postID}/`, data => renderPostCandidates(electionID, postID, postName, data));
var ballotID = electionID;
if (electionID.match(/^local\./)) ballotID = ballotID.replace(/^(.*)\.(.*)$/, `$1.${postID}.$2`);
var wrapper = $(input).closest('p');
wrapper.find('.sjo-ballot-link').remove();
$(`<a class="sjo-ballot-link" target="_blank" href="/elections/${ballotID}/">View ballot</a>`).appendTo(wrapper);
}
function renderPostCandidates(electionID, postID, postName, data) {
var blockID = `#sjo-post-candidates-${electionID.replace(/\./g, '_')}`;
var block = $(blockID);
if (block.length === 0) {
block = $(`<div class="person__actions__action sjo-post-candidates" id="${blockID}"></div>`).appendTo('.person__actions');
var electionsTop = $('#add_election_button').closest('div:has(h2)').offset().top;
if (block.offset().top < electionsTop) {
block.offset({top: electionsTop});
}
}
block.append(`<h2>Current candidates for ${postName}</h2>`);
var candidates = $.grep(data.memberships, member => member.election.id == electionID);
if (candidates.length === 0) {
block.append('<p>There are currently no candidates for this post</p>');
} else {
var match = location.href.match(/\/person\/(\d+)\//);
var currentID = match ? match[1] : '';
var parties = [];
var partyListsInUse = (electionID.match(/^(europarl|sp\.r|naw\.r|gla\.a)\./) != null);
$.each(candidates, (i, candidate) => {
// Add party heading
if (partyListsInUse && parties.indexOf(candidate.on_behalf_of.name) < 0) {
$('<h3></h3>').text(candidate.on_behalf_of.name).appendTo(block);
parties.push(candidate.on_behalf_of.name);
}
var item = $('<p></p>').appendTo(block);
if (candidate.person.id == currentID) {
$('<span class="sjo-is-current"></span>').text(candidate.person.name).appendTo(item);
} else {
$(`<a href="/person/${candidate.person.id}"></a>`).text(candidate.person.name).appendTo(item);
}
if (!partyListsInUse) item.append(` (${Utils.shortPartyName(candidate.on_behalf_of)})`);
});
}
}
}