* Exit the formula mode * Tries to parse and evaluate the math expression, then `set()` the result if it's correct, otherwise reformat with the previous `rawValue` * @private
()
| 6462 | * @private |
| 6463 | */ |
| 6464 | _exitFormulaMode() { |
| 6465 | // Parse the formula |
| 6466 | let formula = AutoNumericHelper.getElementValue(this.domElement); |
| 6467 | formula = formula.replace(/^\s*=/, ''); // Remove all the leading whitespaces and the equal sign from the formula |
| 6468 | let result; |
| 6469 | try { |
| 6470 | const ast = new Parser(formula, this.settings.decimalCharacter); |
| 6471 | result = (new Evaluator()).evaluate(ast); |
| 6472 | } catch (e) { |
| 6473 | // Error when parsing the math expression |
| 6474 | this._triggerEvent(AutoNumeric.events.invalidFormula, this.domElement, { |
| 6475 | formula, |
| 6476 | aNElement: this, |
| 6477 | }); |
| 6478 | this.reformat(); |
| 6479 | this.formulaMode = false; |
| 6480 | |
| 6481 | return; |
| 6482 | } |
| 6483 | |
| 6484 | // The math expression is correctly parsed |
| 6485 | this._triggerEvent(AutoNumeric.events.validFormula, this.domElement, { |
| 6486 | formula, |
| 6487 | result, |
| 6488 | aNElement: this, |
| 6489 | }); |
| 6490 | this.set(result); // Note: we can have a valid formula, but an invalid value (i.e. out of the min/max range) |
| 6491 | this.formulaMode = false; |
| 6492 | } |
| 6493 | |
| 6494 | /** |
| 6495 | * Returns `true` if the non-printable key is accepted in formula mode |
no test coverage detected