From a5e6ae6afc6d8b9a8d91d70941a991c7d25e97d1 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 15 Feb 2017 22:49:54 +0100 Subject: [PATCH 1/2] Affix sidebar when window is resized Fixes #866. --- .../core/js/forum/src/utils/affixSidebar.js | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/framework/core/js/forum/src/utils/affixSidebar.js b/framework/core/js/forum/src/utils/affixSidebar.js index 0acacbb5f..6d062d7b0 100644 --- a/framework/core/js/forum/src/utils/affixSidebar.js +++ b/framework/core/js/forum/src/utils/affixSidebar.js @@ -4,22 +4,38 @@ * * @param {DOMElement} element * @param {Boolean} isInitialized + * @param {Object} context */ -export default function affixSidebar(element, isInitialized) { +export default function affixSidebar(element, isInitialized, context) { if (isInitialized) return; - const $sidebar = $(element); - const $header = $('#header'); - const $footer = $('#footer'); + const onresize = () => { + const $sidebar = $(element); + const $header = $('#header'); + const $footer = $('#footer'); + const $affixElement = $sidebar.find('> ul'); - // Don't affix the sidebar if it is taller than the viewport (otherwise - // there would be no way to scroll through its content). - if ($sidebar.outerHeight(true) > $(window).height() - $header.outerHeight(true)) return; + $(window).off('.affix'); + $affixElement + .removeClass('affix affix-top affix-bottom') + .removeData('bs.affix'); - $sidebar.find('> ul').affix({ - offset: { - top: () => $sidebar.offset().top - $header.outerHeight(true) - parseInt($sidebar.css('margin-top'), 10), - bottom: () => this.bottom = $footer.outerHeight(true) - } - }); + // Don't affix the sidebar if it is taller than the viewport (otherwise + // there would be no way to scroll through its content). + if ($sidebar.outerHeight(true) > $(window).height() - $header.outerHeight(true)) return; + + $affixElement.affix({ + offset: { + top: () => $sidebar.offset().top - $header.outerHeight(true) - parseInt($sidebar.css('margin-top'), 10), + bottom: () => this.bottom = $footer.outerHeight(true) + } + }); + }; + + // Register the affix plugin to execute on every window resize (and trigger) + $(window).on('resize', onresize).resize(); + + context.onunload = () => { + $(window).off('resize', onresize); + } } From 71a510abe0bc1a1748352991412afa592d635f19 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 17 Feb 2017 00:19:33 +0100 Subject: [PATCH 2/2] Compile dist JS --- framework/core/js/forum/dist/app.js | 45 +++++++++++++++++++---------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/framework/core/js/forum/dist/app.js b/framework/core/js/forum/dist/app.js index a35c8a0b5..78bccf2ed 100644 --- a/framework/core/js/forum/dist/app.js +++ b/framework/core/js/forum/dist/app.js @@ -31146,29 +31146,42 @@ System.register('flarum/utils/abbreviateNumber', [], function (_export, _context System.register('flarum/utils/affixSidebar', [], function (_export, _context) { "use strict"; - function affixSidebar(element, isInitialized) { + function affixSidebar(element, isInitialized, context) { var _this = this; if (isInitialized) return; - var $sidebar = $(element); - var $header = $('#header'); - var $footer = $('#footer'); + var onresize = function onresize() { + var $sidebar = $(element); + var $header = $('#header'); + var $footer = $('#footer'); + var $affixElement = $sidebar.find('> ul'); - // Don't affix the sidebar if it is taller than the viewport (otherwise - // there would be no way to scroll through its content). - if ($sidebar.outerHeight(true) > $(window).height() - $header.outerHeight(true)) return; + $(window).off('.affix'); + $affixElement.removeClass('affix affix-top affix-bottom').removeData('bs.affix'); - $sidebar.find('> ul').affix({ - offset: { - top: function top() { - return $sidebar.offset().top - $header.outerHeight(true) - parseInt($sidebar.css('margin-top'), 10); - }, - bottom: function bottom() { - return _this.bottom = $footer.outerHeight(true); + // Don't affix the sidebar if it is taller than the viewport (otherwise + // there would be no way to scroll through its content). + if ($sidebar.outerHeight(true) > $(window).height() - $header.outerHeight(true)) return; + + $affixElement.affix({ + offset: { + top: function top() { + return $sidebar.offset().top - $header.outerHeight(true) - parseInt($sidebar.css('margin-top'), 10); + }, + bottom: function bottom() { + return _this.bottom = $footer.outerHeight(true); + } } - } - }); + }); + }; + + // Register the affix plugin to execute on every window resize (and trigger) + $(window).on('resize', onresize).resize(); + + context.onunload = function () { + $(window).off('resize', onresize); + }; } _export('default', affixSidebar);