UNPKG

dhxmvp

Version:

A complete boilerplate for building online, offline and syncable MVP Single Page Applications using DHTMLX.

836 lines (741 loc) 26.9 kB
function webViewerLoad(evt) { PDFViewerApplication.initialize().then(webViewerInitialized); } function webViewerInitialized() { //#if (GENERIC || B2G) var queryString = document.location.search.substring(1); var params = PDFViewerApplication.parseQueryString(queryString); var file = 'file' in params ? params.file : DEFAULT_URL; //#endif //#if (FIREFOX || MOZCENTRAL) //var file = window.location.href.split('#')[0]; //#endif //#if CHROME //var file = DEFAULT_URL; //#endif //#if GENERIC var fileInput = document.createElement('input'); fileInput.id = 'fileInput'; fileInput.className = 'fileInput'; fileInput.setAttribute('type', 'file'); fileInput.oncontextmenu = noContextMenuHandler; document.body.appendChild(fileInput); if (!window.File || !window.FileReader || !window.FileList || !window.Blob) { document.getElementById('openFile').setAttribute('hidden', 'true'); document.getElementById('secondaryOpenFile').setAttribute('hidden', 'true'); } else { document.getElementById('fileInput').value = null; } //#else //document.getElementById('openFile').setAttribute('hidden', 'true'); //document.getElementById('secondaryOpenFile').setAttribute('hidden', 'true'); //#endif //#if !(FIREFOX || MOZCENTRAL) var locale = PDFJS.locale || navigator.language; //#endif //#if !PRODUCTION if (true) { //#else //if (PDFViewerApplication.preferencePdfBugEnabled) { //#endif // Special debugging flags in the hash section of the URL. var hash = document.location.hash.substring(1); var hashParams = PDFViewerApplication.parseQueryString(hash); if ('disableworker' in hashParams) { PDFJS.disableWorker = (hashParams['disableworker'] === 'true'); } if ('disablerange' in hashParams) { PDFJS.disableRange = (hashParams['disablerange'] === 'true'); } if ('disablestream' in hashParams) { PDFJS.disableStream = (hashParams['disablestream'] === 'true'); } if ('disableautofetch' in hashParams) { PDFJS.disableAutoFetch = (hashParams['disableautofetch'] === 'true'); } if ('disablefontface' in hashParams) { PDFJS.disableFontFace = (hashParams['disablefontface'] === 'true'); } if ('disablehistory' in hashParams) { PDFJS.disableHistory = (hashParams['disablehistory'] === 'true'); } if ('webgl' in hashParams) { PDFJS.disableWebGL = (hashParams['webgl'] !== 'true'); } if ('useonlycsszoom' in hashParams) { PDFJS.useOnlyCssZoom = (hashParams['useonlycsszoom'] === 'true'); } if ('verbosity' in hashParams) { PDFJS.verbosity = hashParams['verbosity'] | 0; } if ('ignorecurrentpositiononzoom' in hashParams) { IGNORE_CURRENT_POSITION_ON_ZOOM = (hashParams['ignorecurrentpositiononzoom'] === 'true'); } //#if !PRODUCTION if ('disablebcmaps' in hashParams && hashParams['disablebcmaps']) { PDFJS.cMapUrl = '../external/cmaps/'; PDFJS.cMapPacked = false; } //#endif //#if !(FIREFOX || MOZCENTRAL) if ('locale' in hashParams) { locale = hashParams['locale']; } //#endif if ('textlayer' in hashParams) { switch (hashParams['textlayer']) { case 'off': PDFJS.disableTextLayer = true; break; case 'visible': case 'shadow': case 'hover': var viewer = document.getElementById('viewer'); viewer.classList.add('textLayer-' + hashParams['textlayer']); break; } } if ('pdfbug' in hashParams) { PDFJS.pdfBug = true; var pdfBug = hashParams['pdfbug']; var enabled = pdfBug.split(','); PDFBug.enable(enabled); PDFBug.init(); } } //#if !(FIREFOX || MOZCENTRAL) mozL10n.setLanguage(locale); //#endif //#if (FIREFOX || MOZCENTRAL) if (!PDFViewerApplication.supportsDocumentFonts) { PDFJS.disableFontFace = true; console.warn(mozL10n.get('web_fonts_disabled', null, 'Web fonts are disabled: unable to use embedded PDF fonts.')); } //#endif if (!PDFViewerApplication.supportsPrinting) { document.getElementById('print').classList.add('hidden'); document.getElementById('secondaryPrint').classList.add('hidden'); } if (!PDFViewerApplication.supportsFullscreen) { document.getElementById('presentationMode').classList.add('hidden'); document.getElementById('secondaryPresentationMode'). classList.add('hidden'); } if (PDFViewerApplication.supportsIntegratedFind) { document.getElementById('viewFind').classList.add('hidden'); } // Listen for unsupported features to trigger the fallback UI. PDFJS.UnsupportedManager.listen( PDFViewerApplication.fallback.bind(PDFViewerApplication)); // Suppress context menus for some controls document.getElementById('scaleSelect').oncontextmenu = noContextMenuHandler; var mainContainer = document.getElementById('mainContainer'); var outerContainer = document.getElementById('outerContainer'); mainContainer.addEventListener('transitionend', function(e) { if (e.target === mainContainer) { var event = document.createEvent('UIEvents'); event.initUIEvent('resize', false, false, window, 0); window.dispatchEvent(event); outerContainer.classList.remove('sidebarMoving'); } }, true); document.getElementById('sidebarToggle').addEventListener('click', function() { this.classList.toggle('toggled'); outerContainer.classList.add('sidebarMoving'); outerContainer.classList.toggle('sidebarOpen'); PDFViewerApplication.sidebarOpen = outerContainer.classList.contains('sidebarOpen'); if (PDFViewerApplication.sidebarOpen) { PDFViewerApplication.refreshThumbnailViewer(); } PDFViewerApplication.forceRendering(); }); document.getElementById('viewThumbnail').addEventListener('click', function() { PDFViewerApplication.switchSidebarView('thumbs'); }); document.getElementById('viewOutline').addEventListener('click', function() { PDFViewerApplication.switchSidebarView('outline'); }); document.getElementById('viewAttachments').addEventListener('click', function() { PDFViewerApplication.switchSidebarView('attachments'); }); document.getElementById('previous').addEventListener('click', function() { PDFViewerApplication.page--; }); document.getElementById('next').addEventListener('click', function() { PDFViewerApplication.page++; }); document.getElementById('zoomIn').addEventListener('click', function() { PDFViewerApplication.zoomIn(); }); document.getElementById('zoomOut').addEventListener('click', function() { PDFViewerApplication.zoomOut(); }); document.getElementById('pageNumber').addEventListener('click', function() { this.select(); }); document.getElementById('pageNumber').addEventListener('change', function() { // Handle the user inputting a floating point number. PDFViewerApplication.page = (this.value | 0); if (this.value !== (this.value | 0).toString()) { this.value = PDFViewerApplication.page; } }); document.getElementById('scaleSelect').addEventListener('change', function() { PDFViewerApplication.setScale(this.value, false); }); document.getElementById('presentationMode').addEventListener('click', SecondaryToolbar.presentationModeClick.bind(SecondaryToolbar)); document.getElementById('openFile').addEventListener('click', SecondaryToolbar.openFileClick.bind(SecondaryToolbar)); document.getElementById('print').addEventListener('click', SecondaryToolbar.printClick.bind(SecondaryToolbar)); document.getElementById('download').addEventListener('click', SecondaryToolbar.downloadClick.bind(SecondaryToolbar)); //#if (FIREFOX || MOZCENTRAL) //PDFViewerApplication.setTitleUsingUrl(file); //PDFViewerApplication.initPassiveLoading(); //return; //#endif //#if GENERIC if (file && file.lastIndexOf('file:', 0) === 0) { // file:-scheme. Load the contents in the main thread because QtWebKit // cannot load file:-URLs in a Web Worker. file:-URLs are usually loaded // very quickly, so there is no need to set up progress event listeners. PDFViewerApplication.setTitleUsingUrl(file); var xhr = new XMLHttpRequest(); xhr.onload = function() { PDFViewerApplication.open(new Uint8Array(xhr.response), 0); }; try { xhr.open('GET', file); xhr.responseType = 'arraybuffer'; xhr.send(); } catch (e) { PDFViewerApplication.error(mozL10n.get('loading_error', null, 'An error occurred while loading the PDF.'), e); } return; } if (file) { PDFViewerApplication.open(file, 0); } //#endif //#if CHROME //if (file) { // ChromeCom.openPDFFile(file); //} //#endif } //document.addEventListener('DOMContentLoaded', webViewerLoad, true); document.addEventListener('pagerendered', function (e) { var pageNumber = e.detail.pageNumber; var pageIndex = pageNumber - 1; var pageView = PDFViewerApplication.pdfViewer.getPageView(pageIndex); if (PDFViewerApplication.sidebarOpen) { var thumbnailView = PDFViewerApplication.pdfThumbnailViewer. getThumbnail(pageIndex); thumbnailView.setImage(pageView); } if (PDFJS.pdfBug && Stats.enabled && pageView.stats) { Stats.add(pageNumber, pageView.stats); } if (pageView.error) { PDFViewerApplication.error(mozL10n.get('rendering_error', null, 'An error occurred while rendering the page.'), pageView.error); } // If the page is still visible when it has finished rendering, // ensure that the page number input loading indicator is hidden. if (pageNumber === PDFViewerApplication.page) { var pageNumberInput = document.getElementById('pageNumber'); pageNumberInput.classList.remove(PAGE_NUMBER_LOADING_INDICATOR); } //#if !PRODUCTION if (true) { return; } //#endif //#if (FIREFOX || MOZCENTRAL) FirefoxCom.request('reportTelemetry', JSON.stringify({ type: 'pageInfo' })); // It is a good time to report stream and font types. PDFViewerApplication.pdfDocument.getStats().then(function (stats) { FirefoxCom.request('reportTelemetry', JSON.stringify({ type: 'documentStats', stats: stats })); }); //#endif }, true); document.addEventListener('textlayerrendered', function (e) { var pageIndex = e.detail.pageNumber - 1; var pageView = PDFViewerApplication.pdfViewer.getPageView(pageIndex); //#if !PRODUCTION if (true) { return; } //#endif //#if (FIREFOX || MOZCENTRAL) if (pageView.textLayer && pageView.textLayer.textDivs && pageView.textLayer.textDivs.length > 0 && !PDFViewerApplication.supportsDocumentColors) { console.error(mozL10n.get('document_colors_disabled', null, 'PDF documents are not allowed to use their own colors: ' + '\'Allow pages to choose their own colors\' ' + 'is deactivated in the browser.')); PDFViewerApplication.fallback(); } //#endif }, true); window.addEventListener('presentationmodechanged', function (e) { var active = e.detail.active; var switchInProgress = e.detail.switchInProgress; PDFViewerApplication.pdfViewer.presentationModeState = switchInProgress ? PresentationModeState.CHANGING : active ? PresentationModeState.FULLSCREEN : PresentationModeState.NORMAL; }); function updateViewarea() { if (!PDFViewerApplication.initialized) { return; } PDFViewerApplication.pdfViewer.update(); } window.addEventListener('updateviewarea', function (evt) { if (!PDFViewerApplication.initialized) { return; } var location = evt.location; PDFViewerApplication.store.initializedPromise.then(function() { PDFViewerApplication.store.setMultiple({ 'exists': true, 'page': location.pageNumber, 'zoom': location.scale, 'scrollLeft': location.left, 'scrollTop': location.top }).catch(function() { // unable to write to storage }); }); var href = PDFViewerApplication.getAnchorUrl(location.pdfOpenParams); document.getElementById('viewBookmark').href = href; document.getElementById('secondaryViewBookmark').href = href; // Update the current bookmark in the browsing history. PDFHistory.updateCurrentBookmark(location.pdfOpenParams, location.pageNumber); // Show/hide the loading indicator in the page number input element. var pageNumberInput = document.getElementById('pageNumber'); var currentPage = PDFViewerApplication.pdfViewer.getPageView(PDFViewerApplication.page - 1); if (currentPage.renderingState === RenderingStates.FINISHED) { pageNumberInput.classList.remove(PAGE_NUMBER_LOADING_INDICATOR); } else { pageNumberInput.classList.add(PAGE_NUMBER_LOADING_INDICATOR); } }, true); window.addEventListener('resize', function webViewerResize(evt) { if (PDFViewerApplication.initialized && (document.getElementById('pageAutoOption').selected || /* Note: the scale is constant for |pageActualOption|. */ document.getElementById('pageFitOption').selected || document.getElementById('pageWidthOption').selected)) { var selectedScale = document.getElementById('scaleSelect').value; PDFViewerApplication.setScale(selectedScale, false); } updateViewarea(); // Set the 'max-height' CSS property of the secondary toolbar. SecondaryToolbar.setMaxHeight(document.getElementById('viewerContainer')); }); window.addEventListener('hashchange', function webViewerHashchange(evt) { if (PDFHistory.isHashChangeUnlocked) { PDFViewerApplication.setHash(document.location.hash.substring(1)); } }); //#if GENERIC window.addEventListener('change', function webViewerChange(evt) { var files = evt.target.files; if (!files || files.length === 0) { return; } var file = files[0]; if (!PDFJS.disableCreateObjectURL && typeof URL !== 'undefined' && URL.createObjectURL) { PDFViewerApplication.open(URL.createObjectURL(file), 0); } else { // Read the local file into a Uint8Array. var fileReader = new FileReader(); fileReader.onload = function webViewerChangeFileReaderOnload(evt) { var buffer = evt.target.result; var uint8Array = new Uint8Array(buffer); PDFViewerApplication.open(uint8Array, 0); }; fileReader.readAsArrayBuffer(file); } PDFViewerApplication.setTitleUsingUrl(file.name); // URL does not reflect proper document location - hiding some icons. document.getElementById('viewBookmark').setAttribute('hidden', 'true'); document.getElementById('secondaryViewBookmark'). setAttribute('hidden', 'true'); document.getElementById('download').setAttribute('hidden', 'true'); document.getElementById('secondaryDownload').setAttribute('hidden', 'true'); }, true); //#endif function selectScaleOption(value) { var options = document.getElementById('scaleSelect').options; var predefinedValueFound = false; for (var i = 0; i < options.length; i++) { var option = options[i]; if (option.value !== value) { option.selected = false; continue; } option.selected = true; predefinedValueFound = true; } return predefinedValueFound; } window.addEventListener('localized', function localized(evt) { document.getElementsByTagName('html')[0].dir = mozL10n.getDirection(); PDFViewerApplication.animationStartedPromise.then(function() { // Adjust the width of the zoom box to fit the content. // Note: If the window is narrow enough that the zoom box is not visible, // we temporarily show it to be able to adjust its width. var container = document.getElementById('scaleSelectContainer'); if (container.clientWidth === 0) { container.setAttribute('style', 'display: inherit;'); } if (container.clientWidth > 0) { var select = document.getElementById('scaleSelect'); select.setAttribute('style', 'min-width: inherit;'); var width = select.clientWidth + SCALE_SELECT_CONTAINER_PADDING; select.setAttribute('style', 'min-width: ' + (width + SCALE_SELECT_PADDING) + 'px;'); container.setAttribute('style', 'min-width: ' + width + 'px; ' + 'max-width: ' + width + 'px;'); } // Set the 'max-height' CSS property of the secondary toolbar. SecondaryToolbar.setMaxHeight(document.getElementById('viewerContainer')); }); }, true); window.addEventListener('scalechange', function scalechange(evt) { document.getElementById('zoomOut').disabled = (evt.scale === MIN_SCALE); document.getElementById('zoomIn').disabled = (evt.scale === MAX_SCALE); var customScaleOption = document.getElementById('customScaleOption'); customScaleOption.selected = false; if (!PDFViewerApplication.updateScaleControls && (document.getElementById('pageAutoOption').selected || document.getElementById('pageActualOption').selected || document.getElementById('pageFitOption').selected || document.getElementById('pageWidthOption').selected)) { updateViewarea(); return; } if (evt.presetValue) { selectScaleOption(evt.presetValue); updateViewarea(); return; } var predefinedValueFound = selectScaleOption('' + evt.scale); if (!predefinedValueFound) { var customScale = Math.round(evt.scale * 10000) / 100; customScaleOption.textContent = mozL10n.get('page_scale_percent', { scale: customScale }, '{{scale}}%'); customScaleOption.selected = true; } updateViewarea(); }, true); window.addEventListener('pagechange', function pagechange(evt) { var page = evt.pageNumber; if (evt.previousPageNumber !== page) { document.getElementById('pageNumber').value = page; if (PDFViewerApplication.sidebarOpen) { PDFViewerApplication.pdfThumbnailViewer.scrollThumbnailIntoView(page); } } var numPages = PDFViewerApplication.pagesCount; document.getElementById('previous').disabled = (page <= 1); document.getElementById('next').disabled = (page >= numPages); document.getElementById('firstPage').disabled = (page <= 1); document.getElementById('lastPage').disabled = (page >= numPages); // we need to update stats if (PDFJS.pdfBug && Stats.enabled) { var pageView = PDFViewerApplication.pdfViewer.getPageView(page - 1); if (pageView.stats) { Stats.add(page, pageView.stats); } } // checking if the this.page was called from the updateViewarea function if (evt.updateInProgress) { return; } // Avoid scrolling the first page during loading if (this.loading && page === 1) { return; } PDFViewerApplication.pdfViewer.scrollPageIntoView(page); }, true); function handleMouseWheel(evt) { var MOUSE_WHEEL_DELTA_FACTOR = 40; var ticks = (evt.type === 'DOMMouseScroll') ? -evt.detail : evt.wheelDelta / MOUSE_WHEEL_DELTA_FACTOR; var direction = (ticks < 0) ? 'zoomOut' : 'zoomIn'; if (PDFViewerApplication.pdfViewer.isInPresentationMode) { evt.preventDefault(); PDFViewerApplication.scrollPresentationMode(ticks * MOUSE_WHEEL_DELTA_FACTOR); } else if (evt.ctrlKey || evt.metaKey) { // Only zoom the pages, not the entire viewer. evt.preventDefault(); PDFViewerApplication[direction](Math.abs(ticks)); } } window.addEventListener('DOMMouseScroll', handleMouseWheel); window.addEventListener('mousewheel', handleMouseWheel); window.addEventListener('click', function click(evt) { if (SecondaryToolbar.opened && PDFViewerApplication.pdfViewer.containsElement(evt.target)) { SecondaryToolbar.close(); } }, false); window.addEventListener('keydown', function keydown(evt) { if (OverlayManager.active) { return; } var handled = false; var cmd = (evt.ctrlKey ? 1 : 0) | (evt.altKey ? 2 : 0) | (evt.shiftKey ? 4 : 0) | (evt.metaKey ? 8 : 0); var pdfViewer = PDFViewerApplication.pdfViewer; var isViewerInPresentationMode = pdfViewer && pdfViewer.isInPresentationMode; // First, handle the key bindings that are independent whether an input // control is selected or not. if (cmd === 1 || cmd === 8 || cmd === 5 || cmd === 12) { // either CTRL or META key with optional SHIFT. switch (evt.keyCode) { case 70: // f if (!PDFViewerApplication.supportsIntegratedFind) { PDFViewerApplication.findBar.open(); handled = true; } break; case 71: // g if (!PDFViewerApplication.supportsIntegratedFind) { PDFViewerApplication.findBar.dispatchEvent('again', cmd === 5 || cmd === 12); handled = true; } break; case 61: // FF/Mac '=' case 107: // FF '+' and '=' case 187: // Chrome '+' case 171: // FF with German keyboard if (!isViewerInPresentationMode) { PDFViewerApplication.zoomIn(); } handled = true; break; case 173: // FF/Mac '-' case 109: // FF '-' case 189: // Chrome '-' if (!isViewerInPresentationMode) { PDFViewerApplication.zoomOut(); } handled = true; break; case 48: // '0' case 96: // '0' on Numpad of Swedish keyboard if (!isViewerInPresentationMode) { // keeping it unhandled (to restore page zoom to 100%) setTimeout(function () { // ... and resetting the scale after browser adjusts its scale PDFViewerApplication.setScale(DEFAULT_SCALE, true); }); handled = false; } break; } } //#if !(FIREFOX || MOZCENTRAL) // CTRL or META without shift if (cmd === 1 || cmd === 8) { switch (evt.keyCode) { case 83: // s PDFViewerApplication.download(); handled = true; break; } } //#endif // CTRL+ALT or Option+Command if (cmd === 3 || cmd === 10) { switch (evt.keyCode) { case 80: // p PDFViewerApplication.requestPresentationMode(); handled = true; break; case 71: // g // focuses input#pageNumber field document.getElementById('pageNumber').select(); handled = true; break; } } if (handled) { evt.preventDefault(); return; } // Some shortcuts should not get handled if a control/input element // is selected. var curElement = document.activeElement || document.querySelector(':focus'); var curElementTagName = curElement && curElement.tagName.toUpperCase(); if (curElementTagName === 'INPUT' || curElementTagName === 'TEXTAREA' || curElementTagName === 'SELECT') { // Make sure that the secondary toolbar is closed when Escape is pressed. if (evt.keyCode !== 27) { // 'Esc' return; } } if (cmd === 0) { // no control key pressed at all. switch (evt.keyCode) { case 38: // up arrow case 33: // pg up case 8: // backspace if (!isViewerInPresentationMode && PDFViewerApplication.currentScaleValue !== 'page-fit') { break; } /* in presentation mode */ /* falls through */ case 37: // left arrow // horizontal scrolling using arrow keys if (pdfViewer.isHorizontalScrollbarEnabled) { break; } /* falls through */ case 75: // 'k' case 80: // 'p' PDFViewerApplication.page--; handled = true; break; case 27: // esc key if (SecondaryToolbar.opened) { SecondaryToolbar.close(); handled = true; } if (!PDFViewerApplication.supportsIntegratedFind && PDFViewerApplication.findBar.opened) { PDFViewerApplication.findBar.close(); handled = true; } break; case 40: // down arrow case 34: // pg down case 32: // spacebar if (!isViewerInPresentationMode && PDFViewerApplication.currentScaleValue !== 'page-fit') { break; } /* falls through */ case 39: // right arrow // horizontal scrolling using arrow keys if (pdfViewer.isHorizontalScrollbarEnabled) { break; } /* falls through */ case 74: // 'j' case 78: // 'n' PDFViewerApplication.page++; handled = true; break; case 36: // home if (isViewerInPresentationMode || PDFViewerApplication.page > 1) { PDFViewerApplication.page = 1; handled = true; } break; case 35: // end if (isViewerInPresentationMode || (PDFViewerApplication.pdfDocument && PDFViewerApplication.page < PDFViewerApplication.pagesCount)) { PDFViewerApplication.page = PDFViewerApplication.pagesCount; handled = true; } break; case 72: // 'h' if (!isViewerInPresentationMode) { HandTool.toggle(); } break; case 82: // 'r' PDFViewerApplication.rotatePages(90); break; } } if (cmd === 4) { // shift-key switch (evt.keyCode) { case 32: // spacebar if (!isViewerInPresentationMode && PDFViewerApplication.currentScaleValue !== 'page-fit') { break; } PDFViewerApplication.page--; handled = true; break; case 82: // 'r' PDFViewerApplication.rotatePages(-90); break; } } if (!handled && !isViewerInPresentationMode) { // 33=Page Up 34=Page Down 35=End 36=Home // 37=Left 38=Up 39=Right 40=Down if (evt.keyCode >= 33 && evt.keyCode <= 40 && !pdfViewer.containsElement(curElement)) { // The page container is not focused, but a page navigation key has been // pressed. Change the focus to the viewer container to make sure that // navigation by keyboard works as expected. pdfViewer.focus(); } // 32=Spacebar if (evt.keyCode === 32 && curElementTagName !== 'BUTTON' && !pdfViewer.containsElement(curElement)) { pdfViewer.focus(); } } if (cmd === 2) { // alt-key switch (evt.keyCode) { case 37: // left arrow if (isViewerInPresentationMode) { PDFHistory.back(); handled = true; } break; case 39: // right arrow if (isViewerInPresentationMode) { PDFHistory.forward(); handled = true; } break; } } if (handled) { evt.preventDefault(); } }); window.addEventListener('beforeprint', function beforePrint(evt) { PDFViewerApplication.beforePrint(); }); window.addEventListener('afterprint', function afterPrint(evt) { PDFViewerApplication.afterPrint(); });