From 42dc0d65b836ee7f83f8160fed7e3e09bdfaabf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Konvi=C4=8Dka?= Date: Thu, 25 Jun 2026 21:40:19 +0200 Subject: [PATCH] feat: Keep scroll position after form submit --- assets/js/history.ajax.js | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/assets/js/history.ajax.js b/assets/js/history.ajax.js index f16ffb7..03eab95 100644 --- a/assets/js/history.ajax.js +++ b/assets/js/history.ajax.js @@ -168,3 +168,44 @@ }); })(jQuery); + +(function ($, undefined) { + + var getScrollContainer = function () { + var $container = $('[data-app-container]'); + return $container.length ? $container.get(0) : (document.scrollingElement || document.documentElement); + }; + + var pendingScroll = null; + var inFlight = 0; + + $.nette.ext('scrollRestore', { + before: function (xhr, settings) { + if (settings.nette && settings.nette.form) { + var container = getScrollContainer(); + pendingScroll = { + top: container.scrollTop, + left: container.scrollLeft + }; + } + }, + start: function () { + inFlight++; + }, + complete: function () { + inFlight = Math.max(0, inFlight - 1); + if (inFlight === 0 && pendingScroll !== null) { + var scroll = pendingScroll; + pendingScroll = null; + var restore = function () { + var container = getScrollContainer(); + container.scrollTop = scroll.top; + container.scrollLeft = scroll.left; + }; + restore(); + window.requestAnimationFrame(restore); + } + } + }); + +})(jQuery);