-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix discard behavior in quiz editor -> edit answer
- Loading branch information
Showing
7 changed files
with
141 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# https://stackoverflow.com/a/8133832/ | ||
<% environment.context_class.instance_eval { include ApplicationHelper } %> | ||
|
||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ | ||
|
||
# change button 'Bearbeiten' to 'verwerfen' after answer body is revealed | ||
|
||
# the "target button" is either the "discard" button or the "edit" button | ||
# what its purpose is is stored in this object with a mapping | ||
# id -> boolean | ||
targetButtonIsDiscardButton = {} | ||
window.registeredDiscardListeners = new Set(); # set of answer ids | ||
|
||
$(document).on 'turbolinks:load', -> | ||
|
||
$(document).on 'shown.bs.collapse', '[id^="collapse-answer-"]', -> | ||
# Answer is now shown to the user and can be edited | ||
answerId = $(this).data('id'); | ||
registerDiscardListeners(); | ||
targetButtonIsDiscardButton[answerId] = true; | ||
$target = $('#targets-answer-' + answerId) | ||
$target.empty().append($target.data('discard')) | ||
.removeClass('btn-primary').addClass('btn-secondary') | ||
|
||
$(document).on 'hidden.bs.collapse', '[id^="collapse-answer-"]', -> | ||
# Answer is now hidden from the user | ||
answerId = $(this).data('id') | ||
targetButtonIsDiscardButton[answerId] = false; | ||
$target = $('#targets-answer-' + answerId) | ||
$target.empty().append($target.data('edit')) | ||
.removeClass('btn-secondary').addClass('btn-primary') | ||
|
||
# change color of box depending on whether answer is marked | ||
# as correct or incorrect | ||
$(document).on 'change', '[id^="answer-value-"]', -> | ||
id = $(this).data('id') | ||
isCorrectAnswer = $('#answer-true-' + id).is(':checked') | ||
|
||
# Set background color | ||
if isCorrectAnswer | ||
newClass = "<%= bgcolor(true) %>"; | ||
else | ||
newClass = "<%= bgcolor(false) %>"; | ||
$('#answer-header-' + id) | ||
.removeClass('bg-correct') | ||
.removeClass('bg-incorrect') | ||
.addClass(newClass) | ||
|
||
# Set ballot box | ||
answerBox = $('#answer-box-' + id) | ||
answerBox.empty() | ||
if isCorrectAnswer | ||
answerBox.append '<%= ballot_box(true) %>' | ||
else | ||
answerBox.append '<%= ballot_box(false) %>' | ||
|
||
# Cancel new answer creation | ||
$(document).on 'click', '#new-answer-cancel', -> | ||
$('#new-answer').show() | ||
$('#new-answer-field').empty() | ||
|
||
# clean up everything before turbolinks caches | ||
$(document).on 'turbolinks:before-cache', -> | ||
$(document).off 'shown.bs.collapse', '[id^="collapse-answer-"]' | ||
$(document).off 'hidden.bs.collapse', '[id^="collapse-answer-"]' | ||
$(document).off 'change', '[id^="answer-value-"]' | ||
$(document).off 'click', '#new-answer-cancel' | ||
|
||
|
||
registerDiscardListeners = () -> | ||
buttons = $('[id^=targets-answer-]'); | ||
$.each(buttons, (i,btn) -> | ||
btn = $(btn); | ||
answerId = btn.attr('id').split('-')[2]; | ||
|
||
# Don't register listeners multiple times | ||
if answerId in window.registeredDiscardListeners | ||
return; | ||
|
||
window.registeredDiscardListeners.add(answerId); | ||
$(this).on('click', (evt) => | ||
isDiscardButton = targetButtonIsDiscardButton[answerId]; | ||
if not isDiscardButton | ||
return; | ||
|
||
# On discard | ||
$.ajax Routes.cancel_edit_answer_path(answerId), | ||
type: 'GET' | ||
dataType: 'script' | ||
error: (jqXHR, textStatus, errorThrown) -> | ||
console.log("AJAX Error: #{textStatus}") | ||
); | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var answerId = <%= @answer.id %>; | ||
var answerCard = $(`#answers-accordion > #answer-card-${answerId}`); | ||
window.registeredDiscardListeners.delete(answerId); | ||
|
||
// eslint-disable-next-line @stylistic/quotes | ||
var newAnswerCardElements = $(`<%= j render partial: 'answers/card', locals: { answer: @answer } %>`).children(); | ||
|
||
// re-render possible MathJax content | ||
// eslint-disable-next-line no-undef | ||
renderMathInElement(newAnswerCardElements.get(0), { | ||
delimiters: [ | ||
{ | ||
left: "$$", | ||
right: "$$", | ||
display: true, | ||
}, | ||
{ | ||
left: "$", | ||
right: "$", | ||
display: false, | ||
}, | ||
{ | ||
left: "\\(", | ||
right: "\\)", | ||
display: false, | ||
}, | ||
{ | ||
left: "\\[", | ||
right: "\\]", | ||
display: true, | ||
}, | ||
], | ||
throwOnError: false, | ||
}, | ||
); | ||
|
||
setTimeout(() => { | ||
answerCard.empty().append(newAnswerCardElements); | ||
}, 100); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters