2014-09-30 01:05:28 +09:00
|
|
|
/* Copyright 2014 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-11-10 10:24:15 +09:00
|
|
|
/*globals PDFJS, mozL10n, SimpleLinkService */
|
2014-09-30 01:05:28 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
2015-12-17 20:54:53 +09:00
|
|
|
* @typedef {Object} AnnotationLayerBuilderOptions
|
2014-09-30 01:05:28 +09:00
|
|
|
* @property {HTMLDivElement} pageDiv
|
|
|
|
* @property {PDFPage} pdfPage
|
|
|
|
* @property {IPDFLinkService} linkService
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*/
|
2015-12-17 20:54:53 +09:00
|
|
|
var AnnotationLayerBuilder = (function AnnotationLayerBuilderClosure() {
|
2014-09-30 01:05:28 +09:00
|
|
|
/**
|
2015-12-17 20:54:53 +09:00
|
|
|
* @param {AnnotationLayerBuilderOptions} options
|
|
|
|
* @constructs AnnotationLayerBuilder
|
2014-09-30 01:05:28 +09:00
|
|
|
*/
|
2015-12-17 20:54:53 +09:00
|
|
|
function AnnotationLayerBuilder(options) {
|
2014-09-30 01:05:28 +09:00
|
|
|
this.pageDiv = options.pageDiv;
|
|
|
|
this.pdfPage = options.pdfPage;
|
|
|
|
this.linkService = options.linkService;
|
|
|
|
|
|
|
|
this.div = null;
|
|
|
|
}
|
2015-12-03 08:20:18 +09:00
|
|
|
|
2015-12-17 20:54:53 +09:00
|
|
|
AnnotationLayerBuilder.prototype =
|
|
|
|
/** @lends AnnotationLayerBuilder.prototype */ {
|
2014-09-30 01:05:28 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {PageViewport} viewport
|
2015-11-22 21:56:52 +09:00
|
|
|
* @param {string} intent (default value is 'display')
|
2014-09-30 01:05:28 +09:00
|
|
|
*/
|
2015-12-17 20:54:53 +09:00
|
|
|
render: function AnnotationLayerBuilder_render(viewport, intent) {
|
2014-09-30 01:05:28 +09:00
|
|
|
var self = this;
|
2015-12-09 07:38:32 +09:00
|
|
|
var parameters = {
|
2015-11-22 21:56:52 +09:00
|
|
|
intent: (intent === undefined ? 'display' : intent),
|
|
|
|
};
|
2014-09-30 01:05:28 +09:00
|
|
|
|
2015-12-09 07:38:32 +09:00
|
|
|
this.pdfPage.getAnnotations(parameters).then(function (annotations) {
|
2014-09-30 01:05:28 +09:00
|
|
|
viewport = viewport.clone({ dontFlip: true });
|
2015-12-17 23:55:11 +09:00
|
|
|
parameters = {
|
|
|
|
viewport: viewport,
|
|
|
|
div: self.div,
|
|
|
|
annotations: annotations,
|
|
|
|
page: self.pdfPage,
|
|
|
|
linkService: self.linkService
|
|
|
|
};
|
2014-09-30 01:05:28 +09:00
|
|
|
|
|
|
|
if (self.div) {
|
|
|
|
// If an annotationLayer already exists, refresh its children's
|
2015-12-09 07:38:32 +09:00
|
|
|
// transformation matrices.
|
2015-12-17 23:55:11 +09:00
|
|
|
PDFJS.AnnotationLayer.update(parameters);
|
2014-09-30 01:05:28 +09:00
|
|
|
} else {
|
2015-12-16 00:48:55 +09:00
|
|
|
// Create an annotation layer div and render the annotations
|
|
|
|
// if there is at least one annotation.
|
2015-12-09 07:38:32 +09:00
|
|
|
if (annotations.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.div = document.createElement('div');
|
|
|
|
self.div.className = 'annotationLayer';
|
|
|
|
self.pageDiv.appendChild(self.div);
|
2015-12-17 23:55:11 +09:00
|
|
|
parameters.div = self.div;
|
2015-12-09 07:38:32 +09:00
|
|
|
|
2015-12-17 23:55:11 +09:00
|
|
|
PDFJS.AnnotationLayer.render(parameters);
|
2015-12-16 00:48:55 +09:00
|
|
|
if (typeof mozL10n !== 'undefined') {
|
|
|
|
mozL10n.translate(self.div);
|
2014-09-30 01:05:28 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-12-17 20:54:53 +09:00
|
|
|
hide: function AnnotationLayerBuilder_hide() {
|
2014-09-30 01:05:28 +09:00
|
|
|
if (!this.div) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.div.setAttribute('hidden', 'true');
|
|
|
|
}
|
|
|
|
};
|
2015-12-03 08:20:18 +09:00
|
|
|
|
2015-12-17 20:54:53 +09:00
|
|
|
return AnnotationLayerBuilder;
|
2014-09-30 01:05:28 +09:00
|
|
|
})();
|
2014-12-18 05:47:14 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
2015-12-17 20:54:53 +09:00
|
|
|
* @implements IPDFAnnotationLayerFactory
|
2014-12-18 05:47:14 +09:00
|
|
|
*/
|
2015-12-17 20:54:53 +09:00
|
|
|
function DefaultAnnotationLayerFactory() {}
|
|
|
|
DefaultAnnotationLayerFactory.prototype = {
|
2014-12-18 05:47:14 +09:00
|
|
|
/**
|
|
|
|
* @param {HTMLDivElement} pageDiv
|
|
|
|
* @param {PDFPage} pdfPage
|
2015-12-17 20:54:53 +09:00
|
|
|
* @returns {AnnotationLayerBuilder}
|
2014-12-18 05:47:14 +09:00
|
|
|
*/
|
2015-12-17 20:54:53 +09:00
|
|
|
createAnnotationLayerBuilder: function (pageDiv, pdfPage) {
|
|
|
|
return new AnnotationLayerBuilder({
|
2014-12-18 05:47:14 +09:00
|
|
|
pageDiv: pageDiv,
|
Simplify the SimpleLinkService and use it to pass in a linkService instance in DefaultAnnotationsLayerFactory
Considering that most methods of `SimpleLinkService` are complete stubs, or practically "useless" considering what they return, we can actually simplify it even more.
*Note:* This depends on the previous patch, that did a small amount of refactoring of `PDFViewer_scrollPageIntoView`, since `PDFViewer.linkService.page` is no longer accessed.
----------
Currently the `pageviewer` components example doesn't work correctly (an error is printed in the console), since no `linkService` is present when the `AnnotationsLayerBuilder` is created.
*Note:* Given that this uses the `SimpleLinkService`, clicking on e.g. internal links won't actually do anything. However, given that internal links (and similar features) are pretty much useless when only *one* page is loaded the `pageviewer` example, I don't think that really matters.
Also, using the complete `PDFLinkService` would require a `PDFViewer` instance. That would significantly complicate the example, thus making it both less clear and less self contained.
2015-06-12 05:20:04 +09:00
|
|
|
pdfPage: pdfPage,
|
|
|
|
linkService: new SimpleLinkService(),
|
2014-12-18 05:47:14 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|