errors: make widget hold prop errors and show them in the editor instead of throwing

This commit is contained in:
jean-emmanuel 2018-02-09 20:15:23 +01:00
parent 0248017183
commit d22e67a971
5 changed files with 24 additions and 7 deletions

View file

@ -35,7 +35,7 @@ var editObject = function(widget, options = {}){
$(`<div class="separator"><span>${data.type == 'tab' ? 'Tab' : data.type == 'root' ? 'Root' : 'Widget'}</span></div>`).appendTo(form)
for (var i in params) {
for (let i in params) {
if (i.indexOf('_')==0) {
$(`<div class="separator"><span>${params[i]}</span></div>`).appendTo(form)
@ -114,13 +114,17 @@ var editObject = function(widget, options = {}){
try {
updateWidget(widget)
wrapper.removeClass('error')
} catch (err) {
wrapper.addClass('error')
throw err
}
})
if (widget.errors[i]) {
wrapper.addClass('error')
label.attr('title', widget.errors[i])
}
}
}

View file

@ -27,6 +27,7 @@ class Widget extends EventEmitter {
this.container = options.container || fallbackContainer
this.widget = DOM.create(options.html)
this.props = options.props
this.errors = {}
this.parent = options.root ? widgetManager : options.parent
this.parentNode = options.parentNode
this.hash = shortid.generate()
@ -70,6 +71,11 @@ class Widget extends EventEmitter {
}
}
if (this.getProp('id') == 'root' && !options.root) {
this.cachedProps.id = '_root'
this.errors.id = 'There can only be one root'
}
if (Object.keys(this.linkedProps).length) {
widgetManager.on(`widget-created.${this.hash}`, (e)=>{
@ -110,8 +116,6 @@ class Widget extends EventEmitter {
this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0))
}
if (this.getProp('id') == 'root' && !options.root) throw new Error('There can only be one root')
this.on('widget-created', (e)=>{
if (e.widget == this) return

View file

@ -141,8 +141,11 @@ class Panel extends Widget {
this.applyLayout()
}, {element: this.widget})
delete this.errors.layout
} catch(err) {
throw new Error(`Visual Format Language error in ${this.getProp('id')}.layout: ${err.message} (line ${err.line})`)
this.errors.layout = `Visual Format Language error in ${this.getProp('id')}.layout: ${err.message} (line ${err.line})`
}
}

View file

@ -134,8 +134,11 @@ module.exports = class Formula extends Widget {
this.conditionState = s.data !== undefined ? s.data : s
delete this.errors.condition
} catch(err) {
this.errors.condition = 'Error parsing formula "' + this.conditionString + '" (' + err + ')'
this.conditionState = false
}
@ -154,8 +157,11 @@ module.exports = class Formula extends Widget {
if (e.options.sync && this.conditionState) this.changed(e.options)
if (e.options.send && this.conditionState) this.sendValue()
delete this.errors.formula
} catch(err) {
this.errors['formula'] = 'Error parsing formula "' + this.formulaString + '" (' + err + ')'
this.value = undefined
}

View file

@ -153,7 +153,7 @@
font-size: 12rem!important;
}
.error label{
border-right: 1rem solid rgba(red, .75);
color: red;
}
.select-wrapper {
position: relative;