From 8ac0aabbebe0d1463d7e33b121bd742dbd5308b5 Mon Sep 17 00:00:00 2001 From: Jason Brackins Date: Fri, 29 May 2015 12:05:58 -0700 Subject: [PATCH] Enhancement: Zoom amount and ZoomTo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added optional `amount` parameter to delegate’s `zoomIn` and `zoomOut` methods * Added `zoomTo` method to delate --- dist/angular-pdf-viewer.min.js | 2 +- readme.md | 5 +++-- src/js/pdf-ctrl.js | 18 ++++++++++++++---- src/js/pdf-viewer-delegate.js | 1 + 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/dist/angular-pdf-viewer.min.js b/dist/angular-pdf-viewer.min.js index ed88fe6..96759cb 100644 --- a/dist/angular-pdf-viewer.min.js +++ b/dist/angular-pdf-viewer.min.js @@ -1 +1 @@ -function delegateService(e){return["$log",function(t){function n(e){this.handle=e}var a=this,o=this._instances=[];this._registerInstance=function(e,t){return e.$$delegateHandle=t,o.push(e),function(){var t=o.indexOf(e);-1!==t&&o.splice(t,1)}},this.$getByHandle=function(e){return e?new n(e):a},e.forEach(function(e){n.prototype[e]=function(){var n,a,r=this.handle,l=arguments,u=0;return o.forEach(function(t){t.$$delegateHandle===r&&(u++,a=t[e].apply(t,l),1===u&&(n=a))}),u?n:t.warn('Delegate for handle "'+this.handle+'" could not find a corresponding element with delegate-handle="'+this.handle+'"! '+e+"() was not called!\nPossible cause: If you are calling "+e+'() immediately, and your element with delegate-handle="'+this.handle+'" is a child of your controller, then your element may not be compiled yet. Put a $timeout around your call to '+e+"() and try again.")},a[e]=function(){var t,n,a=arguments;return o.forEach(function(o,r){n=o[e].apply(o,a),0===r&&(t=n)}),t}})}]}angular.module("pdf",[]).service("pdfDelegate",delegateService(["prev","next","zoomIn","zoomOut","rotate","getPageCount","getCurrentPage","goToPage","load"])),angular.module("pdf").controller("PdfCtrl",["$scope","$element","$attrs","pdfDelegate","$log",function(e,t,n,a,o){var r=a._registerInstance(this,n.delegateHandle);e.$on("$destroy",r);var l,u=this,i=e.$eval(n.url),c=e.$eval(n.headers);e.pageCount=0;var d=1,g=0,s=n.scale?n.scale:1,f=t.find("canvas")[0],p=f.getContext("2d"),h=function(e){angular.isNumber(e)||(e=parseInt(e)),l.getPage(e).then(function(e){var t=e.getViewport(s);f.height=t.height,f.width=t.width;var n={canvasContext:p,viewport:t};e.render(n)})},m=function(){f.style.webkitTransform="rotate("+g+"deg)",f.style.MozTransform="rotate("+g+"deg)",f.style.msTransform="rotate("+g+"deg)",f.style.OTransform="rotate("+g+"deg)",f.style.transform="rotate("+g+"deg)"};u.prev=function(){1>=d||(d=parseInt(d,10)-1,h(d))},u.next=function(){d>=l.numPages||(d=parseInt(d,10)+1,h(d))},u.zoomIn=function(){return s=parseFloat(s)+.2,h(d),s},u.zoomOut=function(){return s=parseFloat(s)-.2,h(d),s},u.rotate=function(){g=0===g?90:90===g?180:180===g?270:0,m()},u.getPageCount=function(){return e.pageCount},u.getCurrentPage=function(){return d},u.goToPage=function(e){null!==l&&(d=e,h(e))},u.load=function(t){t&&(i=t);var n={};c?(n.url=i,n.httpHeaders=c):n.url=i,PDFJS.getDocument(n).then(function(t){l=t,h(1),e.$apply(function(){e.pageCount=t.numPages})},o.error)},u.load()}]),angular.module("pdf").directive("pdfViewerToolbar",["pdfDelegate",function(e){return{restrict:"E",template:'
BackNextZoom InZoom OutRotatePage / {{pageCount}}
',scope:{pageCount:"="},link:function(t,n,a){var o=a.delegateHandle;t.currentPage=1,t.prev=function(){e.$getByHandle(o).prev(),r()},t.next=function(){e.$getByHandle(o).next(),r()},t.zoomIn=function(){e.$getByHandle(o).zoomIn()},t.zoomOut=function(){e.$getByHandle(o).zoomOut()},t.rotate=function(){e.$getByHandle(o).rotate()},t.goToPage=function(){e.$getByHandle(o).goToPage(t.currentPage)};var r=function(){t.currentPage=e.$getByHandle(o).getCurrentPage()}}}}]),angular.module("pdf").directive("pdfViewer",["$window","$log","pdfDelegate",function(){return{restrict:"E",template:'',scope:!0,controller:"PdfCtrl",link:function(e,t,n){e.id=n.delegateHandle,e.showToolbar=e.$eval(n.showToolbar)||!1}}}]); \ No newline at end of file +function delegateService(e){return["$log",function(t){function n(e){this.handle=e}var a=this,o=this._instances=[];this._registerInstance=function(e,t){return e.$$delegateHandle=t,o.push(e),function(){var t=o.indexOf(e);-1!==t&&o.splice(t,1)}},this.$getByHandle=function(e){return e?new n(e):a},e.forEach(function(e){n.prototype[e]=function(){var n,a,r=this.handle,l=arguments,u=0;return o.forEach(function(t){t.$$delegateHandle===r&&(u++,a=t[e].apply(t,l),1===u&&(n=a))}),u?n:t.warn('Delegate for handle "'+this.handle+'" could not find a corresponding element with delegate-handle="'+this.handle+'"! '+e+"() was not called!\nPossible cause: If you are calling "+e+'() immediately, and your element with delegate-handle="'+this.handle+'" is a child of your controller, then your element may not be compiled yet. Put a $timeout around your call to '+e+"() and try again.")},a[e]=function(){var t,n,a=arguments;return o.forEach(function(o,r){n=o[e].apply(o,a),0===r&&(t=n)}),t}})}]}angular.module("pdf",[]).service("pdfDelegate",delegateService(["prev","next","zoomIn","zoomOut","zoomTo","rotate","getPageCount","getCurrentPage","goToPage","load"])),angular.module("pdf").controller("PdfCtrl",["$scope","$element","$attrs","pdfDelegate","$log",function(e,t,n,a,o){var r=a._registerInstance(this,n.delegateHandle);e.$on("$destroy",r);var l,u=this,i=e.$eval(n.url),c=e.$eval(n.headers);e.pageCount=0;var d=1,g=0,s=n.scale?n.scale:1,f=t.find("canvas")[0],p=f.getContext("2d"),h=function(e){angular.isNumber(e)||(e=parseInt(e)),l.getPage(e).then(function(e){var t=e.getViewport(s);f.height=t.height,f.width=t.width;var n={canvasContext:p,viewport:t};e.render(n)})},m=function(){f.style.webkitTransform="rotate("+g+"deg)",f.style.MozTransform="rotate("+g+"deg)",f.style.msTransform="rotate("+g+"deg)",f.style.OTransform="rotate("+g+"deg)",f.style.transform="rotate("+g+"deg)"};u.prev=function(){1>=d||(d=parseInt(d,10)-1,h(d))},u.next=function(){d>=l.numPages||(d=parseInt(d,10)+1,h(d))},u.zoomIn=function(e){return e=e||.2,s=parseFloat(s)+e,h(d),s},u.zoomOut=function(e){return e=e||.2,s=parseFloat(s)-e,s=s>0?s:.1,h(d),s},u.zoomTo=function(e){return e=e?e:1,s=parseFloat(e),h(d),s},u.rotate=function(){g=0===g?90:90===g?180:180===g?270:0,m()},u.getPageCount=function(){return e.pageCount},u.getCurrentPage=function(){return d},u.goToPage=function(e){null!==l&&(d=e,h(e))},u.load=function(t){t&&(i=t);var n={};return c?(n.url=i,n.httpHeaders=c):n.url=i,PDFJS.getDocument(n).then(function(t){l=t,h(1),e.$apply(function(){e.pageCount=t.numPages})},o.error)},u.load()}]),angular.module("pdf").directive("pdfViewerToolbar",["pdfDelegate",function(e){return{restrict:"E",template:'
BackNextZoom InZoom OutRotatePage / {{pageCount}}
',scope:{pageCount:"="},link:function(t,n,a){var o=a.delegateHandle;t.currentPage=1,t.prev=function(){e.$getByHandle(o).prev(),r()},t.next=function(){e.$getByHandle(o).next(),r()},t.zoomIn=function(){e.$getByHandle(o).zoomIn()},t.zoomOut=function(){e.$getByHandle(o).zoomOut()},t.rotate=function(){e.$getByHandle(o).rotate()},t.goToPage=function(){e.$getByHandle(o).goToPage(t.currentPage)};var r=function(){t.currentPage=e.$getByHandle(o).getCurrentPage()}}}}]),angular.module("pdf").directive("pdfViewer",["$window","$log","pdfDelegate",function(){return{restrict:"E",template:'',scope:!0,controller:"PdfCtrl",link:function(e,t,n){e.id=n.delegateHandle,e.showToolbar=e.$eval(n.showToolbar)||!1}}}]); \ No newline at end of file diff --git a/readme.md b/readme.md index 3724e75..6695cde 100644 --- a/readme.md +++ b/readme.md @@ -54,8 +54,9 @@ pdfDelegate.$getByHandle('my-pdf-container').zoomIn(); The following methods are available to the delegate: - prev - next -- zoomIn -- zoomOut +- zoomIn(amount) *default amount = 0.2* +- zoomOut(amount) *default amount = 0.2* +- zoomTo(amount) - rotate *(clockwise by 90 degrees)* - getPageCount - getCurrentPage diff --git a/src/js/pdf-ctrl.js b/src/js/pdf-ctrl.js index bd9ce5d..673d060 100644 --- a/src/js/pdf-ctrl.js +++ b/src/js/pdf-ctrl.js @@ -65,14 +65,24 @@ angular.module('pdf') renderPage(currentPage); }; - self.zoomIn = function() { - scale = parseFloat(scale) + 0.2; + self.zoomIn = function(amount) { + amount = amount || 0.2; + scale = parseFloat(scale) + amount; renderPage(currentPage); return scale; }; - self.zoomOut = function() { - scale = parseFloat(scale) - 0.2; + self.zoomOut = function(amount) { + amount = amount || 0.2; + scale = parseFloat(scale) - amount; + scale = (scale > 0) ? scale : 0.1; + renderPage(currentPage); + return scale; + }; + + self.zoomTo = function(zoomToScale) { + zoomToScale = (zoomToScale) ? zoomToScale : 1.0; + scale = parseFloat(zoomToScale); renderPage(currentPage); return scale; }; diff --git a/src/js/pdf-viewer-delegate.js b/src/js/pdf-viewer-delegate.js index 085b815..801fec1 100644 --- a/src/js/pdf-viewer-delegate.js +++ b/src/js/pdf-viewer-delegate.js @@ -4,6 +4,7 @@ angular.module('pdf', []) 'next', 'zoomIn', 'zoomOut', + 'zoomTo', 'rotate', 'getPageCount', 'getCurrentPage',