pdf_document_properties.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.PDFDocumentProperties = undefined;
  27. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  28. 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; }; }();
  29. var _ui_utils = require('./ui_utils');
  30. var _pdf = require('../pdf');
  31. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  32. var DEFAULT_FIELD_CONTENT = '-';
  33. var PDFDocumentProperties = function () {
  34. function PDFDocumentProperties(_ref, overlayManager) {
  35. var overlayName = _ref.overlayName,
  36. fields = _ref.fields,
  37. container = _ref.container,
  38. closeButton = _ref.closeButton;
  39. var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
  40. _classCallCheck(this, PDFDocumentProperties);
  41. this.overlayName = overlayName;
  42. this.fields = fields;
  43. this.container = container;
  44. this.overlayManager = overlayManager;
  45. this.l10n = l10n;
  46. this._reset();
  47. if (closeButton) {
  48. closeButton.addEventListener('click', this.close.bind(this));
  49. }
  50. this.overlayManager.register(this.overlayName, this.container, this.close.bind(this));
  51. }
  52. _createClass(PDFDocumentProperties, [{
  53. key: 'open',
  54. value: function open() {
  55. var _this = this;
  56. var freezeFieldData = function freezeFieldData(data) {
  57. Object.defineProperty(_this, 'fieldData', {
  58. value: Object.freeze(data),
  59. writable: false,
  60. enumerable: true,
  61. configurable: true
  62. });
  63. };
  64. Promise.all([this.overlayManager.open(this.overlayName), this._dataAvailableCapability.promise]).then(function () {
  65. if (_this.fieldData) {
  66. _this._updateUI();
  67. return;
  68. }
  69. _this.pdfDocument.getMetadata().then(function (_ref2) {
  70. var info = _ref2.info,
  71. metadata = _ref2.metadata,
  72. contentDispositionFilename = _ref2.contentDispositionFilename;
  73. return Promise.all([info, metadata, contentDispositionFilename || (0, _ui_utils.getPDFFileNameFromURL)(_this.url), _this._parseFileSize(_this.maybeFileSize), _this._parseDate(info.CreationDate), _this._parseDate(info.ModDate)]);
  74. }).then(function (_ref3) {
  75. var _ref4 = _slicedToArray(_ref3, 6),
  76. info = _ref4[0],
  77. metadata = _ref4[1],
  78. fileName = _ref4[2],
  79. fileSize = _ref4[3],
  80. creationDate = _ref4[4],
  81. modDate = _ref4[5];
  82. freezeFieldData({
  83. 'fileName': fileName,
  84. 'fileSize': fileSize,
  85. 'title': info.Title,
  86. 'author': info.Author,
  87. 'subject': info.Subject,
  88. 'keywords': info.Keywords,
  89. 'creationDate': creationDate,
  90. 'modificationDate': modDate,
  91. 'creator': info.Creator,
  92. 'producer': info.Producer,
  93. 'version': info.PDFFormatVersion,
  94. 'pageCount': _this.pdfDocument.numPages
  95. });
  96. _this._updateUI();
  97. return _this.pdfDocument.getDownloadInfo();
  98. }).then(function (_ref5) {
  99. var length = _ref5.length;
  100. return _this._parseFileSize(length);
  101. }).then(function (fileSize) {
  102. var data = (0, _ui_utils.cloneObj)(_this.fieldData);
  103. data['fileSize'] = fileSize;
  104. freezeFieldData(data);
  105. _this._updateUI();
  106. });
  107. });
  108. }
  109. }, {
  110. key: 'close',
  111. value: function close() {
  112. this.overlayManager.close(this.overlayName);
  113. }
  114. }, {
  115. key: 'setDocument',
  116. value: function setDocument(pdfDocument, url) {
  117. if (this.pdfDocument) {
  118. this._reset();
  119. this._updateUI(true);
  120. }
  121. if (!pdfDocument) {
  122. return;
  123. }
  124. this.pdfDocument = pdfDocument;
  125. this.url = url;
  126. this._dataAvailableCapability.resolve();
  127. }
  128. }, {
  129. key: 'setFileSize',
  130. value: function setFileSize(fileSize) {
  131. if (typeof fileSize === 'number' && fileSize > 0) {
  132. this.maybeFileSize = fileSize;
  133. }
  134. }
  135. }, {
  136. key: '_reset',
  137. value: function _reset() {
  138. this.pdfDocument = null;
  139. this.url = null;
  140. this.maybeFileSize = 0;
  141. delete this.fieldData;
  142. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  143. }
  144. }, {
  145. key: '_updateUI',
  146. value: function _updateUI() {
  147. var reset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  148. if (reset || !this.fieldData) {
  149. for (var id in this.fields) {
  150. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  151. }
  152. return;
  153. }
  154. if (this.overlayManager.active !== this.overlayName) {
  155. return;
  156. }
  157. for (var _id in this.fields) {
  158. var content = this.fieldData[_id];
  159. this.fields[_id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  160. }
  161. }
  162. }, {
  163. key: '_parseFileSize',
  164. value: function _parseFileSize() {
  165. var fileSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  166. var kb = fileSize / 1024;
  167. if (!kb) {
  168. return Promise.resolve(undefined);
  169. } else if (kb < 1024) {
  170. return this.l10n.get('document_properties_kb', {
  171. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  172. size_b: fileSize.toLocaleString()
  173. }, '{{size_kb}} KB ({{size_b}} bytes)');
  174. }
  175. return this.l10n.get('document_properties_mb', {
  176. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  177. size_b: fileSize.toLocaleString()
  178. }, '{{size_mb}} MB ({{size_b}} bytes)');
  179. }
  180. }, {
  181. key: '_parseDate',
  182. value: function _parseDate(inputDate) {
  183. if (!inputDate) {
  184. return;
  185. }
  186. var dateToParse = inputDate;
  187. if (dateToParse.substring(0, 2) === 'D:') {
  188. dateToParse = dateToParse.substring(2);
  189. }
  190. var year = parseInt(dateToParse.substring(0, 4), 10);
  191. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  192. var day = parseInt(dateToParse.substring(6, 8), 10);
  193. var hours = parseInt(dateToParse.substring(8, 10), 10);
  194. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  195. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  196. var utRel = dateToParse.substring(14, 15);
  197. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  198. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  199. if (utRel === '-') {
  200. hours += offsetHours;
  201. minutes += offsetMinutes;
  202. } else if (utRel === '+') {
  203. hours -= offsetHours;
  204. minutes -= offsetMinutes;
  205. }
  206. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  207. var dateString = date.toLocaleDateString();
  208. var timeString = date.toLocaleTimeString();
  209. return this.l10n.get('document_properties_date_string', {
  210. date: dateString,
  211. time: timeString
  212. }, '{{date}}, {{time}}');
  213. }
  214. }]);
  215. return PDFDocumentProperties;
  216. }();
  217. exports.PDFDocumentProperties = PDFDocumentProperties;