Built motion from commit 1020cd7.|0.0.107
[motion.git] / public / assets / plugins / angular-pdf / dist / angular-pdf.js
1 /*! Angular-PDF Version: 0.4.0 | (C) Sayanee Basu 2014, released under an MIT license */
2 (function() {
3
4   'use strict';
5
6   angular.module('pdf', []).directive('ngPdf', ['$window', function($window) {
7     return {
8       restrict: 'E',
9       templateUrl: function(element, attr) {
10         return attr.templateUrl ? attr.templateUrl :
11           'partials/viewer.html'
12       },
13       link: function(scope, element, attrs) {
14         var url = attrs.pdfUrl,
15           pdfDoc = null,
16           pageNum = (attrs.page ? attrs.page : 1),
17           scale = attrs.scale > 0 ? attrs.scale : 1,
18           canvasArr = element.find('canvas');
19         var canvas = canvasArr[0];
20         var ctx = canvas.getContext('2d'),
21           windowEl = angular.element($window);
22
23         windowEl.on('scroll', function() {
24           scope.$apply(function() {
25             scope.scroll = windowEl[0].scrollY;
26           });
27         });
28
29         PDFJS.disableWorker = true;
30         scope.pageNum = pageNum;
31
32         scope.renderPage = function(num) {
33           pdfDoc.getPage(num).then(function(page) {
34             var viewport,
35               pageWidthScale,
36               pageHeightScale,
37               renderContext = {},
38               pageRendering;
39
40             if (attrs.scale === 'page-fit' && !scale) {
41               viewport = page.getViewport(1);
42               pageWidthScale = element[0].clientWidth /
43                 viewport.width;
44               pageHeightScale = element[0].clientHeight /
45                 viewport.height;
46               scale = Math.min(pageWidthScale, pageHeightScale);
47             } else {
48               viewport = page.getViewport(scale)
49             }
50
51             canvas.height = viewport.height;
52             canvas.width = viewport.width;
53
54             renderContext = {
55               canvasContext: ctx,
56               viewport: viewport
57             };
58
59             page.render(renderContext).promise.then(function() {
60               if (typeof scope.onPageRender === 'function') {
61                 scope.onPageRender();
62               }
63             });
64           });
65         };
66
67         scope.goPrevious = function() {
68           if (scope.pageToDisplay <= 1) {
69             return;
70           }
71           scope.pageNum = parseInt(scope.pageNum) - 1;
72         };
73
74         scope.goNext = function() {
75           if (scope.pageToDisplay >= pdfDoc.numPages) {
76             return;
77           }
78           scope.pageNum = parseInt(scope.pageNum) + 1;
79         };
80
81         scope.zoomIn = function() {
82           scale = parseFloat(scale) + 0.2;
83           scope.renderPage(scope.pageToDisplay);
84           return scale;
85         };
86
87         scope.zoomOut = function() {
88           scale = parseFloat(scale) - 0.2;
89           scope.renderPage(scope.pageToDisplay);
90           return scale;
91         };
92
93         scope.changePage = function() {
94           scope.renderPage(scope.pageToDisplay);
95         };
96
97         scope.rotate = function() {
98           if (canvas.getAttribute('class') === 'rotate0') {
99             canvas.setAttribute('class', 'rotate90');
100           } else if (canvas.getAttribute('class') === 'rotate90') {
101             canvas.setAttribute('class', 'rotate180');
102           } else if (canvas.getAttribute('class') === 'rotate180') {
103             canvas.setAttribute('class', 'rotate270');
104           } else {
105             canvas.setAttribute('class', 'rotate0');
106           }
107         };
108
109         PDFJS.getDocument(url, null, null, scope.onProgress).then(
110           function(_pdfDoc) {
111             if (typeof scope.onLoad === 'function') {
112               scope.onLoad();
113             }
114
115             pdfDoc = _pdfDoc;
116             scope.renderPage(scope.pageToDisplay);
117
118             scope.$apply(function() {
119               scope.pageCount = _pdfDoc.numPages;
120             });
121           },
122           function(error) {
123             if (error) {
124               if (typeof scope.onError === 'function') {
125                 scope.onError(error);
126               }
127             }
128           }
129         );
130
131         scope.$watch('pageNum', function(newVal) {
132           scope.pageToDisplay = parseInt(newVal);
133           if (pdfDoc !== null) {
134             scope.renderPage(scope.pageToDisplay);
135           }
136         });
137
138       }
139     };
140   }]);
141
142 })();