Skip to content
Snippets Groups Projects
Commit 3b3d8716 authored by Martin Mareš's avatar Martin Mareš
Browse files

"Changes not saved" does not react to the Submit button :)

Also, added a Cancel button to the form for editing posts.
parent 1dbfe1ad
No related branches found
No related tags found
No related merge requests found
......@@ -70,13 +70,19 @@ exports.add_ctrl_enter = function (f) {
}
}
let do_unload_check = true;
exports.add_unload_check = function (f) {
if (f) {
const orig_contents = f.value;
window.addEventListener('beforeunload', (event) => {
if (f.value != orig_contents) {
if (do_unload_check && f.value != orig_contents) {
event.preventDefault();
}
});
}
}
exports.cancel_unload_check = function () {
do_unload_check = false;
}
......@@ -320,6 +320,12 @@ def edit_post(sident: str, cident: str, pid: int):
else:
must_copy = deadline_passed or has_followup(post)
if g.is_grader and post.target_uid >= 0:
student_uid = post.target_uid
else:
student_uid = None
return_to = url_for('topic_index', sident=g.course.semester.ident, cident=g.course.ident, tident=g.topic.ident, student_uid=student_uid)
if request.method == 'POST':
form = EditPostForm()
if not form.validate_on_submit():
......@@ -329,12 +335,6 @@ def edit_post(sident: str, cident: str, pid: int):
flash('Post not submitted, see below for errors.', 'error')
return render_template('edit-post.html', form=form, must_copy=must_copy, deadline_passed=deadline_passed)
if g.is_grader and post.target_uid >= 0:
student_uid = post.target_uid
else:
student_uid = None
return_to = url_for('topic_index', sident=g.course.semester.ident, cident=g.course.ident, tident=g.topic.ident, student_uid=student_uid)
if form.delete.data:
if g.is_teacher:
app.logger.info(f"Deleted post: pid={pid} tid={post.tid} author_uid={post.author_uid} target_uid={post.target_uid} by_uid={g.uid} public={g.topic.public}")
......@@ -400,7 +400,11 @@ def edit_post(sident: str, cident: str, pid: int):
return redirect(return_to)
form = EditPostForm(obj=post)
return render_template('edit-post.html', form=form, must_copy=must_copy, deadline_passed=deadline_passed)
return render_template('edit-post.html',
form=form,
must_copy=must_copy,
deadline_passed=deadline_passed,
return_to=return_to)
# Serving files
......
......@@ -12,7 +12,7 @@
{% endif %}
{% endif %}
<form id=postform method="POST" action="">
<form id=postform method="POST" action="" onsubmit='window.owl.cancel_unload_check()'>
{{ form.csrf_token }}
{% if form.errors['comment'] %}
<p class=error>{{ form.errors['comment'][0] }}
......@@ -32,11 +32,13 @@
<div id=preview>
</div>
</div>
<p>{{ form.submit_button(class='ok', accesskey='s') }}
<p class='inline-buttons'>{{ form.submit_button(class='ok', accesskey='s') }}
{% if not must_copy %}
{{ form.silent_submit(class='ok', title="Submit, but do not send notifications.") }}
{% endif %}
<button type=button accesskey='p' onclick='javascript:window.owl.preview()'>Preview</button>
<a class=button href="{{ return_to }}" onclick='window.owl.cancel_unload_check()'>Cancel</a>
</span>
{% if not must_copy %}
<p>{{ form.delete(class='danger') }}
{% endif %}
......
......@@ -129,7 +129,7 @@
</div>
<h3>New post <span id=syntaxhelp>(You can use <a href='https://www.markdownguide.org/cheat-sheet/'>Markdown</a> with <a href='https://katex.org/docs/supported.html'>KaTeX math</a> here)</span></h3>
<form id=postform method="POST" action="" enctype="multipart/form-data">
<form id=postform method="POST" action="" enctype="multipart/form-data" onsubmit='window.owl.cancel_unload_check()'>
{{ form.csrf_token }}
{% if form.errors['comment'] %}
<p class=error>{{ form.errors['comment'][0] }}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment