IMPR: events router

This commit is contained in:
Tony Air 2020-09-10 06:55:27 +07:00
parent 4d34a29fdc
commit 0076e6697b
2 changed files with 70 additions and 0 deletions

63
src/js/_events.router.js Normal file
View File

@ -0,0 +1,63 @@
/**
* Route side-wide events
*/
const EventsUI = (($) => {
const on = $.fn.on;
const off = $.fn.off;
// Constants
const W = window;
const $W = $(W);
const D = document;
const $Body = $('body');
const NAME = 'EventsUI';
class EventsUI {
static process(el, args) {
const eventName = args[0];
const tagName = typeof el !== undefined ? $(el).prop('tagName') : null;
switch (tagName) {
case 'HTML':
case 'BODY':
el = $W;
break;
}
return [el, args];
}
}
// rewrite jQuery functions
$.fn.on = function () {
const result = EventsUI.process(this, arguments);
return on.apply(...result);
};
$.fn.off = function () {
const result = EventsUI.process(this, arguments);
return off.apply(...result);
};
const scrollTop = $.fn.scrollTop;
// rewrite scrollTop
$.fn.scrollTop = function () {
let el = this;
let args = arguments;
const tagName = typeof el !== undefined ? $(el).prop('tagName') : null;
switch (tagName) {
case 'HTML':
case 'BODY':
el = $W;
break;
}
return scrollTop.apply(el, args);
};
})($);
export default EventsUI;

View File

@ -5,6 +5,7 @@ import $ from 'jquery';
import Events from './_events';
import Consts from './_consts';
import EventsRouter from './_events.router';
import Spinner from './_components/_ui.spinner';
// AJAX functionality
@ -47,6 +48,9 @@ const MainUI = (($) => {
$W.on(`${Events.LODEDANDREADY}`, () => {
console.groupEnd('Init');
console.timeEnd('init');
console.time('Post-init');
console.groupCollapsed('Post-init');
});
// get browser locale
@ -524,6 +528,9 @@ const MainUI = (($) => {
setTimeout(() => {
$W.trigger(`${Events.LAZYIMAGESREADY}`);
console.groupEnd('Post-init');
console.timeEnd('Post-init');
}, 100);
});
});