njpma/app/static/js/films.js

88 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-10-28 17:17:34 +00:00
// FIXME: move to utils
// From: https://stackoverflow.com/a/175787
2022-01-28 16:03:23 +00:00
function isNumeric (str) {
2021-10-28 17:17:34 +00:00
return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
!isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
}
2022-01-27 14:42:48 +00:00
var options = parseHash();
function sortChanged() {
2021-10-28 16:31:08 +00:00
const sortValue = document.getElementById('sort-select').value
2021-10-28 16:19:16 +00:00
const $films = [...document.querySelectorAll('.film')]
2022-01-27 14:42:48 +00:00
options.sort = sortValue
updateHash()
2021-10-28 16:19:16 +00:00
$films.sort((a, b) => {
2021-10-28 17:17:34 +00:00
let aVal = a.dataset[sortValue]
let bVal = b.dataset[sortValue]
if (isNumeric(aVal) && isNumeric(bVal)) {
aVal = parseFloat(aVal)
bVal = parseFloat(bVal)
}
2021-10-28 16:19:16 +00:00
return aVal < bVal ? -1 : 1
})
2021-10-28 16:33:47 +00:00
document.getElementById('films-list').innerHTML = ''
2021-10-28 16:19:16 +00:00
$films.forEach($film => {
2021-10-28 16:33:47 +00:00
document.getElementById('films-list').appendChild($film)
2021-10-28 16:19:16 +00:00
})
2021-10-28 16:31:08 +00:00
}
2022-01-27 14:42:48 +00:00
function filterChanged() {
const filterValue = document.getElementById('filter-select').value
options.filter = filterValue
document.querySelectorAll('.film').forEach(film => {
2022-01-28 16:08:31 +00:00
if (!options.filter || film.dataset.type.toLowerCase().split(',').indexOf(options.filter.toLowerCase()) > -1) {
2022-01-27 14:42:48 +00:00
console.log(film.dataset.type.split(','), film.dataset.type.split(',').indexOf(options.filter))
film.style.display = ''
} else {
film.style.display = 'none'
}
})
updateHash()
}
function parseHash() {
var options = {}
location.hash.slice(1).split('&').forEach(kv => {
kv = kv.split('=')
2022-01-28 16:03:23 +00:00
const k = kv.shift()
if (k.length) {
options[k] = kv.join('=')
}
2022-01-27 14:42:48 +00:00
})
return options;
}
function updateHash() {
2022-01-28 16:03:23 +00:00
const value = Object.keys(options).filter(key => {
return key && options[key];
}).map(key => {
return key + '=' + options[key];
}).join('&')
location.hash = value ? '#' + value : ''
2022-01-27 14:42:48 +00:00
}
document.getElementById('sort-select').addEventListener('change', sortChanged)
document.getElementById('filter-select').addEventListener('change', filterChanged)
2021-10-28 16:31:08 +00:00
2022-01-27 14:42:48 +00:00
if (options.sort) {
document.getElementById('sort-select').value = options.sort
sortChanged()
}
document.getElementById('filter-select').value = options.filter
if (options.filter) {
filterChanged()
2021-11-12 10:21:00 +00:00
}
document.querySelectorAll('.films .film figure').forEach(figure => {
var imgs = figure.querySelectorAll('img')
if (imgs.length > 1) {
setInterval(() => {
var img = figure.querySelector('img')
img.parentElement.appendChild(img)
2021-11-12 13:02:02 +00:00
}, 5000 + Math.random() * 1000)
2021-11-12 10:21:00 +00:00
}
})