2017-05-10 19:53:32 +08:00
|
|
|
(function(global) {
|
|
|
|
'use strict';
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-06-07 19:29:39 +08:00
|
|
|
global.ResutlAssets = (function() {
|
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);
|
2017-06-07 19:29:39 +08:00
|
|
|
dragNdropAssetsInit('results');
|
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
|
|
|
|
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();
|
2017-05-12 21:59:58 +08:00
|
|
|
initNewResultAsset();
|
2017-05-17 21:14:35 +08:00
|
|
|
}).on('ajax:error', function(e, xhr) {
|
2017-05-15 20:49:37 +08:00
|
|
|
$form.renderFormErrors('result', xhr.responseJSON, true, e);
|
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
|
|
|
|
2017-05-11 22:54:28 +08:00
|
|
|
$(document).ready(function() {
|
|
|
|
ResutlAssets.initNewResultAsset();
|
|
|
|
ResutlAssets.applyEditResultAssetCallback();
|
|
|
|
global.initPreviewModal();
|
|
|
|
});
|
2017-05-10 19:53:32 +08:00
|
|
|
})(window);
|