| @@ -1,14 +1,378 @@ | ||
| 1 | -jQuery(document).ready(function () { | |
| 2 | - if (jQuery('.e2pdf-download.e2pdf-auto').length > 0) { | |
| 3 | - jQuery('.e2pdf-download.e2pdf-auto').each(function () { | |
| 4 | - if (jQuery(this).hasClass('e2pdf-inline')) { | |
| 5 | - window.open(jQuery(this).attr('href'), '_blank'); | |
| 1 | +var e2pdfViewer = { | |
| 2 | + updateViewArea: function (pdfIframe, listener) { | |
| 3 | + if (pdfIframe.hasClass('e2pdf-pages-loaded') && pdfIframe.hasClass('e2pdf-responsive')) { | |
| 4 | + var pdfIframeContents = pdfIframe.contents(); | |
| 5 | + var viewerHeight = parseInt(pdfIframeContents.find('#viewer').outerHeight()); | |
| 6 | + if (pdfIframe.hasClass('e2pdf-responsive-page')) { | |
| 7 | + viewerHeight = parseInt(pdfIframeContents.find('#viewer .page').first().outerHeight()); | |
| 8 | + } | |
| 9 | + var viewerContainerTop = parseInt(pdfIframeContents.find('#viewerContainer').offset().top); | |
| 10 | + pdfIframe.innerHeight(viewerHeight + viewerContainerTop + 2); | |
| 11 | + if (!pdfIframe.hasClass('e2pdf-responsive-page')) { | |
| 12 | + pdfIframeContents.find('#viewerContainer').scrollTop(0); | |
| 13 | + } | |
| 14 | + } | |
| 15 | + if (listener == 'pagesloaded') { | |
| 16 | + var pdfIframeContents = pdfIframe.contents(); | |
| 17 | + pdfIframeContents.find('#viewerContainer').scrollTop(0); | |
| 18 | + } | |
| 19 | + }, | |
| 20 | + viewSinglePageSwitch: function (pdfIframe, page) { | |
| 21 | + if (pdfIframe.hasClass('e2pdf-single-page-mode') && pdfIframe.hasClass('e2pdf-responsive')) { | |
| 22 | + var page = parseInt(page); | |
| 23 | + if (page) { | |
| 24 | + var pdfIframeContents = pdfIframe.contents(); | |
| 25 | + pdfIframeContents.find('.page').not('.page[data-page-number="' + page + '"]').css({'position': 'absolute', 'visibility': 'hidden', 'z-index': '-1'}); | |
| 26 | + pdfIframeContents.find('.page[data-page-number="' + page + '"]').css({'position': 'relative', 'visibility': '', 'z-index': ''}); | |
| 27 | + } | |
| 28 | + } | |
| 29 | + }, | |
| 30 | + iframeLoad: function (iframe) { | |
| 31 | + var pdfIframe = jQuery(iframe); | |
| 32 | + if (!pdfIframe.hasClass('e2pdf-preload')) { | |
| 33 | + var pdfIframeContents = pdfIframe.contents(); | |
| 34 | + pdfIframe.addClass('e2pdf-view-loaded'); | |
| 35 | + pdfIframeContents.find('html').addClass('e2pdf-view-loaded'); | |
| 36 | + if (iframe.contentWindow && iframe.contentWindow.PDFViewerApplication) { | |
| 37 | + var PDFViewerApplication = iframe.contentWindow.PDFViewerApplication; | |
| 38 | + if (pdfIframe.attr('cursor')) { | |
| 39 | + PDFViewerApplication.pdfCursorTools.switchTool(parseInt(pdfIframe.attr('cursor'))); | |
| 40 | + } | |
| 41 | + if (iframe.contentWindow.PDFViewerApplicationOptions) { | |
| 42 | + if (pdfIframe.attr('resolution')) { | |
| 43 | + iframe.contentWindow.PDFViewerApplicationOptions.set('printResolution', parseInt(pdfIframe.attr('resolution'))); | |
| 44 | + } | |
| 45 | + if (pdfIframe.attr('scroll')) { | |
| 46 | + iframe.contentWindow.PDFViewerApplicationOptions.set('scrollModeOnLoad', parseInt(pdfIframe.attr('scroll'))); | |
| 47 | + } | |
| 48 | + if (pdfIframe.attr('spread')) { | |
| 49 | + iframe.contentWindow.PDFViewerApplicationOptions.set('spreadModeOnLoad', parseInt(pdfIframe.attr('spread'))); | |
| 50 | + } | |
| 51 | + } | |
| 52 | + PDFViewerApplication.initializedPromise.then(function () { | |
| 53 | + PDFViewerApplication.eventBus.on('pagesloaded', function (event) { | |
| 54 | + pdfIframe.addClass('e2pdf-pages-loaded'); | |
| 55 | + pdfIframeContents.find('html').addClass('e2pdf-pages-loaded'); | |
| 56 | + e2pdfViewer.viewSinglePageSwitch(pdfIframe, 1); | |
| 57 | + e2pdfViewer.updateViewArea(pdfIframe, 'pagesloaded'); | |
| 58 | + }); | |
| 59 | + PDFViewerApplication.eventBus.on('pagechanging', function (event) { | |
| 60 | + if (event && event.pageNumber) { | |
| 61 | + e2pdfViewer.viewSinglePageSwitch(pdfIframe, event.pageNumber); | |
| 62 | + e2pdfViewer.updateViewArea(pdfIframe, 'pagechanging'); | |
| 63 | + } | |
| 64 | + }); | |
| 65 | + var title = document.title; | |
| 66 | + PDFViewerApplication.eventBus.on('beforeprint', function (event) { | |
| 67 | + if (PDFViewerApplication.printService) { | |
| 68 | + var pdfTitle; | |
| 69 | + var metadataTitle = PDFViewerApplication.metadata && PDFViewerApplication.metadata.get("dc:title"); | |
| 70 | + if (metadataTitle) { | |
| 71 | + if (metadataTitle !== "Untitled" && !/[\uFFF0-\uFFFF]/g.test(metadataTitle)) { | |
| 72 | + pdfTitle = metadataTitle; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + if (pdfTitle) { | |
| 76 | + document.title = pdfTitle; | |
| 77 | + } else if (PDFViewerApplication.contentDispositionFilename) { | |
| 78 | + document.title = PDFViewerApplication.contentDispositionFilename; | |
| 79 | + } | |
| 80 | + } | |
| 81 | + }); | |
| 82 | + PDFViewerApplication.eventBus.on('afterprint', function (event) { | |
| 83 | + document.title = title; | |
| 84 | + }); | |
| 85 | + var listeners = [ | |
| 86 | + 'scalechanging', | |
| 87 | + 'scalechanged', | |
| 88 | + 'rotationchanging', | |
| 89 | + 'updateviewarea', | |
| 90 | + 'scrollmodechanged', | |
| 91 | + 'spreadmodechanged', | |
| 92 | + 'pagechanging', | |
| 93 | + 'zoomin', | |
| 94 | + 'zoomout', | |
| 95 | + 'zoomreset', | |
| 96 | + 'nextpage', | |
| 97 | + 'previouspage' | |
| 98 | + ]; | |
| 99 | + listeners.forEach(function (listener) { | |
| 100 | + PDFViewerApplication.eventBus.on(listener, function (event) { | |
| 101 | + e2pdfViewer.updateViewArea(pdfIframe, listener); | |
| 102 | + }); | |
| 103 | + }); | |
| 104 | + }); | |
| 6 | 105 | } else { |
| 7 | - location.href = jQuery(this).attr('href'); | |
| 106 | + pdfIframeContents[0].addEventListener('pagesloaded', function (event) { | |
| 107 | + pdfIframe.addClass('e2pdf-pages-loaded'); | |
| 108 | + pdfIframeContents.find('html').addClass('e2pdf-pages-loaded'); | |
| 109 | + e2pdfViewer.viewSinglePageSwitch(pdfIframe, 1); | |
| 110 | + e2pdfViewer.updateViewArea(pdfIframe, 'pagesloaded'); | |
| 111 | + }); | |
| 112 | + pdfIframeContents[0].addEventListener('pagechanging', function (event) { | |
| 113 | + if (event && event.detail && event.detail.pageNumber) { | |
| 114 | + e2pdfViewer.viewSinglePageSwitch(pdfIframe, event.detail.pageNumber); | |
| 115 | + e2pdfViewer.updateViewArea(pdfIframe, 'pagechanging'); | |
| 116 | + } | |
| 117 | + }); | |
| 118 | + var listeners = [ | |
| 119 | + 'scalechanging', | |
| 120 | + 'scalechanged', | |
| 121 | + 'rotationchanging', | |
| 122 | + 'updateviewarea', | |
| 123 | + 'scrollmodechanged', | |
| 124 | + 'spreadmodechanged', | |
| 125 | + 'pagechanging', | |
| 126 | + 'zoomin', | |
| 127 | + 'zoomout', | |
| 128 | + 'zoomreset', | |
| 129 | + 'nextpage', | |
| 130 | + 'previouspage' | |
| 131 | + ]; | |
| 132 | + listeners.forEach(function (listener) { | |
| 133 | + pdfIframeContents[0].addEventListener(listener, function (event) { | |
| 134 | + e2pdfViewer.updateViewArea(pdfIframe, listener); | |
| 135 | + }); | |
| 136 | + }); | |
| 8 | 137 | } |
| 138 | + } | |
| 139 | + }, | |
| 140 | + imageLoad: function (image) { | |
| 141 | + var img = jQuery(image); | |
| 142 | + var preload = img.attr('preload'); | |
| 143 | + if (preload) { | |
| 144 | + img.removeClass('e2pdf-preload') | |
| 145 | + img.removeAttr('preload'); | |
| 146 | + img.attr('src', preload); | |
| 147 | + } | |
| 148 | + }, | |
| 149 | + print: { | |
| 150 | + browser: { | |
| 151 | + isFirefox: function () { | |
| 152 | + return typeof InstallTrigger !== 'undefined'; | |
| 153 | + }, | |
| 154 | + isIE: function () { | |
| 155 | + return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode; | |
| 156 | + }, | |
| 157 | + isEdge: function () { | |
| 158 | + return !e2pdfViewer.print.browser.isIE() && !!window.StyleMedia; | |
| 159 | + } | |
| 160 | + }, | |
| 161 | + iframeSupport: function () { | |
| 162 | + if (navigator.userAgent.indexOf('Mobile') !== -1) { | |
| 163 | + return false; | |
| 164 | + } | |
| 165 | + return true; | |
| 166 | + }, | |
| 167 | + pdf: function (blobURL) { | |
| 168 | + var iframe = document.createElement('iframe'); | |
| 169 | + if (e2pdfViewer.print.iframeSupport()) { | |
| 170 | + if (e2pdfViewer.print.browser.isFirefox()) { | |
| 171 | + iframe.setAttribute('style', 'width: 1px; height: 100px; position: fixed; left: 0; top: 0; opacity: 0; border-width: 0; margin: 0; padding: 0'); | |
| 172 | + } else { | |
| 173 | + iframe.setAttribute('style', 'visibility: hidden; height: 0; width: 0; position: absolute; border: 0'); | |
| 174 | + } | |
| 175 | + iframe.onload = function () { | |
| 176 | + try { | |
| 177 | + iframe.focus(); | |
| 178 | + if (e2pdfViewer.print.browser.isEdge() || e2pdfViewer.print.browser.isIE()) { | |
| 179 | + try { | |
| 180 | + iframe.contentWindow.document.execCommand('print', false, null); | |
| 181 | + } catch (e) { | |
| 182 | + iframe.contentWindow.print(); | |
| 183 | + } | |
| 184 | + } else { | |
| 185 | + iframe.contentWindow.print(); | |
| 186 | + } | |
| 187 | + } catch (error) { | |
| 188 | + | |
| 189 | + } finally { | |
| 190 | + if (e2pdfViewer.print.browser.isFirefox()) { | |
| 191 | + iframe.style.visibility = 'hidden'; | |
| 192 | + iframe.style.left = '-1px'; | |
| 193 | + } | |
| 194 | + | |
| 195 | + } | |
| 196 | + }; | |
| 197 | + iframe.setAttribute('src', blobURL); | |
| 198 | + document.getElementsByTagName('body')[0].appendChild(iframe); | |
| 199 | + } | |
| 200 | + } | |
| 201 | + }, | |
| 202 | + autoDownload: function (lid) { | |
| 203 | + jQuery(document).ready(function () { | |
| 204 | + var link = jQuery('a[lid="' + lid + '"]').first(); | |
| 205 | + if (link.length > 0 && !link.hasClass('e2pdf-auto-download-ready')) { | |
| 206 | + link.addClass('e2pdf-auto-download-ready'); | |
| 207 | + link[0].click(); | |
| 208 | + } | |
| 9 | 209 | }); |
| 10 | 210 | } |
| 11 | -}); | |
| 12 | - | |
| 13 | - | |
| 14 | - | |
| 211 | +}; | |
| 212 | +jQuery(document).ready(function () { | |
| 213 | + jQuery(document).on('click', 'a.e2pdf-download-loader', function (e) { | |
| 214 | + var link = jQuery(this); | |
| 215 | + if (!link.hasClass('e2pdf-download-ready')) { | |
| 216 | + e.preventDefault(); | |
| 217 | + } | |
| 218 | + var linkURL = link.attr('href'); | |
| 219 | + if (!link.hasClass('e2pdf-download-progress')) { | |
| 220 | + link.addClass('e2pdf-download-progress'); | |
| 221 | + fetch(link.attr('href'), { | |
| 222 | + method: 'GET', | |
| 223 | + headers: { | |
| 224 | + 'X-E2PDF-REQUEST': 'true' | |
| 225 | + } | |
| 226 | + }).then(resp => { | |
| 227 | + if (resp.ok) { | |
| 228 | + return resp.blob().then((blob) => { | |
| 229 | + const blobURL = URL.createObjectURL(blob); | |
| 230 | + if (link.hasClass('e2pdf-ios-safari-loader')) { | |
| 231 | + window.addEventListener('message', function handleMessage(event) { | |
| 232 | + if (event.data === 'e2pdf-download-ready') { | |
| 233 | + link.removeClass('e2pdf-download-progress'); | |
| 234 | + window.removeEventListener('message', handleMessage); | |
| 235 | + } | |
| 236 | + }); | |
| 237 | + const iframe = document.createElement('iframe'); | |
| 238 | + iframe.style.setProperty('display', 'none', 'important'); | |
| 239 | + document.body.appendChild(iframe); | |
| 240 | + const doc = iframe.contentDocument || iframe.contentWindow.document; | |
| 241 | + doc.open(); | |
| 242 | + doc.write(` | |
| 243 | + <!DOCTYPE html> | |
| 244 | + <html> | |
| 245 | + <body> | |
| 246 | + <a href="${blobURL}" download="${link.attr('download')}"></a> | |
| 247 | + <script> | |
| 248 | + const link = document.querySelector('a'); | |
| 249 | + parent.postMessage('e2pdf-download-ready', '*'); | |
| 250 | + link.click(); | |
| 251 | + </script> | |
| 252 | + </body> | |
| 253 | + </html> | |
| 254 | + `); | |
| 255 | + doc.close(); | |
| 256 | + } else { | |
| 257 | + link.attr('href', blobURL).addClass('e2pdf-download-ready'); | |
| 258 | + link[0].click(); | |
| 259 | + link.attr('href', linkURL).removeClass('e2pdf-download-ready e2pdf-download-progress'); | |
| 260 | + } | |
| 261 | + }); | |
| 262 | + } else { | |
| 263 | + var errorMessage = 'Something went wrong!'; | |
| 264 | + return resp.json().then((json) => { | |
| 265 | + if (json && json.redirect_url) { | |
| 266 | + link.attr('href', linkURL).removeClass('e2pdf-download-ready e2pdf-download-progress'); | |
| 267 | + window.location.href = json.redirect_url; | |
| 268 | + } else { | |
| 269 | + if (json && json.error) { | |
| 270 | + errorMessage = json.error; | |
| 271 | + } | |
| 272 | + throw new Error(errorMessage); | |
| 273 | + } | |
| 274 | + }).catch(() => { | |
| 275 | + throw new Error(errorMessage); | |
| 276 | + }); | |
| 277 | + } | |
| 278 | + }).catch((error) => { | |
| 279 | + link.attr('href', linkURL).removeClass('e2pdf-download-ready e2pdf-download-progress'); | |
| 280 | + alert(error.message || 'Something went wrong!'); | |
| 281 | + }); | |
| 282 | + } | |
| 283 | + }); | |
| 284 | + jQuery(document).on('click', 'a.e2pdf-print-pdf', function (e) { | |
| 285 | + var link = jQuery(this); | |
| 286 | + e.preventDefault(); | |
| 287 | + if (!link.hasClass('e2pdf-download-ready')) { | |
| 288 | + e.preventDefault(); | |
| 289 | + } | |
| 290 | + var linkURL = link.attr('href'); | |
| 291 | + if (!link.hasClass('e2pdf-download-progress')) { | |
| 292 | + link.addClass('e2pdf-download-progress'); | |
| 293 | + fetch(link.attr('href'), { | |
| 294 | + method: 'GET', | |
| 295 | + headers: { | |
| 296 | + 'X-E2PDF-REQUEST': 'true' | |
| 297 | + } | |
| 298 | + }).then(resp => { | |
| 299 | + if (resp.ok) { | |
| 300 | + resp.blob().then((blob) => { | |
| 301 | + const blobURL = URL.createObjectURL(new Blob([blob], {type: 'application/pdf'})); | |
| 302 | + e2pdfViewer.print.pdf(blobURL); | |
| 303 | + link.addClass('e2pdf-download-ready'); | |
| 304 | + link.removeClass('e2pdf-download-ready e2pdf-download-progress'); | |
| 305 | + }); | |
| 306 | + } else { | |
| 307 | + var errorMessage = 'Something went wrong!'; | |
| 308 | + return resp.json().then((json) => { | |
| 309 | + if (json && json.redirect_url) { | |
| 310 | + link.attr('href', linkURL).removeClass('e2pdf-download-ready e2pdf-download-progress'); | |
| 311 | + window.location.href = json.redirect_url; | |
| 312 | + } else { | |
| 313 | + if (json && json.error) { | |
| 314 | + errorMessage = json.error; | |
| 315 | + } | |
| 316 | + throw new Error(errorMessage); | |
| 317 | + } | |
| 318 | + }).catch(() => { | |
| 319 | + throw new Error(errorMessage); | |
| 320 | + }); | |
| 321 | + } | |
| 322 | + }).catch((error) => { | |
| 323 | + link.attr('href', linkURL).removeClass('e2pdf-download-ready e2pdf-download-progress'); | |
| 324 | + alert(error.message || 'Something went wrong!'); | |
| 325 | + }); | |
| 326 | + } | |
| 327 | + }); | |
| 328 | + if (jQuery('.e2pdf-download.e2pdf-auto').not('.e2pdf-iframe-download').length > 0) { | |
| 329 | + jQuery('.e2pdf-download.e2pdf-auto').not('.e2pdf-iframe-download').each(function (i, el) { | |
| 330 | + setTimeout(function () { | |
| 331 | + jQuery(el).click(); | |
| 332 | + }, i * 500); | |
| 333 | + }); | |
| 334 | + } | |
| 335 | + jQuery('.modal').on('show.bs.modal', function () { | |
| 336 | + var modal = jQuery(this); | |
| 337 | + modal.find('iframe.e2pdf-preload, img.e2pdf-preload').each(function () { | |
| 338 | + jQuery(this).removeClass('e2pdf-preload').attr('src', jQuery(this).attr('preload')); | |
| 339 | + }); | |
| 340 | + }); | |
| 341 | + var wpcf = document.querySelector('.wpcf7'); | |
| 342 | + if (wpcf !== null) { | |
| 343 | + wpcf.addEventListener('wpcf7mailsent', function (event) { | |
| 344 | + var message = event.detail.apiResponse.message; | |
| 345 | + if (message && (message.includes('e2pdf-view') || message.includes('e2pdf-download'))) { | |
| 346 | + if (jQuery('.wpcf7-response-output').length > 0) { | |
| 347 | + if (window.MutationObserver) { | |
| 348 | + new MutationObserver((mutationsList, observer) => { | |
| 349 | + for (var mutation of mutationsList) { | |
| 350 | + observer.disconnect(); | |
| 351 | + jQuery('.wpcf7-response-output').html(jQuery('.wpcf7-response-output').text()); | |
| 352 | + } | |
| 353 | + }).observe(jQuery('.wpcf7-response-output')[0], {attributes: false, childList: true, characterData: false}); | |
| 354 | + } else { | |
| 355 | + setTimeout(function () { | |
| 356 | + jQuery('.wpcf7-response-output').html(jQuery('.wpcf7-response-output').text()); | |
| 357 | + }, 500); | |
| 358 | + } | |
| 359 | + } | |
| 360 | + } | |
| 361 | + }, false); | |
| 362 | + } | |
| 363 | + jQuery(document).on('metform/after_submit', function (event, payload) { | |
| 364 | + if (payload && payload.response) { | |
| 365 | + var response = payload.response; | |
| 366 | + if (response.status && response.data && response.data.message) { | |
| 367 | + var message = response.data.message; | |
| 368 | + if (message && (message.includes('e2pdf-view') || message.includes('e2pdf-download'))) { | |
| 369 | + if (jQuery('.mf-main-response-wrap').length > 0 && jQuery('.mf-main-response-wrap p').length > 0) { | |
| 370 | + var response = jQuery('.mf-main-response-wrap').clone(); | |
| 371 | + response.find('p').html(message); | |
| 372 | + jQuery('.mf-main-response-wrap').replaceWith(response); | |
| 373 | + } | |
| 374 | + } | |
| 375 | + } | |
| 376 | + } | |
| 377 | + }); | |
| 378 | +}); | |