formula: add 'condition' option

This commit is contained in:
jean-emmanuel 2017-05-19 13:27:56 +02:00
parent a4c057a7ea
commit dd8d2aa510
3 changed files with 55 additions and 5 deletions

View file

@ -15,6 +15,7 @@ module.exports = class Formula extends _widgets_base {
_math: 'math',
formula: '',
condition: '',
_style:'style',
@ -47,14 +48,35 @@ module.exports = class Formula extends _widgets_base {
super(...arguments, widgetHtml)
widgetData.formula = String(widgetData.formula)
widgetData.condition = String(widgetData.condition)
this.split = typeof widgetData.split == 'object' && widgetData.split.length ? widgetData.split : false
this.input = this.widget.find('.input').fakeInput({align:'center', disabled:true})
this.linkedWidgets = widgetData.formula.match(/\$\{([^\}]*)\}/g) == null ? [] : widgetData.formula.match(/\$\{([^\}]*)\}/g).map((a)=>{return a.substr(2, a.length - 3)})
this.linkedWidgets = []
if (widgetData.formula.length) {
if (widgetData.formula.match(/\$\{([^\}]*)\}/g) != null) this.linkedWidgets = this.linkedWidgets.concat(widgetData.formula.match(/\$\{([^\}]*)\}/g).map((a)=>{return a.substr(2, a.length - 3)}))
}
if (widgetData.condition.length) {
if (widgetData.condition.match(/\$\{([^\}]*)\}/g) != null) this.linkedWidgets = this.linkedWidgets.concat(widgetData.condition.match(/\$\{([^\}]*)\}/g).map((a)=>{return a.substr(2, a.length - 3)}))
}
this.formula = math.compile(widgetData.formula.replace(/\$\{([^\}]*)\}/g, '_$1'))
this.condition = math.compile(widgetData.condition.replace(/\$\{([^\}]*)\}/g, '_$1'))
this.conditionState = true
$('body').on(`sync.${this.hash}`,this.syncHandle.bind(this))
this.input.val('...')
@ -85,6 +107,22 @@ module.exports = class Formula extends _widgets_base {
}
}
if (this.widgetData.condition.length) {
try {
var s = this.condition.eval(variables).valueOf()
this.conditionState = s.data !== undefined ? s.data : s
} catch(err) {
throw 'Error parsing formula "' + this.condition + '" (' + err + ')'
}
}
try {
var v = this.formula.eval(variables).valueOf()
@ -93,12 +131,12 @@ module.exports = class Formula extends _widgets_base {
this.showValue()
if (e.options.sync) this.widget.trigger({type: 'sync',id: this.widgetData.id,widget: this.widget, linkId: this.widgetData.linkId, options: e.options})
if (e.options.send) this.sendValue()
if (e.options.sync && this.conditionState) this.widget.trigger({type: 'sync',id: this.widgetData.id,widget: this.widget, linkId: this.widgetData.linkId, options: e.options})
if (e.options.send && this.conditionState) this.sendValue()
} catch(err) {
throw 'Error parsing formula "' + formula + '" (' + err + ')'
throw 'Error parsing formula "' + this.formula + '" (' + err + ')'
}
@ -124,7 +162,10 @@ module.exports = class Formula extends _widgets_base {
showValue() {
this.input.val(JSON.stringify(Formula.deepCopyWithPrecision(this.value, this.widgetData.precision)))
this.input.val(
(this.widgetData.condition.length && !this.conditionState ? '* ' : '') +
JSON.stringify(Formula.deepCopyWithPrecision(this.value, this.widgetData.precision))
)
}