2
0

pdf_document_properties.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. return Promise.all([info, metadata, _this._parseFileSize(_this.maybeFileSize), _this._parseDate(info.CreationDate), _this._parseDate(info.ModDate)]);
  73. }).then(function (_ref3) {
  74. var _ref4 = _slicedToArray(_ref3, 5),
  75. info = _ref4[0],
  76. metadata = _ref4[1],
  77. fileSize = _ref4[2],
  78. creationDate = _ref4[3],
  79. modificationDate = _ref4[4];
  80. freezeFieldData({
  81. 'fileName': (0, _ui_utils.getPDFFileNameFromURL)(_this.url),
  82. 'fileSize': fileSize,
  83. 'title': info.Title,
  84. 'author': info.Author,
  85. 'subject': info.Subject,
  86. 'keywords': info.Keywords,
  87. 'creationDate': creationDate,
  88. 'modificationDate': modificationDate,
  89. 'creator': info.Creator,
  90. 'producer': info.Producer,
  91. 'version': info.PDFFormatVersion,
  92. 'pageCount': _this.pdfDocument.numPages
  93. });
  94. _this._updateUI();
  95. return _this.pdfDocument.getDownloadInfo();
  96. }).then(function (_ref5) {
  97. var length = _ref5.length;
  98. return _this._parseFileSize(length);
  99. }).then(function (fileSize) {
  100. var data = (0, _ui_utils.cloneObj)(_this.fieldData);
  101. data['fileSize'] = fileSize;
  102. freezeFieldData(data);
  103. _this._updateUI();
  104. });
  105. });
  106. }
  107. }, {
  108. key: 'close',
  109. value: function close() {
  110. this.overlayManager.close(this.overlayName);
  111. }
  112. }, {
  113. key: 'setDocument',
  114. value: function setDocument(pdfDocument, url) {
  115. if (this.pdfDocument) {
  116. this._reset();
  117. this._updateUI(true);
  118. }
  119. if (!pdfDocument) {
  120. return;
  121. }
  122. this.pdfDocument = pdfDocument;
  123. this.url = url;
  124. this._dataAvailableCapability.resolve();
  125. }
  126. }, {
  127. key: 'setFileSize',
  128. value: function setFileSize(fileSize) {
  129. if (typeof fileSize === 'number' && fileSize > 0) {
  130. this.maybeFileSize = fileSize;
  131. }
  132. }
  133. }, {
  134. key: '_reset',
  135. value: function _reset() {
  136. this.pdfDocument = null;
  137. this.url = null;
  138. this.maybeFileSize = 0;
  139. delete this.fieldData;
  140. this._dataAvailableCapability = (0, _pdf.createPromiseCapability)();
  141. }
  142. }, {
  143. key: '_updateUI',
  144. value: function _updateUI() {
  145. var reset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  146. if (reset || !this.fieldData) {
  147. for (var id in this.fields) {
  148. this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
  149. }
  150. return;
  151. }
  152. if (this.overlayManager.active !== this.overlayName) {
  153. return;
  154. }
  155. for (var _id in this.fields) {
  156. var content = this.fieldData[_id];
  157. this.fields[_id].textContent = content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
  158. }
  159. }
  160. }, {
  161. key: '_parseFileSize',
  162. value: function _parseFileSize() {
  163. var fileSize = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  164. var kb = fileSize / 1024;
  165. if (!kb) {
  166. return Promise.resolve(undefined);
  167. } else if (kb < 1024) {
  168. return this.l10n.get('document_properties_kb', {
  169. size_kb: (+kb.toPrecision(3)).toLocaleString(),
  170. size_b: fileSize.toLocaleString()
  171. }, '{{size_kb}} KB ({{size_b}} bytes)');
  172. }
  173. return this.l10n.get('document_properties_mb', {
  174. size_mb: (+(kb / 1024).toPrecision(3)).toLocaleString(),
  175. size_b: fileSize.toLocaleString()
  176. }, '{{size_mb}} MB ({{size_b}} bytes)');
  177. }
  178. }, {
  179. key: '_parseDate',
  180. value: function _parseDate(inputDate) {
  181. if (!inputDate) {
  182. return;
  183. }
  184. var dateToParse = inputDate;
  185. if (dateToParse.substring(0, 2) === 'D:') {
  186. dateToParse = dateToParse.substring(2);
  187. }
  188. var year = parseInt(dateToParse.substring(0, 4), 10);
  189. var month = parseInt(dateToParse.substring(4, 6), 10) - 1;
  190. var day = parseInt(dateToParse.substring(6, 8), 10);
  191. var hours = parseInt(dateToParse.substring(8, 10), 10);
  192. var minutes = parseInt(dateToParse.substring(10, 12), 10);
  193. var seconds = parseInt(dateToParse.substring(12, 14), 10);
  194. var utRel = dateToParse.substring(14, 15);
  195. var offsetHours = parseInt(dateToParse.substring(15, 17), 10);
  196. var offsetMinutes = parseInt(dateToParse.substring(18, 20), 10);
  197. if (utRel === '-') {
  198. hours += offsetHours;
  199. minutes += offsetMinutes;
  200. } else if (utRel === '+') {
  201. hours -= offsetHours;
  202. minutes -= offsetMinutes;
  203. }
  204. var date = new Date(Date.UTC(year, month, day, hours, minutes, seconds));
  205. var dateString = date.toLocaleDateString();
  206. var timeString = date.toLocaleTimeString();
  207. return this.l10n.get('document_properties_date_string', {
  208. date: dateString,
  209. time: timeString
  210. }, '{{date}}, {{time}}');
  211. }
  212. }]);
  213. return PDFDocumentProperties;
  214. }();
  215. exports.PDFDocumentProperties = PDFDocumentProperties;