From 4575f4dcbd5c76c3c34d16ee332be5fe93b72298 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 18 Oct 2018 17:01:02 +0200 Subject: [PATCH] editor: at last a fix that works to avoid data shifting on editor.undo --- .../js/app/widgets/common/container.js | 27 +++++++++++++++++++ .../js/app/widgets/containers/panel.js | 9 ++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/browser/js/app/widgets/common/container.js b/src/browser/js/app/widgets/common/container.js index 1980fc97..59f39fb3 100644 --- a/src/browser/js/app/widgets/common/container.js +++ b/src/browser/js/app/widgets/common/container.js @@ -36,6 +36,33 @@ class Container extends Widget { } + alignChildrenProps() { + + // this is needed after editor.undo to fix data shifting: + // say we have children [a, b, c] bound with session data [a, b, c] + // we delete one widget [ b, c] the data changes too: [ b, c] + // undo first only mutates the data: [a, b, c] + // so children have their data mutated too, child 'b' gets data 'a' and so on + // each child has to get the data corresponding to its index again. + // ...right ? :) + var tabs = this.getProp('tabs'), + widgets= this.getProp('widgets'), + type = tabs && tabs.length ? 'tabs' : widgets && widgets.length ? 'widgets' : null + + if (!type) return + + for (let i in this.children) { + if (this.children[i]) { + this.children[i].props = this.getProp(type)[i] + this.children[i].cachedProps[type] = this.children[i].props[type] + if (this.children[i].alignChildrenProps) { + this.children[i].alignChildrenProps() + } + } + } + + } + onRemove() { super.onRemove() diff --git a/src/browser/js/app/widgets/containers/panel.js b/src/browser/js/app/widgets/containers/panel.js index a746ea4d..ce617193 100644 --- a/src/browser/js/app/widgets/containers/panel.js +++ b/src/browser/js/app/widgets/containers/panel.js @@ -56,7 +56,6 @@ class Panel extends Container { this.wrapper.appendChild(this.children[i].container) this.children[i].parent = this this.children[i].parentNode = this.wrapper - this.children[i].props = this.getProp('tabs')[i] } else { parser.parse({ data: this.getProp('tabs')[i], @@ -68,6 +67,8 @@ class Panel extends Container { } } + if (options.children) this.alignChildrenProps() + this.createNavigation() this.navigation.addEventListener('fast-click', (e)=>{ @@ -85,13 +86,12 @@ class Panel extends Container { } else if (this.getProp('widgets') && this.getProp('widgets').length) { - this.children = options.children || new Array(this.getProp('widgets').length) + this.children = options.children || new Array(this.getProp('widgets').length) for (let i = 0; i < this.children.length; i++) { if (this.children[i]) { this.widget.appendChild(this.children[i].container) this.children[i].parent = this this.children[i].parentNode = this.widget - this.children[i].props = this.getProp('widgets')[i] } else { parser.parse({ data: this.getProp('widgets')[i], @@ -102,6 +102,9 @@ class Panel extends Container { } } + if (options.children) this.alignChildrenProps() + + } if (this.getProp('tabs') && !this.getProp('tabs').length) {