2014-12-01 10:42:47 +00:00
|
|
|
define [
|
|
|
|
"base"
|
|
|
|
], (App) ->
|
|
|
|
# App = angular.module 'pdfPage', ['pdfHighlights']
|
2014-11-25 16:00:21 +00:00
|
|
|
|
2016-07-01 15:33:54 +00:00
|
|
|
App.directive 'pdfPage', ['$timeout', 'pdfHighlights', 'pdfSpinner', ($timeout, pdfHighlights, pdfSpinner) ->
|
2014-12-01 10:42:47 +00:00
|
|
|
{
|
|
|
|
require: '^pdfViewer',
|
|
|
|
template: '''
|
|
|
|
<div class="plv-page-view page-view">
|
2014-12-08 14:46:59 +00:00
|
|
|
<div class="pdf-canvas pdfng-empty"></div>
|
2014-12-01 10:42:47 +00:00
|
|
|
<div class="plv-text-layer text-layer"></div>
|
|
|
|
<div class="plv-annotations-layer annotations-layer"></div>
|
|
|
|
<div class="plv-highlights-layer highlights-layer"></div>
|
|
|
|
</div>
|
|
|
|
'''
|
|
|
|
link: (scope, element, attrs, ctrl) ->
|
|
|
|
canvasElement = $(element).find('.pdf-canvas')
|
|
|
|
textElement = $(element).find('.text-layer')
|
|
|
|
annotationsElement = $(element).find('.annotations-layer')
|
|
|
|
highlightsElement = $(element).find('.highlights-layer')
|
2014-11-25 16:00:21 +00:00
|
|
|
|
2014-12-01 10:42:47 +00:00
|
|
|
updatePageSize = (size) ->
|
2014-12-02 11:01:45 +00:00
|
|
|
h = Math.floor(size[0])
|
|
|
|
w = Math.floor(size[1])
|
|
|
|
element.height(h)
|
|
|
|
element.width(w)
|
|
|
|
canvasElement.height(h)
|
|
|
|
canvasElement.width(w)
|
2014-12-01 10:42:47 +00:00
|
|
|
scope.page.sized = true
|
2014-11-25 16:00:21 +00:00
|
|
|
|
2014-12-01 10:42:47 +00:00
|
|
|
# keep track of our page element, so we can access it in the
|
2015-01-19 16:55:44 +00:00
|
|
|
# parent with scope.pages[i].element, and the contained
|
|
|
|
# elements for each part
|
2014-12-01 10:42:47 +00:00
|
|
|
scope.page.element = element
|
2015-01-19 16:55:44 +00:00
|
|
|
scope.page.elementChildren = {
|
|
|
|
canvas: canvasElement,
|
|
|
|
text: textElement
|
|
|
|
annotations: annotationsElement
|
|
|
|
highlights: highlightsElement
|
2015-01-20 14:28:14 +00:00
|
|
|
container: element
|
2015-01-19 16:55:44 +00:00
|
|
|
}
|
2014-11-25 16:00:21 +00:00
|
|
|
|
2015-01-19 14:36:20 +00:00
|
|
|
if !scope.page.sized
|
|
|
|
if scope.defaultPageSize?
|
|
|
|
updatePageSize scope.defaultPageSize
|
|
|
|
else
|
|
|
|
# shouldn't get here - the default page size should now
|
|
|
|
# always be set before redraw is called
|
|
|
|
handler = scope.$watch 'defaultPageSize', (defaultPageSize) ->
|
|
|
|
return unless defaultPageSize?
|
|
|
|
updatePageSize defaultPageSize
|
|
|
|
handler()
|
2014-11-25 16:00:21 +00:00
|
|
|
|
2014-12-01 10:42:47 +00:00
|
|
|
if scope.page.current
|
2014-12-02 11:42:57 +00:00
|
|
|
# console.log 'we must scroll to this page', scope.page.pageNum, 'at position', scope.page.position
|
2014-12-01 10:42:47 +00:00
|
|
|
# this is the current page, we want to scroll it into view
|
2015-01-20 21:33:49 +00:00
|
|
|
# and render it immediately
|
|
|
|
scope.document.renderPage scope.page
|
2014-12-04 16:50:00 +00:00
|
|
|
ctrl.setPdfPosition(scope.page, scope.page.position)
|
2014-11-25 16:00:21 +00:00
|
|
|
|
2014-12-05 14:33:06 +00:00
|
|
|
element.on 'dblclick', (e) ->
|
|
|
|
offset = $(element).find('.pdf-canvas').offset()
|
|
|
|
dx = e.pageX - offset.left
|
|
|
|
dy = e.pageY - offset.top
|
|
|
|
scope.document.getPdfViewport(scope.page.pageNum).then (viewport) ->
|
|
|
|
pdfPoint = viewport.convertToPdfPoint(dx, dy);
|
|
|
|
event = {
|
|
|
|
page: scope.page.pageNum
|
|
|
|
x: pdfPoint[0],
|
|
|
|
y: viewport.viewBox[3] - pdfPoint[1]
|
|
|
|
}
|
|
|
|
scope.$emit 'pdfDoubleClick', event
|
|
|
|
|
2014-12-01 10:42:47 +00:00
|
|
|
highlightsLayer = new pdfHighlights({
|
|
|
|
highlights: highlightsElement
|
|
|
|
})
|
2014-11-28 11:57:23 +00:00
|
|
|
|
2015-01-19 14:25:38 +00:00
|
|
|
scope.$on 'pdf:highlights', (event, highlights) ->
|
2014-12-01 10:42:47 +00:00
|
|
|
return unless highlights?
|
|
|
|
return unless highlights.length > 0
|
2014-12-04 16:50:38 +00:00
|
|
|
if scope.timeoutHandler
|
|
|
|
$timeout.cancel(scope.timeoutHandler)
|
|
|
|
highlightsLayer.clearHighlights()
|
2016-07-05 15:09:33 +00:00
|
|
|
scope.timeoutHandler = null
|
2014-12-04 16:50:38 +00:00
|
|
|
|
2014-12-02 11:42:57 +00:00
|
|
|
# console.log 'got highlight watch in pdfPage', scope.page
|
2014-12-01 10:42:47 +00:00
|
|
|
pageHighlights = (h for h in highlights when h.page == scope.page.pageNum)
|
|
|
|
return unless pageHighlights.length
|
|
|
|
scope.document.getPdfViewport(scope.page.pageNum).then (viewport) ->
|
|
|
|
for hl in pageHighlights
|
2014-12-02 11:42:57 +00:00
|
|
|
# console.log 'adding highlight', h, viewport
|
2014-12-01 10:42:47 +00:00
|
|
|
top = viewport.viewBox[3] - hl.v
|
|
|
|
highlightsLayer.addHighlight viewport, hl.h, top, hl.width, hl.height
|
2014-12-04 16:50:38 +00:00
|
|
|
scope.timeoutHandler = $timeout () ->
|
2014-12-01 10:42:47 +00:00
|
|
|
highlightsLayer.clearHighlights()
|
2014-12-04 16:50:38 +00:00
|
|
|
scope.timeoutHandler = null
|
2014-12-01 10:42:47 +00:00
|
|
|
, 1000
|
2014-11-28 11:57:23 +00:00
|
|
|
|
2014-12-01 10:42:47 +00:00
|
|
|
scope.$on "$destroy", () ->
|
2014-12-05 12:12:17 +00:00
|
|
|
if scope.timeoutHandler?
|
2014-12-04 16:50:38 +00:00
|
|
|
$timeout.cancel(scope.timeoutHandler)
|
|
|
|
highlightsLayer.clearHighlights()
|
2014-11-28 15:08:24 +00:00
|
|
|
|
2014-12-01 10:42:47 +00:00
|
|
|
}
|
|
|
|
]
|