Skip to content
This repository has been archived by the owner on Jan 20, 2021. It is now read-only.

Commit

Permalink
fixes bug when add twice listener (multiple videos on the same page)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin committed Jun 15, 2020
1 parent d65a305 commit b630f1b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,37 @@ export function liveBlock(
* Block to replace Watcher
* On $event on element find via $attribute, set attribute's content in element.innerHTML
*/
export function replaceOn(attribute = 'replaceBy', event = 'click') {
var loadVideo = function (event) {
var element = event.currentTarget;
export function replaceOn(attribute = 'replaceBy', eventName = 'click') {
var loadVideo = function (element) {
var content = element.getAttribute(attribute);
element.style.zIndex = '2000';
if (
element.classList.contains('hero-banner-overlay-lg') &&
element.querySelector('picture') &&
window.innerWidth < 992
) {
element.querySelector('picture').outerHTML = content;
element.querySelector('.btn-play').outerHTML = ' ';
element.style.zIndex = '2000';
} else {
element.innerHTML = content;
}
element.removeAttribute(attribute);
document.dispatchEvent(new Event('DOMChanged'));
};

document.querySelectorAll('[' + attribute + ']').forEach(function (element) {
element.addEventListener(event, loadVideo, { once: true });
});
document
.querySelectorAll('[' + attribute + ']:not([listen])')
.forEach(function (element) {
element.setAttribute('listen', '');
element.addEventListener(
eventName,
function (event) {
loadVideo(event.currentTarget); //event.currentTarget;
element.removeAttribute('listen');
},
{ once: true }
);
});
}

/**
Expand Down

0 comments on commit b630f1b

Please sign in to comment.