pdf_attachment_viewer.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFAttachmentViewer = void 0;
  27. var _pdf = require("../pdf");
  28. class PDFAttachmentViewer {
  29. constructor({
  30. container,
  31. eventBus,
  32. downloadManager
  33. }) {
  34. this.container = container;
  35. this.eventBus = eventBus;
  36. this.downloadManager = downloadManager;
  37. this.reset();
  38. this.eventBus._on("fileattachmentannotation", this._appendAttachment.bind(this));
  39. }
  40. reset(keepRenderedCapability = false) {
  41. this.attachments = null;
  42. this.container.textContent = "";
  43. if (!keepRenderedCapability) {
  44. this._renderedCapability = (0, _pdf.createPromiseCapability)();
  45. }
  46. }
  47. _dispatchEvent(attachmentsCount) {
  48. this._renderedCapability.resolve();
  49. this.eventBus.dispatch("attachmentsloaded", {
  50. source: this,
  51. attachmentsCount
  52. });
  53. }
  54. _bindPdfLink(button, content, filename) {
  55. if (this.downloadManager.disableCreateObjectURL) {
  56. throw new Error('bindPdfLink: Unsupported "disableCreateObjectURL" value.');
  57. }
  58. let blobUrl;
  59. button.onclick = function () {
  60. if (!blobUrl) {
  61. blobUrl = (0, _pdf.createObjectURL)(content, "application/pdf");
  62. }
  63. let viewerUrl;
  64. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
  65. window.open(viewerUrl);
  66. return false;
  67. };
  68. }
  69. _bindLink(button, content, filename) {
  70. button.onclick = () => {
  71. this.downloadManager.downloadData(content, filename, "");
  72. return false;
  73. };
  74. }
  75. render({
  76. attachments,
  77. keepRenderedCapability = false
  78. }) {
  79. let attachmentsCount = 0;
  80. if (this.attachments) {
  81. this.reset(keepRenderedCapability === true);
  82. }
  83. this.attachments = attachments || null;
  84. if (!attachments) {
  85. this._dispatchEvent(attachmentsCount);
  86. return;
  87. }
  88. const names = Object.keys(attachments).sort(function (a, b) {
  89. return a.toLowerCase().localeCompare(b.toLowerCase());
  90. });
  91. attachmentsCount = names.length;
  92. for (let i = 0; i < attachmentsCount; i++) {
  93. const item = attachments[names[i]];
  94. const filename = (0, _pdf.removeNullCharacters)((0, _pdf.getFilenameFromUrl)(item.filename));
  95. const div = document.createElement("div");
  96. div.className = "attachmentsItem";
  97. const button = document.createElement("button");
  98. button.textContent = filename;
  99. if (/\.pdf$/i.test(filename) && !this.downloadManager.disableCreateObjectURL) {
  100. this._bindPdfLink(button, item.content, filename);
  101. } else {
  102. this._bindLink(button, item.content, filename);
  103. }
  104. div.appendChild(button);
  105. this.container.appendChild(div);
  106. }
  107. this._dispatchEvent(attachmentsCount);
  108. }
  109. _appendAttachment({
  110. id,
  111. filename,
  112. content
  113. }) {
  114. this._renderedCapability.promise.then(() => {
  115. let attachments = this.attachments;
  116. if (!attachments) {
  117. attachments = Object.create(null);
  118. } else {
  119. for (const name in attachments) {
  120. if (id === name) {
  121. return;
  122. }
  123. }
  124. }
  125. attachments[id] = {
  126. filename,
  127. content
  128. };
  129. this.render({
  130. attachments,
  131. keepRenderedCapability: true
  132. });
  133. });
  134. }
  135. }
  136. exports.PDFAttachmentViewer = PDFAttachmentViewer;