A fix that involves changind handsontable library

This commit is contained in:
Zanz2 2018-07-03 14:52:12 +02:00
parent 10554fecb4
commit 1a263e995b

View file

@ -1,6 +1,9 @@
(function(Handsontable) {
'use strict';
// [MODIFICATION]
var formulasResults = {};
// _!_MODIFICATION
function HandsontableFormula() {
var isFormula = function(value) {
@ -13,6 +16,22 @@
return false;
};
// [MODIFICATION]
var beforeRender = function (isForced) {
formulasResults = {};
var data = this.getData();
for (var i = 0; i < data.length; ++i) {
for (var j = 0; j < data[i].length; ++j) {
var value = data[i][j];
if (value && value[0] === '=') {
var cellId = this.plugin.utils.translateCellCoords({row: i, col: j});
this.plugin.matrix.removeItem(cellId);
}
}
}
};
// _!_MODIFICATION
var formulaRenderer = function(instance, TD, row, col, prop, value, cellProperties) {
if (instance.formulasEnabled && isFormula(value)) {
// translate coordinates into cellId
@ -49,6 +68,10 @@
// check if typed formula or cell value should be recalculated
if ((value && value[0] === '=') || needUpdate) {
// [MODIFICATION]
if (formulasResults[cellId] === undefined) {
// _!_MODIFICATION
formula = value.substr(1).toUpperCase();
if (!error || formula !== prevFormula) {
@ -91,8 +114,17 @@
// update cell value in hot
value = error || result;
}
}
// [MODIFICATION]
} else {
var newValue = formulasResults[cellId];
error = newValue.error;
result = newValue.result;
value = error || result;
}
// _!_MODIFICATION
}
if (error) {
// clear cell value
if (!value) {
@ -379,7 +411,34 @@
if (instance.formulasEnabled) {
var custom = {
cellValue: instance.getDataAtCell
//
// [MODIFICATION]
// Previously: "cellValue: instance.getDataAtCell"
//
cellValue: function(row, col){
var value = instance.getDataAtCell(row, col);
if (value && value[0] === '=') {
var formula = value.substr(1).toUpperCase();
var cellId = instance.plugin.utils.translateCellCoords({row: row, col: col});
var item = instance.plugin.matrix.getItem(cellId);
if (!item) {
item = instance.plugin.matrix.addItem({id: cellId, formula: formula});
} else {
item = instance.plugin.matrix.updateItem({id: cellId, formula: formula});
}
// parse formula
var newValue = instance.plugin.parse(formula, {row: row, col: col, id: cellId});
// cache result
formulasResults[cellId] = newValue;
// update item value and error
instance.plugin.matrix.updateItem(item, {formula: formula, value: newValue.result, error: newValue.error});
value = newValue.error || newValue.result;
}
return value;
}
// _!_MODIFICATION
};
instance.plugin = new ruleJS();
@ -391,6 +450,10 @@
Handsontable.TextCell.renderer = formulaRenderer;
Handsontable.NumericCell.renderer = formulaRenderer;
// [MODIFICATION]
// This hook is new
instance.addHook('beforeRender', beforeRender);
// _!_MODIFICATION
instance.addHook('afterChange', afterChange);
instance.addHook('beforeAutofillInsidePopulate', beforeAutofillInsidePopulate);
@ -398,6 +461,10 @@
instance.addHook('afterCreateCol', afterCreateCol);
} else {
// [MODIFICATION]
// This hook is new
instance.removeHook('beforeRender', beforeRender);
// _!_MODIFICATION
instance.removeHook('afterChange', afterChange);
instance.removeHook('beforeAutofillInsidePopulate', beforeAutofillInsidePopulate);