base_viewer.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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.BaseViewer = void 0;
  27. var _ui_utils = require("./ui_utils");
  28. var _pdf_rendering_queue = require("./pdf_rendering_queue");
  29. var _annotation_layer_builder = require("./annotation_layer_builder");
  30. var _pdf = require("../pdf");
  31. var _pdf_page_view = require("./pdf_page_view");
  32. var _pdf_link_service = require("./pdf_link_service");
  33. var _text_layer_builder = require("./text_layer_builder");
  34. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  35. 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); } }
  36. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  37. var DEFAULT_CACHE_SIZE = 10;
  38. function PDFPageViewBuffer(size) {
  39. var data = [];
  40. this.push = function (view) {
  41. var i = data.indexOf(view);
  42. if (i >= 0) {
  43. data.splice(i, 1);
  44. }
  45. data.push(view);
  46. if (data.length > size) {
  47. data.shift().destroy();
  48. }
  49. };
  50. this.resize = function (newSize, pagesToKeep) {
  51. size = newSize;
  52. if (pagesToKeep) {
  53. var pageIdsToKeep = new Set();
  54. for (var i = 0, iMax = pagesToKeep.length; i < iMax; ++i) {
  55. pageIdsToKeep.add(pagesToKeep[i].id);
  56. }
  57. (0, _ui_utils.moveToEndOfArray)(data, function (page) {
  58. return pageIdsToKeep.has(page.id);
  59. });
  60. }
  61. while (data.length > size) {
  62. data.shift().destroy();
  63. }
  64. };
  65. }
  66. function isSameScale(oldScale, newScale) {
  67. if (newScale === oldScale) {
  68. return true;
  69. }
  70. if (Math.abs(newScale - oldScale) < 1e-15) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. var BaseViewer =
  76. /*#__PURE__*/
  77. function () {
  78. function BaseViewer(options) {
  79. var _this = this;
  80. _classCallCheck(this, BaseViewer);
  81. if (this.constructor === BaseViewer) {
  82. throw new Error('Cannot initialize BaseViewer.');
  83. }
  84. this._name = this.constructor.name;
  85. this.container = options.container;
  86. this.viewer = options.viewer || options.container.firstElementChild;
  87. this.eventBus = options.eventBus || (0, _ui_utils.getGlobalEventBus)();
  88. this.linkService = options.linkService || new _pdf_link_service.SimpleLinkService();
  89. this.downloadManager = options.downloadManager || null;
  90. this.findController = options.findController || null;
  91. this.removePageBorders = options.removePageBorders || false;
  92. this.textLayerMode = Number.isInteger(options.textLayerMode) ? options.textLayerMode : _ui_utils.TextLayerMode.ENABLE;
  93. this.imageResourcesPath = options.imageResourcesPath || '';
  94. this.renderInteractiveForms = options.renderInteractiveForms || false;
  95. this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
  96. this.renderer = options.renderer || _ui_utils.RendererType.CANVAS;
  97. this.enableWebGL = options.enableWebGL || false;
  98. this.useOnlyCssZoom = options.useOnlyCssZoom || false;
  99. this.maxCanvasPixels = options.maxCanvasPixels;
  100. this.l10n = options.l10n || _ui_utils.NullL10n;
  101. this.defaultRenderingQueue = !options.renderingQueue;
  102. if (this.defaultRenderingQueue) {
  103. this.renderingQueue = new _pdf_rendering_queue.PDFRenderingQueue();
  104. this.renderingQueue.setViewer(this);
  105. } else {
  106. this.renderingQueue = options.renderingQueue;
  107. }
  108. this.scroll = (0, _ui_utils.watchScroll)(this.container, this._scrollUpdate.bind(this));
  109. this.presentationModeState = _ui_utils.PresentationModeState.UNKNOWN;
  110. this._resetView();
  111. if (this.removePageBorders) {
  112. this.viewer.classList.add('removePageBorders');
  113. }
  114. Promise.resolve().then(function () {
  115. _this.eventBus.dispatch('baseviewerinit', {
  116. source: _this
  117. });
  118. });
  119. }
  120. _createClass(BaseViewer, [{
  121. key: "getPageView",
  122. value: function getPageView(index) {
  123. return this._pages[index];
  124. }
  125. }, {
  126. key: "_setCurrentPageNumber",
  127. value: function _setCurrentPageNumber(val) {
  128. var resetCurrentPageView = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  129. if (this._currentPageNumber === val) {
  130. if (resetCurrentPageView) {
  131. this._resetCurrentPageView();
  132. }
  133. return true;
  134. }
  135. if (!(0 < val && val <= this.pagesCount)) {
  136. return false;
  137. }
  138. this._currentPageNumber = val;
  139. this.eventBus.dispatch('pagechanging', {
  140. source: this,
  141. pageNumber: val,
  142. pageLabel: this._pageLabels && this._pageLabels[val - 1]
  143. });
  144. if (resetCurrentPageView) {
  145. this._resetCurrentPageView();
  146. }
  147. return true;
  148. }
  149. }, {
  150. key: "setDocument",
  151. value: function setDocument(pdfDocument) {
  152. var _this2 = this;
  153. if (this.pdfDocument) {
  154. this._cancelRendering();
  155. this._resetView();
  156. if (this.findController) {
  157. this.findController.setDocument(null);
  158. }
  159. }
  160. this.pdfDocument = pdfDocument;
  161. if (!pdfDocument) {
  162. return;
  163. }
  164. var pagesCount = pdfDocument.numPages;
  165. var pagesCapability = (0, _pdf.createPromiseCapability)();
  166. this.pagesPromise = pagesCapability.promise;
  167. pagesCapability.promise.then(function () {
  168. _this2._pageViewsReady = true;
  169. _this2.eventBus.dispatch('pagesloaded', {
  170. source: _this2,
  171. pagesCount: pagesCount
  172. });
  173. });
  174. var onePageRenderedCapability = (0, _pdf.createPromiseCapability)();
  175. this.onePageRendered = onePageRenderedCapability.promise;
  176. var bindOnAfterAndBeforeDraw = function bindOnAfterAndBeforeDraw(pageView) {
  177. pageView.onBeforeDraw = function () {
  178. _this2._buffer.push(pageView);
  179. };
  180. pageView.onAfterDraw = function () {
  181. if (!onePageRenderedCapability.settled) {
  182. onePageRenderedCapability.resolve();
  183. }
  184. };
  185. };
  186. var firstPagePromise = pdfDocument.getPage(1);
  187. this.firstPagePromise = firstPagePromise;
  188. firstPagePromise.then(function (pdfPage) {
  189. var scale = _this2.currentScale;
  190. var viewport = pdfPage.getViewport({
  191. scale: scale * _ui_utils.CSS_UNITS
  192. });
  193. for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) {
  194. var textLayerFactory = null;
  195. if (_this2.textLayerMode !== _ui_utils.TextLayerMode.DISABLE) {
  196. textLayerFactory = _this2;
  197. }
  198. var pageView = new _pdf_page_view.PDFPageView({
  199. container: _this2._setDocumentViewerElement,
  200. eventBus: _this2.eventBus,
  201. id: pageNum,
  202. scale: scale,
  203. defaultViewport: viewport.clone(),
  204. renderingQueue: _this2.renderingQueue,
  205. textLayerFactory: textLayerFactory,
  206. textLayerMode: _this2.textLayerMode,
  207. annotationLayerFactory: _this2,
  208. imageResourcesPath: _this2.imageResourcesPath,
  209. renderInteractiveForms: _this2.renderInteractiveForms,
  210. renderer: _this2.renderer,
  211. enableWebGL: _this2.enableWebGL,
  212. useOnlyCssZoom: _this2.useOnlyCssZoom,
  213. maxCanvasPixels: _this2.maxCanvasPixels,
  214. l10n: _this2.l10n
  215. });
  216. bindOnAfterAndBeforeDraw(pageView);
  217. _this2._pages.push(pageView);
  218. }
  219. if (_this2._spreadMode !== _ui_utils.SpreadMode.NONE) {
  220. _this2._updateSpreadMode();
  221. }
  222. onePageRenderedCapability.promise.then(function () {
  223. if (pdfDocument.loadingParams['disableAutoFetch']) {
  224. pagesCapability.resolve();
  225. return;
  226. }
  227. var getPagesLeft = pagesCount;
  228. var _loop = function _loop(_pageNum) {
  229. pdfDocument.getPage(_pageNum).then(function (pdfPage) {
  230. var pageView = _this2._pages[_pageNum - 1];
  231. if (!pageView.pdfPage) {
  232. pageView.setPdfPage(pdfPage);
  233. }
  234. _this2.linkService.cachePageRef(_pageNum, pdfPage.ref);
  235. if (--getPagesLeft === 0) {
  236. pagesCapability.resolve();
  237. }
  238. }, function (reason) {
  239. console.error("Unable to get page ".concat(_pageNum, " to initialize viewer"), reason);
  240. if (--getPagesLeft === 0) {
  241. pagesCapability.resolve();
  242. }
  243. });
  244. };
  245. for (var _pageNum = 1; _pageNum <= pagesCount; ++_pageNum) {
  246. _loop(_pageNum);
  247. }
  248. });
  249. _this2.eventBus.dispatch('pagesinit', {
  250. source: _this2
  251. });
  252. if (_this2.findController) {
  253. _this2.findController.setDocument(pdfDocument);
  254. }
  255. if (_this2.defaultRenderingQueue) {
  256. _this2.update();
  257. }
  258. }).catch(function (reason) {
  259. console.error('Unable to initialize viewer', reason);
  260. });
  261. }
  262. }, {
  263. key: "setPageLabels",
  264. value: function setPageLabels(labels) {
  265. if (!this.pdfDocument) {
  266. return;
  267. }
  268. if (!labels) {
  269. this._pageLabels = null;
  270. } else if (!(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)) {
  271. this._pageLabels = null;
  272. console.error("".concat(this._name, ".setPageLabels: Invalid page labels."));
  273. } else {
  274. this._pageLabels = labels;
  275. }
  276. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  277. var pageView = this._pages[i];
  278. var label = this._pageLabels && this._pageLabels[i];
  279. pageView.setPageLabel(label);
  280. }
  281. }
  282. }, {
  283. key: "_resetView",
  284. value: function _resetView() {
  285. this._pages = [];
  286. this._currentPageNumber = 1;
  287. this._currentScale = _ui_utils.UNKNOWN_SCALE;
  288. this._currentScaleValue = null;
  289. this._pageLabels = null;
  290. this._buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
  291. this._location = null;
  292. this._pagesRotation = 0;
  293. this._pagesRequests = [];
  294. this._pageViewsReady = false;
  295. this._scrollMode = _ui_utils.ScrollMode.VERTICAL;
  296. this._spreadMode = _ui_utils.SpreadMode.NONE;
  297. this.viewer.textContent = '';
  298. this._updateScrollMode();
  299. }
  300. }, {
  301. key: "_scrollUpdate",
  302. value: function _scrollUpdate() {
  303. if (this.pagesCount === 0) {
  304. return;
  305. }
  306. this.update();
  307. }
  308. }, {
  309. key: "_scrollIntoView",
  310. value: function _scrollIntoView(_ref) {
  311. var pageDiv = _ref.pageDiv,
  312. _ref$pageSpot = _ref.pageSpot,
  313. pageSpot = _ref$pageSpot === void 0 ? null : _ref$pageSpot,
  314. _ref$pageNumber = _ref.pageNumber,
  315. pageNumber = _ref$pageNumber === void 0 ? null : _ref$pageNumber;
  316. (0, _ui_utils.scrollIntoView)(pageDiv, pageSpot);
  317. }
  318. }, {
  319. key: "_setScaleUpdatePages",
  320. value: function _setScaleUpdatePages(newScale, newValue) {
  321. var noScroll = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  322. var preset = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  323. this._currentScaleValue = newValue.toString();
  324. if (isSameScale(this._currentScale, newScale)) {
  325. if (preset) {
  326. this.eventBus.dispatch('scalechanging', {
  327. source: this,
  328. scale: newScale,
  329. presetValue: newValue
  330. });
  331. }
  332. return;
  333. }
  334. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  335. this._pages[i].update(newScale);
  336. }
  337. this._currentScale = newScale;
  338. if (!noScroll) {
  339. var page = this._currentPageNumber,
  340. dest;
  341. if (this._location && !(this.isInPresentationMode || this.isChangingPresentationMode)) {
  342. page = this._location.pageNumber;
  343. dest = [null, {
  344. name: 'XYZ'
  345. }, this._location.left, this._location.top, null];
  346. }
  347. this.scrollPageIntoView({
  348. pageNumber: page,
  349. destArray: dest,
  350. allowNegativeOffset: true
  351. });
  352. }
  353. this.eventBus.dispatch('scalechanging', {
  354. source: this,
  355. scale: newScale,
  356. presetValue: preset ? newValue : undefined
  357. });
  358. if (this.defaultRenderingQueue) {
  359. this.update();
  360. }
  361. }
  362. }, {
  363. key: "_setScale",
  364. value: function _setScale(value) {
  365. var noScroll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  366. var scale = parseFloat(value);
  367. if (scale > 0) {
  368. this._setScaleUpdatePages(scale, value, noScroll, false);
  369. } else {
  370. var currentPage = this._pages[this._currentPageNumber - 1];
  371. if (!currentPage) {
  372. return;
  373. }
  374. var noPadding = this.isInPresentationMode || this.removePageBorders;
  375. var hPadding = noPadding ? 0 : _ui_utils.SCROLLBAR_PADDING;
  376. var vPadding = noPadding ? 0 : _ui_utils.VERTICAL_PADDING;
  377. if (!noPadding && this._isScrollModeHorizontal) {
  378. var _ref2 = [vPadding, hPadding];
  379. hPadding = _ref2[0];
  380. vPadding = _ref2[1];
  381. }
  382. var pageWidthScale = (this.container.clientWidth - hPadding) / currentPage.width * currentPage.scale;
  383. var pageHeightScale = (this.container.clientHeight - vPadding) / currentPage.height * currentPage.scale;
  384. switch (value) {
  385. case 'page-actual':
  386. scale = 1;
  387. break;
  388. case 'page-width':
  389. scale = pageWidthScale;
  390. break;
  391. case 'page-height':
  392. scale = pageHeightScale;
  393. break;
  394. case 'page-fit':
  395. scale = Math.min(pageWidthScale, pageHeightScale);
  396. break;
  397. case 'auto':
  398. var horizontalScale = (0, _ui_utils.isPortraitOrientation)(currentPage) ? pageWidthScale : Math.min(pageHeightScale, pageWidthScale);
  399. scale = Math.min(_ui_utils.MAX_AUTO_SCALE, horizontalScale);
  400. break;
  401. default:
  402. console.error("".concat(this._name, "._setScale: \"").concat(value, "\" is an unknown zoom value."));
  403. return;
  404. }
  405. this._setScaleUpdatePages(scale, value, noScroll, true);
  406. }
  407. }
  408. }, {
  409. key: "_resetCurrentPageView",
  410. value: function _resetCurrentPageView() {
  411. if (this.isInPresentationMode) {
  412. this._setScale(this._currentScaleValue, true);
  413. }
  414. var pageView = this._pages[this._currentPageNumber - 1];
  415. this._scrollIntoView({
  416. pageDiv: pageView.div
  417. });
  418. }
  419. }, {
  420. key: "scrollPageIntoView",
  421. value: function scrollPageIntoView(_ref3) {
  422. var pageNumber = _ref3.pageNumber,
  423. _ref3$destArray = _ref3.destArray,
  424. destArray = _ref3$destArray === void 0 ? null : _ref3$destArray,
  425. _ref3$allowNegativeOf = _ref3.allowNegativeOffset,
  426. allowNegativeOffset = _ref3$allowNegativeOf === void 0 ? false : _ref3$allowNegativeOf;
  427. if (!this.pdfDocument) {
  428. return;
  429. }
  430. var pageView = Number.isInteger(pageNumber) && this._pages[pageNumber - 1];
  431. if (!pageView) {
  432. console.error("".concat(this._name, ".scrollPageIntoView: ") + "\"".concat(pageNumber, "\" is not a valid pageNumber parameter."));
  433. return;
  434. }
  435. if (this.isInPresentationMode || !destArray) {
  436. this._setCurrentPageNumber(pageNumber, true);
  437. return;
  438. }
  439. var x = 0,
  440. y = 0;
  441. var width = 0,
  442. height = 0,
  443. widthScale,
  444. heightScale;
  445. var changeOrientation = pageView.rotation % 180 === 0 ? false : true;
  446. var pageWidth = (changeOrientation ? pageView.height : pageView.width) / pageView.scale / _ui_utils.CSS_UNITS;
  447. var pageHeight = (changeOrientation ? pageView.width : pageView.height) / pageView.scale / _ui_utils.CSS_UNITS;
  448. var scale = 0;
  449. switch (destArray[1].name) {
  450. case 'XYZ':
  451. x = destArray[2];
  452. y = destArray[3];
  453. scale = destArray[4];
  454. x = x !== null ? x : 0;
  455. y = y !== null ? y : pageHeight;
  456. break;
  457. case 'Fit':
  458. case 'FitB':
  459. scale = 'page-fit';
  460. break;
  461. case 'FitH':
  462. case 'FitBH':
  463. y = destArray[2];
  464. scale = 'page-width';
  465. if (y === null && this._location) {
  466. x = this._location.left;
  467. y = this._location.top;
  468. }
  469. break;
  470. case 'FitV':
  471. case 'FitBV':
  472. x = destArray[2];
  473. width = pageWidth;
  474. height = pageHeight;
  475. scale = 'page-height';
  476. break;
  477. case 'FitR':
  478. x = destArray[2];
  479. y = destArray[3];
  480. width = destArray[4] - x;
  481. height = destArray[5] - y;
  482. var hPadding = this.removePageBorders ? 0 : _ui_utils.SCROLLBAR_PADDING;
  483. var vPadding = this.removePageBorders ? 0 : _ui_utils.VERTICAL_PADDING;
  484. widthScale = (this.container.clientWidth - hPadding) / width / _ui_utils.CSS_UNITS;
  485. heightScale = (this.container.clientHeight - vPadding) / height / _ui_utils.CSS_UNITS;
  486. scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
  487. break;
  488. default:
  489. console.error("".concat(this._name, ".scrollPageIntoView: ") + "\"".concat(destArray[1].name, "\" is not a valid destination type."));
  490. return;
  491. }
  492. if (scale && scale !== this._currentScale) {
  493. this.currentScaleValue = scale;
  494. } else if (this._currentScale === _ui_utils.UNKNOWN_SCALE) {
  495. this.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  496. }
  497. if (scale === 'page-fit' && !destArray[4]) {
  498. this._scrollIntoView({
  499. pageDiv: pageView.div,
  500. pageNumber: pageNumber
  501. });
  502. return;
  503. }
  504. var boundingRect = [pageView.viewport.convertToViewportPoint(x, y), pageView.viewport.convertToViewportPoint(x + width, y + height)];
  505. var left = Math.min(boundingRect[0][0], boundingRect[1][0]);
  506. var top = Math.min(boundingRect[0][1], boundingRect[1][1]);
  507. if (!allowNegativeOffset) {
  508. left = Math.max(left, 0);
  509. top = Math.max(top, 0);
  510. }
  511. this._scrollIntoView({
  512. pageDiv: pageView.div,
  513. pageSpot: {
  514. left: left,
  515. top: top
  516. },
  517. pageNumber: pageNumber
  518. });
  519. }
  520. }, {
  521. key: "_updateLocation",
  522. value: function _updateLocation(firstPage) {
  523. var currentScale = this._currentScale;
  524. var currentScaleValue = this._currentScaleValue;
  525. var normalizedScaleValue = parseFloat(currentScaleValue) === currentScale ? Math.round(currentScale * 10000) / 100 : currentScaleValue;
  526. var pageNumber = firstPage.id;
  527. var pdfOpenParams = '#page=' + pageNumber;
  528. pdfOpenParams += '&zoom=' + normalizedScaleValue;
  529. var currentPageView = this._pages[pageNumber - 1];
  530. var container = this.container;
  531. var topLeft = currentPageView.getPagePoint(container.scrollLeft - firstPage.x, container.scrollTop - firstPage.y);
  532. var intLeft = Math.round(topLeft[0]);
  533. var intTop = Math.round(topLeft[1]);
  534. pdfOpenParams += ',' + intLeft + ',' + intTop;
  535. this._location = {
  536. pageNumber: pageNumber,
  537. scale: normalizedScaleValue,
  538. top: intTop,
  539. left: intLeft,
  540. rotation: this._pagesRotation,
  541. pdfOpenParams: pdfOpenParams
  542. };
  543. }
  544. }, {
  545. key: "_updateHelper",
  546. value: function _updateHelper(visiblePages) {
  547. throw new Error('Not implemented: _updateHelper');
  548. }
  549. }, {
  550. key: "update",
  551. value: function update() {
  552. var visible = this._getVisiblePages();
  553. var visiblePages = visible.views,
  554. numVisiblePages = visiblePages.length;
  555. if (numVisiblePages === 0) {
  556. return;
  557. }
  558. var newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
  559. this._buffer.resize(newCacheSize, visiblePages);
  560. this.renderingQueue.renderHighestPriority(visible);
  561. this._updateHelper(visiblePages);
  562. this._updateLocation(visible.first);
  563. this.eventBus.dispatch('updateviewarea', {
  564. source: this,
  565. location: this._location
  566. });
  567. }
  568. }, {
  569. key: "containsElement",
  570. value: function containsElement(element) {
  571. return this.container.contains(element);
  572. }
  573. }, {
  574. key: "focus",
  575. value: function focus() {
  576. this.container.focus();
  577. }
  578. }, {
  579. key: "_getCurrentVisiblePage",
  580. value: function _getCurrentVisiblePage() {
  581. if (!this.pagesCount) {
  582. return {
  583. views: []
  584. };
  585. }
  586. var pageView = this._pages[this._currentPageNumber - 1];
  587. var element = pageView.div;
  588. var view = {
  589. id: pageView.id,
  590. x: element.offsetLeft + element.clientLeft,
  591. y: element.offsetTop + element.clientTop,
  592. view: pageView
  593. };
  594. return {
  595. first: view,
  596. last: view,
  597. views: [view]
  598. };
  599. }
  600. }, {
  601. key: "_getVisiblePages",
  602. value: function _getVisiblePages() {
  603. return (0, _ui_utils.getVisibleElements)(this.container, this._pages, true, this._isScrollModeHorizontal);
  604. }
  605. }, {
  606. key: "isPageVisible",
  607. value: function isPageVisible(pageNumber) {
  608. if (!this.pdfDocument) {
  609. return false;
  610. }
  611. if (this.pageNumber < 1 || pageNumber > this.pagesCount) {
  612. console.error("".concat(this._name, ".isPageVisible: \"").concat(pageNumber, "\" is out of bounds."));
  613. return false;
  614. }
  615. return this._getVisiblePages().views.some(function (view) {
  616. return view.id === pageNumber;
  617. });
  618. }
  619. }, {
  620. key: "cleanup",
  621. value: function cleanup() {
  622. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  623. if (this._pages[i] && this._pages[i].renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED) {
  624. this._pages[i].reset();
  625. }
  626. }
  627. }
  628. }, {
  629. key: "_cancelRendering",
  630. value: function _cancelRendering() {
  631. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  632. if (this._pages[i]) {
  633. this._pages[i].cancelRendering();
  634. }
  635. }
  636. }
  637. }, {
  638. key: "_ensurePdfPageLoaded",
  639. value: function _ensurePdfPageLoaded(pageView) {
  640. var _this3 = this;
  641. if (pageView.pdfPage) {
  642. return Promise.resolve(pageView.pdfPage);
  643. }
  644. var pageNumber = pageView.id;
  645. if (this._pagesRequests[pageNumber]) {
  646. return this._pagesRequests[pageNumber];
  647. }
  648. var promise = this.pdfDocument.getPage(pageNumber).then(function (pdfPage) {
  649. if (!pageView.pdfPage) {
  650. pageView.setPdfPage(pdfPage);
  651. }
  652. _this3._pagesRequests[pageNumber] = null;
  653. return pdfPage;
  654. }).catch(function (reason) {
  655. console.error('Unable to get page for page view', reason);
  656. _this3._pagesRequests[pageNumber] = null;
  657. });
  658. this._pagesRequests[pageNumber] = promise;
  659. return promise;
  660. }
  661. }, {
  662. key: "forceRendering",
  663. value: function forceRendering(currentlyVisiblePages) {
  664. var _this4 = this;
  665. var visiblePages = currentlyVisiblePages || this._getVisiblePages();
  666. var scrollAhead = this._isScrollModeHorizontal ? this.scroll.right : this.scroll.down;
  667. var pageView = this.renderingQueue.getHighestPriority(visiblePages, this._pages, scrollAhead);
  668. if (pageView) {
  669. this._ensurePdfPageLoaded(pageView).then(function () {
  670. _this4.renderingQueue.renderView(pageView);
  671. });
  672. return true;
  673. }
  674. return false;
  675. }
  676. }, {
  677. key: "createTextLayerBuilder",
  678. value: function createTextLayerBuilder(textLayerDiv, pageIndex, viewport) {
  679. var enhanceTextSelection = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  680. return new _text_layer_builder.TextLayerBuilder({
  681. textLayerDiv: textLayerDiv,
  682. eventBus: this.eventBus,
  683. pageIndex: pageIndex,
  684. viewport: viewport,
  685. findController: this.isInPresentationMode ? null : this.findController,
  686. enhanceTextSelection: this.isInPresentationMode ? false : enhanceTextSelection
  687. });
  688. }
  689. }, {
  690. key: "createAnnotationLayerBuilder",
  691. value: function createAnnotationLayerBuilder(pageDiv, pdfPage) {
  692. var imageResourcesPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  693. var renderInteractiveForms = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  694. var l10n = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : _ui_utils.NullL10n;
  695. return new _annotation_layer_builder.AnnotationLayerBuilder({
  696. pageDiv: pageDiv,
  697. pdfPage: pdfPage,
  698. imageResourcesPath: imageResourcesPath,
  699. renderInteractiveForms: renderInteractiveForms,
  700. linkService: this.linkService,
  701. downloadManager: this.downloadManager,
  702. l10n: l10n
  703. });
  704. }
  705. }, {
  706. key: "getPagesOverview",
  707. value: function getPagesOverview() {
  708. var pagesOverview = this._pages.map(function (pageView) {
  709. var viewport = pageView.pdfPage.getViewport({
  710. scale: 1
  711. });
  712. return {
  713. width: viewport.width,
  714. height: viewport.height,
  715. rotation: viewport.rotation
  716. };
  717. });
  718. if (!this.enablePrintAutoRotate) {
  719. return pagesOverview;
  720. }
  721. var isFirstPagePortrait = (0, _ui_utils.isPortraitOrientation)(pagesOverview[0]);
  722. return pagesOverview.map(function (size) {
  723. if (isFirstPagePortrait === (0, _ui_utils.isPortraitOrientation)(size)) {
  724. return size;
  725. }
  726. return {
  727. width: size.height,
  728. height: size.width,
  729. rotation: (size.rotation + 90) % 360
  730. };
  731. });
  732. }
  733. }, {
  734. key: "_updateScrollMode",
  735. value: function _updateScrollMode() {
  736. var pageNumber = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  737. var scrollMode = this._scrollMode,
  738. viewer = this.viewer;
  739. viewer.classList.toggle('scrollHorizontal', scrollMode === _ui_utils.ScrollMode.HORIZONTAL);
  740. viewer.classList.toggle('scrollWrapped', scrollMode === _ui_utils.ScrollMode.WRAPPED);
  741. if (!this.pdfDocument || !pageNumber) {
  742. return;
  743. }
  744. if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
  745. this._setScale(this._currentScaleValue, true);
  746. }
  747. this._setCurrentPageNumber(pageNumber, true);
  748. this.update();
  749. }
  750. }, {
  751. key: "_updateSpreadMode",
  752. value: function _updateSpreadMode() {
  753. var pageNumber = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  754. if (!this.pdfDocument) {
  755. return;
  756. }
  757. var viewer = this.viewer,
  758. pages = this._pages;
  759. viewer.textContent = '';
  760. if (this._spreadMode === _ui_utils.SpreadMode.NONE) {
  761. for (var i = 0, iMax = pages.length; i < iMax; ++i) {
  762. viewer.appendChild(pages[i].div);
  763. }
  764. } else {
  765. var parity = this._spreadMode - 1;
  766. var spread = null;
  767. for (var _i = 0, _iMax = pages.length; _i < _iMax; ++_i) {
  768. if (spread === null) {
  769. spread = document.createElement('div');
  770. spread.className = 'spread';
  771. viewer.appendChild(spread);
  772. } else if (_i % 2 === parity) {
  773. spread = spread.cloneNode(false);
  774. viewer.appendChild(spread);
  775. }
  776. spread.appendChild(pages[_i].div);
  777. }
  778. }
  779. if (!pageNumber) {
  780. return;
  781. }
  782. this._setCurrentPageNumber(pageNumber, true);
  783. this.update();
  784. }
  785. }, {
  786. key: "pagesCount",
  787. get: function get() {
  788. return this._pages.length;
  789. }
  790. }, {
  791. key: "pageViewsReady",
  792. get: function get() {
  793. return this._pageViewsReady;
  794. }
  795. }, {
  796. key: "currentPageNumber",
  797. get: function get() {
  798. return this._currentPageNumber;
  799. },
  800. set: function set(val) {
  801. if (!Number.isInteger(val)) {
  802. throw new Error('Invalid page number.');
  803. }
  804. if (!this.pdfDocument) {
  805. return;
  806. }
  807. if (!this._setCurrentPageNumber(val, true)) {
  808. console.error("".concat(this._name, ".currentPageNumber: \"").concat(val, "\" is not a valid page."));
  809. }
  810. }
  811. }, {
  812. key: "currentPageLabel",
  813. get: function get() {
  814. return this._pageLabels && this._pageLabels[this._currentPageNumber - 1];
  815. },
  816. set: function set(val) {
  817. if (!this.pdfDocument) {
  818. return;
  819. }
  820. var page = val | 0;
  821. if (this._pageLabels) {
  822. var i = this._pageLabels.indexOf(val);
  823. if (i >= 0) {
  824. page = i + 1;
  825. }
  826. }
  827. if (!this._setCurrentPageNumber(page, true)) {
  828. console.error("".concat(this._name, ".currentPageLabel: \"").concat(val, "\" is not a valid page."));
  829. }
  830. }
  831. }, {
  832. key: "currentScale",
  833. get: function get() {
  834. return this._currentScale !== _ui_utils.UNKNOWN_SCALE ? this._currentScale : _ui_utils.DEFAULT_SCALE;
  835. },
  836. set: function set(val) {
  837. if (isNaN(val)) {
  838. throw new Error('Invalid numeric scale.');
  839. }
  840. if (!this.pdfDocument) {
  841. return;
  842. }
  843. this._setScale(val, false);
  844. }
  845. }, {
  846. key: "currentScaleValue",
  847. get: function get() {
  848. return this._currentScaleValue;
  849. },
  850. set: function set(val) {
  851. if (!this.pdfDocument) {
  852. return;
  853. }
  854. this._setScale(val, false);
  855. }
  856. }, {
  857. key: "pagesRotation",
  858. get: function get() {
  859. return this._pagesRotation;
  860. },
  861. set: function set(rotation) {
  862. if (!(0, _ui_utils.isValidRotation)(rotation)) {
  863. throw new Error('Invalid pages rotation angle.');
  864. }
  865. if (!this.pdfDocument) {
  866. return;
  867. }
  868. if (this._pagesRotation === rotation) {
  869. return;
  870. }
  871. this._pagesRotation = rotation;
  872. var pageNumber = this._currentPageNumber;
  873. for (var i = 0, ii = this._pages.length; i < ii; i++) {
  874. var pageView = this._pages[i];
  875. pageView.update(pageView.scale, rotation);
  876. }
  877. if (this._currentScaleValue) {
  878. this._setScale(this._currentScaleValue, true);
  879. }
  880. this.eventBus.dispatch('rotationchanging', {
  881. source: this,
  882. pagesRotation: rotation,
  883. pageNumber: pageNumber
  884. });
  885. if (this.defaultRenderingQueue) {
  886. this.update();
  887. }
  888. }
  889. }, {
  890. key: "_setDocumentViewerElement",
  891. get: function get() {
  892. throw new Error('Not implemented: _setDocumentViewerElement');
  893. }
  894. }, {
  895. key: "_isScrollModeHorizontal",
  896. get: function get() {
  897. return this.isInPresentationMode ? false : this._scrollMode === _ui_utils.ScrollMode.HORIZONTAL;
  898. }
  899. }, {
  900. key: "isInPresentationMode",
  901. get: function get() {
  902. return this.presentationModeState === _ui_utils.PresentationModeState.FULLSCREEN;
  903. }
  904. }, {
  905. key: "isChangingPresentationMode",
  906. get: function get() {
  907. return this.presentationModeState === _ui_utils.PresentationModeState.CHANGING;
  908. }
  909. }, {
  910. key: "isHorizontalScrollbarEnabled",
  911. get: function get() {
  912. return this.isInPresentationMode ? false : this.container.scrollWidth > this.container.clientWidth;
  913. }
  914. }, {
  915. key: "isVerticalScrollbarEnabled",
  916. get: function get() {
  917. return this.isInPresentationMode ? false : this.container.scrollHeight > this.container.clientHeight;
  918. }
  919. }, {
  920. key: "hasEqualPageSizes",
  921. get: function get() {
  922. var firstPageView = this._pages[0];
  923. for (var i = 1, ii = this._pages.length; i < ii; ++i) {
  924. var pageView = this._pages[i];
  925. if (pageView.width !== firstPageView.width || pageView.height !== firstPageView.height) {
  926. return false;
  927. }
  928. }
  929. return true;
  930. }
  931. }, {
  932. key: "scrollMode",
  933. get: function get() {
  934. return this._scrollMode;
  935. },
  936. set: function set(mode) {
  937. if (this._scrollMode === mode) {
  938. return;
  939. }
  940. if (!(0, _ui_utils.isValidScrollMode)(mode)) {
  941. throw new Error("Invalid scroll mode: ".concat(mode));
  942. }
  943. this._scrollMode = mode;
  944. this.eventBus.dispatch('scrollmodechanged', {
  945. source: this,
  946. mode: mode
  947. });
  948. this._updateScrollMode(this._currentPageNumber);
  949. }
  950. }, {
  951. key: "spreadMode",
  952. get: function get() {
  953. return this._spreadMode;
  954. },
  955. set: function set(mode) {
  956. if (this._spreadMode === mode) {
  957. return;
  958. }
  959. if (!(0, _ui_utils.isValidSpreadMode)(mode)) {
  960. throw new Error("Invalid spread mode: ".concat(mode));
  961. }
  962. this._spreadMode = mode;
  963. this.eventBus.dispatch('spreadmodechanged', {
  964. source: this,
  965. mode: mode
  966. });
  967. this._updateSpreadMode(this._currentPageNumber);
  968. }
  969. }]);
  970. return BaseViewer;
  971. }();
  972. exports.BaseViewer = BaseViewer;