|
| 1 | +import UIkit from "uikit/dist/js/uikit.js"; |
| 2 | + |
| 3 | +(() => { |
| 4 | + const markSubmitted = () => { |
| 5 | + if (!localStorage) { |
| 6 | + return; |
| 7 | + } |
| 8 | + // expires in a month |
| 9 | + const expiresAt = new Date(); |
| 10 | + expiresAt.setMonth(expiresAt.getMonth() + 1); |
| 11 | + localStorage.setItem("feedback-submitted", expiresAt.toString()); |
| 12 | + |
| 13 | + // send a google analytics event |
| 14 | + if (window.ga) { |
| 15 | + window.ga("send", "event", "feedback", "open", "show feedback form", "true"); |
| 16 | + } |
| 17 | + }; |
| 18 | + const wasRecentlySubmitted = () => { |
| 19 | + if (!localStorage) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + const r = localStorage.getItem("feedback-submitted"); |
| 23 | + if (!r) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + const expiresAt = new Date(r); |
| 27 | + // check if expired |
| 28 | + return expiresAt > new Date(); |
| 29 | + }; |
| 30 | + |
| 31 | + const showForm = (formLink) => { |
| 32 | + const url = `${formLink}${encodeURIComponent(location.href)}&embedded=true`; |
| 33 | + UIkit.modal.dialog( |
| 34 | + `<button class="uk-modal-close-default" type="button" uk-close></button><div class="feedback-modal"><iframe src="${url}" uk-video></iframe></div>` |
| 35 | + ); |
| 36 | + }; |
| 37 | + const btn = document.querySelector(".feedback-button"); |
| 38 | + if (btn) { |
| 39 | + btn.addEventListener("click", (e) => { |
| 40 | + e.preventDefault(); |
| 41 | + markSubmitted(); |
| 42 | + showForm(btn.href); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + const container = document.querySelector(".feedback-popup-container"); |
| 47 | + |
| 48 | + if (container && !wasRecentlySubmitted()) { |
| 49 | + const delayMin = Number.parseInt(container.dataset.delayMin, 10) * 1000; |
| 50 | + const delayMax = Number.parseInt(container.dataset.delayMax, 10) * 1000; |
| 51 | + const delay = delayMin + Math.random() * (delayMax - delayMin); |
| 52 | + |
| 53 | + const feedbackLikelihoodDesktop = Number.parseFloat(container.dataset.likeDesktop); |
| 54 | + const feedbackLikelihoodMobile = Number.parseFloat(container.dataset.likeMobile); |
| 55 | + const isMobile = window.matchMedia("only screen and (max-width: 700px)").matches; |
| 56 | + |
| 57 | + const showByChance = |
| 58 | + (isMobile && Math.random() <= feedbackLikelihoodMobile) || |
| 59 | + (!isMobile && Math.random() <= feedbackLikelihoodDesktop); |
| 60 | + |
| 61 | + const duration = Number.parseInt(container.dataset.duration, 10) * 1000; |
| 62 | + const formLink = container.dataset.href; |
| 63 | + |
| 64 | + const showFeedbackNotification = () => { |
| 65 | + const elem = UIkit.notification({ |
| 66 | + message: container.innerHTML, |
| 67 | + pos: "bottom-right", |
| 68 | + timeout: duration, |
| 69 | + clsContainer: "feedback-message uk-notification", |
| 70 | + }); |
| 71 | + elem.$el.querySelector("div").addEventListener("click", (e) => { |
| 72 | + // stop auto close |
| 73 | + e.stopPropagation(); |
| 74 | + e.preventDefault(); |
| 75 | + }); |
| 76 | + elem.$el.querySelector(".feedback-button").addEventListener("click", (e) => { |
| 77 | + e.preventDefault(); |
| 78 | + elem.close(true); |
| 79 | + markSubmitted(); |
| 80 | + showForm(formLink); |
| 81 | + }); |
| 82 | + }; |
| 83 | + // initial delay |
| 84 | + if (showByChance) { |
| 85 | + setTimeout(showFeedbackNotification, delay); |
| 86 | + } |
| 87 | + } |
| 88 | +})(); |
0 commit comments