From d943bfe673427c6f6a6bafa60b228dad2872caa1 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 4 Feb 2018 16:16:29 +0100 Subject: [PATCH] property inheritance: handle prop changing in a way that can be overrided (e.g. in case reCreateWidget is not needed) --- .../js/app/widgets/common/_widgets_base.js | 63 ++++++++++++++----- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/src/browser/js/app/widgets/common/_widgets_base.js b/src/browser/js/app/widgets/common/_widgets_base.js index dbe7247a..19191620 100644 --- a/src/browser/js/app/widgets/common/_widgets_base.js +++ b/src/browser/js/app/widgets/common/_widgets_base.js @@ -74,7 +74,7 @@ module.exports = class _widgets_base extends EventEmitter { widgetManager.on(`widget-created.${this.hash}`, (e)=>{ var {id} = e if (this.linkedProps[id] && id != this.getProp('id')) { - this.checkPropsChanged(this.linkedProps[id]) + this.updateProps(this.linkedProps[id]) } }) @@ -83,14 +83,30 @@ module.exports = class _widgets_base extends EventEmitter { if (Object.keys(this.linkedPropsValue).length) { widgetManager.on(`change.${this.hash}`, (e)=>{ - var {id} = e + var {id, options} = e if (this.linkedPropsValue[id] && id != this.getProp('id')) { - this.checkPropsChanged(this.linkedPropsValue[id], e.options.send) + this.updateProps(this.linkedPropsValue[id], options) } }) } + if (Object.keys(this.linkedProps).length || Object.keys(this.linkedPropsValue).length) { + + widgetManager.on(`prop-changed.${this.hash}`, (e)=>{ + var {id, prop, options} = e + if ( + ((this.linkedProps[id] && this.linkedProps[id].indexOf(prop) != -1) || + (this.linkedPropsValue[id] && this.linkedPropsValue[id].indexOf(prop) != -1)) + && id != this.getProp('id') + ) { + this.updateProps([prop], options) + } + }) + + } + + // cache precision if (this.props.precision != undefined) { this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0)) @@ -270,28 +286,46 @@ module.exports = class _widgets_base extends EventEmitter { return this.cachedProps[propName] } - checkPropsChanged(propNames, send){ - - if (propNames.indexOf('value') != -1 && this.propHasChanged('value')) { - this.cachedProps['value'] = this.resolveProp('value', undefined, false) - this.setValue(this.getProp('value'), {sync:true, send: send}) - } + updateProps(propNames, options){ for (var propName of propNames) { - if (propName != 'value' && this.propHasChanged(propName)) { - return this.reCreateWidget() + + let propValue = this.resolveProp(propName, undefined, false) + + if (JSON.stringify(this.getProp(propName)) != JSON.stringify(propValue)) { + + this.cachedProps[propName] = propValue + + if (this.onPropChanged(propName, options)) return + + widgetManager.trigger(/prop-changed(\..*)?/, [{ + id: this.getProp('id'), + prop: propName, + widget: this + }]) + } } } - propHasChanged(propName) { + onPropChanged(propName, options) { - return JSON.stringify(this.cachedProps[propName]) != JSON.stringify(this.resolveProp(propName, undefined, false)) + switch(propName) { + + case 'value': + this.setValue(this.getProp('value'), {sync:true, send: options && options.send}) + return + + default: + this.reCreateWidget() + return true + + } } - reCreateWidget(valueChanged){ + reCreateWidget(){ updateWidget(this, {remote: true}) @@ -299,6 +333,7 @@ module.exports = class _widgets_base extends EventEmitter { onRemove(){ widgetManager.off(`widget-created.${this.hash}`) + widgetManager.off(`prop-changed.${this.hash}`) widgetManager.off(`change.${this.hash}`) }