album/admin.js

150 lines
4.7 KiB
JavaScript
Raw Normal View History

2017-09-25 01:32:33 +04:00
// Admin functionality for rphotos
function rpadmin() {
var details = document.querySelector('.details');
function rotate(event) {
2017-11-06 21:20:45 +04:00
var imgid = details.dataset.imgid;
var angle = event.target.dataset.angle;
2017-09-25 01:32:33 +04:00
var r = new XMLHttpRequest();
document.body.classList.add('busy');
r.open('POST', '/adm/rotate');
r.onload = function() {
if (r.status === 200) {
2017-11-06 21:20:45 +04:00
// Yay!
document.location.reload(true);
2017-09-25 01:32:33 +04:00
} else {
2017-11-06 21:20:45 +04:00
alert("Rotating failed: " + r.status);
2017-09-25 01:32:33 +04:00
}
document.body.classList.remove('busy');
}
r.onerror = function() {
2017-11-06 21:20:45 +04:00
alert("Rotating failed.");
2017-09-25 01:32:33 +04:00
document.body.classList.remove('busy');
}
r.ontimeout = function() {
2017-11-06 21:20:45 +04:00
alert("Rotating timed out");
2017-09-25 01:32:33 +04:00
document.body.classList.remove('busy');
}
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
r.send("angle=" + angle + "&image=" + imgid)
}
2017-11-06 02:03:55 +04:00
var list;
2017-11-10 22:26:39 +04:00
function tag_form(event, category) {
2017-11-06 21:20:45 +04:00
event.target.disabled = true;
var imgid = details.dataset.imgid;
var f = document.createElement("form");
2017-11-10 22:26:39 +04:00
f.className = category;
f.action = "/adm/" + category;
2017-11-06 21:20:45 +04:00
f.method = "post";
var i = document.createElement("input");
i.type="hidden";
i.name="image";
i.value = imgid;
f.appendChild(i);
i = document.createElement("input");
i.type = "text";
2017-11-06 02:03:55 +04:00
i.autocomplete="off";
i.tabindex="1";
2017-11-10 22:26:39 +04:00
i.name = category;
i.addEventListener('keyup', e => {
let c = e.code;
if (c == 'ArrowUp' || c == 'ArrowDown' || c == 'Escape' || c == 'Enter') {
return true;
}
let i = e.target, v = i.value;
if (v.length > 0) {
let r = new XMLHttpRequest();
r.onload = function() {
let t = JSON.parse(this.responseText);
list.innerHTML = '';
t.map(x => {
let a = document.createElement('a');
a.innerHTML = x;
a.tabIndex = 2;
a.href = "#";
a.onclick = function(e) {
i.value = x;
list.innerHTML = '';
i.focus();
e.preventDefault();
e.stopPropagation();
return true;
}
list.appendChild(a)
})
};
r.open('GET', document.location.origin + '/ac/' + category + '?q=' + encodeURIComponent(v));
r.send(null);
} else {
list.innerHTML = '';
}
e.preventDefault();
e.stopPropagation();
return false;
});
2017-11-06 21:20:45 +04:00
f.appendChild(i);
list = document.createElement("div");
list.className = "completions";
f.appendChild(list);
2017-11-06 02:03:55 +04:00
f.addEventListener('keypress', e => {
let t = e.target;
switch(e.code) {
case 'ArrowUp':
2017-11-06 21:20:45 +04:00
(t.parentNode == list && t.previousSibling || list.querySelector('a:last-child')).focus();
break;
2017-11-06 02:03:55 +04:00
case 'ArrowDown':
2017-11-06 21:20:45 +04:00
(t.parentNode == list && t.nextSibling || list.querySelector('a:first-child')).focus();
break;
2017-11-06 02:03:55 +04:00
case 'Escape':
2017-11-06 21:20:45 +04:00
list.innerHTML = '';
i.focus();
break;
2017-11-06 02:03:55 +04:00
default:
2017-11-06 21:20:45 +04:00
return true;
2017-11-06 02:03:55 +04:00
};
e.preventDefault();
e.stopPropagation();
return false;
});
2017-11-06 21:20:45 +04:00
meta.appendChild(f);
2017-11-06 02:03:55 +04:00
i.focus();
}
2017-09-25 01:32:33 +04:00
var meta = details.querySelector('.meta');
if (meta) {
2017-11-06 21:20:45 +04:00
p = document.createElement("p");
r = document.createElement("button");
r.onclick = rotate;
r.innerHTML = "\u27f2";
r.dataset.angle = "-90";
r.title = "Rotate left";
p.appendChild(r);
p.appendChild(document.createTextNode(" "));
r = document.createElement("button");
r.onclick = rotate;
r.innerHTML = "\u27f3";
r.dataset.angle = "90";
r.title = "Rotate right";
p.appendChild(r);
2017-11-06 02:03:55 +04:00
2017-11-06 21:20:45 +04:00
p.appendChild(document.createTextNode(" "));
r = document.createElement("button");
2017-11-10 22:26:39 +04:00
r.onclick = e => tag_form(e, 'tag');
2017-11-06 21:20:45 +04:00
r.innerHTML = "🏷";
r.title = "Tag photo";
r.accessKey = "T";
p.appendChild(r);
2017-11-10 22:26:39 +04:00
p.appendChild(document.createTextNode(" "));
r = document.createElement("button");
r.onclick = e => tag_form(e, 'person');
r.innerHTML = "\u263a";
r.title = "Person in picture";
r.accessKey = "P";
p.appendChild(r);
2017-11-06 21:20:45 +04:00
meta.appendChild(p);
2017-09-25 01:32:33 +04:00
}
}