1/** 2 * PDFãã¦ã³ãã¼ãã²ã¼ã 3 * 4 * .js-pdf-gate ã¯ã©ã¹ãä»ãããªã³ã¯ãã¯ãªãã¯ããã¨ãã 5 * cookieï¼ãã©ã¼ã éä¿¡æ¸ã¿ãã©ã°ï¼ããªããã°CF7ãã©ã¼ã ã®ãããã¢ããã表示ãã 6 * ããã° data-pdf 屿§ï¼ãªããã° hrefï¼ã®PDFãå¥ã¿ãã§éãã 7 * ãã©ã¼ã ã¯ãå ¥å â ç¢ºèª â éä¿¡ãã®2ã¹ãããæ§æã 8 * ãã©ã¼ã éä¿¡å®äºï¼wpcf7mailsentï¼ã§ cookie ãä¿åãã 9 * ãPDFãéãããã¿ã³ã表示ããï¼èªåã§ã®ãã¼ã¸é·ç§»ã¯ããªãï¼ã 10 * 11 * ãã©ã¼ã éä¿¡ãä¸è¦ãªè¨èªï¼æ¥æ¬èªï¼ã§ã¯ãPHPå´ãã requiresForm ãç©ºã§æ¸¡ããã 12 * ã¢ã¼ãã«ãåºåãããªããããã¯ãªãã¯ã§ãã®ã¾ã¾PDFãå¥ã¿ãã§éãã 13 */ 14(function () { 15 "use strict"; 16 17 const COOKIE_NAME = "dfc_pdf_gate"; 18 const COOKIE_VALUE = "1"; 19 const COOKIE_DAYS = 7; 20 21 let pendingPdfUrl = ""; 22 23 /** 24 * cookie ã«éä¿¡æ¸ã¿ãã©ã°ãããã 25 */ 26 function hasCookie() { 27 return ( 28 document.cookie 29 .split("; ") 30 .indexOf(COOKIE_NAME + "=" + COOKIE_VALUE) !== -1 31 ); 32 } 33 34 /** 35 * éä¿¡æ¸ã¿ãã©ã°ã cookie ã«ä¿å 36 */ 37 function setCookie() { 38 const expires = new Date(); 39 expires.setTime(expires.getTime() + COOKIE_DAYS * 24 * 60 * 60 * 1000); 40 document.cookie = 41 COOKIE_NAME + 42 "=" + 43 COOKIE_VALUE + 44 "; expires=" + 45 expires.toUTCString() + 46 "; path=/; SameSite=Lax"; 47 } 48 49 /** 50 * PDFãå¥ã¿ãã§éãï¼ãããã¢ãããããã¯æã¯ false ãè¿ãï¼ 51 */ 52 function openPdf(url) { 53 const win = window.open(url, "_blank", "noopener"); 54 return !!win; 55 } 56 57 /** 58 * ã²ã¼ã対象ãªã³ã¯ã®ã¯ãªãã¯ãè³¼èªãã 59 * 60 * ããã©ã«ãæåï¼href="#" ã«ãããã¼ã¸ä¸é¨ã¸ã®ã¸ã£ã³ãçï¼ã常ã«ç¡å¹åãã 61 * PDFã®URLããã³ãã©ã«æ¸¡ããURLãæªè¨å®ã®ãªã³ã¯ã¯ä½ãããªãã 62 */ 63 function onGateClick(handler) { 64 document.addEventListener("click", function (e) { 65 const link = e.target.closest("a.js-pdf-gate"); 66 67 if (!link) { 68 return; 69 } 70 71 e.preventDefault(); 72 73 const url = 74 link.getAttribute("data-pdf") || link.getAttribute("href"); 75 76 if (!url || url === "#") { 77 return; 78 } 79 80 handler(url); 81 }); 82 } 83 84 function init() { 85 const config = window.dfcPdfGate || {}; 86 const modal = document.getElementById("pdf-gate-modal"); 87 88 // ãã©ã¼ã éä¿¡ãä¸è¦ï¼æ¥æ¬èªï¼ãªå ´åã¯ã²ã¼ãããããã«ç´æ¥PDFãéã 89 if (!config.requiresForm || !modal) { 90 onGateClick(openPdf); 91 return; 92 } 93 94 const content = modal.querySelector(".pdf-gate-modal__content"); 95 const errorEl = modal.querySelector(".pdf-gate-modal__error"); 96 const fallback = modal.querySelector(".pdf-gate-modal__fallback"); 97 const fallbackLink = fallback ? fallback.querySelector("a") : null; 98 99 /** 100 * ãã©ã¼ã å ã®å ¥åãã£ã¼ã«ãä¸è¦§ï¼hiddenã»ãã¿ã³é¡ã¯é¤ãï¼ 101 */ 102 function getFields() { 103 return modal.querySelectorAll( 104 '.wpcf7-form input:not([type="hidden"]):not([type="submit"]):not([type="button"]), .wpcf7-form textarea', 105 ); 106 } 107 108 function showError(show) { 109 if (errorEl) { 110 errorEl.hidden = !show; 111 } 112 } 113 114 /** 115 * 確èªã¹ãããã¸ï¼å ¥åæ¬ã readonly ã«ãã¦è¡¨ç¤ºãåãæ¿ããï¼ 116 */ 117 function enterConfirm() { 118 getFields().forEach(function (el) { 119 el.setAttribute("readonly", "readonly"); 120 }); 121 modal.classList.add("is-confirm"); 122 showError(false); 123 if (content) { 124 content.scrollTop = 0; 125 } 126 } 127 128 /** 129 * å ¥åã¹ãããã¸æ»ã 130 */ 131 function exitConfirm() { 132 getFields().forEach(function (el) { 133 el.removeAttribute("readonly"); 134 }); 135 modal.classList.remove("is-confirm"); 136 if (content) { 137 content.scrollTop = 0; 138 } 139 } 140 141 /** 142 * 確èªã¹ãããã¸é²ãåã®ç°¡æããªãã¼ã·ã§ã³ 143 * ï¼å¿ é æªå ¥åã»ã¡ã¼ã«çã®å½¢å¼ã¨ã©ã¼ããã§ãã¯ãæçµå¤å®ã¯CF7å´ã§å®æ½ï¼ 144 */ 145 function validateForm() { 146 let valid = true; 147 let firstInvalid = null; 148 149 getFields().forEach(function (el) { 150 let ok = true; 151 152 if ( 153 "true" === el.getAttribute("aria-required") && 154 !el.value.trim() 155 ) { 156 ok = false;
157 } 158 159 if (ok && el.value && el.checkValidity && !el.checkValidity()) { 160 ok = false; 161 } 162 163 el.classList.toggle("pdf-gate-invalid", !ok); 164 165 if (!ok) { 166 valid = false; 167 if (!firstInvalid) { 168 firstInvalid = el; 169 } 170 } 171 }); 172 173 showError(!valid); 174 175 if (firstInvalid) { 176 firstInvalid.focus(); 177 } 178 179 return valid; 180 } 181 182 function openModal(url) { 183 pendingPdfUrl = url; 184 185 // éä¿¡å®äºå¾ã«è¡¨ç¤ºãããPDFãéãããã¿ã³ãæºå 186 if (fallback) { 187 fallback.hidden = true; 188 } 189 if (fallbackLink) { 190 fallbackLink.setAttribute("href", url); 191 } 192 193 exitConfirm(); 194 showError(false); 195 modal.classList.remove("is-sent"); 196 modal.classList.add("is-open"); 197 modal.setAttribute("aria-hidden", "false"); 198 document.body.classList.add("pdf-gate-open"); 199 } 200 201 function closeModal() { 202 modal.classList.remove("is-open"); 203 modal.setAttribute("aria-hidden", "true"); 204 document.body.classList.remove("pdf-gate-open"); 205 exitConfirm(); 206 } 207 208 // ã²ã¼ã対象ãªã³ã¯ã®ã¯ãªã㯠209 onGateClick(function (url) { 210 if (hasCookie()) { 211 openPdf(url); 212 return; 213 } 214 215 openModal(url); 216 }); 217 218 // ã¢ã¼ãã«å ã®ã¯ãªãã¯ï¼éããã»ç¢ºèªã¸é²ãã»æ»ãï¼ 219 modal.addEventListener("click", function (e) { 220 if (e.target.closest(".js-pdf-gate-close")) { 221 e.preventDefault(); 222 closeModal(); 223 return; 224 } 225 226 if (e.target.closest(".pdf-gate-confirm-btn")) { 227 e.preventDefault(); 228 if (validateForm()) { 229 enterConfirm(); 230 } 231 return; 232 } 233 234 if (e.target.closest(".pdf-gate-back-btn")) { 235 e.preventDefault(); 236 exitConfirm(); 237 } 238 }); 239 240 // ESCãã¼ã§éãã 241 document.addEventListener("keydown", function (e) { 242 if (e.key === "Escape" && modal.classList.contains("is-open")) { 243 closeModal(); 244 } 245 }); 246 247 // CF7ããªãã¼ã·ã§ã³ã¨ã©ã¼ã»é信失ææã¯å ¥åã¹ãããã«æ»ãï¼ã¨ã©ã¼è¡¨ç¤ºãè¦ããããï¼ 248 ["wpcf7invalid", "wpcf7spam", "wpcf7mailfailed"].forEach( 249 function (eventName) { 250 document.addEventListener(eventName, function (e) { 251 if (modal.contains(e.target)) { 252 exitConfirm(); 253 } 254 }); 255 }, 256 ); 257 258 // CF7éä¿¡å®äº 259 document.addEventListener("wpcf7mailsent", function (e) { 260 // ãã®ã¢ã¼ãã«å ã®ãã©ã¼ã 以å¤ï¼ãåãåãããã¼ã¸çï¼ã¯å¯¾è±¡å¤ 261 if (!modal.contains(e.target)) { 262 return; 263 } 264 265 setCookie(); 266 exitConfirm(); 267 modal.classList.add("is-sent"); 268 269 // èªåã§ã¯éããããPDFãéãããã¿ã³ããã¦ã¼ã¶ã¼æä½ã§éãã¦ããã 270 if (fallback && pendingPdfUrl) { 271 fallback.hidden = false; 272 } 273 }); 274 } 275 276 if ("loading" === document.readyState) { 277 document.addEventListener("DOMContentLoaded", init); 278 } else { 279 init(); 280 } 281})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.