From 70462e4c24bfa631c02a5e479a87ae9d49046c7c Mon Sep 17 00:00:00 2001 From: bordun Date: Mon, 23 Mar 2020 12:34:50 +0100 Subject: [PATCH] widget: reuse hash upon recreation widget: fix js locals reuse tree: use hash instead of id to determine expanded containers --- src/client/editor/data-workers.js | 1 + src/client/parser.js | 9 ++++++--- src/client/ui/ui-tree.js | 21 +++++++++++---------- src/client/widgets/common/widget.js | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/client/editor/data-workers.js b/src/client/editor/data-workers.js index 7c5dd4de..318dde83 100644 --- a/src/client/editor/data-workers.js +++ b/src/client/editor/data-workers.js @@ -100,6 +100,7 @@ function updateWidget(widget, options={}) { parentNode: widget.parentNode, reCreateOptions: options.reCreateOptions, locals: widget.parsersLocalScope, + hash: widget.hash, children: children, index: index }) diff --git a/src/client/parser.js b/src/client/parser.js index 0a7fbfd3..e4a63533 100644 --- a/src/client/parser.js +++ b/src/client/parser.js @@ -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) diff --git a/src/client/ui/ui-tree.js b/src/client/ui/ui-tree.js index 8ed783db..f50e57bd 100644 --- a/src/client/ui/ui-tree.js +++ b/src/client/ui/ui-tree.js @@ -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`` - 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`
  • + data-widget="${widget.hash}"> ${id}
  • ` if (widget instanceof Panel && !(widget instanceof Matrix || widget instanceof Keyboard)) { node.insertBefore(html``, 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`
      `) for (let child of widget.children) { if (child) sublist.appendChild(this.parseWidgets(child, selectedWidgets, depth)) diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index 62094a8c..f998cc63 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -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