pdf_attachment_viewer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 = undefined;
  27. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  28. var _pdf = require('../pdf');
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. var PDFAttachmentViewer = function () {
  31. function PDFAttachmentViewer(_ref) {
  32. var container = _ref.container,
  33. eventBus = _ref.eventBus,
  34. downloadManager = _ref.downloadManager;
  35. _classCallCheck(this, PDFAttachmentViewer);
  36. this.container = container;
  37. this.eventBus = eventBus;
  38. this.downloadManager = downloadManager;
  39. this.reset();
  40. this.eventBus.on('fileattachmentannotation', this._appendAttachment.bind(this));
  41. }
  42. _createClass(PDFAttachmentViewer, [{
  43. key: 'reset',
  44. value: function reset() {
  45. var keepRenderedCapability = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  46. this.attachments = null;
  47. this.container.textContent = '';
  48. if (!keepRenderedCapability) {
  49. this._renderedCapability = (0, _pdf.createPromiseCapability)();
  50. }
  51. }
  52. }, {
  53. key: '_dispatchEvent',
  54. value: function _dispatchEvent(attachmentsCount) {
  55. this._renderedCapability.resolve();
  56. this.eventBus.dispatch('attachmentsloaded', {
  57. source: this,
  58. attachmentsCount: attachmentsCount
  59. });
  60. }
  61. }, {
  62. key: '_bindPdfLink',
  63. value: function _bindPdfLink(button, content, filename) {
  64. if (_pdf.PDFJS.disableCreateObjectURL) {
  65. throw new Error('bindPdfLink: ' + 'Unsupported "PDFJS.disableCreateObjectURL" value.');
  66. }
  67. var blobUrl = void 0;
  68. button.onclick = function () {
  69. if (!blobUrl) {
  70. blobUrl = (0, _pdf.createObjectURL)(content, 'application/pdf');
  71. }
  72. var viewerUrl = void 0;
  73. viewerUrl = '?file=' + encodeURIComponent(blobUrl + '#' + filename);
  74. window.open(viewerUrl);
  75. return false;
  76. };
  77. }
  78. }, {
  79. key: '_bindLink',
  80. value: function _bindLink(button, content, filename) {
  81. var _this = this;
  82. button.onclick = function () {
  83. _this.downloadManager.downloadData(content, filename, '');
  84. return false;
  85. };
  86. }
  87. }, {
  88. key: 'render',
  89. value: function render(_ref2) {
  90. var attachments = _ref2.attachments,
  91. _ref2$keepRenderedCap = _ref2.keepRenderedCapability,
  92. keepRenderedCapability = _ref2$keepRenderedCap === undefined ? false : _ref2$keepRenderedCap;
  93. var attachmentsCount = 0;
  94. if (this.attachments) {
  95. this.reset(keepRenderedCapability === true);
  96. }
  97. this.attachments = attachments || null;
  98. if (!attachments) {
  99. this._dispatchEvent(attachmentsCount);
  100. return;
  101. }
  102. var names = Object.keys(attachments).sort(function (a, b) {
  103. return a.toLowerCase().localeCompare(b.toLowerCase());
  104. });
  105. attachmentsCount = names.length;
  106. for (var i = 0; i < attachmentsCount; i++) {
  107. var item = attachments[names[i]];
  108. var filename = (0, _pdf.removeNullCharacters)((0, _pdf.getFilenameFromUrl)(item.filename));
  109. var div = document.createElement('div');
  110. div.className = 'attachmentsItem';
  111. var button = document.createElement('button');
  112. button.textContent = filename;
  113. if (/\.pdf$/i.test(filename) && !_pdf.PDFJS.disableCreateObjectURL) {
  114. this._bindPdfLink(button, item.content, filename);
  115. } else {
  116. this._bindLink(button, item.content, filename);
  117. }
  118. div.appendChild(button);
  119. this.container.appendChild(div);
  120. }
  121. this._dispatchEvent(attachmentsCount);
  122. }
  123. }, {
  124. key: '_appendAttachment',
  125. value: function _appendAttachment(_ref3) {
  126. var _this2 = this;
  127. var id = _ref3.id,
  128. filename = _ref3.filename,
  129. content = _ref3.content;
  130. this._renderedCapability.promise.then(function () {
  131. var attachments = _this2.attachments;
  132. if (!attachments) {
  133. attachments = Object.create(null);
  134. } else {
  135. for (var name in attachments) {
  136. if (id === name) {
  137. return;
  138. }
  139. }
  140. }
  141. attachments[id] = {
  142. filename: filename,
  143. content: content
  144. };
  145. _this2.render({
  146. attachments: attachments,
  147. keepRenderedCapability: true
  148. });
  149. });
  150. }
  151. }]);
  152. return PDFAttachmentViewer;
  153. }();
  154. exports.PDFAttachmentViewer = PDFAttachmentViewer;