From 817c1f5f1d9cdd97862ca14e9caca292908a011f Mon Sep 17 00:00:00 2001 From: Bilel Kihal <61744974+Bilelkihal@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:56:24 +0200 Subject: [PATCH] Fix: Summary page scroll up auto multiple times (#781) * fix summary page scroll up auto multiple times * clean center tree view code and use it for: 'classes', 'properties', 'schemes', 'collections', 'instances' * revert adding useVisibility, and extract tree_view_pages to a constant --- .../controllers/simple_tree_controller.js | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/app/javascript/controllers/simple_tree_controller.js b/app/javascript/controllers/simple_tree_controller.js index 858bfde2c8..4cece829a7 100644 --- a/app/javascript/controllers/simple_tree_controller.js +++ b/app/javascript/controllers/simple_tree_controller.js @@ -1,4 +1,7 @@ import { Controller } from '@hotwired/stimulus' + +const TREE_VIEW_PAGES = ['classes', 'properties', 'schemes', 'collections', 'instances'] + // Connects to data-controller="simple-tree" export default class extends Controller { @@ -7,17 +10,7 @@ export default class extends Controller { } connect () { - setTimeout(() => { - let activeElem = this.element.querySelector('.tree-link.active'); - if (activeElem) { - activeElem.scrollIntoView({ block: 'center' }); - window.scrollTo({top: 0,}); - if (this.autoClickValue) { - activeElem.click(); - } - } - this.#onClickTooManyChildrenInit(); - }, 0); + this.#centerTreeView() } select (event) { @@ -33,6 +26,29 @@ export default class extends Controller { event.target.nextElementSibling.nextElementSibling.classList.toggle('hidden') } + #centerTreeView() { + setTimeout(() => { + const location = window.location.href; + + const isTreeViewPage = TREE_VIEW_PAGES.some(param => location.includes(`p=${param}`)); + + if (isTreeViewPage) { + const activeElem = this.element.querySelector('.tree-link.active'); + + if (activeElem) { + activeElem.scrollIntoView({ block: 'center' }); + window.scrollTo({ top: 0 }); + + if (this.autoClickValue) { + activeElem.click(); + } + } + + this.#onClickTooManyChildrenInit(); + } + }, 0); + } + #onClickTooManyChildrenInit () { jQuery('.too_many_children_override').live('click', (event) => { event.preventDefault()