2017-05-11 22:54:28 +08:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var ResultTables = (function() {
|
|
|
|
// Init handsontable which can be edited
|
|
|
|
function _initEditableHandsOnTable(root) {
|
|
|
|
root.find('.editable-table').each(function() {
|
|
|
|
var $container = $(this).find('.hot');
|
|
|
|
var data = null;
|
|
|
|
var contents = $(this).find('.hot-contents');
|
2023-02-06 19:55:02 +08:00
|
|
|
var metadata = JSON.parse($(this).find('.hot-metadata').val() || '{}');
|
2017-05-11 22:54:28 +08:00
|
|
|
if (contents.attr('value')) {
|
|
|
|
data = JSON.parse(contents.attr('value')).data;
|
|
|
|
}
|
|
|
|
|
|
|
|
$container.handsontable({
|
|
|
|
data: data,
|
|
|
|
startRows: <%= Constants::HANDSONTABLE_INIT_ROWS_CNT %>,
|
|
|
|
startCols: <%= Constants::HANDSONTABLE_INIT_COLS_CNT %>,
|
|
|
|
minRows: 1,
|
|
|
|
minCols: 1,
|
|
|
|
rowHeaders: true,
|
|
|
|
colHeaders: true,
|
|
|
|
contextMenu: true,
|
2023-02-06 19:55:02 +08:00
|
|
|
cell: metadata.cells || [],
|
2017-05-11 22:54:28 +08:00
|
|
|
formulas: true,
|
2020-11-03 05:19:33 +08:00
|
|
|
preventOverflow: 'horizontal',
|
|
|
|
afterChange: function() {
|
|
|
|
$container.addClass(GLOBAL_CONSTANTS.HAS_UNSAVED_DATA_CLASS_NAME);
|
|
|
|
}
|
2017-05-11 22:54:28 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _onSubmitExtractTable($form) {
|
|
|
|
$form.submit(function(){
|
|
|
|
var hot = $('.hot').handsontable('getInstance');
|
|
|
|
var contents = $('.hot-contents');
|
2023-02-06 19:55:02 +08:00
|
|
|
var metadata = $('.hot-metadata');
|
2017-05-11 22:54:28 +08:00
|
|
|
var data = JSON.stringify({data: hot.getData()});
|
|
|
|
contents.attr('value', data);
|
2023-03-16 19:02:20 +08:00
|
|
|
metadata.attr('value', JSON.stringify({cells: hot.getCellsMeta().map(
|
|
|
|
(x) => {
|
|
|
|
if (x) {
|
|
|
|
return {
|
|
|
|
col: x.col,
|
|
|
|
row: x.row,
|
|
|
|
className: x.className || ''
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}).filter(e => { return e !== null})
|
|
|
|
}))
|
2017-05-11 22:54:28 +08:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply ajax callback to form
|
2022-02-26 05:32:21 +08:00
|
|
|
function _formAjaxResultTable($form, $prevResult) {
|
2017-05-11 22:54:28 +08:00
|
|
|
$form.on('ajax:success', function(e, data) {
|
|
|
|
var $result;
|
2022-02-26 05:32:21 +08:00
|
|
|
if ($prevResult) $prevResult.remove();
|
2017-05-11 22:54:28 +08:00
|
|
|
$form.after(data.html);
|
|
|
|
$result = $(this).next();
|
|
|
|
initFormSubmitLinks($result);
|
|
|
|
$(this).remove();
|
|
|
|
|
|
|
|
applyEditResultTableCallback();
|
|
|
|
Results.initHandsOnTables($result);
|
|
|
|
Results.toggleResultEditButtons(true);
|
|
|
|
Results.expandResult($result);
|
2019-06-04 20:40:21 +08:00
|
|
|
Comments.init();
|
2017-05-12 21:59:58 +08:00
|
|
|
initNewResultTable();
|
2017-05-11 22:54:28 +08:00
|
|
|
});
|
|
|
|
$form.on('ajax:error', function(e, xhr, status, error) {
|
|
|
|
var data = xhr.responseJSON;
|
|
|
|
$form.renderFormErrors('result', data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edit result table button behaviour
|
|
|
|
function applyEditResultTableCallback() {
|
2022-02-26 05:32:21 +08:00
|
|
|
$('.edit-result-table').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();
|
2017-05-11 22:54:28 +08:00
|
|
|
|
2022-02-26 05:32:21 +08:00
|
|
|
_formAjaxResultTable($form, $prevResult);
|
2017-05-11 22:54:28 +08:00
|
|
|
_initEditableHandsOnTable($form);
|
|
|
|
_onSubmitExtractTable($form);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
applyEditResultTableCallback();
|
|
|
|
Results.toggleResultEditButtons(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
Results.toggleResultEditButtons(false);
|
|
|
|
|
|
|
|
$('#result_name').focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// New result text behaviour
|
|
|
|
function initNewResultTable() {
|
2017-05-12 21:59:58 +08:00
|
|
|
$('#new-result-table').on('click', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
event.stopPropagation();
|
|
|
|
var $btn = $(this);
|
|
|
|
$btn.off();
|
|
|
|
animateSpinner(null, true);
|
|
|
|
// get new result form
|
|
|
|
$.ajax({
|
|
|
|
url: $btn.data('href'),
|
|
|
|
method: 'GET',
|
|
|
|
success: function(data) {
|
|
|
|
var $form = $(data.html);
|
|
|
|
animateSpinner(null, false);
|
|
|
|
$('#results').prepend($form);
|
|
|
|
_formAjaxResultTable($form);
|
|
|
|
_initEditableHandsOnTable($form);
|
|
|
|
_onSubmitExtractTable($form);
|
|
|
|
Results.initCancelFormButton($form, initNewResultTable);
|
|
|
|
Results.toggleResultEditButtons(false);
|
|
|
|
$('#result_name').focus();
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
animateSpinner(null, false);
|
|
|
|
initNewResultTable();
|
|
|
|
}
|
2017-05-11 22:54:28 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var publicAPI = Object.freeze({
|
|
|
|
initNewResultTable: initNewResultTable,
|
|
|
|
applyEditResultTableCallback: applyEditResultTableCallback
|
|
|
|
});
|
|
|
|
|
|
|
|
return publicAPI;
|
|
|
|
})();
|
|
|
|
|
2018-07-19 23:56:42 +08:00
|
|
|
ResultTables.initNewResultTable();
|
|
|
|
ResultTables.applyEditResultTableCallback();
|
2017-05-11 22:54:28 +08:00
|
|
|
})();
|