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() {
|
2017-05-12 21:59:58 +08:00
|
|
|
$('#new-result-text').on('click', function(event) {
|
2018-07-19 23:56:42 +08:00
|
|
|
var $btn = $(this);
|
2017-05-12 21:59:58 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.stopPropagation();
|
|
|
|
$btn.off();
|
|
|
|
animateSpinner(null, true);
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-12 21:59:58 +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);
|
2017-05-12 21:59:58 +08:00
|
|
|
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() {
|
2017-05-12 21:59:58 +08:00
|
|
|
$('.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
|
|
|
|
2018-07-19 23:56:42 +08:00
|
|
|
formAjaxResultText($form);
|
2017-05-11 22:54:28 +08:00
|
|
|
|
2017-05-12 21:59:58 +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
|
|
|
});
|
2017-05-12 21:59:58 +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
|
2018-07-19 23:56:42 +08:00
|
|
|
function formAjaxResultText($form) {
|
2017-05-12 21:59:58 +08:00
|
|
|
$form.on('ajax:success', function(e, data) {
|
2018-07-19 23:56:42 +08:00
|
|
|
var newResult;
|
2017-05-12 21:59:58 +08:00
|
|
|
$form.after(data.html);
|
2018-07-19 23:56:42 +08:00
|
|
|
newResult = $form.next();
|
2017-05-12 21:59:58 +08:00
|
|
|
initFormSubmitLinks(newResult);
|
|
|
|
$(this).remove();
|
|
|
|
applyEditResultTextCallback();
|
|
|
|
Results.applyCollapseLinkCallBack();
|
|
|
|
Results.toggleResultEditButtons(true);
|
|
|
|
Results.expandResult(newResult);
|
|
|
|
TinyMCE.destroyAll();
|
|
|
|
Comments.initialize();
|
|
|
|
initNewReslutText();
|
|
|
|
});
|
2018-07-19 23:56:42 +08:00
|
|
|
$form.on('ajax:error', function(e, xhr) {
|
2017-05-12 21:59:58 +08:00
|
|
|
var data = xhr.responseJSON;
|
2018-07-19 23:56:42 +08:00
|
|
|
var $el;
|
2017-05-12 21:59:58 +08:00
|
|
|
$form.renderFormErrors('result', data);
|
|
|
|
TinyMCE.highlight();
|
|
|
|
if (data['result_text.text']) {
|
2018-07-19 23:56:42 +08:00
|
|
|
$el = $form.find(
|
2017-05-12 21:59:58 +08:00
|
|
|
'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-12 21:59:58 +08:00
|
|
|
}
|
|
|
|
});
|
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();
|
|
|
|
}());
|