docbrown/app/assets/javascripts/initializers/initializeSponsorshipVisibility.js
Suzanne Aitchison 75b6c8ed96
Use a single listener for all follow buttons on a page (#14246)
* create new pack, handle user follow buttons, use pack on article page, remove initializeUserFollowButts

* init all follow button types, add pack to tag index and podcast episode pages

* add pack to all relevant pages, listen for newly inserted follow buttons

* change to searchParams to remove follow button initializer calls

* fix bug with tag page, add pack to notifications page

* fix issue with follow back inner text

* update cypress specs

* run the followbuttons code on sponsors page

* remove extra foreach

* add test for follow from article sidebar

* add test for follow and unfollow tag

* add test for the tag index page

* add spec for organisation profile follow

* add tests for follow buttons in search results

* add tests for notification follows

* commit missed file - woops

* show login modal if user is logged out when they click

* add cypress tests for logged out state

* change tag button initialization

* remove data-button-initialized

* init follow buttons from base pack

* handle the case where multiple follow buttons exist on a page for the same user

* account for instantclick and userdata not being defined, lower coverage for jest

* use getInstantClick

* fix issue with set initialisation

* only listen for mutations in areas we know follow buttons may be added dynamically

* small refactors
2021-07-27 10:45:50 +01:00

59 lines
1.4 KiB
JavaScript

/*
* kept as a stand function so it can be loaded again without issue
* see: https://github.com/thepracticaldev/dev.to/issues/6468
*/
function sponsorClickHandler(event) {
ga(
'send',
'event',
'click',
'click sponsor link',
event.target.dataset.details,
null,
);
}
function listenForSponsorClick() {
setTimeout(() => {
if (window.ga) {
var links = document.getElementsByClassName('partner-link');
// eslint-disable-next-line no-plusplus
for (var i = 0; i < links.length; i++) {
links[i].onclick = sponsorClickHandler;
}
}
}, 400);
}
function initializeSponsorshipVisibility() {
var el =
document.getElementById('sponsorship-widget') ||
document.getElementById('partner-content-display');
var user = userData();
if (el) {
setTimeout(() => {
if (window.ga) {
if (document.querySelectorAll('[data-partner-seen]').length === 0) {
ga(
'send',
'event',
'view',
'sponsor displayed on page',
el.dataset.details,
null,
);
el.dataset.partnerSeen = 'true';
}
}
}, 400);
}
if (el && user && user.display_sponsors) {
el.classList.remove('hidden');
listenForSponsorClick();
} else if (el && user) {
el.classList.add('hidden');
} else if (el) {
el.classList.remove('hidden');
listenForSponsorClick();
}
}