Skip to content

Commit

Permalink
wip: remove jquery
Browse files Browse the repository at this point in the history
removed jquery from component: AffixedSidebar
  • Loading branch information
YUCLing committed Nov 18, 2024
1 parent 1644f2e commit 11fc74d
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions framework/core/js/src/forum/components/AffixedSidebar.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import Component from '../../common/Component';
import heightWithMargin from '../../common/utils/heightWithMargin';

/**
* The `AffixedSidebar` component uses Bootstrap's "affix" plugin to keep a
* The `AffixedSidebar` component uses sticky position to keep a
* sidebar navigation at the top of the viewport when scrolling.
*
* ### Children
*
* The component must wrap an element that itself wraps an <ul> element, which
* will be "affixed".
*
* @see https://getbootstrap.com/docs/3.4/javascript/#affix
*/
export default class AffixedSidebar extends Component {
view(vnode) {
Expand All @@ -19,36 +18,35 @@ export default class AffixedSidebar extends Component {
oncreate(vnode) {
super.oncreate(vnode);

// Register the affix plugin to execute on every window resize (and trigger)
// Register the affix to execute on every window resize (and trigger)
this.boundOnresize = this.onresize.bind(this);
$(window).on('resize', this.boundOnresize).resize();
window.addEventListener('resize', this.boundOnresize);
window.dispatchEvent(new Event('resize'));
}

onremove(vnode) {
super.onremove(vnode);

$(window).off('resize', this.boundOnresize);
window.removeEventListener('resize', this.boundOnresize);
}

onresize() {
return;
const $sidebar = this.$();
const $header = $('#header');
const $footer = $('#footer');
const $affixElement = $sidebar.find('> ul');

$(window).off('.affix');
$affixElement.removeClass('affix affix-top affix-bottom').removeData('bs.affix');
const header = document.getElementById('header');
const affixElement = this.element.querySelector(':scope > 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;

$affixElement.affix({
offset: {
top: () => $sidebar.offset().top - $header.outerHeight(true) - parseInt($sidebar.css('margin-top'), 10),
bottom: () => (this.bottom = $footer.outerHeight(true)),
},
});
const enabled = heightWithMargin(this.element) <= window.innerHeight - heightWithMargin(header);
affixElement.classList.toggle('affix', enabled);
if (enabled) {
const top = heightWithMargin(header) + parseInt(getComputedStyle(this.element).marginTop, 10);
affixElement.style.position = 'sticky';
affixElement.style.top = top + 'px';
this.element.style.display = 'initial'; // Workaround for sticky not working
} else {
affixElement.style.position = '';
affixElement.style.top = '';
this.element.style.display = '';
}
}
}

0 comments on commit 11fc74d

Please sign in to comment.