diff --git a/bower.json b/bower.json index d280c1a..9716d3b 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-pdf-viewer", - "version": "0.1.0", + "version": "0.2.0", "description": "An AngularJS directive to display PDFs", "main": "./dist/angular-pdf-viewer.min.js", "license": "MIT", diff --git a/dist/angular-pdf-viewer.min.js b/dist/angular-pdf-viewer.min.js index a7e5b5b..2d0b185 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"])),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);e.pageCount=0;var c=1,g=0,d=n.scale?n.scale:1,s=t.find("canvas")[0],f=s.getContext("2d"),p=function(e){angular.isNumber(e)||(e=parseInt(e)),l.getPage(e).then(function(e){var t=e.getViewport(d);s.height=t.height,s.width=t.width;var n={canvasContext:f,viewport:t};e.render(n)})},h=function(){s.style.webkitTransform="rotate("+g+"deg)",s.style.MozTransform="rotate("+g+"deg)",s.style.msTransform="rotate("+g+"deg)",s.style.OTransform="rotate("+g+"deg)",s.style.transform="rotate("+g+"deg)"};u.prev=function(){1>=c||(c=parseInt(c,10)-1,p(c))},u.next=function(){c>=l.numPages||(c=parseInt(c,10)+1,p(c))},u.zoomIn=function(){return d=parseFloat(d)+.2,p(c),d},u.zoomOut=function(){return d=parseFloat(d)-.2,p(c),d},u.rotate=function(){g=0===g?90:90===g?180:180===g?270:0,h()},u.getPageCount=function(){return e.pageCount},u.getCurrentPage=function(){return c},u.goToPage=function(e){null!==l&&(c=e,p(e))},PDFJS.getDocument(i).then(function(t){l=t,p(c),e.$apply(function(){e.pageCount=t.numPages})},o.error)}]),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(){console.log(t.currentPage),e.$getByHandle(o).goToPage(t.currentPage)};var r=function(){t.currentPage=e.$getByHandle(o).getCurrentPage(),console.log(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.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","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);e.pageCount=0;var c=1,d=0,g=n.scale?n.scale:1,s=t.find("canvas")[0],f=s.getContext("2d"),p=function(e){angular.isNumber(e)||(e=parseInt(e)),l.getPage(e).then(function(e){var t=e.getViewport(g);s.height=t.height,s.width=t.width;var n={canvasContext:f,viewport:t};e.render(n)})},m=function(){s.style.webkitTransform="rotate("+d+"deg)",s.style.MozTransform="rotate("+d+"deg)",s.style.msTransform="rotate("+d+"deg)",s.style.OTransform="rotate("+d+"deg)",s.style.transform="rotate("+d+"deg)"};u.prev=function(){1>=c||(c=parseInt(c,10)-1,p(c))},u.next=function(){c>=l.numPages||(c=parseInt(c,10)+1,p(c))},u.zoomIn=function(){return g=parseFloat(g)+.2,p(c),g},u.zoomOut=function(){return g=parseFloat(g)-.2,p(c),g},u.rotate=function(){d=0===d?90:90===d?180:180===d?270:0,m()},u.getPageCount=function(){return e.pageCount},u.getCurrentPage=function(){return c},u.goToPage=function(e){null!==l&&(c=e,p(e))},u.load=function(t){t&&(i=t),PDFJS.getDocument(i).then(function(t){l=t,p(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/package.json b/package.json index 3f2c922..0f432c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-pdf-viewer", - "version": "0.1.0", + "version": "0.2.0", "author": "Varun Vachhar", "description": "An AngularJS directive to display PDFs", "repository": { diff --git a/readme.md b/readme.md index 53c3cc3..69f72ad 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# Angular PDF Viewer (0.1.0) +# Angular PDF Viewer (0.2.0) An AngularJS directive to display PDFs. [DEMO](http://codepen.io/winkerVSbecks/full/50010e383d0f80deab97858571400d86/) @@ -22,7 +22,7 @@ An AngularJS directive to display PDFs. [DEMO](http://codepen.io/winkerVSbecks/f 3. Include the lib as a dependency in your angular app: -``` +``` js var app = angular.module('App', ['pdf']); ``` @@ -31,9 +31,9 @@ var app = angular.module('App', ['pdf']); The URL, scale and delegate-handle can be set using the attributes: -``` +``` html @@ -46,8 +46,8 @@ The pdfDelegate service allows you to access and control individual instances of Inject the `pdfDelegate` service into your controller. You can then fetch an instance using it's delegate handle and call methods on it: -``` -pdfDelegate.$getByHandle('relativity-special-general-theory').zoomIn(); +``` js +pdfDelegate.$getByHandle('my-pdf-container').zoomIn(); ``` The following methods are available to the delegate: @@ -59,6 +59,23 @@ The following methods are available to the delegate: - getPageCount - getCurrentPage - goToPage(pageNumber) +- load + + +## Change the PDF File + +In order to replace the active PDF with another one, you can call the `load` method of the delegate. For example: + +``` js +pdfDelegate + .$getByHandle('my-pdf-container') + .load('url-of-the-new-file.pdf'); +``` + + +## Example + +Run `npm install && bower install` to install all dependencies. And then `gulp dev` to start a local server. The example will now be available at [localhost:3000/src](http://localhost:3000/src) ## Toolbar @@ -73,4 +90,4 @@ The default toolbar can be shown or hidden using the `show-toolbar` attribute. S ## Credit -PDF example used is [Relativity: The Special and General Theory by Albert Einstein](http://www.gutenberg.org/ebooks/30155) as kindly organized and made available free by [Project Gutenberg](http://www.gutenberg.org/wiki/Main_Page). \ No newline at end of file +PDF examples used are [Relativity: The Special and General Theory by Albert Einstein](http://www.gutenberg.org/ebooks/30155) as kindly organized and made available free by [Project Gutenberg](http://www.gutenberg.org/wiki/Main_Page). And the [This is Material Design](http://static.googleusercontent.com/media/www.google.com/en//design/material-design.pdf) by Google. \ No newline at end of file diff --git a/src/index.html b/src/index.html index 2b4cddc..2f93881 100644 --- a/src/index.html +++ b/src/index.html @@ -21,10 +21,28 @@

AngularJS PDF Viewer

+ + + @@ -33,10 +51,11 @@

AngularJS PDF Viewer

- // + diff --git a/src/js/pdf-ctrl.js b/src/js/pdf-ctrl.js index 4b8a8b9..fe50a9a 100644 --- a/src/js/pdf-ctrl.js +++ b/src/js/pdf-ctrl.js @@ -104,13 +104,23 @@ angular.module('pdf') } }; - PDFJS - .getDocument(url) - .then(function (_pdfDoc) { - pdfDoc = _pdfDoc; - renderPage(currentPage); - $scope.$apply(function() { - $scope.pageCount = _pdfDoc.numPages; - }); - }, $log.error); + self.load = function(_url) { + if (_url) { + url = _url; + } + + PDFJS + .getDocument(url) + .then(function (_pdfDoc) { + + pdfDoc = _pdfDoc; + renderPage(1); + $scope.$apply(function() { + $scope.pageCount = _pdfDoc.numPages; + }); + + }, $log.error); + }; + + self.load(); }]); \ No newline at end of file diff --git a/src/js/pdf-viewer-delegate.js b/src/js/pdf-viewer-delegate.js index 7956dd8..085b815 100644 --- a/src/js/pdf-viewer-delegate.js +++ b/src/js/pdf-viewer-delegate.js @@ -7,5 +7,6 @@ angular.module('pdf', []) 'rotate', 'getPageCount', 'getCurrentPage', - 'goToPage' + 'goToPage', + 'load' ])); \ No newline at end of file diff --git a/src/js/pdf-viewer-toolbar.js b/src/js/pdf-viewer-toolbar.js index 1c224c3..249e38d 100644 --- a/src/js/pdf-viewer-toolbar.js +++ b/src/js/pdf-viewer-toolbar.js @@ -67,7 +67,6 @@ angular.module('pdf') .rotate(); }; scope.goToPage = function() { - console.log(scope.currentPage) pdfDelegate .$getByHandle(id) .goToPage(scope.currentPage); @@ -77,7 +76,6 @@ angular.module('pdf') scope.currentPage = pdfDelegate .$getByHandle(id) .getCurrentPage(); - console.log(pdfDelegate.$getByHandle(id).getCurrentPage()); }; } }; diff --git a/src/js/pdf-viewer.js b/src/js/pdf-viewer.js index addd249..39189c2 100644 --- a/src/js/pdf-viewer.js +++ b/src/js/pdf-viewer.js @@ -6,10 +6,11 @@ angular.module('pdf') function($window, $log, pdfDelegate) { return { restrict: 'E', - template: '', + template: '', scope: true, controller: 'PdfCtrl', link: function(scope, element, attrs) { + scope.id = attrs.delegateHandle; scope.showToolbar = scope.$eval(attrs.showToolbar) || false; } }; diff --git a/src/pdf/material-design.pdf b/src/pdf/material-design.pdf new file mode 100644 index 0000000..5e09457 Binary files /dev/null and b/src/pdf/material-design.pdf differ diff --git a/src/test-app.js b/src/test-app.js index c5cac47..07b17fe 100644 --- a/src/test-app.js +++ b/src/test-app.js @@ -2,6 +2,13 @@ angular.module('testApp', ['pdf']) .controller('AppCtrl', [ '$scope', 'pdfDelegate', - function($scope, pdfDelegate) { - $scope.pdfUrl = 'pdf/relativity.pdf'; + '$timeout', + function($scope, pdfDelegate, $timeout) { + $scope.pdfUrl = 'pdf/material-design.pdf'; + + $scope.loadNewFile = function(url) { + pdfDelegate + .$getByHandle('my-pdf-container') + .load(url); + }; }]); \ No newline at end of file