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

225 lines
6.7 KiB
JavaScript
Raw Normal View History

/*
global Results ActiveStorage animateSpinner Comments ResultAssets FilePreviewModal
TinyMCE getParam applyCreateWopiFileCallback initFormSubmitLinks textValidator
GLOBAL_CONSTANTS
*/
2017-05-10 19:53:32 +08:00
(function(global) {
'use strict';
global.Results = (function() {
var ResultTypeEnum = Object.freeze({
FILE: 0,
TABLE: 1,
TEXT: 2
});
2017-05-10 19:53:32 +08:00
function initHandsOnTables(root) {
root.find('div.hot-table').each(function() {
2017-05-10 19:53:32 +08:00
var $container = $(this).find('.step-result-hot-table');
var contents = $(this).find('.hot-contents');
$container.handsontable({
width: '100%',
startRows: 5,
startCols: 5,
rowHeaders: true,
colHeaders: true,
fillHandle: false,
formulas: true,
cells: function(row, col) {
2017-05-10 19:53:32 +08:00
var cellProperties = {};
if (col >= 0) {
2017-05-10 19:53:32 +08:00
cellProperties.readOnly = true;
} else {
2017-05-10 19:53:32 +08:00
cellProperties.readOnly = false;
}
2017-05-10 19:53:32 +08:00
return cellProperties;
}
});
let hot = $container.handsontable('getInstance');
let data = JSON.parse(contents.attr('value'));
2019-09-18 17:47:33 +08:00
if (Array.isArray(data.data)) hot.loadData(data.data);
setTimeout(() => {
hot.render()
}, 0)
2017-05-10 19:53:32 +08:00
});
}
// Toggle editing buttons
function toggleResultEditButtons(show) {
if (show) {
2017-05-10 19:53:32 +08:00
$('#results-toolbar').show();
$('.edit-result-button').show();
} else {
$('.edit-result-button').hide();
$('#results-toolbar').hide();
}
}
function renderTable(table) {
$(table).handsontable('render');
// Yet another dirty hack to solve HandsOnTable problems
if (parseInt($(table).css('height'), 10) < parseInt($(table).css('max-height'), 10) - 30) {
$(table).find('.ht_master .wtHolder').css({ height: '100%', width: '100%' });
}
}
2017-05-10 19:53:32 +08:00
// Expand all results
function expandAllResults() {
2017-05-10 19:53:32 +08:00
$('.result .panel-collapse').collapse('show');
$(document).find('div.step-result-hot-table').each(function() {
renderTable(this);
2017-05-10 19:53:32 +08:00
});
}
2016-02-12 23:52:43 +08:00
2017-05-10 19:53:32 +08:00
function expandResult(result) {
$('.panel-collapse', result).collapse('show');
renderTable($(result).find('div.step-result-hot-table'));
2017-05-10 19:53:32 +08:00
animateSpinner(null, false);
}
// create custom ajax request in order to fix issuses with remote: true from
function handleResultFileSubmit(form, ev) {
2020-02-07 22:54:57 +08:00
if (!(form.find('#result_asset_attributes_file')[0].files.length > 0)) {
// Assuming that only result name is getting updated
return;
}
ev.preventDefault();
ev.stopPropagation();
const url = form.find('#result_asset_attributes_file').data('directUploadUrl');
const file = form.find('#result_asset_attributes_file')[0].files[0];
const upload = new ActiveStorage.DirectUpload(file, url);
animateSpinner();
upload.create((error, blob) => {
if (error) {
// Handle the error
} else {
let formData = new FormData();
formData.append('result[name]', form.find('#result_name').val());
formData.append('result[asset_attributes][id]', form.find('#result_asset_attributes_id').val());
formData.append('result[asset_attributes][signed_blob_id]', blob.signed_id);
$.ajax({
type: 'PUT',
url: form.attr('action'),
data: formData,
success: function(data) {
animateSpinner(null, false);
$('.edit_result').parent().remove();
$(data.html).prependTo('#results').promise().done(() => {
$.each($('#results').find('.result'), function() {
initFormSubmitLinks($(this));
ResultAssets.applyEditResultAssetCallback();
applyCreateWopiFileCallback();
toggleResultEditButtons(true);
FilePreviewModal.init();
Comments.init();
ResultAssets.initNewResultAsset();
expandResult($(this));
});
});
$('#results-toolbar').show();
},
error: function(XHR) {
animateSpinner(null, false);
$('.edit_result').renderFormErrors('result', XHR.responseJSON.errors);
},
processData: false,
contentType: false
});
}
});
2017-05-10 19:53:32 +08:00
}
function processResult(ev, resultTypeEnum) {
2017-05-10 19:53:32 +08:00
var $form = $(ev.target.form);
$form.clearFormErrors();
textValidator(ev, $form.find('#result_name'), 0, GLOBAL_CONSTANTS.NAME_MAX_LENGTH);
switch (resultTypeEnum) {
case ResultTypeEnum.FILE:
handleResultFileSubmit($form, ev);
break;
2017-05-11 22:54:28 +08:00
case ResultTypeEnum.TABLE:
2017-05-10 19:53:32 +08:00
break;
2017-05-11 22:54:28 +08:00
case ResultTypeEnum.TEXT:
textValidator(
ev, $form.find('#result_text_attributes_textarea'), 1,
$form.data('rich-text-max-length'), false, TinyMCE.getContent()
);
2017-05-10 19:53:32 +08:00
break;
default:
// do nothing
2017-05-10 19:53:32 +08:00
}
}
// init cancel button
function initCancelFormButton(form, callback) {
$(form).find('.cancel-new').click(function(event) {
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
$(form).remove();
toggleResultEditButtons(true);
2019-04-26 23:37:01 +08:00
TinyMCE.destroyAll();
callback();
});
}
function archive(e, element) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
let el = $(element);
if (confirm(el.data('confirm-text'))) {
animateSpinner();
$('#' + el.data('form-id')).submit();
}
}
function init() {
initHandsOnTables($(document));
expandAllResults();
applyCreateWopiFileCallback();
$(function() {
$('#results-collapse-btn').click(function() {
$('.result .panel-collapse').collapse('hide');
});
$('#results-expand-btn').click(expandAllResults);
});
// This checks if the ctarget param exist in the rendered url and opens the
// comment tab
if (getParam('ctarget')) {
let target = '#' + getParam('ctarget');
$(target).find('a.comment-tab-link').click();
}
}
let publicAPI = Object.freeze({
2017-05-10 19:53:32 +08:00
init: init,
initHandsOnTables: initHandsOnTables,
toggleResultEditButtons: toggleResultEditButtons,
expandResult: expandResult,
processResult: processResult,
ResultTypeEnum: ResultTypeEnum,
initCancelFormButton: initCancelFormButton,
archive: archive
2017-05-10 19:53:32 +08:00
});
2017-05-10 19:53:32 +08:00
return publicAPI;
}());
2016-12-08 18:26:13 +08:00
2018-07-19 23:56:42 +08:00
Results.init();
}(window));