From 6f4d6c4d954386ec598ce1fb523320870ec8e1e1 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Thu, 25 Mar 2021 19:40:21 -0400
Subject: fixed golden layout tab adding behaviors to prevent scrolled docs
 from being reset to 0.

---
 .../views/collections/CollectionDockingView.tsx    | 28 +++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

(limited to 'src/client/views/collections/CollectionDockingView.tsx')

diff --git a/src/client/views/collections/CollectionDockingView.tsx b/src/client/views/collections/CollectionDockingView.tsx
index 57f4555a1..ec34f1260 100644
--- a/src/client/views/collections/CollectionDockingView.tsx
+++ b/src/client/views/collections/CollectionDockingView.tsx
@@ -162,6 +162,27 @@ export class CollectionDockingView extends CollectionSubView(doc => doc) {
         if (!instance) return false;
         const docContentConfig = CollectionDockingView.makeDocumentConfig(document, panelName);
 
+        //if a tab is removed from the DOM and reinserted somewhere else all nested scrollTops seems to get reset to 0. (golden layout does this when a row structure needs to be converted to col or vice-versa)
+        const saveScrollTops = (element: any) => {
+            const children = Array.from(element.children()) as any[];
+            while (children.length) {
+                const child = children.pop();
+                if (child.children) children.push(...(Array.from(child.children) as any[]));
+                if (child?.scrollTop) child.preScrollTop = child.scrollTop;
+            }
+        }
+        const restoreScrollTops = (element: any) => {
+            const children = Array.from(element.children()) as any[];
+            while (children.length) {
+                const child = children.pop();
+                if (child.children) children.push(...(Array.from(child.children) as any[]));
+                if (child?.preScrollTop) {
+                    child.scrollTop = child.preScrollTop;
+                    child.preScrollTop = undefined;
+                }
+            }
+        }
+
         if (!pullSide && stack) {
             stack.addChild(docContentConfig, undefined);
             stack.setActiveContentItem(stack.contentItems[stack.contentItems.length - 1]);
@@ -188,6 +209,8 @@ export class CollectionDockingView extends CollectionSubView(doc => doc) {
                         // if not going in a row layout, must add already existing content into column
                         const rowlayout = instance._goldenLayout.root.contentItems[0];
                         const newColumn = rowlayout.layoutManager.createContentItem({ type: "column" }, instance._goldenLayout);
+
+                        saveScrollTops(rowlayout.element);
                         rowlayout.parent.replaceChild(rowlayout, newColumn);
                         if (pullSide === "top") {
                             newColumn.addChild(rowlayout, undefined, true);
@@ -196,6 +219,7 @@ export class CollectionDockingView extends CollectionSubView(doc => doc) {
                             newColumn.addChild(newContentItem, undefined, true);
                             newColumn.addChild(rowlayout, 0, true);
                         }
+                        restoreScrollTops(rowlayout.element);
 
                         rowlayout.config.height = 50;
                         newContentItem.config.height = 50;
@@ -210,8 +234,9 @@ export class CollectionDockingView extends CollectionSubView(doc => doc) {
                         // if not going in a row layout, must add already existing content into column
                         const collayout = instance._goldenLayout.root.contentItems[0];
                         const newRow = collayout.layoutManager.createContentItem({ type: "row" }, instance._goldenLayout);
-                        collayout.parent.replaceChild(collayout, newRow);
 
+                        saveScrollTops(collayout.element);
+                        collayout.parent.replaceChild(collayout, newRow);
                         if (pullSide === "left") {
                             newRow.addChild(collayout, undefined, true);
                             newRow.addChild(newContentItem, 0, true);
@@ -219,6 +244,7 @@ export class CollectionDockingView extends CollectionSubView(doc => doc) {
                             newRow.addChild(newContentItem, undefined, true);
                             newRow.addChild(collayout, 0, true);
                         }
+                        restoreScrollTops(collayout.element);
 
                         collayout.config.width = 50;
                         newContentItem.config.width = 50;
-- 
cgit v1.2.3-70-g09d2

