pdf_attachment_viewer.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. let blobUrl;
  56. button.onclick = () => {
  57. if (!blobUrl) {
  58. blobUrl = URL.createObjectURL(new Blob([content], {
  59. type: "application/pdf"
  60. }));
  61. }
  62. let viewerUrl;
  63. viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
  64. try {
  65. window.open(viewerUrl);
  66. } catch (ex) {
  67. console.error(`_bindPdfLink: ${ex}`);
  68. URL.revokeObjectURL(blobUrl);
  69. blobUrl = null;
  70. this.downloadManager.downloadData(content, filename, "application/pdf");
  71. }
  72. return false;
  73. };
  74. }
  75. _bindLink(button, content, filename) {
  76. button.onclick = () => {
  77. this.downloadManager.downloadData(content, filename, "");
  78. return false;
  79. };
  80. }
  81. render({
  82. attachments,
  83. keepRenderedCapability = false
  84. }) {
  85. let attachmentsCount = 0;
  86. if (this.attachments) {
  87. this.reset(keepRenderedCapability === true);
  88. }
  89. this.attachments = attachments || null;
  90. if (!attachments) {
  91. this._dispatchEvent(attachmentsCount);
  92. return;
  93. }
  94. const names = Object.keys(attachments).sort(function (a, b) {
  95. return a.toLowerCase().localeCompare(b.toLowerCase());
  96. });
  97. attachmentsCount = names.length;
  98. for (let i = 0; i < attachmentsCount; i++) {
  99. const item = attachments[names[i]];
  100. const filename = (0, _pdf.removeNullCharacters)((0, _pdf.getFilenameFromUrl)(item.filename));
  101. const div = document.createElement("div");
  102. div.className = "attachmentsItem";
  103. const button = document.createElement("button");
  104. button.textContent = filename;
  105. if (/\.pdf$/i.test(filename) && !this.downloadManager.disableCreateObjectURL) {
  106. this._bindPdfLink(button, item.content, filename);
  107. } else {
  108. this._bindLink(button, item.content, filename);
  109. }
  110. div.appendChild(button);
  111. this.container.appendChild(div);
  112. }
  113. this._dispatchEvent(attachmentsCount);
  114. }
  115. _appendAttachment({
  116. id,
  117. filename,
  118. content
  119. }) {
  120. this._renderedCapability.promise.then(() => {
  121. let attachments = this.attachments;
  122. if (!attachments) {
  123. attachments = Object.create(null);
  124. } else {
  125. for (const name in attachments) {
  126. if (id === name) {
  127. return;
  128. }
  129. }
  130. }
  131. attachments[id] = {
  132. filename,
  133. content
  134. };
  135. this.render({
  136. attachments,
  137. keepRenderedCapability: true
  138. });
  139. });
  140. }
  141. }
  142. exports.PDFAttachmentViewer = PDFAttachmentViewer;