pdf_attachment_viewer.js 4.1 KB

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