-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
sort-stories.js
119 lines (96 loc) · 2.86 KB
/
sort-stories.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
import {createOptionsBar, getGroupedStories} from '../libs/dom-utils';
import {paths} from '../libs/paths';
function updateStoriesHtml(stories) {
const itemlistTable = document.querySelector('table.itemlist > tbody');
const moreRow = itemlistTable.lastElementChild;
const morespaceRow = moreRow.previousElementSibling;
let extraItems = [];
if (window.location.pathname === '/show') {
extraItems = [...document.querySelectorAll('table.itemlist > tbody > tr')].slice(0, 3);
}
itemlistTable.innerHTML = '';
itemlistTable.append(...extraItems);
for (const story of stories) {
for (const element of story.elements) {
itemlistTable.append(element);
}
}
itemlistTable.append(morespaceRow, moreRow);
}
function sort() {
const method = document.querySelector('#sort-stories-input').value;
const stories = getGroupedStories(document.querySelector('table.itemlist'));
switch (method) {
case 'time': {
stories.sort((a, b) => a.id < b.id ? 1 : -1);
break;
}
case 'score': {
stories.sort((a, b) => a.score < b.score ? 1 : -1);
break;
}
case 'comments/score ratio': {
stories.sort((a, b) => {
const ratioA = (a.commentsCount ? a.commentsCount : 0) / a.score;
const ratioB = (b.commentsCount ? b.commentsCount : 0) / b.score;
return ratioA < ratioB ? 1 : -1;
});
break;
}
case 'default':
default: {
stories.sort((a, b) => a.defaultRank > b.defaultRank ? 1 : -1);
break;
}
}
updateStoriesHtml(stories);
}
function init(metadata) {
const optionsBar = createOptionsBar(metadata.options.optionsBarPosition);
const sortLabel = document.createElement('label');
const sortSelect = document.createElement('select');
const reverseButton = document.createElement('a');
sortLabel.innerHTML = 'sort stories: ';
sortLabel.setAttribute('for', 'sort-stories-input');
sortSelect.id = 'sort-stories-input';
sortSelect.innerHTML = '';
reverseButton.innerHTML = 'reverse';
reverseButton.href = 'javascript:void(0)';
[
'default',
'time',
'score',
'comments/score ratio'
].forEach(opt => {
sortSelect.innerHTML += `<option value="${opt}">${opt}</option>`;
});
if (document.querySelector('#auto-refresh-input')) {
const {firstChild} = optionsBar;
optionsBar.insertBefore(sortLabel, firstChild);
optionsBar.insertBefore(sortSelect, firstChild);
optionsBar.insertBefore(reverseButton, firstChild);
} else {
optionsBar.append(sortLabel, sortSelect, reverseButton);
}
sortSelect.addEventListener('change', sort);
reverseButton.addEventListener('click', () => {
const stories = getGroupedStories(document.querySelector('table.itemlist'));
updateStoriesHtml(stories.reverse());
});
return true;
}
const details = {
id: 'sort-stories',
pages: {
include: paths.stories,
exclude: [
'/front',
'/jobs'
]
},
loginRequired: false,
awaitInit: true,
init
};
export default details;
export {sort as sortStories};