annotation_layer_builder.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var uiUtils = require('./ui_utils.js');
  17. var pdfLinkService = require('./pdf_link_service.js');
  18. var pdfjsLib = require('./pdfjs.js');
  19. var mozL10n = uiUtils.mozL10n;
  20. var SimpleLinkService = pdfLinkService.SimpleLinkService;
  21. var AnnotationLayerBuilder = function AnnotationLayerBuilderClosure() {
  22. function AnnotationLayerBuilder(options) {
  23. this.pageDiv = options.pageDiv;
  24. this.pdfPage = options.pdfPage;
  25. this.renderInteractiveForms = options.renderInteractiveForms;
  26. this.linkService = options.linkService;
  27. this.downloadManager = options.downloadManager;
  28. this.div = null;
  29. }
  30. AnnotationLayerBuilder.prototype = {
  31. render: function AnnotationLayerBuilder_render(viewport, intent) {
  32. var self = this;
  33. var parameters = { intent: intent === undefined ? 'display' : intent };
  34. this.pdfPage.getAnnotations(parameters).then(function (annotations) {
  35. viewport = viewport.clone({ dontFlip: true });
  36. parameters = {
  37. viewport: viewport,
  38. div: self.div,
  39. annotations: annotations,
  40. page: self.pdfPage,
  41. renderInteractiveForms: self.renderInteractiveForms,
  42. linkService: self.linkService,
  43. downloadManager: self.downloadManager
  44. };
  45. if (self.div) {
  46. pdfjsLib.AnnotationLayer.update(parameters);
  47. } else {
  48. if (annotations.length === 0) {
  49. return;
  50. }
  51. self.div = document.createElement('div');
  52. self.div.className = 'annotationLayer';
  53. self.pageDiv.appendChild(self.div);
  54. parameters.div = self.div;
  55. pdfjsLib.AnnotationLayer.render(parameters);
  56. if (typeof mozL10n !== 'undefined') {
  57. mozL10n.translate(self.div);
  58. }
  59. }
  60. });
  61. },
  62. hide: function AnnotationLayerBuilder_hide() {
  63. if (!this.div) {
  64. return;
  65. }
  66. this.div.setAttribute('hidden', 'true');
  67. }
  68. };
  69. return AnnotationLayerBuilder;
  70. }();
  71. function DefaultAnnotationLayerFactory() {}
  72. DefaultAnnotationLayerFactory.prototype = {
  73. createAnnotationLayerBuilder: function (pageDiv, pdfPage, renderInteractiveForms) {
  74. return new AnnotationLayerBuilder({
  75. pageDiv: pageDiv,
  76. pdfPage: pdfPage,
  77. renderInteractiveForms: renderInteractiveForms,
  78. linkService: new SimpleLinkService()
  79. });
  80. }
  81. };
  82. exports.AnnotationLayerBuilder = AnnotationLayerBuilder;
  83. exports.DefaultAnnotationLayerFactory = DefaultAnnotationLayerFactory;