pdf_history.js 15 KB

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