pdf_history.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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.isDestHashesEqual = isDestHashesEqual;
  27. exports.isDestArraysEqual = isDestArraysEqual;
  28. exports.PDFHistory = void 0;
  29. var _ui_utils = require("./ui_utils");
  30. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  31. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  32. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  33. function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } 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"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  34. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  35. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  36. 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); } }
  37. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  38. var HASH_CHANGE_TIMEOUT = 1000;
  39. var POSITION_UPDATED_THRESHOLD = 50;
  40. var UPDATE_VIEWAREA_TIMEOUT = 1000;
  41. function getCurrentHash() {
  42. return document.location.hash;
  43. }
  44. function parseCurrentHash(linkService) {
  45. var hash = unescape(getCurrentHash()).substring(1);
  46. var params = (0, _ui_utils.parseQueryString)(hash);
  47. var page = params.page | 0;
  48. if (!(Number.isInteger(page) && page > 0 && page <= linkService.pagesCount)) {
  49. page = null;
  50. }
  51. return {
  52. hash: hash,
  53. page: page,
  54. rotation: linkService.rotation
  55. };
  56. }
  57. var PDFHistory =
  58. /*#__PURE__*/
  59. function () {
  60. function PDFHistory(_ref) {
  61. var _this = this;
  62. var linkService = _ref.linkService,
  63. eventBus = _ref.eventBus;
  64. _classCallCheck(this, PDFHistory);
  65. this.linkService = linkService;
  66. this.eventBus = eventBus || (0, _ui_utils.getGlobalEventBus)();
  67. this.initialized = false;
  68. this.initialBookmark = null;
  69. this.initialRotation = null;
  70. this._boundEvents = Object.create(null);
  71. this._isViewerInPresentationMode = false;
  72. this._isPagesLoaded = false;
  73. this.eventBus.on('presentationmodechanged', function (evt) {
  74. _this._isViewerInPresentationMode = evt.active || evt.switchInProgress;
  75. });
  76. this.eventBus.on('pagesloaded', function (evt) {
  77. _this._isPagesLoaded = !!evt.pagesCount;
  78. });
  79. }
  80. _createClass(PDFHistory, [{
  81. key: "initialize",
  82. value: function initialize(_ref2) {
  83. var fingerprint = _ref2.fingerprint,
  84. _ref2$resetHistory = _ref2.resetHistory,
  85. resetHistory = _ref2$resetHistory === void 0 ? false : _ref2$resetHistory,
  86. _ref2$updateUrl = _ref2.updateUrl,
  87. updateUrl = _ref2$updateUrl === void 0 ? false : _ref2$updateUrl;
  88. if (!fingerprint || typeof fingerprint !== 'string') {
  89. console.error('PDFHistory.initialize: The "fingerprint" must be a non-empty string.');
  90. return;
  91. }
  92. var reInitialized = this.initialized && this.fingerprint !== fingerprint;
  93. this.fingerprint = fingerprint;
  94. this._updateUrl = updateUrl === true;
  95. if (!this.initialized) {
  96. this._bindEvents();
  97. }
  98. var state = window.history.state;
  99. this.initialized = true;
  100. this.initialBookmark = null;
  101. this.initialRotation = null;
  102. this._popStateInProgress = false;
  103. this._blockHashChange = 0;
  104. this._currentHash = getCurrentHash();
  105. this._numPositionUpdates = 0;
  106. this._uid = this._maxUid = 0;
  107. this._destination = null;
  108. this._position = null;
  109. if (!this._isValidState(state, true) || resetHistory) {
  110. var _parseCurrentHash = parseCurrentHash(this.linkService),
  111. hash = _parseCurrentHash.hash,
  112. page = _parseCurrentHash.page,
  113. rotation = _parseCurrentHash.rotation;
  114. if (!hash || reInitialized || resetHistory) {
  115. this._pushOrReplaceState(null, true);
  116. return;
  117. }
  118. this._pushOrReplaceState({
  119. hash: hash,
  120. page: page,
  121. rotation: rotation
  122. }, true);
  123. return;
  124. }
  125. var destination = state.destination;
  126. this._updateInternalState(destination, state.uid, true);
  127. if (this._uid > this._maxUid) {
  128. this._maxUid = this._uid;
  129. }
  130. if (destination.rotation !== undefined) {
  131. this.initialRotation = destination.rotation;
  132. }
  133. if (destination.dest) {
  134. this.initialBookmark = JSON.stringify(destination.dest);
  135. this._destination.page = null;
  136. } else if (destination.hash) {
  137. this.initialBookmark = destination.hash;
  138. } else if (destination.page) {
  139. this.initialBookmark = "page=".concat(destination.page);
  140. }
  141. }
  142. }, {
  143. key: "push",
  144. value: function push(_ref3) {
  145. var _this2 = this;
  146. var _ref3$namedDest = _ref3.namedDest,
  147. namedDest = _ref3$namedDest === void 0 ? null : _ref3$namedDest,
  148. explicitDest = _ref3.explicitDest,
  149. pageNumber = _ref3.pageNumber;
  150. if (!this.initialized) {
  151. return;
  152. }
  153. if (namedDest && typeof namedDest !== 'string') {
  154. console.error('PDFHistory.push: ' + "\"".concat(namedDest, "\" is not a valid namedDest parameter."));
  155. return;
  156. } else if (!Array.isArray(explicitDest)) {
  157. console.error('PDFHistory.push: ' + "\"".concat(explicitDest, "\" is not a valid explicitDest parameter."));
  158. return;
  159. } else if (!(Number.isInteger(pageNumber) && pageNumber > 0 && pageNumber <= this.linkService.pagesCount)) {
  160. if (pageNumber !== null || this._destination) {
  161. console.error('PDFHistory.push: ' + "\"".concat(pageNumber, "\" is not a valid pageNumber parameter."));
  162. return;
  163. }
  164. }
  165. var hash = namedDest || JSON.stringify(explicitDest);
  166. if (!hash) {
  167. return;
  168. }
  169. var forceReplace = false;
  170. if (this._destination && (isDestHashesEqual(this._destination.hash, hash) || isDestArraysEqual(this._destination.dest, explicitDest))) {
  171. if (this._destination.page) {
  172. return;
  173. }
  174. forceReplace = true;
  175. }
  176. if (this._popStateInProgress && !forceReplace) {
  177. return;
  178. }
  179. this._pushOrReplaceState({
  180. dest: explicitDest,
  181. hash: hash,
  182. page: pageNumber,
  183. rotation: this.linkService.rotation
  184. }, forceReplace);
  185. if (!this._popStateInProgress) {
  186. this._popStateInProgress = true;
  187. Promise.resolve().then(function () {
  188. _this2._popStateInProgress = false;
  189. });
  190. }
  191. }
  192. }, {
  193. key: "pushCurrentPosition",
  194. value: function pushCurrentPosition() {
  195. if (!this.initialized || this._popStateInProgress) {
  196. return;
  197. }
  198. this._tryPushCurrentPosition();
  199. }
  200. }, {
  201. key: "back",
  202. value: function back() {
  203. if (!this.initialized || this._popStateInProgress) {
  204. return;
  205. }
  206. var state = window.history.state;
  207. if (this._isValidState(state) && state.uid > 0) {
  208. window.history.back();
  209. }
  210. }
  211. }, {
  212. key: "forward",
  213. value: function forward() {
  214. if (!this.initialized || this._popStateInProgress) {
  215. return;
  216. }
  217. var state = window.history.state;
  218. if (this._isValidState(state) && state.uid < this._maxUid) {
  219. window.history.forward();
  220. }
  221. }
  222. }, {
  223. key: "_pushOrReplaceState",
  224. value: function _pushOrReplaceState(destination) {
  225. var forceReplace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  226. var shouldReplace = forceReplace || !this._destination;
  227. var newState = {
  228. fingerprint: this.fingerprint,
  229. uid: shouldReplace ? this._uid : this._uid + 1,
  230. destination: destination
  231. };
  232. this._updateInternalState(destination, newState.uid);
  233. var newUrl;
  234. if (this._updateUrl && destination && destination.hash) {
  235. var baseUrl = document.location.href.split('#')[0];
  236. if (!baseUrl.startsWith('file://')) {
  237. newUrl = "".concat(baseUrl, "#").concat(destination.hash);
  238. }
  239. }
  240. if (shouldReplace) {
  241. if (newUrl) {
  242. window.history.replaceState(newState, '', newUrl);
  243. } else {
  244. window.history.replaceState(newState, '');
  245. }
  246. } else {
  247. this._maxUid = this._uid;
  248. if (newUrl) {
  249. window.history.pushState(newState, '', newUrl);
  250. } else {
  251. window.history.pushState(newState, '');
  252. }
  253. }
  254. }
  255. }, {
  256. key: "_tryPushCurrentPosition",
  257. value: function _tryPushCurrentPosition() {
  258. var temporary = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  259. if (!this._position) {
  260. return;
  261. }
  262. var position = this._position;
  263. if (temporary) {
  264. position = Object.assign(Object.create(null), this._position);
  265. position.temporary = true;
  266. }
  267. if (!this._destination) {
  268. this._pushOrReplaceState(position);
  269. return;
  270. }
  271. if (this._destination.temporary) {
  272. this._pushOrReplaceState(position, true);
  273. return;
  274. }
  275. if (this._destination.hash === position.hash) {
  276. return;
  277. }
  278. if (!this._destination.page && (POSITION_UPDATED_THRESHOLD <= 0 || this._numPositionUpdates <= POSITION_UPDATED_THRESHOLD)) {
  279. return;
  280. }
  281. var forceReplace = false;
  282. if (this._destination.page >= position.first && this._destination.page <= position.page) {
  283. if (this._destination.dest || !this._destination.first) {
  284. return;
  285. }
  286. forceReplace = true;
  287. }
  288. this._pushOrReplaceState(position, forceReplace);
  289. }
  290. }, {
  291. key: "_isValidState",
  292. value: function _isValidState(state) {
  293. var checkReload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  294. if (!state) {
  295. return false;
  296. }
  297. if (state.fingerprint !== this.fingerprint) {
  298. if (checkReload) {
  299. if (typeof state.fingerprint !== 'string' || state.fingerprint.length !== this.fingerprint.length) {
  300. return false;
  301. }
  302. var _performance$getEntri = performance.getEntriesByType('navigation'),
  303. _performance$getEntri2 = _slicedToArray(_performance$getEntri, 1),
  304. perfEntry = _performance$getEntri2[0];
  305. if (!perfEntry || perfEntry.type !== 'reload') {
  306. return false;
  307. }
  308. } else {
  309. return false;
  310. }
  311. }
  312. if (!Number.isInteger(state.uid) || state.uid < 0) {
  313. return false;
  314. }
  315. if (state.destination === null || _typeof(state.destination) !== 'object') {
  316. return false;
  317. }
  318. return true;
  319. }
  320. }, {
  321. key: "_updateInternalState",
  322. value: function _updateInternalState(destination, uid) {
  323. var removeTemporary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  324. if (this._updateViewareaTimeout) {
  325. clearTimeout(this._updateViewareaTimeout);
  326. this._updateViewareaTimeout = null;
  327. }
  328. if (removeTemporary && destination && destination.temporary) {
  329. delete destination.temporary;
  330. }
  331. this._destination = destination;
  332. this._uid = uid;
  333. this._numPositionUpdates = 0;
  334. }
  335. }, {
  336. key: "_updateViewarea",
  337. value: function _updateViewarea(_ref4) {
  338. var _this3 = this;
  339. var location = _ref4.location;
  340. if (this._updateViewareaTimeout) {
  341. clearTimeout(this._updateViewareaTimeout);
  342. this._updateViewareaTimeout = null;
  343. }
  344. this._position = {
  345. hash: this._isViewerInPresentationMode ? "page=".concat(location.pageNumber) : location.pdfOpenParams.substring(1),
  346. page: this.linkService.page,
  347. first: location.pageNumber,
  348. rotation: location.rotation
  349. };
  350. if (this._popStateInProgress) {
  351. return;
  352. }
  353. if (POSITION_UPDATED_THRESHOLD > 0 && this._isPagesLoaded && this._destination && !this._destination.page) {
  354. this._numPositionUpdates++;
  355. }
  356. if (UPDATE_VIEWAREA_TIMEOUT > 0) {
  357. this._updateViewareaTimeout = setTimeout(function () {
  358. if (!_this3._popStateInProgress) {
  359. _this3._tryPushCurrentPosition(true);
  360. }
  361. _this3._updateViewareaTimeout = null;
  362. }, UPDATE_VIEWAREA_TIMEOUT);
  363. }
  364. }
  365. }, {
  366. key: "_popState",
  367. value: function _popState(_ref5) {
  368. var _this4 = this;
  369. var state = _ref5.state;
  370. var newHash = getCurrentHash(),
  371. hashChanged = this._currentHash !== newHash;
  372. this._currentHash = newHash;
  373. if (!state || false) {
  374. this._uid++;
  375. var _parseCurrentHash2 = parseCurrentHash(this.linkService),
  376. hash = _parseCurrentHash2.hash,
  377. page = _parseCurrentHash2.page,
  378. rotation = _parseCurrentHash2.rotation;
  379. this._pushOrReplaceState({
  380. hash: hash,
  381. page: page,
  382. rotation: rotation
  383. }, true);
  384. return;
  385. }
  386. if (!this._isValidState(state)) {
  387. return;
  388. }
  389. this._popStateInProgress = true;
  390. if (hashChanged) {
  391. this._blockHashChange++;
  392. (0, _ui_utils.waitOnEventOrTimeout)({
  393. target: window,
  394. name: 'hashchange',
  395. delay: HASH_CHANGE_TIMEOUT
  396. }).then(function () {
  397. _this4._blockHashChange--;
  398. });
  399. }
  400. var destination = state.destination;
  401. this._updateInternalState(destination, state.uid, true);
  402. if (this._uid > this._maxUid) {
  403. this._maxUid = this._uid;
  404. }
  405. if ((0, _ui_utils.isValidRotation)(destination.rotation)) {
  406. this.linkService.rotation = destination.rotation;
  407. }
  408. if (destination.dest) {
  409. this.linkService.navigateTo(destination.dest);
  410. } else if (destination.hash) {
  411. this.linkService.setHash(destination.hash);
  412. } else if (destination.page) {
  413. this.linkService.page = destination.page;
  414. }
  415. Promise.resolve().then(function () {
  416. _this4._popStateInProgress = false;
  417. });
  418. }
  419. }, {
  420. key: "_bindEvents",
  421. value: function _bindEvents() {
  422. var _this5 = this;
  423. var _boundEvents = this._boundEvents,
  424. eventBus = this.eventBus;
  425. _boundEvents.updateViewarea = this._updateViewarea.bind(this);
  426. _boundEvents.popState = this._popState.bind(this);
  427. _boundEvents.pageHide = function (evt) {
  428. if (!_this5._destination || _this5._destination.temporary) {
  429. _this5._tryPushCurrentPosition();
  430. }
  431. };
  432. eventBus.on('updateviewarea', _boundEvents.updateViewarea);
  433. window.addEventListener('popstate', _boundEvents.popState);
  434. window.addEventListener('pagehide', _boundEvents.pageHide);
  435. }
  436. }, {
  437. key: "popStateInProgress",
  438. get: function get() {
  439. return this.initialized && (this._popStateInProgress || this._blockHashChange > 0);
  440. }
  441. }]);
  442. return PDFHistory;
  443. }();
  444. exports.PDFHistory = PDFHistory;
  445. function isDestHashesEqual(destHash, pushHash) {
  446. if (typeof destHash !== 'string' || typeof pushHash !== 'string') {
  447. return false;
  448. }
  449. if (destHash === pushHash) {
  450. return true;
  451. }
  452. var _parseQueryString = (0, _ui_utils.parseQueryString)(destHash),
  453. nameddest = _parseQueryString.nameddest;
  454. if (nameddest === pushHash) {
  455. return true;
  456. }
  457. return false;
  458. }
  459. function isDestArraysEqual(firstDest, secondDest) {
  460. function isEntryEqual(first, second) {
  461. if (_typeof(first) !== _typeof(second)) {
  462. return false;
  463. }
  464. if (Array.isArray(first) || Array.isArray(second)) {
  465. return false;
  466. }
  467. if (first !== null && _typeof(first) === 'object' && second !== null) {
  468. if (Object.keys(first).length !== Object.keys(second).length) {
  469. return false;
  470. }
  471. for (var key in first) {
  472. if (!isEntryEqual(first[key], second[key])) {
  473. return false;
  474. }
  475. }
  476. return true;
  477. }
  478. return first === second || Number.isNaN(first) && Number.isNaN(second);
  479. }
  480. if (!(Array.isArray(firstDest) && Array.isArray(secondDest))) {
  481. return false;
  482. }
  483. if (firstDest.length !== secondDest.length) {
  484. return false;
  485. }
  486. for (var i = 0, ii = firstDest.length; i < ii; i++) {
  487. if (!isEntryEqual(firstDest[i], secondDest[i])) {
  488. return false;
  489. }
  490. }
  491. return true;
  492. }