2014-12-01 05:42:47 -05:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
"ide/pdfng/directives/pdfTextLayer"
|
|
|
|
"ide/pdfng/directives/pdfAnnotations"
|
|
|
|
"ide/pdfng/directives/pdfHighlights"
|
|
|
|
"ide/pdfng/directives/pdfRenderer"
|
|
|
|
"ide/pdfng/directives/pdfPage"
|
2014-12-08 09:46:59 -05:00
|
|
|
"ide/pdfng/directives/pdfSpinner"
|
2014-12-01 09:35:12 -05:00
|
|
|
"libs/pdf" # needs pdfjs-1.0.712, override the path in require.js to get it
|
2014-12-01 05:42:47 -05:00
|
|
|
], (
|
|
|
|
App
|
|
|
|
pdfTextLayer
|
|
|
|
pdfAnnotations
|
|
|
|
pdfHighlights
|
|
|
|
pdfRenderer
|
|
|
|
pdfPage
|
2014-12-08 09:46:59 -05:00
|
|
|
pdfSpinner
|
2014-12-01 05:42:47 -05:00
|
|
|
pdf
|
|
|
|
) ->
|
|
|
|
|
|
|
|
# App = angular.module 'pdfViewerApp', ['pdfPage', 'PDFRenderer', 'pdfHighlights']
|
|
|
|
|
2014-12-08 09:46:59 -05:00
|
|
|
App.controller 'pdfViewerController', ['$scope', '$q', '$timeout', 'PDFRenderer', '$element', 'pdfHighlights', 'pdfSpinner', ($scope, $q, $timeout, PDFRenderer, $element, pdfHighlights, pdfSpinner) ->
|
2014-12-01 05:42:47 -05:00
|
|
|
@load = () ->
|
2014-12-08 12:00:43 -05:00
|
|
|
# $scope.pages = []
|
2014-12-08 09:46:59 -05:00
|
|
|
|
2014-12-09 08:46:44 -05:00
|
|
|
$scope.document.destroy() if $scope.document?
|
2015-01-26 09:00:42 -05:00
|
|
|
$scope.loadCount = if $scope.loadCount? then $scope.loadCount + 1 else 1
|
2014-12-12 11:47:43 -05:00
|
|
|
# TODO need a proper url manipulation library to add to query string
|
|
|
|
$scope.document = new PDFRenderer($scope.pdfSrc + '&pdfng=true' , {
|
2014-12-01 05:42:47 -05:00
|
|
|
scale: 1,
|
|
|
|
navigateFn: (ref) ->
|
|
|
|
# this function captures clicks on the annotation links
|
|
|
|
$scope.navigateTo = ref
|
|
|
|
$scope.$apply()
|
2014-12-08 12:00:26 -05:00
|
|
|
progressCallback: (progress) ->
|
|
|
|
$scope.$emit 'progress', progress
|
2015-01-13 11:28:24 -05:00
|
|
|
loadedCallback: () ->
|
|
|
|
$scope.$emit 'loaded'
|
2015-01-12 11:45:24 -05:00
|
|
|
errorCallback: (error) ->
|
2015-01-26 07:24:05 -05:00
|
|
|
Raven?.captureMessage?('pdfng error ' + error)
|
2015-01-12 11:45:24 -05:00
|
|
|
$scope.$emit 'pdf:error', error
|
2015-01-20 09:28:14 -05:00
|
|
|
pageSizeChangeCallback: (pageNum, deltaH) ->
|
|
|
|
$scope.$broadcast 'pdf:page:size-change', pageNum, deltaH
|
2014-12-01 05:42:47 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
# we will have all the main information needed to start display
|
|
|
|
# after the following promise is resolved
|
|
|
|
$scope.loaded = $q.all({
|
|
|
|
numPages: $scope.document.getNumPages()
|
|
|
|
# get size of first page as default @ scale 1
|
|
|
|
pdfViewport: $scope.document.getPdfViewport 1, 1
|
|
|
|
}).then (result) ->
|
|
|
|
$scope.pdfViewport = result.pdfViewport
|
|
|
|
$scope.pdfPageSize = [
|
|
|
|
result.pdfViewport.height,
|
|
|
|
result.pdfViewport.width
|
|
|
|
]
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'resolved q.all, page size is', result
|
2014-12-01 05:42:47 -05:00
|
|
|
$scope.numPages = result.numPages
|
2015-01-26 09:00:22 -05:00
|
|
|
.catch (error) ->
|
2015-01-26 09:37:43 -05:00
|
|
|
$scope.$emit 'pdf:error', error
|
|
|
|
return $q.reject(error)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
@setScale = (scale, containerHeight, containerWidth) ->
|
|
|
|
$scope.loaded.then () ->
|
|
|
|
scale = {} if not scale?
|
2014-12-08 05:28:02 -05:00
|
|
|
if containerHeight == 0 or containerWidth == 0
|
|
|
|
numScale = 1
|
|
|
|
else if scale.scaleMode == 'scale_mode_fit_width'
|
2014-12-01 05:42:47 -05:00
|
|
|
# TODO make this dynamic
|
|
|
|
numScale = (containerWidth - 40) / ($scope.pdfPageSize[1])
|
|
|
|
else if scale.scaleMode == 'scale_mode_fit_height'
|
|
|
|
# TODO magic numbers for jquery ui layout
|
|
|
|
numScale = (containerHeight - 20) / ($scope.pdfPageSize[0])
|
|
|
|
else if scale.scaleMode == 'scale_mode_value'
|
|
|
|
numScale = scale.scale
|
|
|
|
else if scale.scaleMode == 'scale_mode_auto'
|
|
|
|
# TODO
|
|
|
|
else
|
|
|
|
scale.scaleMode = 'scale_mode_fit_width'
|
|
|
|
numScale = (containerWidth - 40) / ($scope.pdfPageSize[1])
|
|
|
|
# TODO
|
|
|
|
$scope.scale.scale = numScale
|
|
|
|
$scope.document.setScale(numScale)
|
|
|
|
$scope.defaultPageSize = [
|
|
|
|
numScale * $scope.pdfPageSize[0],
|
|
|
|
numScale * $scope.pdfPageSize[1]
|
2014-11-25 11:00:21 -05:00
|
|
|
]
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'in setScale result', $scope.scale.scale, $scope.defaultPageSize
|
2015-01-26 09:37:43 -05:00
|
|
|
.catch (error) ->
|
|
|
|
$scope.$emit 'pdf:error', error
|
|
|
|
return $q.reject(error)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
@redraw = (position) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'in redraw'
|
|
|
|
# console.log 'reseting pages array for', $scope.numPages
|
2014-12-01 05:42:47 -05:00
|
|
|
$scope.pages = ({
|
|
|
|
pageNum: i + 1
|
|
|
|
} for i in [0 .. $scope.numPages-1])
|
|
|
|
if position? && position.page?
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'position is', position.page, position.offset
|
|
|
|
# console.log 'setting current page', position.page
|
2014-12-01 05:42:47 -05:00
|
|
|
pagenum = position.page
|
2015-01-12 11:47:38 -05:00
|
|
|
if pagenum > $scope.numPages - 1
|
|
|
|
pagenum = $scope.numPages - 1
|
2014-12-01 05:42:47 -05:00
|
|
|
$scope.pages[pagenum].current = true
|
|
|
|
$scope.pages[pagenum].position = position
|
|
|
|
|
|
|
|
@zoomIn = () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'zoom in'
|
2014-12-01 05:42:47 -05:00
|
|
|
newScale = $scope.scale.scale * 1.2
|
|
|
|
$scope.forceScale = { scaleMode: 'scale_mode_value', scale: newScale }
|
|
|
|
|
|
|
|
@zoomOut = () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'zoom out'
|
2014-12-01 05:42:47 -05:00
|
|
|
newScale = $scope.scale.scale / 1.2
|
|
|
|
$scope.forceScale = { scaleMode: 'scale_mode_value', scale: newScale }
|
|
|
|
|
|
|
|
@fitWidth = () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'fit width'
|
2014-12-01 05:42:47 -05:00
|
|
|
$scope.forceScale = { scaleMode: 'scale_mode_fit_width' }
|
|
|
|
|
|
|
|
@fitHeight = () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'fit height'
|
2014-12-01 05:42:47 -05:00
|
|
|
$scope.forceScale = { scaleMode: 'scale_mode_fit_height' }
|
|
|
|
|
|
|
|
@checkPosition = () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'check position'
|
2014-12-01 05:42:47 -05:00
|
|
|
$scope.forceCheck = ($scope.forceCheck || 0) + 1
|
|
|
|
|
|
|
|
@showRandomHighlights = () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'show highlights'
|
2014-12-01 05:42:47 -05:00
|
|
|
$scope.highlights = [
|
|
|
|
{
|
|
|
|
page: 3
|
|
|
|
h: 100
|
|
|
|
v: 100
|
|
|
|
height: 30
|
|
|
|
width: 200
|
|
|
|
}
|
2014-11-25 11:00:21 -05:00
|
|
|
]
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
# we work with (pagenumber, % of height down page from top)
|
|
|
|
# pdfListView works with (pagenumber, vertical position up page from
|
|
|
|
# bottom measured in pts)
|
|
|
|
|
|
|
|
@getPdfPosition = () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'in getPdfPosition'
|
2014-12-01 05:42:47 -05:00
|
|
|
topPageIdx = 0
|
|
|
|
topPage = $scope.pages[0]
|
|
|
|
# find first visible page
|
|
|
|
visible = $scope.pages.some (page, i) ->
|
|
|
|
[topPageIdx, topPage] = [i, page] if page.visible
|
2015-01-20 06:06:40 -05:00
|
|
|
if visible && topPage.element?
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'found it', topPageIdx
|
2014-12-01 05:42:47 -05:00
|
|
|
else
|
2014-12-08 10:32:05 -05:00
|
|
|
# console.log 'CANNOT FIND TOP PAGE'
|
|
|
|
return
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'top page is', topPage.pageNum, topPage.elemTop, topPage.elemBottom, topPage
|
2015-01-20 06:06:40 -05:00
|
|
|
top = topPage.element.offset().top
|
|
|
|
bottom = top + topPage.element.innerHeight()
|
|
|
|
viewportTop = $element.offset().top
|
|
|
|
viewportBottom = viewportTop + $element.height()
|
|
|
|
topVisible = (top >= viewportTop && top < viewportBottom)
|
2014-12-01 05:42:47 -05:00
|
|
|
someContentVisible = (top < viewportTop && bottom > viewportTop)
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'in PdfListView', top, topVisible, someContentVisible, viewportTop
|
2014-12-01 05:42:47 -05:00
|
|
|
if topVisible
|
|
|
|
canvasOffset = 0
|
|
|
|
else if someContentVisible
|
|
|
|
canvasOffset = viewportTop - top
|
|
|
|
else
|
|
|
|
canvasOffset = null
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'pdfListview position = ', canvasOffset
|
2014-12-01 05:42:47 -05:00
|
|
|
# instead of using promise, check if size is known and revert to
|
|
|
|
# default otherwise
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'looking up viewport', topPage.viewport, $scope.pdfViewport
|
2014-12-01 05:42:47 -05:00
|
|
|
if topPage.viewport
|
|
|
|
viewport = topPage.viewport
|
|
|
|
pdfOffset = viewport.convertToPdfPoint(0, canvasOffset);
|
|
|
|
else
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'WARNING: had to default to global page size'
|
2014-12-01 05:42:47 -05:00
|
|
|
viewport = $scope.pdfViewport
|
|
|
|
scaledOffset = canvasOffset / $scope.scale.scale
|
|
|
|
pdfOffset = viewport.convertToPdfPoint(0, scaledOffset);
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'converted to offset = ', pdfOffset
|
2014-12-01 05:42:47 -05:00
|
|
|
newPosition = {
|
|
|
|
"page": topPageIdx,
|
|
|
|
"offset" : { "top" : pdfOffset[1], "left": 0 }
|
|
|
|
}
|
|
|
|
return newPosition
|
|
|
|
|
|
|
|
@computeOffset = (page, position) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'computing offset for', page, position
|
2014-12-01 05:42:47 -05:00
|
|
|
element = page.element
|
2014-12-04 11:44:41 -05:00
|
|
|
#console.log 'element =', $(element), 'parent =', $(element).parent()
|
|
|
|
t1 = $(element).offset()?.top
|
|
|
|
t2 = $(element).parent().offset()?.top
|
|
|
|
if not (t1? and t2?)
|
|
|
|
return $q((resolve, reject) -> reject('elements destroyed'))
|
2014-12-01 05:42:47 -05:00
|
|
|
pageTop = $(element).offset().top - $(element).parent().offset().top
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log('top of page scroll is', pageTop, 'vs', page.elemTop)
|
|
|
|
# console.log('inner height is', $(element).innerHeight())
|
2014-12-01 05:42:47 -05:00
|
|
|
currentScroll = $(element).parent().scrollTop()
|
|
|
|
offset = position.offset
|
|
|
|
# convert offset to pixels
|
|
|
|
return $scope.document.getPdfViewport(page.pageNum).then (viewport) ->
|
|
|
|
page.viewport = viewport
|
|
|
|
pageOffset = viewport.convertToViewportPoint(offset.left, offset.top)
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'addition offset =', pageOffset
|
|
|
|
# console.log 'total', pageTop + pageOffset[1]
|
2014-12-01 05:42:47 -05:00
|
|
|
Math.round(pageTop + pageOffset[1] + currentScroll) ## 10 is margin
|
|
|
|
|
|
|
|
@setPdfPosition = (page, position) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'required pdf Position is', position
|
2014-12-01 05:42:47 -05:00
|
|
|
@computeOffset(page, position).then (offset) ->
|
|
|
|
$scope.pleaseScrollTo = offset
|
|
|
|
$scope.position = position
|
|
|
|
|
|
|
|
return this
|
|
|
|
|
|
|
|
]
|
|
|
|
|
2014-12-08 09:46:59 -05:00
|
|
|
App.directive 'pdfViewer', ['$q', '$timeout', 'pdfSpinner', ($q, $timeout, pdfSpinner) ->
|
2014-12-01 05:42:47 -05:00
|
|
|
{
|
|
|
|
controller: 'pdfViewerController'
|
|
|
|
controllerAs: 'ctrl'
|
|
|
|
scope: {
|
|
|
|
"pdfSrc": "="
|
|
|
|
"highlights": "="
|
|
|
|
"position": "="
|
|
|
|
"scale": "="
|
|
|
|
"pleaseJumpTo": "="
|
|
|
|
}
|
|
|
|
template: """
|
|
|
|
<div data-pdf-page class='pdf-page-container page-container' ng-repeat='page in pages'></div>
|
|
|
|
"""
|
|
|
|
link: (scope, element, attrs, ctrl) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'in pdfViewer element is', element
|
|
|
|
# console.log 'attrs', attrs
|
2014-12-08 09:46:59 -05:00
|
|
|
spinner = new pdfSpinner
|
2014-12-01 05:42:47 -05:00
|
|
|
layoutReady = $q.defer()
|
|
|
|
layoutReady.notify 'waiting for layout'
|
2014-11-25 11:00:21 -05:00
|
|
|
layoutReady.promise.then () ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'layoutReady was resolved'
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2015-01-20 06:06:40 -05:00
|
|
|
renderVisiblePages = () ->
|
2015-01-21 09:37:59 -05:00
|
|
|
pages = getVisiblePages()
|
|
|
|
# pages = getExtraPages visiblePages
|
2015-01-20 06:06:40 -05:00
|
|
|
scope.document.renderPages(pages)
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2015-01-19 11:55:44 -05:00
|
|
|
getVisiblePages = () ->
|
2015-01-20 11:20:12 -05:00
|
|
|
top = element[0].scrollTop;
|
|
|
|
bottom = top + element[0].clientHeight;
|
2015-01-20 15:35:29 -05:00
|
|
|
visiblePages = scope.pages.filter (page) ->
|
|
|
|
pageElement = page.element[0]
|
2015-01-20 10:36:52 -05:00
|
|
|
pageTop = pageElement.offsetTop
|
|
|
|
pageBottom = pageTop + pageElement.clientHeight
|
2015-01-20 15:35:29 -05:00
|
|
|
page.visible = pageTop < bottom and pageBottom > top
|
|
|
|
return page.visible
|
2015-01-20 06:06:40 -05:00
|
|
|
return visiblePages
|
2015-01-19 11:55:44 -05:00
|
|
|
|
|
|
|
getExtraPages = (visiblePages) ->
|
|
|
|
extra = []
|
|
|
|
firstVisiblePage = visiblePages[0].pageNum
|
|
|
|
firstVisiblePageIdx = firstVisiblePage - 1
|
|
|
|
len = visiblePages.length
|
|
|
|
lastVisiblePage = visiblePages[len-1].pageNum
|
|
|
|
lastVisiblePageIdx = lastVisiblePage - 1
|
|
|
|
# first page after
|
2015-01-20 09:28:43 -05:00
|
|
|
if lastVisiblePageIdx + 1 < scope.pages.length
|
2015-01-19 11:55:44 -05:00
|
|
|
extra.push scope.pages[lastVisiblePageIdx + 1]
|
|
|
|
# page before
|
|
|
|
if firstVisiblePageIdx > 0
|
|
|
|
extra.push scope.pages[firstVisiblePageIdx - 1]
|
|
|
|
# second page after
|
2015-01-20 09:28:43 -05:00
|
|
|
if lastVisiblePageIdx + 2 < scope.pages.length
|
2015-01-19 11:55:44 -05:00
|
|
|
extra.push scope.pages[lastVisiblePageIdx + 2]
|
|
|
|
return visiblePages.concat extra
|
|
|
|
|
2015-01-26 05:20:30 -05:00
|
|
|
rescaleTimer = null
|
|
|
|
queueRescale = (scale) ->
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'call to queueRescale'
|
2015-01-26 05:20:30 -05:00
|
|
|
return if rescaleTimer? or layoutTimer? or elementTimer?
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'adding to rescale queue'
|
2015-01-26 05:20:30 -05:00
|
|
|
rescaleTimer = setTimeout () ->
|
|
|
|
doRescale scale
|
|
|
|
rescaleTimer = null
|
|
|
|
, 0
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
doRescale = (scale) ->
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'doRescale', scale
|
2015-01-23 11:58:53 -05:00
|
|
|
return unless scale?
|
2014-12-01 05:42:47 -05:00
|
|
|
origposition = angular.copy scope.position
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'origposition', origposition
|
2015-01-23 11:58:53 -05:00
|
|
|
layoutReady.promise.then (parentSize) ->
|
|
|
|
[h, w] = parentSize
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'in promise', h, w
|
2014-12-01 05:42:47 -05:00
|
|
|
ctrl.setScale(scale, h, w).then () ->
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'in setscale then', scale, h, w
|
2015-01-23 11:58:53 -05:00
|
|
|
scope.$evalAsync () ->
|
2015-01-26 05:20:30 -05:00
|
|
|
if spinnerTimer
|
|
|
|
clearTimeout spinnerTimer
|
|
|
|
else
|
|
|
|
spinner.remove(element)
|
2015-01-23 11:58:53 -05:00
|
|
|
ctrl.redraw(origposition)
|
|
|
|
$timeout renderVisiblePages
|
2015-01-26 09:37:43 -05:00
|
|
|
scope.loadSuccess = true
|
|
|
|
.catch (error) ->
|
|
|
|
scope.$emit 'pdf:error', error
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2015-01-23 11:58:53 -05:00
|
|
|
elementTimer = null
|
2015-01-26 05:20:30 -05:00
|
|
|
spinnerTimer = null
|
2015-01-23 11:58:53 -05:00
|
|
|
updateLayout = () ->
|
2014-12-09 09:40:54 -05:00
|
|
|
# if element is zero-sized keep checking until it is ready
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'checking element ready', element.height(), element.width()
|
2014-12-08 09:46:59 -05:00
|
|
|
if element.height() == 0 or element.width() == 0
|
2015-01-23 11:58:53 -05:00
|
|
|
return if elementTimer?
|
|
|
|
elementTimer = setTimeout () ->
|
|
|
|
elementTimer = null
|
|
|
|
updateLayout()
|
|
|
|
, 1000
|
2014-12-09 09:40:54 -05:00
|
|
|
else
|
2015-01-23 11:58:53 -05:00
|
|
|
scope.parentSize = [
|
|
|
|
element.innerHeight(),
|
|
|
|
element.innerWidth()
|
|
|
|
]
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'resolving layoutReady with', scope.parentSize
|
2015-01-23 11:58:53 -05:00
|
|
|
$timeout () ->
|
2015-01-26 05:20:30 -05:00
|
|
|
if not spinnerTimer?
|
|
|
|
spinnerTimer = setTimeout () ->
|
|
|
|
spinner.add(element)
|
|
|
|
spinnerTimer = null
|
|
|
|
, 100
|
2015-01-23 11:58:53 -05:00
|
|
|
layoutReady.resolve scope.parentSize
|
|
|
|
scope.$emit 'flash-controls'
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2015-01-26 05:20:30 -05:00
|
|
|
layoutTimer = null
|
|
|
|
queueLayout = () ->
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'call to queue layout'
|
2015-01-26 05:20:30 -05:00
|
|
|
return if layoutTimer?
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'added to queue layoyt'
|
2015-01-26 05:20:30 -05:00
|
|
|
layoutReady = $q.defer()
|
|
|
|
layoutTimer = setTimeout () ->
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'calling update layout'
|
2015-01-26 05:20:30 -05:00
|
|
|
updateLayout()
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'setting layout timer to null'
|
2015-01-26 05:20:30 -05:00
|
|
|
layoutTimer = null
|
|
|
|
, 0
|
|
|
|
|
|
|
|
queueLayout()
|
|
|
|
|
2015-01-26 05:36:56 -05:00
|
|
|
#scope.$on 'layout:pdf:view', (e, args) ->
|
|
|
|
# console.log 'pdf view change', element, e, args
|
|
|
|
# queueLayout()
|
2014-12-08 05:28:02 -05:00
|
|
|
|
2015-01-23 11:58:53 -05:00
|
|
|
scope.$on 'layout:main:resize', () ->
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'GOT LAYOUT-MAIN-RESIZE EVENT'
|
2015-01-26 05:20:30 -05:00
|
|
|
queueLayout()
|
2014-12-08 05:28:02 -05:00
|
|
|
|
|
|
|
scope.$on 'layout:pdf:resize', () ->
|
2015-01-23 15:53:27 -05:00
|
|
|
# FIXME we get this event twice
|
|
|
|
# also we need to start a new layout when we get it
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'GOT LAYOUT-PDF-RESIZE EVENT'
|
2015-01-26 05:20:30 -05:00
|
|
|
queueLayout()
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2015-01-12 11:46:17 -05:00
|
|
|
scope.$on 'pdf:error', (event, error) ->
|
|
|
|
return if error == 'cancelled'
|
|
|
|
# check if too many retries or file is missing
|
2015-01-26 09:00:22 -05:00
|
|
|
if scope.loadCount > 3 || error.match(/^Missing PDF/i) || error.match(/^loading/i)
|
2015-01-12 11:46:17 -05:00
|
|
|
scope.$emit 'pdf:error:display'
|
|
|
|
return
|
2015-01-26 09:37:43 -05:00
|
|
|
if scope.loadSuccess
|
2015-01-26 09:53:35 -05:00
|
|
|
ctrl.load().then () ->
|
|
|
|
# trigger a redraw
|
|
|
|
scope.scale = angular.copy (scope.scale)
|
|
|
|
.catch (error) ->
|
|
|
|
scope.$emit 'pdf:error:display'
|
2015-01-26 09:37:43 -05:00
|
|
|
else
|
|
|
|
scope.$emit 'pdf:error:display'
|
|
|
|
return
|
2015-01-12 11:46:17 -05:00
|
|
|
|
2015-01-20 09:28:14 -05:00
|
|
|
scope.$on 'pdf:page:size-change', (event, pageNum, delta) ->
|
|
|
|
#console.log 'page size change event', pageNum, delta
|
|
|
|
origposition = angular.copy scope.position
|
|
|
|
#console.log 'orig position', JSON.stringify(origposition)
|
|
|
|
if pageNum - 1 < origposition.page && delta != 0
|
|
|
|
currentScrollTop = element.scrollTop()
|
2015-01-20 11:22:34 -05:00
|
|
|
#console.log 'adjusting scroll from', currentScrollTop, 'by', delta
|
2015-01-20 09:28:14 -05:00
|
|
|
scope.adjustingScroll = true
|
|
|
|
element.scrollTop(currentScrollTop + delta)
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
element.on 'scroll', () ->
|
2014-12-05 07:12:01 -05:00
|
|
|
#console.log 'scroll event', element.scrollTop(), 'adjusting?', scope.adjustingScroll
|
2015-01-20 06:06:40 -05:00
|
|
|
#scope.scrollPosition = element.scrollTop()
|
2014-12-01 05:42:47 -05:00
|
|
|
if scope.adjustingScroll
|
2015-01-20 06:06:40 -05:00
|
|
|
renderVisiblePages()
|
2014-12-01 05:42:47 -05:00
|
|
|
scope.adjustingScroll = false
|
|
|
|
return
|
2015-01-19 11:55:44 -05:00
|
|
|
if scope.scrollHandlerTimeout
|
2015-01-20 11:20:38 -05:00
|
|
|
clearTimeout(scope.scrollHandlerTimeout)
|
|
|
|
scope.scrollHandlerTimeout = setTimeout scrollHandler, 25
|
2014-12-04 11:49:24 -05:00
|
|
|
|
2014-12-05 07:12:01 -05:00
|
|
|
scrollHandler = () ->
|
2015-01-20 06:06:40 -05:00
|
|
|
renderVisiblePages()
|
2014-12-08 10:32:05 -05:00
|
|
|
newPosition = ctrl.getPdfPosition()
|
|
|
|
if newPosition?
|
|
|
|
scope.position = newPosition
|
2015-01-19 11:55:44 -05:00
|
|
|
scope.scrollHandlerTimeout = null
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
scope.$watch 'pdfSrc', (newVal, oldVal) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'loading pdf', newVal, oldVal
|
2014-12-01 05:42:47 -05:00
|
|
|
return unless newVal?
|
2015-01-12 11:46:17 -05:00
|
|
|
scope.loadCount = 0; # new pdf, so reset load count
|
2015-01-26 09:37:43 -05:00
|
|
|
scope.loadSuccess = false
|
2015-01-26 09:53:35 -05:00
|
|
|
ctrl.load().then () ->
|
|
|
|
# trigger a redraw
|
|
|
|
scope.scale = angular.copy (scope.scale)
|
|
|
|
.catch (error) ->
|
|
|
|
scope.$emit 'pdf:error', error
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
scope.$watch 'scale', (newVal, oldVal) ->
|
|
|
|
# no need to set scale when initialising, done in pdfSrc
|
|
|
|
return if newVal == oldVal
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'XXX calling Setscale in scale watch'
|
2015-01-26 05:20:30 -05:00
|
|
|
queueRescale newVal
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
scope.$watch 'forceScale', (newVal, oldVal) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'got change in numscale watcher', newVal, oldVal
|
2014-12-01 05:42:47 -05:00
|
|
|
return unless newVal?
|
2015-01-26 05:20:30 -05:00
|
|
|
queueRescale newVal
|
2014-12-01 05:42:47 -05:00
|
|
|
|
2014-12-02 06:42:57 -05:00
|
|
|
# scope.$watch 'position', (newVal, oldVal) ->
|
|
|
|
# console.log 'got change in position watcher', newVal, oldVal
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
scope.$watch 'forceCheck', (newVal, oldVal) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'forceCheck', newVal, oldVal
|
2014-12-01 05:42:47 -05:00
|
|
|
return unless newVal?
|
|
|
|
scope.adjustingScroll = true # temporarily disable scroll
|
2015-01-26 05:20:30 -05:00
|
|
|
queueRescale scope.scale
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
scope.$watch('parentSize', (newVal, oldVal) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'XXX in parentSize watch', newVal, oldVal
|
2014-12-05 14:42:16 -05:00
|
|
|
# if newVal == oldVal
|
|
|
|
# console.log 'returning because old and new are the same'
|
|
|
|
# return
|
2015-01-23 11:58:53 -05:00
|
|
|
# return unless oldVal?
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'XXX calling setScale in parentSize watcher'
|
2015-01-23 11:58:53 -05:00
|
|
|
return unless newVal?
|
2015-01-26 05:20:30 -05:00
|
|
|
queueRescale scope.scale
|
2014-12-01 05:42:47 -05:00
|
|
|
, true)
|
|
|
|
|
2014-12-02 06:42:57 -05:00
|
|
|
# scope.$watch 'elementWidth', (newVal, oldVal) ->
|
|
|
|
# console.log '*** watch INTERVAL element width is', newVal, oldVal
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
scope.$watch 'pleaseScrollTo', (newVal, oldVal) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'got request to ScrollTo', newVal, 'oldVal', oldVal
|
2014-12-01 05:42:47 -05:00
|
|
|
return unless newVal?
|
|
|
|
scope.adjustingScroll = true # temporarily disable scroll
|
|
|
|
# handler while we reposition
|
|
|
|
$(element).scrollTop(newVal)
|
|
|
|
scope.pleaseScrollTo = undefined
|
|
|
|
|
|
|
|
scope.$watch 'pleaseJumpTo', (newPosition, oldPosition) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'in pleaseJumpTo', newPosition, oldPosition
|
2014-12-01 05:42:47 -05:00
|
|
|
return unless newPosition?
|
|
|
|
ctrl.setPdfPosition scope.pages[newPosition.page-1], newPosition
|
|
|
|
|
|
|
|
scope.$watch 'navigateTo', (newVal, oldVal) ->
|
|
|
|
return unless newVal?
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'got request to navigate to', newVal, 'oldVal', oldVal
|
2014-12-01 05:42:47 -05:00
|
|
|
scope.navigateTo = undefined
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'navigate to', newVal
|
|
|
|
# console.log 'look up page num'
|
2014-12-09 08:43:25 -05:00
|
|
|
scope.document.getDestination(newVal.dest).then (r) ->
|
2014-12-09 09:39:58 -05:00
|
|
|
# console.log 'need to go to', r
|
|
|
|
# console.log 'page ref is', r[0]
|
2014-12-01 05:42:47 -05:00
|
|
|
scope.document.getPageIndex(r[0]).then (pidx) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'page num is', pidx
|
2014-12-01 05:42:47 -05:00
|
|
|
page = scope.pages[pidx]
|
|
|
|
scope.document.getPdfViewport(page.pageNum).then (viewport) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
#console.log 'got viewport', viewport
|
2014-12-01 05:42:47 -05:00
|
|
|
coords = viewport.convertToViewportPoint r[2], r[3]
|
2014-12-02 06:42:57 -05:00
|
|
|
#console.log 'viewport position', coords
|
|
|
|
#console.log 'r is', r, 'r[1]', r[1], 'r[1].name', r[1].name
|
2014-12-01 05:42:47 -05:00
|
|
|
if r[1].name == 'XYZ'
|
2014-12-02 06:42:57 -05:00
|
|
|
#console.log 'XYZ:', r[2], r[3]
|
2014-12-01 05:42:47 -05:00
|
|
|
newPosition = {page: pidx, offset: {top: r[3], left: r[2]}}
|
|
|
|
ctrl.setPdfPosition scope.pages[pidx], newPosition # XXX?
|
|
|
|
|
|
|
|
scope.$watch "highlights", (areas) ->
|
2014-12-02 06:42:57 -05:00
|
|
|
# console.log 'got HIGHLIGHTS in pdfViewer', areas
|
2014-12-01 05:42:47 -05:00
|
|
|
return if !areas?
|
2014-12-02 06:42:57 -05:00
|
|
|
#console.log 'areas are', areas
|
2014-12-01 05:42:47 -05:00
|
|
|
highlights = for area in areas or []
|
|
|
|
{
|
|
|
|
page: area.page - 1
|
|
|
|
highlight:
|
|
|
|
left: area.h
|
|
|
|
top: area.v
|
|
|
|
height: area.height
|
|
|
|
width: area.width
|
|
|
|
}
|
2014-12-02 06:42:57 -05:00
|
|
|
#console.log 'highlights', highlights
|
2014-12-01 05:42:47 -05:00
|
|
|
|
|
|
|
return if !highlights.length
|
|
|
|
|
2015-01-19 09:25:38 -05:00
|
|
|
scope.$broadcast 'pdf:highlights', areas
|
|
|
|
|
2014-12-01 05:42:47 -05:00
|
|
|
first = highlights[0]
|
|
|
|
|
|
|
|
pageNum = scope.pages[first.page].pageNum
|
|
|
|
|
|
|
|
scope.document.getPdfViewport(pageNum).then (viewport) ->
|
|
|
|
position = {
|
|
|
|
page: first.page
|
|
|
|
offset:
|
|
|
|
left: first.highlight.left
|
|
|
|
top: viewport.viewBox[3] - first.highlight.top + first.highlight.height + 72
|
|
|
|
}
|
|
|
|
ctrl.setPdfPosition(scope.pages[first.page], position)
|
2014-11-25 11:00:21 -05:00
|
|
|
|
2015-01-23 11:58:53 -05:00
|
|
|
scope.$on '$destroy', () ->
|
2015-01-26 05:36:56 -05:00
|
|
|
# console.log 'handle pdfng directive destroy'
|
2015-01-23 11:58:53 -05:00
|
|
|
clearTimeout elementTimer if elementTimer?
|
2015-01-26 05:20:30 -05:00
|
|
|
clearTimeout layoutTimer if layoutTimer?
|
|
|
|
clearTimeout rescaleTimer if rescaleTimer?
|
|
|
|
clearTimeout spinnerTimer if spinnerTimer?
|
2014-12-01 05:42:47 -05:00
|
|
|
}
|
|
|
|
]
|