2019-07-25 20:03:22 +03:00
|
|
|
import ReactDOM from "react-dom";
|
|
|
|
import JoinUs from "./components/join/join.jsx";
|
2020-12-08 04:50:05 +03:00
|
|
|
import { ReactGA } from "../js/common";
|
2019-07-25 20:03:22 +03:00
|
|
|
|
2022-02-01 20:59:36 +03:00
|
|
|
/**
|
|
|
|
* Logic for the Newsletter form that slides down from the top of page
|
|
|
|
* when the "Newsletter" button is clicked in the navigation bar.
|
2022-05-10 02:50:22 +03:00
|
|
|
*
|
2022-02-01 20:59:36 +03:00
|
|
|
*/
|
|
|
|
|
2019-07-25 20:03:22 +03:00
|
|
|
const elements = {
|
2020-10-21 23:51:33 +03:00
|
|
|
primaryNav: `.primary-nav-container`,
|
|
|
|
narrowMenuContainer: `.primary-nav-container .narrow-screen-menu-container`,
|
|
|
|
wideMenuContainer: `.primary-nav-container .wide-screen-menu-container`,
|
|
|
|
buttonMobile: `.primary-nav-container .narrow-screen-menu-container .btn-newsletter`,
|
|
|
|
buttonDesktop: `.primary-nav-container .wide-screen-menu-container .btn-newsletter`,
|
2019-07-25 20:03:22 +03:00
|
|
|
container: `#nav-newsletter-form-wrapper`,
|
|
|
|
joinUs: `#nav-newsletter-form-wrapper .join-us.on-nav`,
|
2020-06-01 22:12:52 +03:00
|
|
|
buttonDismiss: `#nav-newsletter-form-wrapper .form-dismiss`,
|
2019-07-25 20:03:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class NavNewsletter {
|
|
|
|
constructor() {
|
2020-11-24 22:37:01 +03:00
|
|
|
this.visible = false;
|
2019-07-25 20:03:22 +03:00
|
|
|
this.form = null;
|
|
|
|
}
|
|
|
|
|
2020-11-24 22:37:01 +03:00
|
|
|
isVisible() {
|
|
|
|
return this.visible;
|
2019-07-25 20:03:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset form
|
|
|
|
resetForm() {
|
|
|
|
this.form.reset();
|
|
|
|
|
|
|
|
let handleTransitionend = () => {
|
|
|
|
elements.container.removeEventListener(
|
|
|
|
"transitionend",
|
|
|
|
handleTransitionend
|
|
|
|
);
|
|
|
|
elements.buttonDismiss.textContent = "No thanks";
|
|
|
|
};
|
|
|
|
elements.container.addEventListener("transitionend", handleTransitionend);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For desktop+ version:
|
|
|
|
// transition section to its close state,
|
|
|
|
// remove the global 'closeFormClickHandler' click event handler
|
|
|
|
// and reset the form
|
|
|
|
closeDesktopNewsletter(event) {
|
2020-06-02 02:42:45 +03:00
|
|
|
const wrapper = elements.container;
|
|
|
|
wrapper.classList.remove("expanded");
|
2019-07-25 20:03:22 +03:00
|
|
|
elements.buttonDesktop.classList.remove("active");
|
2020-06-02 02:42:45 +03:00
|
|
|
// Schedule a "display:none" to happen after the `expanded` animation finishes.
|
|
|
|
// See `#nav-newsletter-form-wrapper` transition in ./source/sass/components/primary-nav.scss
|
|
|
|
setTimeout(() => {
|
|
|
|
if (wrapper.classList.contains("expanded")) return;
|
|
|
|
wrapper.classList.add("d-none");
|
|
|
|
}, 500);
|
2019-11-12 23:17:22 +03:00
|
|
|
// Make sure we don't leak event listeners
|
2019-07-25 20:03:22 +03:00
|
|
|
document.removeEventListener("click", this.closeFormClickHandler);
|
|
|
|
document.removeEventListener("scroll", this.closeFormClickHandler);
|
|
|
|
this.resetForm();
|
2020-11-24 22:37:01 +03:00
|
|
|
this.visible = false;
|
2019-07-25 20:03:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For desktop+ version:
|
|
|
|
// transition newsletter section to its expanded state
|
|
|
|
expandDesktopNewsletter(event) {
|
2020-06-02 02:42:45 +03:00
|
|
|
const wrapper = elements.container;
|
|
|
|
wrapper.classList.remove("d-none");
|
|
|
|
wrapper.style.top = `${elements.primaryNav.offsetHeight}px`;
|
|
|
|
wrapper.classList.add("expanded");
|
2019-07-25 20:03:22 +03:00
|
|
|
elements.buttonDesktop.classList.add("active");
|
|
|
|
document.addEventListener(`click`, this.closeFormClickHandler);
|
2020-04-10 01:23:05 +03:00
|
|
|
document.addEventListener(`scroll`, this.closeFormClickHandler, {
|
2020-06-01 22:12:52 +03:00
|
|
|
passive: true,
|
2020-04-10 01:23:05 +03:00
|
|
|
});
|
2020-11-24 22:37:01 +03:00
|
|
|
this.visible = true;
|
2019-07-25 20:03:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For mobile version:
|
|
|
|
// transition newsletter section to its close state,
|
|
|
|
// remove the global 'closeFormClickHandler' click event handler,
|
|
|
|
// and reset the form
|
|
|
|
closeMobileNewsletter() {
|
2020-06-02 02:42:45 +03:00
|
|
|
const wrapper = elements.container;
|
2019-07-25 20:03:22 +03:00
|
|
|
elements.narrowMenuContainer.classList.remove("d-none");
|
2020-06-02 02:42:45 +03:00
|
|
|
wrapper.classList.remove("faded-in");
|
|
|
|
// Schedule a "display:none" to happen after the `expanded` animation finishes.
|
|
|
|
// See `#nav-newsletter-form-wrapper` transition in ./source/sass/components/primary-nav.scss
|
|
|
|
setTimeout(() => {
|
|
|
|
if (wrapper.classList.contains("expanded")) return;
|
|
|
|
wrapper.classList.add("d-none");
|
|
|
|
}, 500);
|
2019-07-25 20:03:22 +03:00
|
|
|
this.resetForm();
|
2020-11-24 22:37:01 +03:00
|
|
|
this.visible = false;
|
2019-07-25 20:03:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For mobile version:
|
|
|
|
// transition section to its expanded state
|
|
|
|
expandMobileNewsletter() {
|
2020-06-02 02:42:45 +03:00
|
|
|
const wrapper = elements.container;
|
2019-07-25 20:03:22 +03:00
|
|
|
elements.narrowMenuContainer.classList.add(`d-none`);
|
2020-06-02 02:42:45 +03:00
|
|
|
wrapper.classList.remove("d-none");
|
|
|
|
wrapper.classList.add("faded-in");
|
2020-11-24 22:37:01 +03:00
|
|
|
this.visible = true;
|
2019-07-25 20:03:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For desktop+ version:
|
|
|
|
// create click handler to detect clicking event outside of the newsletter section
|
|
|
|
closeFormClickHandler(event) {
|
|
|
|
// close newsletter section if clicking anywhere outside of the section
|
|
|
|
if (
|
|
|
|
!elements.container.contains(event.target) &&
|
|
|
|
event.target !== elements.container
|
|
|
|
) {
|
|
|
|
navNewsletter.closeDesktopNewsletter();
|
2019-11-12 23:17:22 +03:00
|
|
|
// Make sure we don't leak event listeners:
|
|
|
|
document.removeEventListener("click", this.closeFormClickHandler);
|
|
|
|
document.removeEventListener("scroll", this.closeFormClickHandler);
|
2019-07-25 20:03:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and bind all necessary DOM nodes, returning "false" if not all DOM nodes were found.
|
|
|
|
*/
|
|
|
|
checkDomNodes() {
|
2020-06-01 22:12:52 +03:00
|
|
|
return Object.keys(elements).every((key) => {
|
2019-07-25 20:03:22 +03:00
|
|
|
// if this element already resolved to a DOM node, move on to the next
|
|
|
|
let value = elements[key];
|
|
|
|
if (typeof value !== "string") return true;
|
|
|
|
|
|
|
|
// find this DOM node, and report on the result (binding it if found for later use)
|
|
|
|
let element = document.querySelector(value);
|
|
|
|
if (element) elements[key] = element;
|
2019-11-12 23:17:22 +03:00
|
|
|
|
2019-07-25 20:03:22 +03:00
|
|
|
return !!element;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:34:29 +03:00
|
|
|
buttonDesktopClickHandler(event) {
|
2020-11-24 22:37:01 +03:00
|
|
|
if (!this.visible) {
|
2019-10-10 23:34:29 +03:00
|
|
|
event.stopPropagation();
|
|
|
|
this.expandDesktopNewsletter();
|
2020-12-08 04:50:05 +03:00
|
|
|
|
|
|
|
ReactGA.event({
|
|
|
|
category: `signup`,
|
|
|
|
action: `newsletter tap`,
|
|
|
|
label: `newsletter button from header`,
|
|
|
|
});
|
2019-10-10 23:34:29 +03:00
|
|
|
} else {
|
|
|
|
this.closeDesktopNewsletter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 17:48:25 +03:00
|
|
|
init(foundationSiteURL) {
|
2019-07-25 20:03:22 +03:00
|
|
|
// some DOM nodes do not exist, return
|
|
|
|
if (!this.checkDomNodes()) return;
|
|
|
|
|
|
|
|
var props = elements.joinUs.dataset;
|
2020-06-01 22:12:52 +03:00
|
|
|
props.apiUrl = `${foundationSiteURL}/api/campaign/signups/${
|
|
|
|
props.signupId || 0
|
|
|
|
}/`;
|
2019-07-25 20:03:22 +03:00
|
|
|
props.isHidden = false;
|
|
|
|
this.form = ReactDOM.render(<JoinUs {...props} />, elements.joinUs);
|
|
|
|
|
|
|
|
// For desktop+ version:
|
|
|
|
// make 'buttonDesktop' the trigger to open newsletter section
|
2020-06-01 22:12:52 +03:00
|
|
|
elements.buttonDesktop.addEventListener(`click`, (event) => {
|
2019-10-10 23:34:29 +03:00
|
|
|
this.buttonDesktopClickHandler(event);
|
2019-07-25 20:03:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// For mobile version:
|
|
|
|
// make 'buttonDismiss' the trigger to close newsletter section
|
|
|
|
elements.buttonDismiss.addEventListener("click", () => {
|
|
|
|
this.closeMobileNewsletter();
|
|
|
|
});
|
|
|
|
|
|
|
|
// For mobile version:
|
|
|
|
// make 'buttonMobile' the trigger to show newsletter section
|
|
|
|
elements.buttonMobile.addEventListener("click", () => {
|
2019-10-10 23:34:29 +03:00
|
|
|
this.expandMobileNewsletter();
|
2020-12-08 04:50:05 +03:00
|
|
|
|
|
|
|
ReactGA.event({
|
|
|
|
category: `signup`,
|
|
|
|
action: `newsletter tap`,
|
|
|
|
label: `newsletter button from header`,
|
|
|
|
});
|
2019-07-25 20:03:22 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const navNewsletter = new NavNewsletter();
|
|
|
|
|
|
|
|
export default navNewsletter;
|