mirror of
https://github.com/umami-software/umami.git
synced 2026-02-16 10:35:35 +01:00
Script simplification: Less nesting
This commit is contained in:
parent
b26fc15fa0
commit
5369144b64
1 changed files with 39 additions and 43 deletions
|
|
@ -86,52 +86,48 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleClicks = () => {
|
const handleClicks = () => {
|
||||||
document.addEventListener(
|
const trackElement = async el => {
|
||||||
'click',
|
const attr = el.getAttribute.bind(el);
|
||||||
async e => {
|
const eventName = attr(eventNameAttribute);
|
||||||
const trackElement = async el => {
|
if (eventName) {
|
||||||
const attr = el.getAttribute.bind(el);
|
const eventData = {};
|
||||||
const eventName = attr(eventNameAttribute);
|
|
||||||
if (eventName) {
|
|
||||||
const eventData = {};
|
|
||||||
|
|
||||||
el.getAttributeNames().forEach(name => {
|
el.getAttributeNames().forEach(name => {
|
||||||
const match = name.match(eventRegex);
|
const match = name.match(eventRegex);
|
||||||
if (match) eventData[match[1]] = attr(name);
|
if (match) eventData[match[1]] = attr(name);
|
||||||
});
|
});
|
||||||
|
|
||||||
return track(eventName, eventData);
|
return track(eventName, eventData);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const onClick = async e => {
|
||||||
|
const el = e.target;
|
||||||
|
const parentElement = el.closest('a,button');
|
||||||
|
if (!parentElement) return trackElement(el);
|
||||||
|
|
||||||
|
const { href, target } = parentElement;
|
||||||
|
const eventName = parentElement.getAttribute(eventNameAttribute);
|
||||||
|
if (!eventName) return;
|
||||||
|
|
||||||
|
if (parentElement.tagName === 'BUTTON') {
|
||||||
|
return trackElement(parentElement);
|
||||||
|
}
|
||||||
|
if (parentElement.tagName === 'A' && href) {
|
||||||
|
const external =
|
||||||
|
target === '_blank' ||
|
||||||
|
e.ctrlKey ||
|
||||||
|
e.shiftKey ||
|
||||||
|
e.metaKey ||
|
||||||
|
(e.button && e.button === 1);
|
||||||
|
if (!external) e.preventDefault();
|
||||||
|
return trackElement(parentElement).then(() => {
|
||||||
|
if (!external) {
|
||||||
|
(target === '_top' ? top.location : location).href = href;
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
}
|
||||||
const el = e.target;
|
};
|
||||||
const parentElement = el.closest('a,button');
|
document.addEventListener('click', onClick, true);
|
||||||
if (!parentElement) return trackElement(el);
|
|
||||||
|
|
||||||
const { href, target } = parentElement;
|
|
||||||
const eventName = parentElement.getAttribute(eventNameAttribute);
|
|
||||||
if (!eventName) return;
|
|
||||||
|
|
||||||
if (parentElement.tagName === 'BUTTON') {
|
|
||||||
return trackElement(parentElement);
|
|
||||||
}
|
|
||||||
if (parentElement.tagName === 'A' && href) {
|
|
||||||
const external =
|
|
||||||
target === '_blank' ||
|
|
||||||
e.ctrlKey ||
|
|
||||||
e.shiftKey ||
|
|
||||||
e.metaKey ||
|
|
||||||
(e.button && e.button === 1);
|
|
||||||
if (!external) e.preventDefault();
|
|
||||||
return trackElement(parentElement).then(() => {
|
|
||||||
if (!external) {
|
|
||||||
(target === '_top' ? top.location : location).href = href;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Tracking functions */
|
/* Tracking functions */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue