album/res/ux.js

189 lines
5.8 KiB
JavaScript
Raw Normal View History

(function(d) {
2019-07-20 00:46:26 +04:00
if (d.querySelector('main form.search')) {
d.querySelector('header form.search').remove();
d.querySelector('main form.search input#s_q').focus();
}
let f = d.querySelector('footer');
f.insertAdjacentHTML(
'afterbegin',
'<p><a href="#help" title="Help" accesskey="?">?</a></p>')
2017-12-25 19:43:06 +04:00
f.querySelector('[href="#help"]').addEventListener('click', e => {
2017-11-27 00:31:48 +04:00
if (d.getElementById('help') == null) {
f.insertAdjacentHTML(
'beforebegin',
'<div id="help"><h2>Key bindings</h2>' +
[].map.call(
2017-11-27 00:31:48 +04:00
d.querySelectorAll('[accesskey]'),
e => e.accessKeyLabel + ": " + (e.title || e.innerText)).join('<br/>') +
'</div>');
}
return true;
2017-12-25 19:43:06 +04:00
});
2018-02-11 18:32:15 +04:00
var map;
function resize_map() {
var me = d.getElementById('map');
if (me) {
me.style.height = 4 * me.clientWidth / 5 + "px";
}
if (map) {
map.invalidateSize(false);
}
}
window.addEventListener('resize', resize_map);
2017-12-25 19:43:06 +04:00
let i = d.querySelector('.details .item');
2017-12-12 02:35:23 +04:00
if (i) {
2018-02-11 18:32:15 +04:00
i.addEventListener('click', e => {
i.classList.toggle('zoom');
resize_map();
});
2017-12-12 02:35:23 +04:00
}
2018-02-07 23:56:23 +04:00
function prepare_map(cb) {
let h = d.querySelector('head');
var csslink = d.createElement('link');
csslink.rel = 'stylesheet';
2019-04-26 02:33:59 +04:00
csslink.href = '/static/l140/leaflet.css';
h.append(csslink);
2018-02-07 23:56:23 +04:00
let m = d.querySelector('.meta') || d.querySelector('main');
m.insertAdjacentHTML('beforeend', '<div id="map"></div>');
var slink = d.createElement('script');
slink.type = 'text/javascript';
2019-04-26 02:33:59 +04:00
slink.src = '/static/l140/leaflet.js';
2018-02-07 23:56:23 +04:00
slink.async = 'async';
2018-02-11 16:32:23 +04:00
slink.onload = () => {
2018-02-11 18:32:15 +04:00
map = L.map('map', {'scrollWheelZoom': false, 'trackResize': false});
2018-02-11 16:32:23 +04:00
L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
2018-02-11 18:32:15 +04:00
resize_map();
2018-02-11 16:32:23 +04:00
cb(map);
}
2018-02-07 23:56:23 +04:00
h.append(slink);
}
let details = d.querySelector('.details');
let pos = details && details.dataset.position
if (pos) {
2018-02-11 16:32:23 +04:00
prepare_map((map) => {
2018-02-11 18:32:15 +04:00
let p = JSON.parse(pos);
map.setView(p, 16);
L.marker(p).addTo(map);
2018-02-11 16:32:23 +04:00
})
2018-02-07 23:56:23 +04:00
}
let group = d.querySelector('.group');
let poss = (details && details.dataset.positions) || (group && group.dataset.positions);
if (poss) {
2018-02-11 16:32:23 +04:00
prepare_map((map) => {
let h = d.querySelector('head');
h.insertAdjacentHTML(
'beforeend',
2019-04-26 02:33:59 +04:00
'<link rel="stylesheet" href="/static/lm141/lmc.css">' +
'<link rel="stylesheet" href="/static/lm141/lmc-default.css">'
)
var slink2 = d.createElement('script');
slink2.type = 'text/javascript';
2019-04-26 02:33:59 +04:00
slink2.src = '/static/lm141/lmc.js';
h.append(slink2);
slink2.onload = () => {
let pos = JSON.parse(poss);
var markers = L.markerClusterGroup({maxClusterRadius: 35});
map.fitBounds(L.polyline(pos).getBounds());
pos.forEach(p => {
2018-07-19 23:17:00 +04:00
let n = p.pop();
let m = L.marker(p, { title: n });
m.bindPopup(`<a href="/img/${n}"><img src="/img/${n}-s.jpg"></a>`);
markers.addLayer(m);
});
map.addLayer(markers);
};
2018-02-11 16:32:23 +04:00
})
}
2019-07-20 00:46:26 +04:00
(function(form) {
function prepareQtag(s) {
const i = s.querySelector('input');
i.addEventListener('change', function(e) {
if (!i.checked && !i.value.startsWith('!')) {
i.value = '!' + i.value;
i.checked = true;
s.classList.add('not');
} else if (i.value.startsWith('!')) {
i.value = i.value.substring(1);
s.classList.remove('not');
}
console.log("->", i.checked, i.value);
});
}
[].forEach.call(form.querySelectorAll('.refs label'), prepareQtag)
2019-07-20 00:46:26 +04:00
form.classList.add('hidden');
let sl = form.querySelector('label');
sl.addEventListener('click', e => form.classList.remove('hidden'))
let list = d.createElement('div');
list.className = 'list';
let tags = form.querySelector('div.refs');
form.insertBefore(list, tags);
let kindname = { 't': 'tag', 'p': 'person', 'l': 'place'}
let input = form.querySelector('input[name=q]');
input.autocomplete = "off";
input.addEventListener('keyup', e => {
let v = e.target.value;
if (new Set(['ArrowUp', 'ArrowDown', 'Escape']).has(e.code)) {
return;
}
if (v.length > 1) {
let r = new XMLHttpRequest();
r.onload = function() {
let t = JSON.parse(this.responseText);
list.innerHTML = '';
t.map(x => {
let a = d.createElement('a');
a.innerHTML = x.t + ' <small>(' + kindname[x.k] + ')</small>';
a.className='hit ' + x.k;
a.href = x.s;
a.onclick = function() {
let s = d.createElement('label');
s.innerHTML = x.t + ' <input type="checkbox" checked name="' + x.k +
'" value="' + x.s + '">';
s.className = x.k;
prepareQtag(s);
2019-07-20 00:46:26 +04:00
tags.insertBefore(s, input);
list.innerHTML = '';
input.value = '';
input.focus();
return false;
}
list.appendChild(a)
})
};
r.open('GET', d.location.origin + '/ac?q=' + encodeURIComponent(v));
r.send(null);
} else {
list.innerHTML = '';
}
})
form.addEventListener('keyup', e => {
let t = e.target;
switch(e.code) {
case 'ArrowUp':
(t.parentNode == list && t.previousSibling || list.querySelector('a:last-child')).focus();
break;
case 'ArrowDown':
(t.parentNode == list && t.nextSibling || list.querySelector('a:first-child')).focus();
break;
case 'Escape':
if (list.hasChildNodes()) {
input.focus();
list.innerHTML = '';
} else {
form.classList.add('hidden');
}
break;
default:
return true;
};
e.preventDefault();
e.stopPropagation();
return false;
});
//form.querySelector('.help .js').innerHTML = 'Du kan begränsa din sökning till de taggar som föreslås.';
})(d.querySelector('form.search'));
})(document)