2017-05-10 19:53:32 +08:00
|
|
|
(function(global) {
|
|
|
|
'use strict';
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
global.ResultAssets = (function() {
|
2023-08-16 20:50:40 +08:00
|
|
|
// 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() {
|
2017-05-12 21:59:58 +08:00
|
|
|
$('#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
|
|
|
|
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);
|
2017-06-09 15:49:57 +08:00
|
|
|
$('#results').prepend($form);
|
2017-05-12 21:59:58 +08:00
|
|
|
_formAjaxResultAsset($form);
|
|
|
|
Results.initCancelFormButton($form, initNewResultAsset);
|
|
|
|
Results.toggleResultEditButtons(false);
|
2022-10-24 19:07:10 +08:00
|
|
|
dragNdropAssetsInit();
|
2023-08-16 20:50:40 +08:00
|
|
|
createResultAssetCallback();
|
2017-05-12 21:59:58 +08:00
|
|
|
},
|
2017-05-15 20:49:37 +08:00
|
|
|
error: function(xhr, status, e) {
|
|
|
|
$(this).renderFormErrors('result', xhr.responseJSON, true, e);
|
2017-05-12 21:59:58 +08:00
|
|
|
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
|
|
|
|
2023-08-16 20:50:40 +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() {
|
2022-02-26 05:32:21 +08:00
|
|
|
$('.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);
|
2022-02-26 05:32:21 +08:00
|
|
|
$prevResult.hide();
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2022-02-26 05:32:21 +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 () {
|
2022-02-26 05:32:21 +08:00
|
|
|
$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();
|
2023-08-16 20:50:40 +08:00
|
|
|
saveResultAssetCallback();
|
2017-05-11 22:54:28 +08:00
|
|
|
}).on('ajax:error', function(e, xhr, status, error) {
|
|
|
|
animateSpinner(null, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-26 05:32:21 +08:00
|
|
|
function _formAjaxResultAsset($form, $prevResult) {
|
2017-05-11 22:54:28 +08:00
|
|
|
$form.on('ajax:success', function(e, data) {
|
2022-02-26 05:32:21 +08:00
|
|
|
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);
|
2019-06-04 20:40:21 +08:00
|
|
|
Comments.init();
|
2017-05-12 21:59:58 +08:00
|
|
|
initNewResultAsset();
|
2017-05-17 21:14:35 +08:00
|
|
|
}).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
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
ResultAssets.initNewResultAsset();
|
|
|
|
ResultAssets.applyEditResultAssetCallback();
|
2018-07-19 23:56:42 +08:00
|
|
|
}(window));
|