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

107 lines
3.1 KiB
JavaScript
Raw Normal View History

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() {
// New result text behaviour
function initNewReslutText() {
$('#new-result-text').on('click', function(event) {
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
var $btn = $(this);
$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);
_formAjaxResultText($form);
Results.initCancelFormButton($form, initNewReslutText);
Results.toggleResultEditButtons(false);
TinyMCE.refresh();
TinyMCE.highlight();
$('#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').on('ajax:success', function(e, data) {
var $result = $(this).closest('.result');
var $form = $(data.html);
var $prevResult = $result;
$result.after($form);
$result.remove();
2016-02-12 23:52:43 +08:00
_formAjaxResultText($form);
2017-05-11 22:54:28 +08:00
// Cancel button
$form.find('.cancel-edit').click(function() {
$form.after($prevResult);
$form.remove();
applyEditResultTextCallback();
Results.toggleResultEditButtons(true);
2016-02-12 23:52:43 +08:00
});
Results.toggleResultEditButtons(false);
TinyMCE.refresh();
$('#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) {
$form.on('ajax:success', function(e, data) {
$form.after(data.html);
var newResult = $form.next();
initFormSubmitLinks(newResult);
$(this).remove();
2016-02-12 23:52:43 +08:00
applyEditResultTextCallback();
Results.applyCollapseLinkCallBack();
Results.toggleResultEditButtons(true);
Results.expandResult(newResult);
TinyMCE.destroyAll();
Comments.initialize();
initNewReslutText();
});
$form.on('ajax:error', function(e, xhr, status, error) {
var data = xhr.responseJSON;
$form.renderFormErrors('result', data);
TinyMCE.highlight();
if (data['result_text.text']) {
var $el = $form.find(
'textarea[name=result\\[result_text_attributes\\]\\[text\\]]'
);
2016-02-12 23:52:43 +08:00
$el.closest('.form-group').addClass('has-error');
$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
2017-05-11 22:54:28 +08:00
var publicAPI = Object.freeze({
initNewReslutText: initNewReslutText,
applyEditResultTextCallback: applyEditResultTextCallback
2016-02-12 23:52:43 +08:00
});
2017-05-11 22:54:28 +08:00
return publicAPI;
})();
$(document).ready(function() {
ResultText.initNewReslutText();
ResultText.applyEditResultTextCallback();
});
})();