* Add a watcher so that any external change to the AutoNumeric-managed element would be detected. * As soon as such change is detected, AutoNumeric then tries to `set()` the value so that it gets formatted and stored in the history. * //XXX For now, this only works when watching the `value
()
| 1548 | * @private |
| 1549 | */ |
| 1550 | _addWatcher() { |
| 1551 | // `getterSetter` can be undefined when a non-input element is used |
| 1552 | if (!AutoNumericHelper.isUndefined(this.getterSetter)) { |
| 1553 | const { set: setter, get: getter } = this.getterSetter; |
| 1554 | Object.defineProperty(this.domElement, this.attributeToWatch, { |
| 1555 | configurable: true, // This is needed in some rare cases |
| 1556 | get : () => getter.call(this.domElement), |
| 1557 | set : val => { |
| 1558 | setter.call(this.domElement, val); |
| 1559 | // Only `set()` the value if the modification comes from an external source |
| 1560 | if (this.settings.watchExternalChanges && !this.internalModification) { |
| 1561 | this.set(val); |
| 1562 | } |
| 1563 | }, |
| 1564 | }); |
| 1565 | } |
| 1566 | |
| 1567 | //FIXME The code above fails for the `textContent` attribute since `this.getterSetter` is undefined when using `getOwnPropertyDescriptor()` |
| 1568 | /* //XXX The code below *almost* work for the textContent, but breaks some unit tests |
| 1569 | this.valueWatched = this.domElement[this.attributeToWatch]; |
| 1570 | Object.defineProperty(this.domElement, this.attributeToWatch, { |
| 1571 | configurable: true, // This is needed in some rare cases |
| 1572 | get : () => this.valueWatched, |
| 1573 | set : val => { |
| 1574 | this.valueWatched = val; |
| 1575 | // Only `set()` the value if the modification comes from an external source |
| 1576 | if (this.settings.watchExternalChanges && !this.internalModification) { |
| 1577 | this.set(val); |
| 1578 | } |
| 1579 | }, |
| 1580 | }); |
| 1581 | */ |
| 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * Remove the watcher on the AutoNumeric-managed element |
no test coverage detected