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

109 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-07-19 23:56:42 +08:00
/* global TinyMCE Results Comments animateSpinner initFormSubmitLinks */
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);
2019-04-26 23:37:01 +08:00
TinyMCE.init('#result_text_attributes_textarea');
$('#result_name').focus();
},
error: function() {
animateSpinner(null, false);
initNewReslutText();
}
});
2017-05-11 22:54:28 +08:00
});
}
2016-02-12 23:52:43 +08:00
2017-05-11 22:54:28 +08:00
// Edit result text button behaviour
function applyEditResultTextCallback() {
$('.edit-result-text').off().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);
2019-04-26 23:37:01 +08:00
TinyMCE.init('#result_text_attributes_textarea');
$('#result_name').focus();
});
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();
});
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);
TinyMCE.highlight();
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,
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.applyEditResultTextCallback();
}());