custom composedPath (ISSUE #24)

This commit is contained in:
Matteo Manzinello 2019-05-26 13:43:17 +02:00
parent 9f0138292e
commit 514671d82b
3 changed files with 70 additions and 21 deletions

43
dist/mailgo.js vendored
View File

@ -279,17 +279,23 @@ var isMailgo = function isMailgo(element) {
var mailgoCheckRender = function mailgoCheckRender(event) {
// check if the id=mailgo exists in the body
if (!document.contains(getE("mailgo"))) return;
event.path.forEach(function (element) {
// go in the event.path to find if the user has clicked on a mailgo element
if (isMailgo(element)) {
// stop the normal execution of the element click
event.preventDefault(); // render mailgo
if (!document.contains(getE("mailgo"))) return; // the path of the event
var path = event.path || event.composedPath && event.composedPath() || composedPath(event.target);
if (path) {
path.forEach(function (element) {
// go in the event.path to find if the user has clicked on a mailgo element
if (isMailgo(element)) {
// stop the normal execution of the element click
event.preventDefault(); // render mailgo
mailgoRender(element);
return;
}
});
}
mailgoRender(element);
return;
}
});
return;
};
/**
@ -393,4 +399,21 @@ var encodeEmail = function encodeEmail(email) {
var getE = function getE(id) {
return document.getElementById(id);
}; // custom composedPath
var composedPath = function composedPath(el) {
var path = [];
while (el) {
path.push(el);
if (el.tagName === "HTML") {
path.push(document);
path.push(window);
return path;
}
el = el.parentElement;
}
};

2
dist/mailgo.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -360,18 +360,26 @@ const mailgoCheckRender = event => {
// check if the id=mailgo exists in the body
if (!document.contains(getE("mailgo"))) return;
event.composedPath().forEach(element => {
// go in the event.path to find if the user has clicked on a mailgo element
if (isMailgo(element)) {
// stop the normal execution of the element click
event.preventDefault();
// the path of the event
let path =
event.path ||
(event.composedPath && event.composedPath()) ||
composedPath(event.target);
// render mailgo
mailgoRender(element);
if (path) {
path.forEach(element => {
// go in the event.path to find if the user has clicked on a mailgo element
if (isMailgo(element)) {
// stop the normal execution of the element click
event.preventDefault();
return;
}
});
// render mailgo
mailgoRender(element);
return;
}
});
}
return;
};
@ -472,3 +480,21 @@ const encodeEmail = email => btoa(email);
// getE shorthand
const getE = id => document.getElementById(id);
// custom composedPath
const composedPath = el => {
let path = [];
while (el) {
path.push(el);
if (el.tagName === "HTML") {
path.push(document);
path.push(window);
return path;
}
el = el.parentElement;
}
};