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

232 lines
7.1 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-unused-vars */
/*
global Results ActiveStorage animateSpinner Comments ResultAssets FilePreviewModal
TinyMCE getParam applyCreateWopiFileCallback initFormSubmitLinks textValidator
GLOBAL_CONSTANTS ActiveStoragePreviews
*/
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');
2023-02-06 19:55:02 +08:00
var metadata = $(this).find('.hot-metadata');
2017-05-10 19:53:32 +08:00
$container.handsontable({
width: '100%',
startRows: 5,
startCols: 5,
rowHeaders: true,
colHeaders: true,
fillHandle: false,
formulas: true,
2023-02-06 19:55:02 +08:00
data: JSON.parse(contents.attr('value')).data,
cell: (metadata.val() || {}).cells || [],
2023-02-06 19:55:02 +08:00
readOnly: true
2017-05-10 19:53:32 +08:00
});
let hot = $container.handsontable('getInstance');
setTimeout(() => {
2023-02-06 19:55:02 +08:00
hot.render();
}, 500);
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();
const assetId = form.find('#result_asset_attributes_id').val();
formData.append('result[name]', form.find('#result_name').val());
formData.append('result[asset_attributes][id]', assetId);
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-asset').parent().remove();
$(data.html).prependTo('#results').promise().done(() => {
$.each($('#results').find('.result'), function() {
initFormSubmitLinks($(this));
ResultAssets.applyEditResultAssetCallback();
applyCreateWopiFileCallback();
toggleResultEditButtons(true);
Comments.init();
ResultAssets.initNewResultAsset();
expandResult($(this));
});
ActiveStoragePreviews.reloadPreview(`.asset[data-asset-id=${assetId}] .attachment-preview img`);
});
$('#results-toolbar').show();
},
error: function(XHR) {
animateSpinner(null, false);
$('.edit-result-asset').renderFormErrors('result', XHR.responseJSON.errors);
},
processData: false,
contentType: false
});
}
});
2017-05-10 19:53:32 +08:00
}
function processResult(ev, resultTypeEnum) {
2023-01-19 15:58:05 +08:00
var textWithoutImages;
2017-05-10 19:53:32 +08:00
var $form = $(ev.target.form);
$form.clearFormErrors();
2023-01-19 15:58:05 +08:00
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:
$form
.find(`.${GLOBAL_CONSTANTS.HAS_UNSAVED_DATA_CLASS_NAME}`)
.removeClass(GLOBAL_CONSTANTS.HAS_UNSAVED_DATA_CLASS_NAME);
2017-05-10 19:53:32 +08:00
break;
2017-05-11 22:54:28 +08:00
case ResultTypeEnum.TEXT:
2023-01-19 15:58:05 +08:00
textWithoutImages = TinyMCE.getContent().replaceAll(/src="(data:image\/[^;]+;base64[^"]+)"/i, '');
textValidator(
ev, $form.find('#result_text_attributes_textarea'), 1,
2023-01-19 15:58:05 +08:00
$form.data('rich-text-max-length'), false, textWithoutImages
);
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 initArchive() {
$('#results').on('click', '.form-submit-link', function(event) {
archive(event, this);
});
}
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();
}
initArchive();
}
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));