Skip to content

Commit

Permalink
fix: early button/anchor clicks do nothing
Browse files Browse the repository at this point in the history
This solves a small UX issue where clicking on a button (or anchor
that is not meant to be ajaxified) before ajaxify.end is called
leads to nothing happening because the appropriate click handler
has not been added.

This code will intercept premature clicks, wait until ajaxify.end
is called, and then re-send the click.
  • Loading branch information
julianlam committed Sep 29, 2020
1 parent ea0f326 commit 966d3f7
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions public/src/app.js
Expand Up @@ -17,6 +17,32 @@ app.cacheBuster = null;

app.cacheBuster = config['cache-buster'];

/**
* Occasionally, a button or anchor (not meant to be ajaxified) is clicked before
* ajaxify is ready. Capture that event and re-click it once NodeBB is ready.
*
* e.g. New Topic/Reply, post tools
*/
var earlyQueue = []; // once we can ES6, use Set instead
var earlyClick = function (ev) {
var btnEl = ev.target.closest('button');
if (!btnEl && ev.target.closest('a') && ev.target.closest('a').getAttribute('data-ajaxify') === 'false') {
btnEl = ev.target.closest('a');
}
if (btnEl && !earlyQueue.includes(btnEl)) {
earlyQueue.push(btnEl);
ev.stopImmediatePropagation();
ev.preventDefault();
}
};
document.body.addEventListener('click', earlyClick);
$(window).on('action:ajaxify.end', function () {
document.body.removeEventListener('click', earlyClick);
earlyQueue.forEach(function (el) {
el.click();
});
});

bootbox.setDefaults({
locale: config.userLang,
});
Expand Down

0 comments on commit 966d3f7

Please sign in to comment.