Skip to content

Commit

Permalink
Chore: move isMobile check to user agent and add check back to downlo…
Browse files Browse the repository at this point in the history
…ad button (#35)
  • Loading branch information
Jeremy Press authored Apr 3, 2017
1 parent 8dd9c19 commit d77c0c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d77c0c2

Please sign in to comment.