pdf_attachment_viewer.js 4.6 KB

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