widget: reuse hash upon recreation

widget: fix js locals reuse
tree: use hash instead of id to determine expanded containers
This commit is contained in:
bordun 2020-03-23 12:34:50 +01:00
parent 47005370e5
commit e9fb44884c
4 changed files with 19 additions and 14 deletions

View file

@ -100,6 +100,7 @@ function updateWidget(widget, options={}) {
parentNode: widget.parentNode,
reCreateOptions: options.reCreateOptions,
locals: widget.parsersLocalScope,
hash: widget.hash,
children: children,
index: index
})

View file

@ -35,7 +35,9 @@ var Parser = class Parser {
tab,
reCreateOptions,
children,
index
index,
hash,
locals
} = options
var props = data
@ -76,12 +78,13 @@ var Parser = class Parser {
// create widget
var widget = new this.widgets[props.type]({
container:true,
props,
parent,
parentNode,
reCreateOptions,
children
children,
hash,
locals,
})
widgetManager.addWidget(widget)

View file

@ -3,12 +3,13 @@ var UiWidget = require('./ui-widget'),
doubleClick = require('../events/double-click'),
Sortable = require('sortablejs'),
morph = require('nanomorph'),
Panel, Matrix, Keyboard, widgetManager
Root, Panel, Matrix, Keyboard, widgetManager
class UiTree extends UiWidget {
constructor(options) {
Root = require('../widgets/containers/root')
Panel = require('../widgets/containers/panel')
Matrix = require('../widgets/containers/matrix')
Keyboard = require('../widgets/containers/keyboard')
@ -21,16 +22,17 @@ class UiTree extends UiWidget {
this.dragDummy = html`<span></span>`
this.expanded = {root: true}
this.expanded = {}
this.container.addEventListener('fast-click', (event)=>{
var node = event.target
if (node.classList.contains('toggle')) {
var exp = node.parentNode.classList.toggle('expanded')
if (exp) {
this.expanded[node.parentNode.getAttribute('data-id')] = true
this.expanded[node.parentNode.getAttribute('data-widget')] = true
} else {
delete this.expanded[node.parentNode.getAttribute('data-id')]
delete this.expanded[node.parentNode.getAttribute('data-widget')]
}
}
@ -43,9 +45,9 @@ class UiTree extends UiWidget {
if (node.classList.contains('container')) {
var exp = node.classList.toggle('expanded')
if (exp) {
this.expanded[node.getAttribute('data-id')] = true
this.expanded[node.getAttribute('data-widget')] = true
} else {
delete this.expanded[node.getAttribute('data-id')]
delete this.expanded[node.getAttribute('data-widget')]
}
}
@ -128,7 +130,7 @@ class UiTree extends UiWidget {
while (parent !== this.list) {
if (parent.classList.contains('container')) {
parent.classList.add('expanded')
this.expanded[parent.getAttribute('data-id')] = true
this.expanded[parent.getAttribute('data-widget')] = true
}
parent = parent.parentNode
}
@ -162,14 +164,13 @@ class UiTree extends UiWidget {
var selected = selectedWidgets.includes(widget),
id = widget.getProp('id'),
node = html`<li class="${selected ? 'editing' : ''} ${!widget.getProp('visible') ? 'invisible' : ''}"
data-widget="${widget.hash}"
data-id="${id}"">
data-widget="${widget.hash}">
${id}</li>`
if (widget instanceof Panel && !(widget instanceof Matrix || widget instanceof Keyboard)) {
node.insertBefore(html`<span class="toggle no-widget-select"></span>`, node.childNodes[0])
node.classList.add('container')
if (this.expanded[id]) node.classList.add('expanded')
if (this.expanded[widget.hash] || widget instanceof Root) node.classList.add('expanded')
var sublist = node.appendChild(html`<ol style="--depth:${++depth};"></ol>`)
for (let child of widget.children) {
if (child) sublist.appendChild(this.parseWidgets(child, selectedWidgets, depth))

View file

@ -171,7 +171,7 @@ class Widget extends EventEmitter {
this.errors = {}
this.parent = options.root ? widgetManager : options.parent
this.parentNode = options.parentNode
this.hash = nanoid('abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10)
this.hash = options.hash || nanoid('abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10)
this.children = []
this.reCreateOptions = options.reCreateOptions
this.removed = false