From 9d444b3455df54f401d8306c5fa3110fa5059042 Mon Sep 17 00:00:00 2001 From: Jeremy Press Date: Tue, 9 May 2017 12:56:11 -0700 Subject: [PATCH] Chore: cleaning up DocBaseViewer (#103) --- src/lib/viewers/doc/DocBaseViewer.js | 144 ++++----- .../doc/__tests__/DocBaseViewer-test.js | 294 +++++++++--------- 2 files changed, 219 insertions(+), 219 deletions(-) diff --git a/src/lib/viewers/doc/DocBaseViewer.js b/src/lib/viewers/doc/DocBaseViewer.js index 6e03a128e..d8a3ea886 100644 --- a/src/lib/viewers/doc/DocBaseViewer.js +++ b/src/lib/viewers/doc/DocBaseViewer.js @@ -255,34 +255,6 @@ class DocBaseViewer extends BaseViewer { this.findBar = new DocFindBar(this.findBarEl, this.findController, canDownload); } - /** - * Sets up print notification & prepare PDF for printing. - * - * @return {void} - * @private - */ - initPrint() { - this.printPopup = new Popup(this.containerEl); - - const printCheckmark = document.createElement('div'); - printCheckmark.className = `bp-print-check ${CLASS_HIDDEN}`; - printCheckmark.innerHTML = ICON_PRINT_CHECKMARK.trim(); - - const loadingIndicator = document.createElement('div'); - loadingIndicator.classList.add('bp-crawler'); - loadingIndicator.innerHTML = ` -
-
-
`.trim(); - - this.printPopup.addContent(loadingIndicator, true); - this.printPopup.addContent(printCheckmark, true); - - // Save a reference so they can be hidden or shown later. - this.printPopup.loadingIndicator = loadingIndicator; - this.printPopup.printCheckmark = printCheckmark; - } - /** * Ensures that the print blob is loaded & updates the print UI. * @@ -320,34 +292,6 @@ class DocBaseViewer extends BaseViewer { } } - /** - * Re-sizing logic. - * - * @override - * @return {void} - * @protected - */ - resize() { - if (!this.pdfViewer || !this.pdfViewer.pageViewsReady) { - return; - } - - // Save page and return after resize - const currentPageNumber = this.pdfViewer.currentPageNumber; - - this.pdfViewer.currentScaleValue = this.pdfViewer.currentScaleValue || 'auto'; - this.pdfViewer.update(); - - this.setPage(currentPageNumber); - - // Update annotations scale - if (this.annotator) { - this.annotator.setScale(this.pdfViewer.currentScale); // Set scale to current numerical scale - } - - super.resize(); - } - /** * Go to previous page * @@ -628,6 +572,34 @@ class DocBaseViewer extends BaseViewer { }); } + /** + * Re-sizing logic. + * + * @protected + * @override + * @return {void} + */ + resize() { + if (!this.pdfViewer || !this.pdfViewer.pageViewsReady) { + return; + } + + // Save page and return after resize + const currentPageNumber = this.pdfViewer.currentPageNumber; + + this.pdfViewer.currentScaleValue = this.pdfViewer.currentScaleValue || 'auto'; + this.pdfViewer.update(); + + this.setPage(currentPageNumber); + + // Update annotations scale + if (this.annotator) { + this.annotator.setScale(this.pdfViewer.currentScale); // Set scale to current numerical scale + } + + super.resize(); + } + //-------------------------------------------------------------------------- // Private //-------------------------------------------------------------------------- @@ -676,6 +648,34 @@ class DocBaseViewer extends BaseViewer { PDFJS.maxCanvasPixels = this.isMobile ? MOBILE_MAX_CANVAS_SIZE : PDFJS.maxCanvasPixels; } + /** + * Sets up print notification & prepare PDF for printing. + * + * @private + * @return {void} + */ + initPrint() { + this.printPopup = new Popup(this.containerEl); + + const printCheckmark = document.createElement('div'); + printCheckmark.className = `bp-print-check ${CLASS_HIDDEN}`; + printCheckmark.innerHTML = ICON_PRINT_CHECKMARK.trim(); + + const loadingIndicator = document.createElement('div'); + loadingIndicator.classList.add('bp-crawler'); + loadingIndicator.innerHTML = ` +
+
+
`.trim(); + + this.printPopup.addContent(loadingIndicator, true); + this.printPopup.addContent(printCheckmark, true); + + // Save a reference so they can be hidden or shown later. + this.printPopup.loadingIndicator = loadingIndicator; + this.printPopup.printCheckmark = printCheckmark; + } + /** * Initializes annotations. * @@ -723,8 +723,8 @@ class DocBaseViewer extends BaseViewer { /** * Initializes page number selector. * - * @return {void} * @private + * @return {void} */ initPageNumEl() { const pageNumEl = this.controls.controlsEl.querySelector('.bp-doc-page-num'); @@ -741,9 +741,9 @@ class DocBaseViewer extends BaseViewer { /** * Fetches PDF and converts to blob for printing. * + * @private * @param {string} pdfUrl - URL to PDF * @return {Promise} Promise setting print blob - * @private */ fetchPrintBlob(pdfUrl) { return get(pdfUrl, 'blob').then((blob) => { @@ -754,8 +754,8 @@ class DocBaseViewer extends BaseViewer { /** * Handles logic for printing the PDF representation in browser. * - * @return {void} * @private + * @return {void} */ browserPrint() { // For IE & Edge, use the open or save dialog since we can't open @@ -810,8 +810,8 @@ class DocBaseViewer extends BaseViewer { /** * Creates UI for preview controls. * - * @return {void} * @private + * @return {void} */ loadUI() { this.controls = new Controls(this.containerEl); @@ -822,8 +822,8 @@ class DocBaseViewer extends BaseViewer { /** * Replaces the page number display with an input box that allows the user to type in a page number * - * @return {void} * @private + * @return {void} */ showPageNumInput() { // show the input box with the current page number selected within it @@ -841,8 +841,8 @@ class DocBaseViewer extends BaseViewer { /** * Hide the page number input * - * @return {void} * @private + * @return {void} */ hidePageNumInput() { this.controls.controlsEl.classList.remove(SHOW_PAGE_NUM_INPUT_CLASS); @@ -853,9 +853,9 @@ class DocBaseViewer extends BaseViewer { /** * Update page number in page control widget. * + * @private * @param {number} pageNum - Number of page to update to * @return {void} - * @private */ updateCurrentPage(pageNum) { let truePageNum = pageNum; @@ -886,8 +886,8 @@ class DocBaseViewer extends BaseViewer { /** * Binds DOM listeners for document viewer. * - * @return {void} * @protected + * @return {void} */ bindDOMListeners() { // When page structure is initialized, set default zoom, load controls, @@ -922,8 +922,8 @@ class DocBaseViewer extends BaseViewer { /** * Unbinds DOM listeners for document viewer. * - * @return {void} * @protected + * @return {void} */ unbindDOMListeners() { if (this.docEl) { @@ -951,8 +951,8 @@ class DocBaseViewer extends BaseViewer { /** * Binds listeners for document controls. Overridden. * - * @return {void} * @protected + * @return {void} */ bindControlListeners() {} @@ -977,9 +977,9 @@ class DocBaseViewer extends BaseViewer { /** * Keydown handler for page number input. * + * @private * @param {Event} event - Keydown event * @return {void} - * @private */ pageNumInputKeydownHandler(event) { const key = decodeKeydown(event); @@ -1016,8 +1016,8 @@ class DocBaseViewer extends BaseViewer { /** * Handler for 'pagesinit' event. * - * @return {void} * @private + * @return {void} */ pagesinitHandler() { this.pdfViewer.currentScaleValue = 'auto'; @@ -1044,8 +1044,8 @@ class DocBaseViewer extends BaseViewer { /** * Handler for 'pagerendered' event. * - * @return {void} * @private + * @return {void} */ pagerenderedHandler(event) { const pageNumber = event.detail ? event.detail.pageNumber : undefined; @@ -1066,9 +1066,9 @@ class DocBaseViewer extends BaseViewer { /** * Handler for 'pagechange' event. * + * @private * @param {Event} event - Pagechange event * @return {void} - * @private */ pagechangeHandler(event) { const pageNum = event.pageNumber; @@ -1088,8 +1088,8 @@ class DocBaseViewer extends BaseViewer { * Fullscreen entered handler. Add presentation mode class, set * presentation mode state, and set zoom to fullscreen zoom. * - * @return {void} * @private + * @return {void} */ enterfullscreenHandler() { this.pdfViewer.currentScaleValue = 'page-fit'; @@ -1115,8 +1115,8 @@ class DocBaseViewer extends BaseViewer { /** * Scroll handler. Fires an event on start and stop * - * @return {void} * @private + * @return {void} */ scrollHandler = throttle(() => { // Reset the scroll timer if we are continuing a scroll diff --git a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js index 771183bc9..4823dfc4c 100644 --- a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js +++ b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js @@ -386,100 +386,6 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => { }); }); - describe('initPrint()', () => { - it('should add print checkmark', () => { - docBase.initPrint(); - - const mockCheckmark = document.createElement('div'); - mockCheckmark.innerHTML = `${ICON_PRINT_CHECKMARK}`.trim(); - expect(docBase.printPopup.printCheckmark.innerHTML).to.equal(mockCheckmark.innerHTML); - }); - - it('should hide the print checkmark', () => { - docBase.initPrint(); - - expect(docBase.printPopup.printCheckmark.classList.contains(CLASS_HIDDEN)); - }); - - it('should add the loading indicator', () => { - docBase.initPrint(); - - const mockIndicator = document.createElement('div'); - mockIndicator.innerHTML = ` -
-
-
- `.trim(); - expect(docBase.printPopup.loadingIndicator.innerHTML).to.equal(mockIndicator.innerHTML); - expect(docBase.printPopup.loadingIndicator.classList.contains('bp-crawler')).to.be.true; - }); - }); - - describe('print()', () => { - let clock; - - beforeEach(() => { - clock = sinon.useFakeTimers(); - docBase.printBlob = undefined; - stubs.fetchPrintBlob = sandbox.stub(docBase, 'fetchPrintBlob').returns({ - then: sandbox.stub() - }); - docBase.initPrint(); - stubs.show = sandbox.stub(docBase.printPopup, 'show'); - }); - - afterEach(() => { - clock.restore(); - }); - - it('should request the print blob if it is not ready', () => { - docBase.print(); - expect(stubs.fetchPrintBlob).to.be.called; - }); - - it('should show the print popup and disable the print button if the blob is not ready', () => { - sandbox.stub(docBase.printPopup, 'disableButton'); - - docBase.print(); - clock.tick(PRINT_DIALOG_TIMEOUT_MS + 1); - - expect(stubs.show).to.be.calledWith(__('print_loading'), __('print'), sinon.match.func); - expect(docBase.printPopup.disableButton).to.be.called; - }); - - it('should directly print if print blob is ready and the print dialog hasn\'t been shown yet', () => { - docBase.printBlob = {}; - docBase.printDialogTimeout = setTimeout(() => {}); - sandbox.stub(docBase, 'browserPrint'); - - docBase.print(); - expect(docBase.browserPrint).to.be.called; - }); - - it('should directly print if print blob is ready and the print dialog isn\'t visible', () => { - docBase.printBlob = {}; - docBase.printDialogTimeout = null; - sandbox.stub(docBase.printPopup, 'isVisible').returns(false); - sandbox.stub(docBase, 'browserPrint'); - - docBase.print(); - expect(docBase.browserPrint).to.be.called; - }); - - it('should update the print popup UI if popup is visible and there is no current print timeout', () => { - docBase.printBlob = {}; - - sandbox.stub(docBase.printPopup, 'isVisible').returns(true); - - docBase.print(); - - expect(docBase.printPopup.buttonEl.classList.contains('is-disabled')).to.be.false; - expect(docBase.printPopup.messageEl.textContent).to.equal(__('print_ready')); - expect(docBase.printPopup.loadingIndicator.classList.contains(CLASS_HIDDEN)).to.be.true; - expect(docBase.printPopup.printCheckmark.classList.contains(CLASS_HIDDEN)).to.be.false; - }); - }); - describe('browserPrint()', () => { beforeEach(() => { stubs.emit = sandbox.stub(docBase, 'emit'); @@ -552,59 +458,6 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => { }); }); - describe('resize()', () => { - const resizeFunc = BaseViewer.prototype.resize; - - beforeEach(() => { - docBase.pdfViewer = { - update: sandbox.stub(), - currentScaleValue: 0, - currentPageNumber: 0, - pageViewsReady: true - }; - - stubs.setPage = sandbox.stub(docBase, 'setPage'); - Object.defineProperty(Object.getPrototypeOf(DocBaseViewer.prototype), 'resize', { - value: sandbox.stub() - }); - }); - - afterEach(() => { - Object.defineProperty(Object.getPrototypeOf(DocBaseViewer.prototype), 'resize', { - value: resizeFunc - }); - }); - - it('should do nothing if pdfViewer does not exist', () => { - docBase.pdfViewer = null; - docBase.resize(); - expect(BaseViewer.prototype.resize).to.not.be.called; - }); - - it('should do nothing if the page views are not ready', () => { - docBase.pdfViewer.pageViewsReady = false; - docBase.resize(); - expect(BaseViewer.prototype.resize).to.not.be.called; - }); - - it('should update the pdfViewer and reset the page', () => { - docBase.resize(); - expect(docBase.pdfViewer.update).to.be.called; - expect(stubs.setPage).to.be.called; - expect(BaseViewer.prototype.resize).to.be.called; - }); - - it('should set the annotator scale if it exists', () => { - docBase.annotator = { - setScale: sandbox.stub() - }; - - docBase.resize(); - expect(docBase.annotator.setScale).to.be.called; - expect(stubs.setPage).to.be.called; - }); - }); - describe('Page Methods', () => { beforeEach(() => { docBase.pdfViewer = { @@ -1011,6 +864,59 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => { }); }); + describe('resize()', () => { + const resizeFunc = BaseViewer.prototype.resize; + + beforeEach(() => { + docBase.pdfViewer = { + update: sandbox.stub(), + currentScaleValue: 0, + currentPageNumber: 0, + pageViewsReady: true + }; + + stubs.setPage = sandbox.stub(docBase, 'setPage'); + Object.defineProperty(Object.getPrototypeOf(DocBaseViewer.prototype), 'resize', { + value: sandbox.stub() + }); + }); + + afterEach(() => { + Object.defineProperty(Object.getPrototypeOf(DocBaseViewer.prototype), 'resize', { + value: resizeFunc + }); + }); + + it('should do nothing if pdfViewer does not exist', () => { + docBase.pdfViewer = null; + docBase.resize(); + expect(BaseViewer.prototype.resize).to.not.be.called; + }); + + it('should do nothing if the page views are not ready', () => { + docBase.pdfViewer.pageViewsReady = false; + docBase.resize(); + expect(BaseViewer.prototype.resize).to.not.be.called; + }); + + it('should update the pdfViewer and reset the page', () => { + docBase.resize(); + expect(docBase.pdfViewer.update).to.be.called; + expect(stubs.setPage).to.be.called; + expect(BaseViewer.prototype.resize).to.be.called; + }); + + it('should set the annotator scale if it exists', () => { + docBase.annotator = { + setScale: sandbox.stub() + }; + + docBase.resize(); + expect(docBase.annotator.setScale).to.be.called; + expect(stubs.setPage).to.be.called; + }); + }); + describe('setupPdfjs()', () => { beforeEach(() => { stubs.urlCreator = sandbox.stub(util, 'createAssetUrlCreator').returns(() => { return 'asset'; }); @@ -1121,6 +1027,100 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => { }); }); + describe('initPrint()', () => { + it('should add print checkmark', () => { + docBase.initPrint(); + + const mockCheckmark = document.createElement('div'); + mockCheckmark.innerHTML = `${ICON_PRINT_CHECKMARK}`.trim(); + expect(docBase.printPopup.printCheckmark.innerHTML).to.equal(mockCheckmark.innerHTML); + }); + + it('should hide the print checkmark', () => { + docBase.initPrint(); + + expect(docBase.printPopup.printCheckmark.classList.contains(CLASS_HIDDEN)); + }); + + it('should add the loading indicator', () => { + docBase.initPrint(); + + const mockIndicator = document.createElement('div'); + mockIndicator.innerHTML = ` +
+
+
+ `.trim(); + expect(docBase.printPopup.loadingIndicator.innerHTML).to.equal(mockIndicator.innerHTML); + expect(docBase.printPopup.loadingIndicator.classList.contains('bp-crawler')).to.be.true; + }); + }); + + describe('print()', () => { + let clock; + + beforeEach(() => { + clock = sinon.useFakeTimers(); + docBase.printBlob = undefined; + stubs.fetchPrintBlob = sandbox.stub(docBase, 'fetchPrintBlob').returns({ + then: sandbox.stub() + }); + docBase.initPrint(); + stubs.show = sandbox.stub(docBase.printPopup, 'show'); + }); + + afterEach(() => { + clock.restore(); + }); + + it('should request the print blob if it is not ready', () => { + docBase.print(); + expect(stubs.fetchPrintBlob).to.be.called; + }); + + it('should show the print popup and disable the print button if the blob is not ready', () => { + sandbox.stub(docBase.printPopup, 'disableButton'); + + docBase.print(); + clock.tick(PRINT_DIALOG_TIMEOUT_MS + 1); + + expect(stubs.show).to.be.calledWith(__('print_loading'), __('print'), sinon.match.func); + expect(docBase.printPopup.disableButton).to.be.called; + }); + + it('should directly print if print blob is ready and the print dialog hasn\'t been shown yet', () => { + docBase.printBlob = {}; + docBase.printDialogTimeout = setTimeout(() => {}); + sandbox.stub(docBase, 'browserPrint'); + + docBase.print(); + expect(docBase.browserPrint).to.be.called; + }); + + it('should directly print if print blob is ready and the print dialog isn\'t visible', () => { + docBase.printBlob = {}; + docBase.printDialogTimeout = null; + sandbox.stub(docBase.printPopup, 'isVisible').returns(false); + sandbox.stub(docBase, 'browserPrint'); + + docBase.print(); + expect(docBase.browserPrint).to.be.called; + }); + + it('should update the print popup UI if popup is visible and there is no current print timeout', () => { + docBase.printBlob = {}; + + sandbox.stub(docBase.printPopup, 'isVisible').returns(true); + + docBase.print(); + + expect(docBase.printPopup.buttonEl.classList.contains('is-disabled')).to.be.false; + expect(docBase.printPopup.messageEl.textContent).to.equal(__('print_ready')); + expect(docBase.printPopup.loadingIndicator.classList.contains(CLASS_HIDDEN)).to.be.true; + expect(docBase.printPopup.printCheckmark.classList.contains(CLASS_HIDDEN)).to.be.false; + }); + }); + describe('initAnnotations()', () => { const initFunc = BaseViewer.prototype.initAnnotations;