Commit 89249e12 authored by Henrik Tramberend's avatar Henrik Tramberend

Differentiate between create and update

- Edit does not delete a question and only submits an update
- Update requests carry a comment id, delete requests do not
parent cbc41171
export {contactEngine};
export { contactEngine };
// TODO Make into a proper Reveal 4 plugin
......@@ -9,7 +9,7 @@ let engine = {
api: undefined,
deckId: undefined, // The unique deck identifier.
token: undefined
}
};
// Contacts the engine API at base.
function contactEngine(base, deckId) {
......@@ -29,7 +29,7 @@ function contactEngine(base, deckId) {
});
}
// Strips the document URI from everything that can not be part of the deck id.
// Strips the document URI from everything that can not be part of the deck id.
function deckUrl() {
let url = new URL(window.location);
url.hash = "";
......@@ -57,7 +57,11 @@ function prepareEngine() {
}
// Build the menu, once Reval and the menu are ready.
if (Reveal.isReady() && Reveal.hasPlugin('menu') && Reveal.getPlugin('menu').isInit()) {
if (
Reveal.isReady() &&
Reveal.hasPlugin("menu") &&
Reveal.getPlugin("menu").isInit()
) {
buildMenu();
} else {
Reveal.addEventListener("menu-ready", _ => {
......@@ -68,7 +72,7 @@ function prepareEngine() {
.catch(e => {
// Nothing goes without a token
console.log("API function getToken() failed: " + e);
throw (e);
throw e;
});
}
......@@ -227,11 +231,7 @@ function buildInterface() {
let updateComments = () => {
let slideId = Reveal.getCurrentSlide().id;
engine.api
.getComments(
engine.deckId,
slideId,
engine.token.admin || user.value
)
.getComments(engine.deckId, slideId, engine.token.admin || user.value)
.then(renderList)
.catch(console.log);
};
......@@ -239,6 +239,8 @@ function buildInterface() {
let renderSubmit = () => {
updateComments();
text.value = "";
text.commentId = null;
text.answered = null;
};
let canDelete = comment => {
......@@ -312,10 +314,9 @@ function buildInterface() {
let mod = document.createElement("button");
mod.appendChild(edit.cloneNode(true));
mod.addEventListener("click", _ => {
engine.api
.deleteComment(comment.id, engine.token.admin || user.value)
.then(updateComments);
text.value = comment.markdown;
text.commentId = comment.id;
text.answered = comment.answered;
text.focus();
});
box.appendChild(mod);
......@@ -370,7 +371,7 @@ function buildInterface() {
.getLogin({
login: username.value,
password: password.value,
deck: engine.deckId
deck: engine.deckId
})
.then(token => {
engine.token.admin = token.admin;
......@@ -386,7 +387,7 @@ function buildInterface() {
}
});
if (!(engine.token.authorized)) {
if (!engine.token.authorized) {
user.addEventListener("keydown", e => {
if (e.key === "Enter") {
updateComments();
......@@ -417,7 +418,14 @@ function buildInterface() {
if (e.key === "Enter" && e.shiftKey) {
let slideId = Reveal.getCurrentSlide().id;
engine.api
.submitComment(engine.deckId, slideId, user.value, text.value)
.submitComment(
engine.deckId,
slideId,
user.value,
text.value,
text.commentId,
text.answered
)
.then(renderSubmit)
.catch(console.log);
e.stopPropagation();
......@@ -437,21 +445,25 @@ function buildInterface() {
function buildMenu() {
let updateMenu = list => {
for (let comment of list) {
// get slide info
const slideID = comment.slide;
const slide = document.getElementById(slideID);
const indices = Reveal.getIndices(slide);
// build query string, get menu item
let query = 'ul.slide-menu-items > li.slide-menu-item';
if (indices.h) query += '[data-slide-h=\"' + indices.h + '\"]';
if (indices.v) query += '[data-slide-v=\"' + indices.v + '\"]';
let query = "ul.slide-menu-items > li.slide-menu-item";
if (indices.h) query += '[data-slide-h="' + indices.h + '"]';
if (indices.v) query += '[data-slide-v="' + indices.v + '"]';
let li = document.querySelector(query);
// update question counter
if (li) {
li.setAttribute('data-questions', li.hasAttribute('data-questions') ? parseInt(li.getAttribute('data-questions')) + 1 : 1);
li.setAttribute(
"data-questions",
li.hasAttribute("data-questions")
? parseInt(li.getAttribute("data-questions")) + 1
: 1
);
}
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment