From 7e87970e236bb46bdbb412657b00663c2773fbf1 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 13 Jun 2017 18:37:37 +0200 Subject: [PATCH] editor: only restore state for dirty widgets; restore scroll state manager: list scrolling widgets --- src/browser/js/app/editor/data-workers.js | 34 ++++++++++++++++--- src/browser/js/app/managers.js | 19 +++++++++-- .../js/app/widgets/containers/panel.js | 13 ++++++- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/browser/js/app/editor/data-workers.js b/src/browser/js/app/editor/data-workers.js index 37c29ede..19728bcf 100644 --- a/src/browser/js/app/editor/data-workers.js +++ b/src/browser/js/app/editor/data-workers.js @@ -23,8 +23,22 @@ var updateDom = function(container,data, remote) { // save state var scroll = $('#sidepanel').scrollTop(), - state = actions.stateGet(), - purge = container[0].abstract.hashes || [container[0].abstract.hash] + oldWidgets = container[0].abstract.hashes || [container[0].abstract.hash], + wState = {}, + wScroll = {} + + for (let h of oldWidgets) { + if (widgetManager.widgets[h]) { + wState[widgetManager.widgets[h].getProp('id')] = widgetManager.widgets[h].getValue() + } + } + + // save scroll states + for (let h of widgetManager.scrollingWidgets) { + if (widgetManager.widgets[h] && widgetManager.widgets[h].scroll) { + wScroll[widgetManager.widgets[h].getProp('id')] = widgetManager.widgets[h].scroll() + } + } // widget var newContainer = parsewidgets([data], container[0].abstract.parentNode, container[0].abstract.parent) @@ -37,10 +51,22 @@ var updateDom = function(container,data, remote) { newContainer.trigger('resize') - purgeStores(purge) + purgeStores(oldWidgets) + // restore state - actions.stateSet(state,false) + for (let id in wState) { + for (let w of widgetManager.getWidgetById(id)) { + if (w.setValue) w.setValue(wState[id]) + } + } + + // restore scroll states + for (let id in wScroll) { + for (let w of widgetManager.getWidgetById(id)) { + if (w.scroll) w.scroll(wScroll[id]) + } + } if (!remote) { editObject(newContainer,data,true) diff --git a/src/browser/js/app/managers.js b/src/browser/js/app/managers.js index ecabf589..4ddb024c 100644 --- a/src/browser/js/app/managers.js +++ b/src/browser/js/app/managers.js @@ -8,6 +8,8 @@ var WidgetManager = function(){ this.idRoute = {} this.linkIdRoute = {} + this.scrollingWidgets = [] + this.preArgsSeparator = '||||' $(document).ready(()=>{ @@ -75,7 +77,8 @@ WidgetManager.prototype.addWidget = function(widget) { var hash = widget.hash, address = this.createAddressRef(widget), id = widget.getProp('id'), - linkId = widget.getProp('linkId') + linkId = widget.getProp('linkId'), + scroll = widget.getProp('scroll') this.widgets[hash] = widget @@ -93,6 +96,10 @@ WidgetManager.prototype.addWidget = function(widget) { this.linkIdRoute[linkId].push(hash) } + if (scroll) { + this.scrollingWidgets.push(hash) + } + widget.widget.abstract = widget if (widget.container) widget.container[0].abstract = widget @@ -114,7 +121,8 @@ WidgetManager.prototype.removeWidget = function(hash) { var widget = this.widgets[hash], address = this.createAddressRef(widget), linkId = widget.getProp('linkId'), - id = widget.getProp('id') + id = widget.getProp('id'), + scroll = widget.getProp('scroll') if (this.widgets[hash]) { if (this.widgets[hash].onRemove) this.widgets[hash].onRemove() @@ -127,6 +135,7 @@ WidgetManager.prototype.removeWidget = function(hash) { if (id && this.idRoute[id].indexOf(hash) != -1) this.idRoute[id].splice(this.idRoute[id].indexOf(hash), 1) if (linkId && this.linkIdRoute[linkId].indexOf(hash) != -1) this.linkIdRoute[linkId].splice(this.linkIdRoute[linkId].indexOf(hash), 1) if (address && this.addressRoute[address].indexOf(hash) != -1) this.addressRoute[address].splice(this.addressRoute[address].indexOf(hash), 1) + if (scroll && this.scrollingWidgets.indexOf(hash) != -1) this.scrollingWidgets.splice(this.scrollingWidgets.indexOf(hash), 1) } WidgetManager.prototype.purge = function() { @@ -142,6 +151,12 @@ WidgetManager.prototype.purge = function() { } } + for (let i in this.scrollingWidgets) { + if (!this.widgets[this.scrollingWidgets[i]]) { + this.scrollingWidgets.splice(i, 1) + } + } + } WidgetManager.prototype.reset = function() { diff --git a/src/browser/js/app/widgets/containers/panel.js b/src/browser/js/app/widgets/containers/panel.js index 4c10a9c1..7caa2334 100644 --- a/src/browser/js/app/widgets/containers/panel.js +++ b/src/browser/js/app/widgets/containers/panel.js @@ -57,7 +57,7 @@ module.exports = class Panel extends _widgets_base { if (this.getProp('tabs') && this.getProp('tabs').length) this.widget.addClass('has-tabs') - this.value = 0 + this.value = this.getProp('value') || 0 this.tabs = [] var parsers = require('../../parser'), @@ -189,4 +189,15 @@ module.exports = class Panel extends _widgets_base { } } + scroll(s) { + + if (!s) { + return [this.widget.scrollLeft(), this.widget.scrollTop()] + } else { + this.widget.scrollLeft(s[0]) + this.widget.scrollTop(s[1]) + } + + } + }