aboutsummaryrefslogtreecommitdiffstats
path: root/tiers.js
blob: c33e632baf00f9cc4a3b537d57d6850ab95fa2da (plain) (blame)
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
/*
Interactive Tiers
Original code from: https://github.com/silverweed/tiers
Modified by: pinapelz
Licensed Under WTFPL
*/


'use strict';

const MAX_NAME_LEN = 200;
const DEFAULT_TIERS = ['S', 'A', 'B', 'C', 'D', 'E', 'F'];
const TIER_COLORS = [
	'#ff6666',
	'#f0a731',
	'#f4d95b',
	'#66ff66',
	'#58c8f4',
	'#5b76f4',
	'#f45bed'
];

let unique_id = 0;
let tierlist_div;

window.addEventListener('load', () => {
	tierlist_div = document.querySelector('.tierlist');

	for (let i = 0; i < DEFAULT_TIERS.length; ++i) {
		add_row(i, DEFAULT_TIERS[i]);
	}
	recompute_header_colors();

	const modal = document.getElementById('image-modal');
	const modalImg = document.getElementById('modal-img');
	const modalTitle = document.getElementById('modal-title');
	const modalDesc = document.getElementById('modal-description');
	const modalClose = document.querySelector('.modal .close');
	let currentModalImage = null;

	function showModal(img) {
		currentModalImage = img;
		document.body.style.overflow = 'hidden';
		modal.style.display = 'flex';
		modalImg.src = img.src;
		modalTitle.value = img.dataset.title || 'No title';
		modalDesc.value = img.dataset.description || 'No description';
	}

	modalClose.addEventListener('click', () => {
		modal.style.display = 'none';
		document.body.style.overflow = 'auto';
	});

	window.addEventListener('click', (evt) => {
		if (evt.target == modal) {
			modal.style.display = 'none';
			document.body.style.overflow = 'auto';
		}
	});

	tierlist_div.addEventListener('click', (evt) => {
		if (evt.target.tagName.toUpperCase() === 'IMG') {
			showModal(evt.target);
		}
	});

	hard_reset_list();
	load_tierlist(EMBEDDED_JSON);
});


function create_img_with_src(src) {
	let img = document.createElement('img');
	img.src = src;
	img.style.userSelect = 'none';

	img.addEventListener("mouseenter", (evt) => {
        const title = evt.target.dataset.title;
        if (title) {
            evt.target.title = title;
        }
    });
	
	return img;
}

function hard_reset_list() {
	tierlist_div.innerHTML = '';
}

function load_tierlist(serialized_tierlist) {
	document.querySelector('.title-label').innerText = serialized_tierlist.title;
	for (let idx in serialized_tierlist.rows) {
		let ser_row = serialized_tierlist.rows[idx];
		let elem = add_row(idx, ser_row.name);

		for (let img_obj of (ser_row.imgs || [])) {
			let img = create_img_with_src(img_obj.src);
			img.dataset.title = img_obj.title || '';
			img.dataset.description = img_obj.description || '';
			let td = document.createElement('span');
			td.classList.add('item');
			td.appendChild(img);
			let items_container = elem.querySelector('.items');
			items_container.appendChild(td);
		}

		elem.querySelector('.header').innerText = ser_row.name;
	}

	resize_headers();
	recompute_header_colors();
}

function resize_headers() {
	let headers = tierlist_div.querySelectorAll('.row .header');
	if (headers.length === 0) return;

	let max_width = 100;
	let reference_style = window.getComputedStyle(headers[0]);
	let measurer = document.createElement('span');
	measurer.style.position = 'absolute';
	measurer.style.visibility = 'hidden';
	measurer.style.whiteSpace = 'nowrap';
	measurer.style.fontFamily = reference_style.fontFamily;
	measurer.style.fontSize = reference_style.fontSize;
	measurer.style.fontWeight = reference_style.fontWeight;
	document.body.appendChild(measurer);

	headers.forEach((header) => {
		measurer.innerText = header.innerText;
		let text_width = Math.ceil(measurer.getBoundingClientRect().width);
		max_width = Math.max(max_width, text_width + 24);
	});

	document.body.removeChild(measurer);
	headers.forEach((header) => {
		header.style.minWidth = `${max_width}px`;
	});
}

function add_row(index, name) {
	let div = document.createElement('div');
	div.classList.add('row');

	let header = document.createElement('span');
	header.classList.add('header');
	header.innerText = name;

	let items = document.createElement('span');
	items.classList.add('items');

	div.appendChild(header);
	div.appendChild(items);

	let rows = tierlist_div.children;
	if (index >= rows.length) {
		tierlist_div.appendChild(div);
	} else {
		let nxt_child = rows[index];
		tierlist_div.insertBefore(div, nxt_child);
	}

	return div;
}

function recompute_header_colors() {
	let rows = tierlist_div.querySelectorAll('.row');
	rows.forEach((row, row_idx) => {
		let color = TIER_COLORS[row_idx % TIER_COLORS.length];
		row.querySelector('.header').style.backgroundColor = color;
	});
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage