From d77c0c2eebdba4ce257536b15746b09f11a3146d Mon Sep 17 00:00:00 2001 From: Jeremy Press Date: Mon, 3 Apr 2017 13:26:40 -0700 Subject: [PATCH] Chore: move isMobile check to user agent and add check back to download button (#35) --- src/lib/Browser.js | 8 ++++---- src/lib/Preview.js | 5 +++-- src/lib/__tests__/Preview-test.js | 9 +++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/lib/Browser.js b/src/lib/Browser.js index d111a24e6..142ba2bca 100644 --- a/src/lib/Browser.js +++ b/src/lib/Browser.js @@ -219,19 +219,19 @@ class Browser { } /** - * Returns whether the browser is a mobile browser. Taken from Modernizr: - * https://github.com/Modernizr/Modernizr/blob/master/feature-detects/touchevents.js + * Returns whether the browser is a mobile browser. * * @return {boolean} true if browser supports download */ static isMobile() { - return (('ontouchstart' in window) || (window.DocumentTouch && document instanceof DocumentTouch)); + // Relying on the user agent to avoid desktop browsers on machines with touch screens. + return /iphone|ipad|ipod|android|blackberry|bb10|mini|windows\sce|palm/i.test(navigator.userAgent); } /** * Returns whether the browser can download via HTML5. taken from Modernizr: * https://github.com/Modernizr/Modernizr/blob/master/feature-detects/a/download.js - * + * Currently not supported by IE11 or Safari 10.0 http://caniuse.com/#feat=download * @return {boolean} true if browser supports download */ static canDownload() { diff --git a/src/lib/Preview.js b/src/lib/Preview.js index bede512bb..794134916 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -915,10 +915,11 @@ class Preview extends EventEmitter { */ finishLoading(data = {}) { // Show or hide annotate/print/download buttons - if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload && Browser.canDownload()) { + // canDownload is not supported by all of our browsers, so for now we need to check isMobile + if (checkPermission(this.file, PERMISSION_DOWNLOAD) && this.options.showDownload && (Browser.canDownload() || !Browser.isMobile())) { showDownloadButton(this.download); - if (checkFeature(this.viewer, 'print')) { + if (checkFeature(this.viewer, 'print') && !Browser.isMobile()) { showPrintButton(this.print); } } diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index 67d493a45..3bb70b8f5 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -1328,12 +1328,21 @@ describe('lib/Preview', () => { it('should show download button if download is supported by browser', () => { stubs.canDownload.returns(false); + stubs.isMobile.returns(true); preview.finishLoading(); expect(stubs.showDownloadButton).to.not.be.called; + stubs.canDownload.returns(false); + stubs.isMobile.returns(false); + + preview.finishLoading(); + expect(stubs.showDownloadButton).to.be.called; + stubs.canDownload.returns(true); + stubs.isMobile.returns(false); + preview.finishLoading(); expect(stubs.showDownloadButton).to.be.calledWith(preview.download);