1(function ($) { 2 "use strict"; 3 4 // added to support accessibility for screen reader users 5 var clickedURL; 6 7 // Check if URL is external function 8 $.fn.isExternal = function () { 9 var host = window.location.hostname; 10 var link = $("<a>", { 11 href: this.attr("href"), 12 })[0].hostname; 13 return link !== host; 14 }; 15 16 // Trapping user in modal for web accessibility 17 function trapFocus() { 18 $(".close-modal").focus(); 19 20 var focusableEls = document.querySelectorAll( 21 '.wp-post-modal a[href]:not([disabled]), .wp-post-modal button:not([disabled]), .wp-post-modal textarea:not([disabled]), .wp-post-modal input[type="text"]:not([disabled]), .wp-post-modal input[type="radio"]:not([disabled]), .wp-post-modal input[type="checkbox"]:not([disabled]), .wp-post-modal select:not([disabled])' 22 ), 23 firstFocusableEl = focusableEls[0], 24 lastFocusableEl = focusableEls[focusableEls.length - 1]; 25 26 document.addEventListener("keydown", function (e) { 27 var isTabPressed = e.key === "Tab" || e.keyCode === 9; 28 29 if (!isTabPressed) return; 30 31 if (e.shiftKey && isTabPressed) { 32 /* shift + tab */ if (document.activeElement === firstFocusableEl) {
33 lastFocusableEl.focus(); 34 e.preventDefault(); 35 } 36 } else if (isTabPressed) { 37 /* tab */ if (document.activeElement === lastFocusableEl) { 38 firstFocusableEl.focus(); 39 e.preventDefault(); 40 } 41 } 42 }); 43 } 44 45 // Check if modal is open 46 function popupOpen() { 47 return $(".modal-wrapper").hasClass("show"); 48 } 49 50 // Basename function for JS 51 function basename(path, suffix) { 52 var b = path; 53 var lastChar = b.charAt(b.length - 1); 54 if (lastChar === "/" || lastChar === "\\") { 55 b = b.slice(0, -1); 56 } 57 b = b.replace(/^.*[/\\]/g, ""); 58 if ( 59 typeof suffix === "string" && 60 b.substr(b.length - suffix.length) === suffix 61 ) { 62 b = b.substr(0, b.length - suffix.length); 63 } 64 return b; 65 } 66 67 // Get URL Paramenters 68 var getUrlParameter = function getUrlParameter(sParam) { 69 var sPageURL = decodeURIComponent(window.location.search.substring(1)), 70 sURLVariables = sPageURL.split("&"), 71 sParameterName, 72 i; 73 74 for (i = 0; i < sURLVariables.length; i++) { 75 sParameterName = sURLVariables[i].split("="); 76 77 if (sParameterName[0] === sParam) { 78 return sParameterName[1] === undefined ? true : sParameterName[1]; 79 } 80 } 81 }; 82 83 // Suppress modal link redirect in WP Customizer 84 function modalCustomizer() { 85 if (typeof wp.customize !== "undefined") { 86 var body = $("body"); 87 body.off("click.preview"); 88 89 body.on( 90 "click.preview", 91 `a[href]:not(.${fromPHP.modalLinkClass})`, 92 function (e) { 93 var link = $(this); 94 e.preventDefault(); 95 wp.customize.preview.send("scroll", 0); 96 wp.customize.preview.send("url", link.prop("href")); 97 } 98 ); 99 } 100 } 101 102 // Document Ready 103 $(function () { 104 // Detect windows width function 105 var $window = $(window), 106 $document = $(document), 107 scrollPos, 108 currentURL = window.location.pathname, 109 disablePopup = !!window.MSInputMethodContext && !!document.documentMode; 110 111 // Show modal functionality 112 function showModal(postLink, external) { 113 scrollPos = window.pageYOffset; 114 if (fromPHP.disableScrolling) $("body, html").addClass("no-scroll"); 115 $(".modal-wrapper").addClass("show"); 116 $(".wp-post-modal").addClass("show"); 117 118 // trap focus inside modal 119 setTimeout(function () { 120 trapFocus(); 121 }, 1000); 122 123 // update address bar to show url of popup content page 124 if (postLink) { 125 if (postLink.length > 0 && !external) { 126 history.replaceState("", "", postLink); 127 } 128 } 129 } 130 131 // Close modal functionality 132 function hideModal(currentURL) { 133 var body = $("body"); 134 135 // handle scrolling 136 if (body.hasClass("no-scroll")) { 137 body.removeClass("no-scroll"); 138 $("html").removeClass("no-scroll"); 139 window.scroll(0, scrollPos); 140 } 141 142 // hide popup 143 $(".modal-wrapper").removeClass("show").hide(); 144 $(".wp-post-modal").removeClass("show"); 145 $("#modal-content").empty(); 146 147 // return to previous tab location for screen reader users 148 if (clickedURL) clickedURL.focus(); 149 150 // return original page url in address bar 151 if (window.location.pathname !== currentURL) { 152 history.replaceState("", "", currentURL); 153 } 154 } 155 156 // close modal 157 $document 158 .keyup(function (e) { 159 // close modal when pressing esc 160 if ( 161 e.keyCode === 27 && 162 $(".modal-wrapper").hasClass("show") && 163 popupOpen() 164 ) 165 hideModal(currentURL); 166 }) 167 // when clicking anywhere on page 168 .on("click", function (e) { 169 if (popupOpen()) { 170 const currentTargetIsLink = 171 e.target instanceof HTMLAnchorElement || 172 e.target.parentNode.className.indexOf(fromPHP.modalLinkClass) >= 173 0 || 174 (e.originalEvent.path && 175 e.originalEvent.path[1].className === fromPHP.modalLinkClass); 176 177 if (!currentTargetIsLink) hideModal(currentURL); 178 } 179 }) 180 // Close modal when clicking on close button 181 .on("click", ".close-modal", function () { 182 if (popupOpen()) hideModal(currentURL); 183 }) 184 // when clicking inside of modal don't close 185 .on("click", ".wp-post-modal, .modal-content", function (e) { 186 e.stopPropagation(); 187 }); 188 189 // Initialize entire modal functionality 190 function initModal() { 191 // if the window is greater than breakpoint then show modal, otherwise go to linked page as normal 192 if ($window.width() >= fromPHP.breakpoint) { 193 var modalUrl = getUrlParameter(fromPHP.modalLinkClass); 194 195 // if using URL parameter to open modal 196 if (modalUrl) { 197 // show loading animation if styling is turned on 198 if (fromPHP.loader) {
199 $("#modal-content").html( 200 '<img class="loading" src="' + 201 fromPHP.pluginUrl + 202 '/images/loading.gif" />' 203 ); 204 } 205 206 $.get(modalUrl, function (html) { 207 var htmlContent = 208 html.indexOf("<html") > -1 209 ? $(html).find(fromPHP.containerID).html() 210 : html; 211 212 $("#modal-content").html(htmlContent); 213 }); 214 215 // show modal 216 $(".modal-wrapper").fadeIn("fast", showModal); 217 } 218 219 // When clicking a modal-link 220 $("body").on("click", `.${fromPHP.modalLinkClass}`, function (e) { 221 // Define variables 222 var modalContent = $("#modal-content"); 223 var $this = 224 $(this).attr("href") !== undefined 225 ? $(this) 226 : $(this).find("a").first(); 227 var postLink = $this.attr("href"); 228 var postSlug = 229 postLink.lastIndexOf("/#") > -1 230 ? basename(postLink.substring(0, postLink.lastIndexOf("/#"))) + 231 basename(postLink) 232 : basename(postLink); 233 var postAnchor = 234 postSlug.lastIndexOf("#") !== -1 235 ? postSlug.substring(postSlug.lastIndexOf("#")) 236 : false; 237 var dataDivID = $this.attr("data-div") 238 ? "#" + $this.attr("data-div") 239 : fromPHP.containerID; 240 var dataBuddypress = $this.attr("data-buddypress"); 241 var loader = 242 '<img class="loading" src="' + 243 fromPHP.pluginUrl + 244 '/images/loading.gif" />'; 245 clickedURL = document.activeElement; 246 247 // prevent link from being followed 248 e.preventDefault(); 249 250 // display loading animation or in this case static content 251 if (fromPHP.loader) { 252 modalContent.html(loader); 253 } 254 255 // Load content from external 256 if ($this.isExternal()) { 257 // load external content using iframe method 258 if ($(this).hasClass("iframe") || fromPHP.iframe) { 259 var iframeCode = 260 '<iframe src="' + 261 $(this).attr("href") + 262 '" width="100%"' + 263 ' height="600px" frameborder="0"></iframe>'; 264 modalContent.html(iframeCode); 265 } 266 // load external content normally 267 else { 268 $.ajaxPrefilter(function (options) { 269 if (options.crossDomain && jQuery.support.cors) { 270 var http = 271 window.location.protocol === "http:" ? "http:" : "https:"; 272 options.url = 273 http + "//cors-anywhere.herokuapp.com/" + options.url; 274 //options.url = "http://cors.corsproxy.io/url=" + options.url; 275 } 276 }); 277 278 $.get(postLink, function (html) { 279 modalContent.html($(html).find(dataDivID).html()); 280 }); 281 } 282 } 283 // Load content from internal page 284 else { 285 // when loading buddy press profile 286 if (dataBuddypress) { 287 modalContent.load(postLink + " #buddypress"); 288 } 289 // load internal content using iframe method 290 else if ($(this).hasClass("iframe")) { 291 var iframeCode = 292 '<iframe src="' + 293 $(this).attr("href") + 294 '" width="100%"' + 295 ' height="600px" frameborder="0"></iframe>'; 296 modalContent.html(iframeCode); 297 } 298 // when loading any other type of content 299 else { 300 // use the rest method 301 if (fromPHP.restMethod || $(this).hasClass("rest")) { 302 $.get( 303 fromPHP.siteUrl + 304 "/wp-json/wp-post-modal/v1/any-post-type?slug=" + 305 postSlug, 306 function (response) { 307 $.when(modalContent.html(response.post_content)).done( 308 function () { 309 // scroll to anchor 310 setTimeout(function () { 311 if (postAnchor) { 312 $(".modal-wrapper").animate( 313 { 314 scrollTop: $( 315 "#modal-content " + postAnchor 316 ).offset().top, 317 }, 318 300 319 ); 320 } 321 }, 200); 322 } 323 ); 324 } 325 ); 326 } 327 // use the default method 328 else { 329 $.get(postLink, function (html) { 330 var content = $(html).find(dataDivID), 331 htmlContent = 332 html.indexOf("<html") > -1 333 ? $(html).find(dataDivID).html() 334 : html; 335 336 if (content[0]) { 337 $.when(modalContent.html(htmlContent)).done(function () { 338 // scroll to anchor 339 setTimeout(function () { 340 if (postAnchor) { 341 $(".modal-wrapper").animate( 342 { 343 scrollTop: $( 344 "#modal-content " + postAnchor 345 ).offset().top, 346 }, 347 300 348 ); 349 } 350 }, 200); 351 }); 352 } 353 // fallback to load method 354 else { 355 modalContent.load(postLink, function () { 356 modalContent.html( 357 $(modalContent.html()).find(dataDivID).html() 358 ); 359 360 setTimeout(function () { 361 if (postAnchor) { 362 $(".modal-wrapper").animate( 363 { 364 scrollTop: $( 365 "#modal-content " + postAnchor 366 ).offset().top, 367 }, 368 300 369 ); 370 } 371 }, 200); 372 }); 373 } 374 }); 375 } 376 } 377 } 378 379 // show modal 380 $(".modal-wrapper").fadeIn("fast", function () { 381 // if url state plugin setting is active 382 showModal(fromPHP.urlState ? postLink : "", $this.isExternal()); 383 }); 384 }); 385 } 386 } 387 388 // Initiate modal if not IE11 389 if (!disablePopup) initModal(); 390 }); 391 392 // Window load 393 $(window).on("load", function () { 394 if (fromPHP.customizing) modalCustomizer(); 395 }); 396})(jQuery);
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.