Skip to content

Commit

Permalink
feat: convert DOM events to stored functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
crhallberg committed Jan 29, 2024
1 parent 6cc693c commit f316c99
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
2 changes: 1 addition & 1 deletion themes/bootstrap3/js/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,4 @@ function cartFormHandler(event, data) {
}
}

document.addEventListener('VuFind.lightbox.closed', VuFind.cart.updateCount, false);
VuFind.listen('lightbox.closed', VuFind.cart.updateCount);
45 changes: 30 additions & 15 deletions themes/bootstrap3/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,35 @@ var VuFind = (function VuFind() {
var _elementBase;
var _iconsCache = {};

// Emit a custom event
// Recommendation: prefix with vf-
var emit = function emit(name, detail) {
if (typeof detail === 'undefined') {
document.dispatchEvent(new Event(name));
} else {
var event = document.createEvent('CustomEvent');
event.initCustomEvent(name, true, true, detail); // name, canBubble, cancelable, detail
document.dispatchEvent(event);
// Event controls

let listeners = {};
function unlisten(event, fn) {
const index = listeners[event].indexOf(fn);

if (index > -1) {
listeners[event].splice(index, 1);
}
};
// Listen shortcut to put everyone on the same element
var listen = function listen(name, func) {
document.addEventListener(name, func, false);
};
}

function listen(event, fn, { once = false } = {}) {

Check failure on line 29 in themes/bootstrap3/js/common.js

View workflow job for this annotation

GitHub Actions / Tests with PHP 8.2

'once' is assigned a value but never used
if (typeof listeners[event] === "undefined") {
listeners[event] = [];
}

listeners[event].push(fn);
return () => unlisten(event, fn);
}

function emit(event, ...args) {
if (typeof listeners[event] === "undefined") {
return;
}

listeners[event].forEach((fn) => fn(...args));
}

// Module control

var register = function register(name, module) {
if (_submodules.indexOf(name) === -1) {
Expand Down Expand Up @@ -340,11 +354,12 @@ var VuFind = (function VuFind() {
addTranslations: addTranslations,
init: init,
emit: emit,
listen: listen,
unlisten: unlisten,
evalCallback: evalCallback,
getCspNonce: getCspNonce,
icon: icon,
isPrinting: isPrinting,
listen: listen,
refreshPage: refreshPage,
register: register,
setCspNonce: setCspNonce,
Expand Down
30 changes: 7 additions & 23 deletions themes/bootstrap3/js/lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ VuFind.register('lightbox', function Lightbox() {
_lightboxTitle = false;
_modal.modal('handleUpdate');
}
function _emit(msg, _details) {
var details = _details || {};
var event;
try {
event = new CustomEvent(msg, {
detail: details,
bubbles: true,
cancelable: true
});
} catch (e) {
// Fallback to document.createEvent() if creating a new CustomEvent fails (e.g. IE 11)
event = document.createEvent('CustomEvent');
event.initCustomEvent(msg, true, true, details);
}
return document.dispatchEvent(event);
}

function _addQueryParameters(url, params) {
let fragmentSplit = url.split('#');
Expand Down Expand Up @@ -220,7 +204,7 @@ VuFind.register('lightbox', function Lightbox() {
|| obj.url.match(/MyResearch\/(?!Bulk|Delete|Recover)/)
) && flashMessages.length === 0
) {
var eventResult = _emit('VuFind.lightbox.login', {
var eventResult = VuFind.emit('lightbox.login', {
originalUrl: _originalUrl,
formUrl: obj.url
});
Expand Down Expand Up @@ -338,10 +322,10 @@ VuFind.register('lightbox', function Lightbox() {
}
// onclose behavior
if ('string' === typeof $(form).data('lightboxOnclose')) {
document.addEventListener('VuFind.lightbox.closed', function lightboxClosed(e) {
this.removeEventListener('VuFind.lightbox.closed', arguments.callee);
VuFind.evalCallback($(form).data('lightboxOnclose'), e, form);
}, false);
VuFind.listen('lightbox.closed', function lightboxClosed() {
VuFind.unlisten('lightbox.closed', arguments.callee); // only once
VuFind.evalCallback($(form).data('lightboxOnclose'), null, form);
});
}
// Prevent multiple submission of submit button in lightbox
if (submit.closest(_modal).length > 0) {
Expand Down Expand Up @@ -523,12 +507,12 @@ VuFind.register('lightbox', function Lightbox() {
}
unbindFocus();
this.setAttribute('aria-hidden', true);
_emit('VuFind.lightbox.closing');
VuFind.emit('lightbox.closing');
}
});
_modal.on('hidden.bs.modal', function lightboxHidden() {
VuFind.lightbox.reset();
_emit('VuFind.lightbox.closed');
VuFind.emit('lightbox.closed');
});
_modal.on("shown.bs.modal", function lightboxShown() {
bindFocus();
Expand Down

0 comments on commit f316c99

Please sign in to comment.