pdf_attachment_viewer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 pdfjsLib = require('./pdfjs.js');
  17. var PDFAttachmentViewer = function PDFAttachmentViewerClosure() {
  18. function PDFAttachmentViewer(options) {
  19. this.attachments = null;
  20. this.container = options.container;
  21. this.eventBus = options.eventBus;
  22. this.downloadManager = options.downloadManager;
  23. this._renderedCapability = pdfjsLib.createPromiseCapability();
  24. this.eventBus.on('fileattachmentannotation', this._appendAttachment.bind(this));
  25. }
  26. PDFAttachmentViewer.prototype = {
  27. reset: function PDFAttachmentViewer_reset(keepRenderedCapability) {
  28. this.attachments = null;
  29. this.container.textContent = '';
  30. if (!keepRenderedCapability) {
  31. this._renderedCapability = pdfjsLib.createPromiseCapability();
  32. }
  33. },
  34. _dispatchEvent: function PDFAttachmentViewer_dispatchEvent(attachmentsCount) {
  35. this.eventBus.dispatch('attachmentsloaded', {
  36. source: this,
  37. attachmentsCount: attachmentsCount
  38. });
  39. this._renderedCapability.resolve();
  40. },
  41. _bindPdfLink: function PDFAttachmentViewer_bindPdfLink(button, content, filename) {
  42. var blobUrl;
  43. button.onclick = function () {
  44. if (!blobUrl) {
  45. blobUrl = pdfjsLib.createObjectURL(content, 'application/pdf', pdfjsLib.PDFJS.disableCreateObjectURL);
  46. }
  47. var viewerUrl;
  48. viewerUrl = '?file=' + encodeURIComponent(blobUrl + '#' + filename);
  49. window.open(viewerUrl);
  50. return false;
  51. };
  52. },
  53. _bindLink: function PDFAttachmentViewer_bindLink(button, content, filename) {
  54. button.onclick = function downloadFile(e) {
  55. this.downloadManager.downloadData(content, filename, '');
  56. return false;
  57. }.bind(this);
  58. },
  59. render: function PDFAttachmentViewer_render(params) {
  60. params = params || {};
  61. var attachments = params.attachments || null;
  62. var attachmentsCount = 0;
  63. if (this.attachments) {
  64. var keepRenderedCapability = params.keepRenderedCapability === true;
  65. this.reset(keepRenderedCapability);
  66. }
  67. this.attachments = attachments;
  68. if (!attachments) {
  69. this._dispatchEvent(attachmentsCount);
  70. return;
  71. }
  72. var names = Object.keys(attachments).sort(function (a, b) {
  73. return a.toLowerCase().localeCompare(b.toLowerCase());
  74. });
  75. attachmentsCount = names.length;
  76. for (var i = 0; i < attachmentsCount; i++) {
  77. var item = attachments[names[i]];
  78. var filename = pdfjsLib.getFilenameFromUrl(item.filename);
  79. filename = pdfjsLib.removeNullCharacters(filename);
  80. var div = document.createElement('div');
  81. div.className = 'attachmentsItem';
  82. var button = document.createElement('button');
  83. button.textContent = filename;
  84. if (/\.pdf$/i.test(filename)) {
  85. this._bindPdfLink(button, item.content, filename);
  86. } else {
  87. this._bindLink(button, item.content, filename);
  88. }
  89. div.appendChild(button);
  90. this.container.appendChild(div);
  91. }
  92. this._dispatchEvent(attachmentsCount);
  93. },
  94. _appendAttachment: function PDFAttachmentViewer_appendAttachment(item) {
  95. this._renderedCapability.promise.then(function (id, filename, content) {
  96. var attachments = this.attachments;
  97. if (!attachments) {
  98. attachments = Object.create(null);
  99. } else {
  100. for (var name in attachments) {
  101. if (id === name) {
  102. return;
  103. }
  104. }
  105. }
  106. attachments[id] = {
  107. filename: filename,
  108. content: content
  109. };
  110. this.render({
  111. attachments: attachments,
  112. keepRenderedCapability: true
  113. });
  114. }.bind(this, item.id, item.filename, item.content));
  115. }
  116. };
  117. return PDFAttachmentViewer;
  118. }();
  119. exports.PDFAttachmentViewer = PDFAttachmentViewer;