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

123 lines
3.9 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.ResultAssets = (function() {
// New asset callback
function createResultAssetCallback() {
$('.new-result-assets-buttons')
.on('click', '.save-result', (event) => {
DragNDropResults.processResult(event); // eslint-disable-line no-undef
})
.on('click', '.cancel-new', () => {
DragNDropResults.destroyAll(); // eslint-disable-line no-undef
});
$('#new-result-assets-select').on('change', '#drag-n-drop-assets', function() {
DragNDropResults.init(this.files); // eslint-disable-line no-undef
});
}
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);
2022-10-24 19:07:10 +08:00
dragNdropAssetsInit();
createResultAssetCallback();
},
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
// Save asset callback
function saveResultAssetCallback() {
$('.edit-result-assets-buttons').on('click', '.save-result', (event) => {
Results.processResult(event, Results.ResultTypeEnum.FILE); // eslint-disable-line no-undef
});
}
2017-05-11 22:54:28 +08:00
function applyEditResultAssetCallback() {
$('.edit-result-asset').off('ajax:success ajax:error').on('ajax:success', function(e, data) {
2017-05-11 22:54:28 +08:00
var $result = $(this).closest('.result');
var $form = $(data.html);
var $prevResult = $result;
$result.after($form);
$prevResult.hide();
2016-07-21 19:11:15 +08:00
_formAjaxResultAsset($form, $prevResult);
2016-07-21 19:11:15 +08:00
2017-05-11 22:54:28 +08:00
// Cancel button
$form.find('.cancel-edit').click(function () {
$prevResult.show();
2017-05-11 22:54:28 +08:00
$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
});
2017-05-11 22:54:28 +08:00
Results.toggleResultEditButtons(false);
$('#result_name').focus();
saveResultAssetCallback();
2017-05-11 22:54:28 +08:00
}).on('ajax:error', function(e, xhr, status, error) {
animateSpinner(null, false);
});
}
function _formAjaxResultAsset($form, $prevResult) {
2017-05-11 22:54:28 +08:00
$form.on('ajax:success', function(e, data) {
if ($prevResult) $prevResult.remove();
2017-05-11 22:54:28 +08:00
$form.after(data.html);
var $newResult = $form.next();
initFormSubmitLinks($newResult);
$(this).remove();
applyEditResultAssetCallback();
Results.toggleResultEditButtons(true);
Results.expandResult($newResult);
Comments.init();
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
ResultAssets.initNewResultAsset();
ResultAssets.applyEditResultAssetCallback();
2018-07-19 23:56:42 +08:00
}(window));