scinote-web/app/assets/javascripts/results/result_texts.js

133 lines
4.2 KiB
JavaScript
Raw Normal View History

/* global TinyMCE Results Comments animateSpinner initFormSubmitLinks Prism */
2018-07-19 23:56:42 +08:00
2017-05-11 22:54:28 +08:00
(function() {
'use strict';
2016-02-12 23:52:43 +08:00
2017-05-11 22:54:28 +08:00
var ResultText = (function() {
2018-07-19 23:56:42 +08:00
var publicAPI;
2017-05-11 22:54:28 +08:00
// New result text behaviour
function initNewReslutText() {
$('#new-result-text').on('click', function(event) {
2018-07-19 23:56:42 +08:00
var $btn = $(this);
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
$btn.off();
animateSpinner(null, true);
2016-02-12 23:52:43 +08:00
// get new result form
$.ajax({
url: $btn.data('href'),
method: 'GET',
success: function(data) {
var $form = $(data.html);
animateSpinner(null, false);
$('#results').prepend($form);
2018-07-19 23:56:42 +08:00
formAjaxResultText($form);
Results.initCancelFormButton($form, initNewReslutText);
Results.toggleResultEditButtons(false);
TinyMCE.init('#result_text_attributes_textarea', {
assignableMyModuleId: $('#result_text_attributes_textarea').data('my-module-id')
});
$('#result_name').focus();
2023-08-28 21:16:15 +08:00
setTimeout(function() {
$('.edit-text-result').css('display', 'initial');
}, 600);
setTimeout(function() {
$('.edit-text-result').css('display', 'block');
}, 800)
},
error: function() {
animateSpinner(null, false);
initNewReslutText();
}
});
2017-05-11 22:54:28 +08:00
});
}
2016-02-12 23:52:43 +08:00
function initSaveReslutText() {
$('#results').on('click', '.edit-text-result .save-result', (event) => {
Results.processResult(event, Results.ResultTypeEnum.TEXT);
});
}
2017-05-11 22:54:28 +08:00
// Edit result text button behaviour
function applyEditResultTextCallback() {
$('.edit-result-text').off('ajax:success ajax:error').on('ajax:success', function(e, data) {
var $result = $(this).closest('.result');
var $form = $(data.html);
var $prevResult = $result;
$result.after($form);
$prevResult.hide();
2016-02-12 23:52:43 +08:00
formAjaxResultText($form, $prevResult);
2017-05-11 22:54:28 +08:00
// Cancel button
$form.find('.cancel-edit').click(function() {
$prevResult.show();
$form.remove();
applyEditResultTextCallback();
2019-04-26 23:37:01 +08:00
TinyMCE.destroyAll();
Results.toggleResultEditButtons(true);
2016-02-12 23:52:43 +08:00
});
Results.toggleResultEditButtons(false);
TinyMCE.init('#result_text_attributes_textarea', {
assignableMyModuleId: $('#result_text_attributes_textarea').data('my-module-id')
});
$('#result_name').focus();
2023-08-28 21:16:15 +08:00
setTimeout(function() {
$('.edit-text-result').css('display', 'initial');
}, 600);
setTimeout(function() {
$('.edit-text-result').css('display', 'block');
}, 800)
});
2017-05-11 22:54:28 +08:00
}
2016-02-12 23:52:43 +08:00
2017-05-11 22:54:28 +08:00
// Apply ajax callback to form
function formAjaxResultText($form, $prevResult) {
$form.on('ajax:success', function(e, data) {
2018-07-19 23:56:42 +08:00
var newResult;
if ($prevResult) $prevResult.remove();
$form.after(data.html);
2018-07-19 23:56:42 +08:00
newResult = $form.next();
initFormSubmitLinks(newResult);
$(this).remove();
applyEditResultTextCallback();
Results.toggleResultEditButtons(true);
Results.expandResult(newResult);
TinyMCE.destroyAll();
Comments.init();
initNewReslutText();
Prism.highlightAllUnder(newResult.get(0));
});
2018-07-19 23:56:42 +08:00
$form.on('ajax:error', function(e, xhr) {
var data = xhr.responseJSON;
2018-07-19 23:56:42 +08:00
var $el;
$form.renderFormErrors('result', data);
if (data['result_text.text']) {
2018-07-19 23:56:42 +08:00
$el = $form.find(
'textarea[name=result\\[result_text_attributes\\]\\[text\\]]'
);
$el.closest('.form-group').addClass('has-error');
2018-07-19 23:56:42 +08:00
$el.parent().append('<span class=\'help-block\'>'
+ data['result_text.text'] + '</span>');
}
});
2017-05-11 22:54:28 +08:00
}
2016-02-12 23:52:43 +08:00
2018-07-19 23:56:42 +08:00
publicAPI = Object.freeze({
2017-05-11 22:54:28 +08:00
initNewReslutText: initNewReslutText,
initSaveReslutText: initSaveReslutText,
2017-05-11 22:54:28 +08:00
applyEditResultTextCallback: applyEditResultTextCallback
2016-02-12 23:52:43 +08:00
});
2017-05-11 22:54:28 +08:00
return publicAPI;
2018-07-19 23:56:42 +08:00
}());
2017-05-11 22:54:28 +08:00
2018-07-19 23:56:42 +08:00
ResultText.initNewReslutText();
ResultText.initSaveReslutText();
2018-07-19 23:56:42 +08:00
ResultText.applyEditResultTextCallback();
}());