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

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-05-10 19:53:32 +08:00
(function(global) {
'use strict';
2016-02-12 23:52:43 +08:00
global.ResutlAssets = (function() {
2017-05-11 22:54:28 +08:00
// New result asset behaviour
function initNewResultAsset() {
$('#new-result-asset').on('click', function(event) {
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
var $btn = $(this);
$btn.off();
animateSpinner(null, true);
2016-07-21 19:11:15 +08:00
// get new result form
$.ajax({
url: $btn.data('href'),
method: 'GET',
success: function(data) {
var $form = $(data.html);
animateSpinner(null, false);
2017-06-09 15:49:57 +08:00
$('#results').prepend($form);
_formAjaxResultAsset($form);
Results.initCancelFormButton($form, initNewResultAsset);
Results.toggleResultEditButtons(false);
dragNdropAssetsInit('results');
},
error: function(xhr, status, e) {
$(this).renderFormErrors('result', xhr.responseJSON, true, e);
animateSpinner(null, false);
initNewResultAsset();
}
2017-05-10 19:53:32 +08:00
});
2017-05-11 22:54:28 +08:00
});
}
2016-02-12 23:52:43 +08:00
2017-05-11 22:54:28 +08:00
function applyEditResultAssetCallback() {
$('.edit-result-asset').on('ajax:success', function(e, data) {
var $result = $(this).closest('.result');
var $form = $(data.html);
var $prevResult = $result;
$result.after($form);
$result.remove();
2016-07-21 19:11:15 +08:00
2017-05-11 22:54:28 +08:00
_formAjaxResultAsset($form);
2016-07-21 19:11:15 +08:00
2017-05-11 22:54:28 +08:00
// Cancel button
$form.find('.cancel-edit').click(function () {
$form.after($prevResult);
$form.remove();
2017-05-10 19:53:32 +08:00
applyEditResultAssetCallback();
2017-05-11 22:54:28 +08:00
Results.toggleResultEditButtons(true);
2017-05-10 19:53:32 +08:00
initPreviewModal();
});
2017-05-11 22:54:28 +08:00
Results.toggleResultEditButtons(false);
$('#result_name').focus();
}).on('ajax:error', function(e, xhr, status, error) {
animateSpinner(null, false);
});
}
function _formAjaxResultAsset($form) {
$form.on('ajax:success', function(e, data) {
$form.after(data.html);
var $newResult = $form.next();
initFormSubmitLinks($newResult);
$(this).remove();
applyEditResultAssetCallback();
Results.applyCollapseLinkCallBack();
Results.toggleResultEditButtons(true);
Results.expandResult($newResult);
initPreviewModal();
Comments.initialize();
initNewResultAsset();
}).on('ajax:error', function(e, xhr) {
2017-06-28 17:17:50 +08:00
var errors = xhr.responseJSON.errors;
var formInput = $form.find('#result_asset_attributes_file');
$('[data-status="error"]').remove();
$.each(errors, function(key, value) {
var message = '<span data-status="error" style="color: #a94442">';
message += value + '</span>';
formInput.after(message);
})
2017-05-11 22:54:28 +08:00
animateSpinner(null, false);
});
}
var publicAPI = Object.freeze({
initNewResultAsset: initNewResultAsset,
applyEditResultAssetCallback: applyEditResultAssetCallback
2017-05-10 19:53:32 +08:00
});
2016-07-21 19:11:15 +08:00
2017-05-10 19:53:32 +08:00
return publicAPI;
})();
2016-02-12 23:52:43 +08:00
2018-07-19 23:56:42 +08:00
ResutlAssets.initNewResultAsset();
ResutlAssets.applyEditResultAssetCallback();
global.initPreviewModal();
}(window));