-
Notifications
You must be signed in to change notification settings - Fork 0
/
democlub-version-tree.user.js
240 lines (191 loc) · 9.5 KB
/
democlub-version-tree.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
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
// ==UserScript==
// @name Democracy Club version tree
// @namespace [email protected]
// @include https://candidates.democracyclub.org.uk/person/*
// @exclude https://candidates.democracyclub.org.uk/person/create/*
// @exclude https://candidates.democracyclub.org.uk/person/*/other-names
// @exclude https://candidates.democracyclub.org.uk/person/*/duplicate?*
// @version 2021.04.26.0
// @grant none
// ==/UserScript==
// temporary fix due to c.dc script errors
// $(onready);
window.setTimeout(onready, 0);
function onready() {
$(`<style class="sjo-styles">
table.sjo-tree tr {background-color: inherit;}
.sjo-tree td, .sjo-tree th {font-size: 8pt; padding: 2px; min-width: 20px;}
#sjo-versions-showall {display: block; font-size: 8pt; margin-bottom: 1em;}
.sjo-tree-current {background-color: gold;}
.sjo-tree thead {background-color: inherit;}
.sjo-tree-year-inner {background: white;}
.sjo-tree-year {text-align: center; background: left center repeat-x url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY2BgYAAAAAQAAVzN/2kAAAAASUVORK5CYII=');}
.sjo-tree-horiz {background: repeat-x url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAASdEVYdFNvZnR3YXJlAEdyZWVuc2hvdF5VCAUAAAAsSURBVDhPY/wPBAxUBExQmmpg1EDKweA3kBGIR9MhZWA0L1MORg2kFDAwAAB9tQkdVRptbgAAAABJRU5ErkJggg==');}
.sjo-tree-turnup {background: no-repeat url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAASdEVYdFNvZnR3YXJlAEdyZWVuc2hvdF5VCAUAAABOSURBVDhPY/wPBAx4ACMjI5QFAQSUMzBBaaqBUQMpB/Q1ED3JEANAOvAnLDQwuNMhIdeBANEGEmMYCBDMy6SCgQ1DYsCogZQDKhvIwAAApcUVF6f1O7gAAAAASUVORK5CYII=');}
.sjo-tree-vert {background: repeat-y url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsIAAA7CARUoSoAAAAASdEVYdFNvZnR3YXJlAEdyZWVuc2hvdF5VCAUAAAAySURBVDhPY/wPBAx4ACMjI5QFAQSUMzBBaaqBUQMpB6MGUg5GDaQcjBpIORjsBjIwAAAaYgckACvF4gAAAABJRU5ErkJggg==');}
.sjo-tree-mergein {background: no-repeat url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAoCAYAAAD+MdrbAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAASdEVYdFNvZnR3YXJlAEdyZWVuc2hvdF5VCAUAAABOSURBVEhLY/wPBAxUBExQmmpg1EDKweA3kBGIR9MhZYBgXmZkBAUzKsCnhWQXEipLRmCkjBpIORg1kHIwaiDlYNRAysGogZSDEWcgAwMAmLEPQjc8JtAAAAAASUVORK5CYII=');}
</style>`).appendTo('head');
// Group versions by ID
var versionData = [];
var editsPerYear = [];
$('.full-version-json').each((index, element) => {
// Parse and flatten version JSON
var version = JSON.parse(element.innerText);
//version = flattenObject(version);
// Add version header
var div = $(element).closest('.version');
var header = div.find('dt').toArray().reduce((obj, el) => {
obj[el.innerText] = $(el).next('dd').text().trim();
return obj;
}, {});
version._version_id = header['Version'].split(' ')[0];
version._username = header['Username'];
version._timestamp = header['Timestamp'];
version._source = header['Source'];
div.data({'sjo-version': version._version_id, 'sjo-person': version.id});
// Add to list of versions
versionData.push(version);
// Count number of edits per year
var year = version._timestamp.substr(0, 4);
if (!editsPerYear[year]) editsPerYear[year] = 0;
editsPerYear[year]++;
});
// Sort versions
if (versionData.length === 0) return;
versionData.sort((a, b) => a._timestamp > b._timestamp);
// Create version tree table
var headerCellsHtml = editsPerYear.map((num, year) =>
num == 1 ? `<th></th><th>${year}</th>` :
num ? `<th></th><th class="sjo-tree-year" colspan="${num * 2 - 1}"><span class="sjo-tree-year-inner">${year}</span></th>` : '');
var treeTable = $('<table class="sjo-tree"></table>')
.insertAfter('.person__versions h2')
.append('<thead><tr>' + headerCellsHtml.join('') + '</tr></thead>');
// Build version tree
var stack = [];
stack.push({'id': versionData[versionData.length - 1].id});
while (stack.length > 0) {
// Write a new table row
var currentId = stack.pop();
var treeRow = $(`<tr><th>${currentId.id}</th>` + '<td></td>'.repeat(versionData.length * 2 - 1) + '</tr>').appendTo(treeTable);
var treeRowCells = treeRow.find('td');
var minColNo = null, maxColNo = null, colNo = null;
var versionWritten = null;
for (var sequenceNo = versionData.length - 1; sequenceNo >= 0; sequenceNo--) {
if (versionData[sequenceNo].id == currentId.id) {
// Write this version to the tree
// TODO: display different icons for different actions, e.g. add/remove candidacy
colNo = sequenceNo * 2;
$(`<a class="sjo-tree-version">${versionData[sequenceNo]._version_id.substr(0, 4)}</a>`)
.data('sjo-tree-version', versionData[sequenceNo]._version_id)
.appendTo(treeRowCells.eq(sequenceNo * 2));
// Keep track of first and last versions for this ID
if (!minColNo || colNo < minColNo) minColNo = colNo;
if (!maxColNo || colNo > maxColNo) maxColNo = colNo;
// Join to next version
if (versionWritten) versionWritten._prev_version = versionData[sequenceNo];
versionWritten = versionData[sequenceNo];
// If this is a merge, add the merged ID to the stack
var sourceMatch = versionData[sequenceNo]._source.match(/^After merging person (\d+)$/);
if (sourceMatch) {
stack.push({'id': sourceMatch[1], 'mergeColNo': colNo - 1});
}
}
}
// Add the joining lines
treeRowCells.filter(`:lt(${maxColNo}):gt(${minColNo})`).filter((index, element) => element.innerHTML == '').addClass('sjo-tree-horiz');
if (maxColNo < versionData.length * 2 - 1) {
treeRowCells.filter(`:lt(${currentId.mergeColNo}):gt(${maxColNo})`).addClass('sjo-tree-horiz');
treeRowCells.eq(currentId.mergeColNo).addClass('sjo-tree-turnup');
var treeRows = treeTable.find('tbody tr');
for (var joinRow = treeRows.length - 2; joinRow >= 0; joinRow--) {
var joinCell = treeRows.eq(joinRow).find('td').eq(currentId.mergeColNo);
if (joinCell.hasClass('sjo-tree-horiz')) {
joinCell.removeClass('sjo-tree-horiz').addClass('sjo-tree-mergein');
break;
} else {
joinCell.removeClass('sjo-tree-horiz').addClass('sjo-tree-vert');
}
}
}
}
// Button to show all versions
var allVersions = $('.version');
$('<a id="sjo-versions-showall">Show all versions</a>').insertAfter('.sjo-tree').hide().click((event) => {
allVersions.show();
$('#sjo-versions-showall').hide();
$('.sjo-tree-current').removeClass('sjo-tree-current');
});
// Show selected version
$('.sjo-tree').on('click', '.sjo-tree-version', event => {
var button = $(event.target);
var versionId = button.data('sjo-tree-version');
var selectedVersion = allVersions.filter((index, element) => $(element).data('sjo-version') == versionId).show();
allVersions.not(selectedVersion).hide();
$('#sjo-versions-showall').show();
$('.sjo-tree-current').removeClass('sjo-tree-current');
button.addClass('sjo-tree-current');
$('.person__versions').css({minHeight: '800px'});
});
// Navigate with arrows
$('body').on('keydown', event => {
if (event.shiftKey || event.ctrlKey || event.altKey) return;
if (!event.key.startsWith('Arrow')) return;
if (event.target.tagName != 'BODY') return;
var currentCell = $('.sjo-tree-current').closest('td');
if (currentCell.length == 0) return;
event.preventDefault();
if (event.key == 'ArrowLeft') {
while (currentCell.length > 0 && !currentCell.is('th')) {
currentCell = currentCell.prev();
if (currentCell.find('.sjo-tree-version').length > 0) break;
}
} else if (event.key == 'ArrowRight') {
while (currentCell.length > 0 && !currentCell.is('th')) {
currentCell = currentCell.next();
if (currentCell.find('.sjo-tree-version').length > 0) break;
if (currentCell.is('.sjo-tree-turnup')) {
while (currentCell.length > 0 && !currentCell.is('th') && !currentCell.is('.sjo-tree-mergein')) {
currentCell = cellUp(currentCell);
}
}
}
} else if (event.key == 'ArrowUp') {
currentCell = cellUp(currentCell);
while (currentCell.length > 0 && !currentCell.is('th')) {
currentCell = currentCell.prev();
if (currentCell.find('.sjo-tree-version').length > 0) break;
}
if (currentCell.find('.sjo-tree-version').length == 0) {
while (currentCell.is('th')) {
currentCell = currentCell.next();
}
while (currentCell.length > 0 && !currentCell.is('th')) {
currentCell = currentCell.next();
if (currentCell.find('.sjo-tree-version').length > 0) break;
}
}
} else if (event.key == 'ArrowDown') {
currentCell = cellDown(currentCell);
while (currentCell.length > 0 && !currentCell.is('th')) {
currentCell = currentCell.prev();
if (currentCell.find('.sjo-tree-version').length > 0) break;
}
if (currentCell.find('.sjo-tree-version').length == 0) {
while (currentCell.is('th')) {
currentCell = currentCell.next();
}
while (currentCell.length > 0 && !currentCell.is('th')) {
currentCell = currentCell.next();
if (currentCell.find('.sjo-tree-version').length > 0) break;
}
}
}
currentCell.find('.sjo-tree-version').click();
});
function cellUp(cell) {
return cell.closest('tr').prev().find('td').eq(cell.prevAll('td').length);
}
function cellDown(cell) {
return cell.closest('tr').next().find('td').eq(cell.prevAll('td').length);
}
}