Add submit button

This commit is contained in:
Tim Van Baak 2020-03-30 23:58:05 -07:00
parent e19a2a5b26
commit aa4b94d0c1
2 changed files with 31 additions and 10 deletions

View File

@ -46,18 +46,33 @@ function update(article) {
req.responseType = "json"; req.responseType = "json";
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) { if (req.readyState == 4 && req.status == 200) {
// Update internal state with the returned article object
params.article = req.response.article; params.article = req.response.article;
// Set editor editability based on article status
var title = document.getElementById("editor-title").value; updateEditorStatus();
var previewHtml = "<h1>" + title + "</h1>\n" + req.response.info.rendered; // Update the preview with the parse information
document.getElementById("preview").innerHTML = previewHtml; updatePreview(req.response.info);
document.getElementById("preview-control").innerHTML = req.response.info.word_count;
} }
}; };
var payload = { article: article }; var payload = { article: article };
req.send(JSON.stringify(payload)); req.send(JSON.stringify(payload));
} }
function updateEditorStatus() {
var ready = !!params.article.status.ready
document.getElementById("editor-title").disabled = ready;
document.getElementById("editor-content").disabled = ready;
var submitButton = document.getElementById("button-submit");
submitButton.innerText = ready ? "Edit article" : "Submit article";
}
function updatePreview(info) {
var title = document.getElementById("editor-title").value;
var previewHtml = "<h1>" + title + "</h1>\n" + info.rendered;
document.getElementById("preview").innerHTML = previewHtml;
document.getElementById("preview-control").innerHTML = info.word_count;
}
function onContentChange(timeout=2000) { function onContentChange(timeout=2000) {
ifNoFurtherChanges(() => { ifNoFurtherChanges(() => {
var article = buildArticleObject(); var article = buildArticleObject();
@ -65,6 +80,14 @@ function onContentChange(timeout=2000) {
}, timeout); }, timeout);
} }
function submitArticle() {
ifNoFurtherChanges(() => {
params.article.status.ready = !params.article.status.ready;
var article = buildArticleObject();
update(article);
}, 0);
}
window.addEventListener("beforeunload", function(e) { window.addEventListener("beforeunload", function(e) {
var content = document.getElementById("editor-content").value var content = document.getElementById("editor-content").value
var hasText = content.length > 0 && content != params.article.contents; var hasText = content.length > 0 && content != params.article.contents;

View File

@ -34,11 +34,9 @@
<a href="{{ url_for('lexicon.session', name=g.lexicon.name) }}"> <a href="{{ url_for('lexicon.session', name=g.lexicon.name) }}">
{{ g.lexicon.title }} {{ g.lexicon.title }}
</a> </a>
<!-- <select id="editor-character"> {% if article %}
{% for char in g.lexicon.get_characters_for_player(current_user.id) %} <button id="button-submit" onclick="submitArticle()">Submit article</button>
<option value="{{ char.cid }}">{{ char.name }}</option> {% endif %}
{% endfor %}
</select> -->
<span> <span>
<b> <b>
{% if character %} {% if character %}