album/admin.js

243 lines
7.6 KiB
JavaScript
Raw Normal View History

2017-09-25 01:32:33 +04:00
// Admin functionality for rphotos
2017-12-12 02:11:36 +04:00
(function (d) {
var details = d.querySelector('.details');
2017-09-25 01:32:33 +04:00
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();
2017-12-12 02:11:36 +04:00
d.body.classList.add('busy');
2017-09-25 01:32:33 +04:00
r.open('POST', '/adm/rotate');
r.onload = function() {
if (r.status === 200) {
2017-11-06 21:20:45 +04:00
// Yay!
2017-12-12 02:11:36 +04:00
d.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
}
2017-12-12 02:11:36 +04:00
d.body.classList.remove('busy');
2017-09-25 01:32:33 +04:00
}
r.onerror = function() {
2017-11-06 21:20:45 +04:00
alert("Rotating failed.");
2017-12-12 02:11:36 +04:00
d.body.classList.remove('busy');
2017-09-25 01:32:33 +04:00
}
r.ontimeout = function() {
2017-11-06 21:20:45 +04:00
alert("Rotating timed out");
2017-12-12 02:11:36 +04:00
d.body.classList.remove('busy');
2017-09-25 01:32:33 +04:00
}
r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
r.send("angle=" + angle + "&image=" + imgid)
}
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;
2017-12-12 02:11:36 +04:00
var f = d.createElement("form");
2017-11-12 01:02:01 +04:00
f.className = "admin " + category;
2017-11-10 22:26:39 +04:00
f.action = "/adm/" + category;
2017-11-06 21:20:45 +04:00
f.method = "post";
2017-12-12 02:11:36 +04:00
var l = d.createElement("label");
2017-11-12 01:02:01 +04:00
l.innerHTML = event.target.title;
f.appendChild(l);
2017-12-12 02:11:36 +04:00
var i = d.createElement("input");
2017-11-06 21:20:45 +04:00
i.type="hidden";
i.name="image";
i.value = imgid;
f.appendChild(i);
2017-12-12 02:11:36 +04:00
i = d.createElement("input");
2017-11-06 21:20:45 +04:00
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;
2017-11-12 01:02:01 +04:00
i.id = category+'name';
l.htmlFor = i.id;
2017-12-12 02:11:36 +04:00
var list = d.createElement("div");
2017-12-12 01:07:27 +04:00
list.className = "completions";
2017-11-10 22:26:39 +04:00
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);
2017-11-12 01:02:01 +04:00
list.style.top = i.offsetTop + i.offsetHeight + "px"
list.style.left = i.offsetLeft + "px"
2017-11-10 22:26:39 +04:00
list.innerHTML = '';
t.map(x => {
2017-12-12 02:11:36 +04:00
let a = d.createElement('a');
2017-11-10 22:26:39 +04:00
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)
})
};
2017-12-12 02:11:36 +04:00
r.open('GET', d.location.origin + '/ac/' + category + '?q=' + encodeURIComponent(v));
2017-11-10 22:26:39 +04:00
r.send(null);
} else {
list.innerHTML = '';
}
e.preventDefault();
e.stopPropagation();
return false;
});
2017-11-06 21:20:45 +04:00
f.appendChild(i);
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-12 01:02:01 +04:00
if (i.value) {
list.innerHTML = '';
i.focus();
} else { // close form
e.target.closest('form').remove();
event.target.disabled = false;
event.target.focus();
}
2017-11-06 21:20:45 +04:00
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-12-12 02:11:36 +04:00
let s = d.createElement("button");
2017-11-12 01:02:01 +04:00
s.innerHTML = "Ok";
s.type = "submit";
f.appendChild(s);
2017-12-12 02:11:36 +04:00
let c = d.createElement("button");
2017-11-12 01:02:01 +04:00
c.innerHTML = "🗙";
c.className = 'close';
c.title = 'close';
c.onclick = e => {
e.target.closest('form').remove();
event.target.disabled = false; // The old event creating this form
event.target.focus();
};
f.appendChild(c);
2017-11-06 21:20:45 +04:00
meta.appendChild(f);
2017-11-06 02:03:55 +04:00
i.focus();
}
2017-11-23 00:13:34 +04:00
function grade_form(event) {
event.target.disabled = true;
var imgid = details.dataset.imgid;
var grade = details.dataset.grade;
2017-12-12 02:11:36 +04:00
var f = d.createElement("form");
2017-11-23 00:13:34 +04:00
f.className = "admin grade";
f.action = "/adm/grade";
f.method = "post";
2017-12-12 02:11:36 +04:00
var l = d.createElement("label");
2017-11-23 00:13:34 +04:00
l.innerHTML = event.target.title;
f.appendChild(l);
2017-12-12 02:11:36 +04:00
var i = d.createElement("input");
2017-11-23 00:13:34 +04:00
i.type="hidden";
i.name="image";
i.value = imgid;
f.appendChild(i);
2017-12-12 02:11:36 +04:00
i = d.createElement("input");
2017-11-23 00:13:34 +04:00
i.type="range";
i.name="grade";
if (grade) {
i.value=grade;
}
i.min=0;
i.max=100;
f.appendChild(i);
2017-12-12 02:11:36 +04:00
let s = d.createElement("button");
2017-11-23 00:13:34 +04:00
s.innerHTML = "Ok";
s.type = "submit";
f.appendChild(s);
2017-12-12 02:11:36 +04:00
let c = d.createElement("button");
2017-11-23 00:13:34 +04:00
c.innerHTML = "🗙";
c.className = 'close';
c.title = 'close';
c.onclick = e => {
e.target.closest('form').remove();
event.target.disabled = false; // The old event creating this form
event.target.focus();
};
f.appendChild(c);
f.addEventListener('keypress', e => {
switch(e.code) {
case 'Escape':
e.target.closest('form').remove();
event.target.disabled = false;
event.target.focus();
break;
case 'Enter':
f.submit();
break;
default:
return true;
};
e.preventDefault();
e.stopPropagation();
return false;
});
meta.appendChild(f);
i.focus();
}
2017-09-25 01:32:33 +04:00
var meta = details.querySelector('.meta');
if (meta) {
2017-12-12 02:11:36 +04:00
p = d.createElement("p");
r = d.createElement("button");
2017-11-06 21:20:45 +04:00
r.onclick = rotate;
r.innerHTML = "\u27f2";
r.dataset.angle = "-90";
r.title = "Rotate left";
p.appendChild(r);
2017-12-12 02:11:36 +04:00
p.appendChild(d.createTextNode(" "));
r = d.createElement("button");
2017-11-06 21:20:45 +04:00
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-12-12 02:11:36 +04:00
p.appendChild(d.createTextNode(" "));
r = d.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 = "🏷";
2017-11-12 01:02:01 +04:00
r.title = "Tag";
r.accessKey = "t";
2017-11-06 21:20:45 +04:00
p.appendChild(r);
2017-11-10 22:26:39 +04:00
2017-12-12 02:11:36 +04:00
p.appendChild(d.createTextNode(" "));
r = d.createElement("button");
2017-11-10 22:26:39 +04:00
r.onclick = e => tag_form(e, 'person');
r.innerHTML = "\u263a";
2017-11-12 01:02:01 +04:00
r.title = "Person";
r.accessKey = "p";
2017-11-10 22:26:39 +04:00
p.appendChild(r);
2017-11-23 00:13:34 +04:00
2017-12-12 02:11:36 +04:00
p.appendChild(d.createTextNode(" "));
r = d.createElement("button");
2017-11-23 00:13:34 +04:00
r.onclick = e => grade_form(e);
r.innerHTML = "\u2606";
r.title = "Grade";
r.accessKey = "g";
p.appendChild(r);
2017-11-06 21:20:45 +04:00
meta.appendChild(p);
2017-09-25 01:32:33 +04:00
}
2017-12-12 02:11:36 +04:00
})(document)