UNPKG

unserver-unify

Version:

165 lines (163 loc) 5.72 kB
angular.module("bamboo.course").service('pdfViewService', function($rootScope) { var _pdfurl = ''; return { getpdfUrl: function() { return _pdfurl; }, setpdfUrl: function(value) { _pdfurl = value; $rootScope.$broadcast('pdfurl:updated', value); }, broadCastSizeUpdate: function() { $rootScope.$broadcast('pdfContailerChange'); } } }).controller("PdfViewerCtrl", function($scope, $sce, pdfViewService, ApiService, $stateParams) { var self = this; $scope.isLoading = false; $scope.downloadProgress = 0; $scope.pdfZoomLevels = []; $scope.pdfViewerAPI = {}; $scope.pdfScale = 1; $scope.pdfURL = pdfViewService.getpdfUrl(); $scope.pdfFile = null; $scope.pdfTotalPages = 0; $scope.pdfCurrentPage = 0; $scope.pdfSearchTerm = ""; $scope.pdfSearchResultID = 0; $scope.pdfSearchNumOccurences = 0; this.lid = $stateParams.cid; this.videourl = ApiService.SHOST + '/lcourse/' + self.lid + '/resources/'; self.findfitpage = false; self.findfitwidth = false; $scope.$on('pdfurl:updated', function(event, data) { $scope.loadPDF($scope.trustSrc(data)); }) $scope.onPDFProgress = function(operation, state, value, total, message) { //console.log("onPDFProgress(" + operation + ", " + state + ", " + value + ", " + total + ")"); if (operation === "render" && value === 1) { if (state === "success") { //console.log($scope.pdfViewerAPI); if ($scope.pdfZoomLevels.length === 0) { // Read all the PDF zoom levels in order to populate the combobox... var lastScale = 0.1; do { var curScale = $scope.pdfViewerAPI.getNextZoomInScale(lastScale); if (curScale.value === lastScale) { break; } $scope.pdfZoomLevels.push(curScale); lastScale = curScale.value; } while (true); } //console.log($scope.pdfZoomLevels); //refresh the scale for fit-page and fit-width if ($scope.pdfViewerAPI.viewer.fitPageScale) { self.findfitpage = true; self.fitpagevalue = $scope.pdfViewerAPI.viewer.fitPageScale; } else { self.findfitpage = false; } if ($scope.pdfViewerAPI.viewer.fitWidthScale) { self.findfitwidth = true; self.fitwidthvalue = $scope.pdfViewerAPI.viewer.fitWidthScale; } else { self.findfitwidth = false; } $scope.pdfCurrentPage = 1; $scope.pdfTotalPages = $scope.pdfViewerAPI.getNumPages(); $scope.pdfScale = $scope.pdfViewerAPI.getZoomLevel(); $scope.isLoading = false; console.log($scope.pdfViewerAPI); } else { alert("Failed to render 1st page!\n\n" + message); $scope.isLoading = false; } } else if (operation === "download" && state === "loading") { $scope.downloadProgress = (value / total) * 100.0; } else { if (state === "failed") { alert("Something went really bad!\n\n" + message); } } }; $scope.onPDFZoomLevelChanged = function() { $scope.pdfViewerAPI.zoomTo($scope.pdfScale); }; self.fitpage = function() { // update fitpage scale if ($scope.pdfViewerAPI.viewer.fitPageScale) { self.findfitpage = true; $scope.pdfScale = self.fitpagevalue = $scope.pdfViewerAPI.viewer.fitPageScale; $scope.onPDFZoomLevelChanged(); } else { self.findfitpage = false; } } self.fitwidth = function() { //update fitwidth scale if ($scope.pdfViewerAPI.viewer.fitWidthScale) { self.findfitwidth = true; $scope.pdfScale = self.fitwidthvalue = $scope.pdfViewerAPI.viewer.fitWidthScale; $scope.onPDFZoomLevelChanged(); } else { self.findfitwidth = false; } } $scope.onPDFPageChanged = function() { $scope.pdfViewerAPI.goToPage($scope.pdfCurrentPage); }; $scope.zoomIn = function() { var nextScale = $scope.pdfViewerAPI.getNextZoomInScale($scope.pdfScale); $scope.pdfViewerAPI.zoomTo(nextScale.value); $scope.pdfScale = nextScale.value; //console.log(nextScale); }; $scope.zoomOut = function() { var nextScale = $scope.pdfViewerAPI.getNextZoomOutScale($scope.pdfScale); $scope.pdfViewerAPI.zoomTo(nextScale.value); $scope.pdfScale = nextScale.value; //console.log(nextScale); }; $scope.loadPDF = function(pdfURL) { if ($scope.pdfURL === pdfURL) { return; } $scope.isLoading = true; $scope.downloadProgress = 0; $scope.pdfZoomLevels = []; $scope.pdfSearchTerm = ""; $scope.pdfFile = null; $scope.pdfURL = pdfURL; }; $scope.findNext = function() { $scope.pdfViewerAPI.findNext(); }; $scope.findPrev = function() { $scope.pdfViewerAPI.findPrev(); }; $scope.onPDFFileChanged = function() { $scope.isLoading = true; $scope.downloadProgress = 0; $scope.pdfZoomLevels = []; $scope.pdfSearchTerm = ""; $scope.$apply(function() { $scope.pdfURL = ""; $scope.pdfFile = document.getElementById('file_input').files[0]; }); }; $scope.onPDFPassword = function(reason) { return prompt("The selected PDF is password protected. PDF.js reason: " + reason, ""); }; // $scope.printPDF = function () { // $scope.pdfViewerAPI.print(); // }; $scope.trustSrc = function(src) { return $sce.trustAsResourceUrl(src); }; $scope.onPDFEnd = function() { console.log('End'); $scope.$emit('pdf-end'); }; $scope.loadPDF($scope.trustSrc(pdfViewService.getpdfUrl())); });