2
0

pdf_history.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 (destination.rotation !== undefined) {
  115. this.initialRotation = destination.rotation;
  116. }
  117. if (destination.dest) {
  118. this.initialBookmark = JSON.stringify(destination.dest);
  119. this._destination.page = null;
  120. } else if (destination.hash) {
  121. this.initialBookmark = destination.hash;
  122. } else if (destination.page) {
  123. this.initialBookmark = 'page=' + destination.page;
  124. }
  125. }
  126. }, {
  127. key: 'push',
  128. value: function push(_ref2) {
  129. var _this2 = this;
  130. var namedDest = _ref2.namedDest,
  131. explicitDest = _ref2.explicitDest,
  132. pageNumber = _ref2.pageNumber;
  133. if (!this.initialized) {
  134. return;
  135. }
  136. if (namedDest && typeof namedDest !== 'string' || !(explicitDest instanceof Array) || !(Number.isInteger(pageNumber) && pageNumber > 0 && pageNumber <= this.linkService.pagesCount)) {
  137. console.error('PDFHistory.push: Invalid parameters.');
  138. return;
  139. }
  140. var hash = namedDest || JSON.stringify(explicitDest);
  141. if (!hash) {
  142. return;
  143. }
  144. var forceReplace = false;
  145. if (this._destination && (isDestHashesEqual(this._destination.hash, hash) || isDestArraysEqual(this._destination.dest, explicitDest))) {
  146. if (this._destination.page) {
  147. return;
  148. }
  149. forceReplace = true;
  150. }
  151. if (this._popStateInProgress && !forceReplace) {
  152. return;
  153. }
  154. this._pushOrReplaceState({
  155. dest: explicitDest,
  156. hash: hash,
  157. page: pageNumber,
  158. rotation: this.linkService.rotation
  159. }, forceReplace);
  160. if (!this._popStateInProgress) {
  161. this._popStateInProgress = true;
  162. Promise.resolve().then(function () {
  163. _this2._popStateInProgress = false;
  164. });
  165. }
  166. }
  167. }, {
  168. key: 'pushCurrentPosition',
  169. value: function pushCurrentPosition() {
  170. if (!this.initialized || this._popStateInProgress) {
  171. return;
  172. }
  173. this._tryPushCurrentPosition();
  174. }
  175. }, {
  176. key: 'back',
  177. value: function back() {
  178. if (!this.initialized || this._popStateInProgress) {
  179. return;
  180. }
  181. var state = window.history.state;
  182. if (this._isValidState(state) && state.uid > 0) {
  183. window.history.back();
  184. }
  185. }
  186. }, {
  187. key: 'forward',
  188. value: function forward() {
  189. if (!this.initialized || this._popStateInProgress) {
  190. return;
  191. }
  192. var state = window.history.state;
  193. if (this._isValidState(state) && state.uid < this._maxUid) {
  194. window.history.forward();
  195. }
  196. }
  197. }, {
  198. key: '_pushOrReplaceState',
  199. value: function _pushOrReplaceState(destination) {
  200. var forceReplace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  201. var shouldReplace = forceReplace || !this._destination;
  202. var newState = {
  203. fingerprint: this.fingerprint,
  204. uid: shouldReplace ? this._uid : this._uid + 1,
  205. destination: destination
  206. };
  207. this._updateInternalState(destination, newState.uid);
  208. if (shouldReplace) {
  209. window.history.replaceState(newState, '', document.URL);
  210. } else {
  211. this._maxUid = this._uid;
  212. window.history.pushState(newState, '', document.URL);
  213. }
  214. }
  215. }, {
  216. key: '_tryPushCurrentPosition',
  217. value: function _tryPushCurrentPosition() {
  218. var temporary = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  219. if (!this._position) {
  220. return;
  221. }
  222. var position = this._position;
  223. if (temporary) {
  224. position = (0, _ui_utils.cloneObj)(this._position);
  225. position.temporary = true;
  226. }
  227. if (!this._destination) {
  228. this._pushOrReplaceState(position);
  229. return;
  230. }
  231. if (this._destination.temporary) {
  232. this._pushOrReplaceState(position, true);
  233. return;
  234. }
  235. if (this._destination.hash === position.hash) {
  236. return;
  237. }
  238. if (!this._destination.page && (POSITION_UPDATED_THRESHOLD <= 0 || this._numPositionUpdates <= POSITION_UPDATED_THRESHOLD)) {
  239. return;
  240. }
  241. var forceReplace = false;
  242. if (this._destination.page === position.first || this._destination.page === position.page) {
  243. if (this._destination.dest || !this._destination.first) {
  244. return;
  245. }
  246. forceReplace = true;
  247. }
  248. this._pushOrReplaceState(position, forceReplace);
  249. }
  250. }, {
  251. key: '_isValidState',
  252. value: function _isValidState(state) {
  253. if (!state) {
  254. return false;
  255. }
  256. if (state.fingerprint !== this.fingerprint) {
  257. return false;
  258. }
  259. if (!Number.isInteger(state.uid) || state.uid < 0) {
  260. return false;
  261. }
  262. if (state.destination === null || _typeof(state.destination) !== 'object') {
  263. return false;
  264. }
  265. return true;
  266. }
  267. }, {
  268. key: '_updateInternalState',
  269. value: function _updateInternalState(destination, uid) {
  270. var removeTemporary = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  271. if (this._updateViewareaTimeout) {
  272. clearTimeout(this._updateViewareaTimeout);
  273. this._updateViewareaTimeout = null;
  274. }
  275. if (removeTemporary && destination && destination.temporary) {
  276. delete destination.temporary;
  277. }
  278. this._destination = destination;
  279. this._uid = uid;
  280. this._numPositionUpdates = 0;
  281. }
  282. }, {
  283. key: '_updateViewarea',
  284. value: function _updateViewarea(_ref3) {
  285. var _this3 = this;
  286. var location = _ref3.location;
  287. if (this._updateViewareaTimeout) {
  288. clearTimeout(this._updateViewareaTimeout);
  289. this._updateViewareaTimeout = null;
  290. }
  291. this._position = {
  292. hash: this._isViewerInPresentationMode ? 'page=' + location.pageNumber : location.pdfOpenParams.substring(1),
  293. page: this.linkService.page,
  294. first: location.pageNumber,
  295. rotation: location.rotation
  296. };
  297. if (this._popStateInProgress) {
  298. return;
  299. }
  300. if (POSITION_UPDATED_THRESHOLD > 0 && this._isPagesLoaded && this._destination && !this._destination.page) {
  301. this._numPositionUpdates++;
  302. }
  303. if (UPDATE_VIEWAREA_TIMEOUT > 0) {
  304. this._updateViewareaTimeout = setTimeout(function () {
  305. if (!_this3._popStateInProgress) {
  306. _this3._tryPushCurrentPosition(true);
  307. }
  308. _this3._updateViewareaTimeout = null;
  309. }, UPDATE_VIEWAREA_TIMEOUT);
  310. }
  311. }
  312. }, {
  313. key: '_popState',
  314. value: function _popState(_ref4) {
  315. var _this4 = this;
  316. var state = _ref4.state;
  317. var newHash = getCurrentHash(),
  318. hashChanged = this._currentHash !== newHash;
  319. this._currentHash = newHash;
  320. if (!state || false) {
  321. this._uid++;
  322. var _parseCurrentHash2 = parseCurrentHash(this.linkService),
  323. hash = _parseCurrentHash2.hash,
  324. page = _parseCurrentHash2.page,
  325. rotation = _parseCurrentHash2.rotation;
  326. this._pushOrReplaceState({
  327. hash: hash,
  328. page: page,
  329. rotation: rotation
  330. }, true);
  331. return;
  332. }
  333. if (!this._isValidState(state)) {
  334. return;
  335. }
  336. this._popStateInProgress = true;
  337. if (hashChanged) {
  338. this._blockHashChange++;
  339. (0, _ui_utils.waitOnEventOrTimeout)({
  340. target: window,
  341. name: 'hashchange',
  342. delay: HASH_CHANGE_TIMEOUT
  343. }).then(function () {
  344. _this4._blockHashChange--;
  345. });
  346. }
  347. var destination = state.destination;
  348. this._updateInternalState(destination, state.uid, true);
  349. if ((0, _ui_utils.isValidRotation)(destination.rotation)) {
  350. this.linkService.rotation = destination.rotation;
  351. }
  352. if (destination.dest) {
  353. this.linkService.navigateTo(destination.dest);
  354. } else if (destination.hash) {
  355. this.linkService.setHash(destination.hash);
  356. } else if (destination.page) {
  357. this.linkService.page = destination.page;
  358. }
  359. Promise.resolve().then(function () {
  360. _this4._popStateInProgress = false;
  361. });
  362. }
  363. }, {
  364. key: '_bindEvents',
  365. value: function _bindEvents() {
  366. var _this5 = this;
  367. var _boundEvents = this._boundEvents,
  368. eventBus = this.eventBus;
  369. _boundEvents.updateViewarea = this._updateViewarea.bind(this);
  370. _boundEvents.popState = this._popState.bind(this);
  371. _boundEvents.pageHide = function (evt) {
  372. if (!_this5._destination) {
  373. _this5._tryPushCurrentPosition();
  374. }
  375. };
  376. eventBus.on('updateviewarea', _boundEvents.updateViewarea);
  377. window.addEventListener('popstate', _boundEvents.popState);
  378. window.addEventListener('pagehide', _boundEvents.pageHide);
  379. }
  380. }, {
  381. key: 'popStateInProgress',
  382. get: function get() {
  383. return this.initialized && (this._popStateInProgress || this._blockHashChange > 0);
  384. }
  385. }]);
  386. return PDFHistory;
  387. }();
  388. function isDestHashesEqual(destHash, pushHash) {
  389. if (typeof destHash !== 'string' || typeof pushHash !== 'string') {
  390. return false;
  391. }
  392. if (destHash === pushHash) {
  393. return true;
  394. }
  395. var _parseQueryString = (0, _ui_utils.parseQueryString)(destHash),
  396. nameddest = _parseQueryString.nameddest;
  397. if (nameddest === pushHash) {
  398. return true;
  399. }
  400. return false;
  401. }
  402. function isDestArraysEqual(firstDest, secondDest) {
  403. function isEntryEqual(first, second) {
  404. if ((typeof first === 'undefined' ? 'undefined' : _typeof(first)) !== (typeof second === 'undefined' ? 'undefined' : _typeof(second))) {
  405. return false;
  406. }
  407. if (first instanceof Array || second instanceof Array) {
  408. return false;
  409. }
  410. if (first !== null && (typeof first === 'undefined' ? 'undefined' : _typeof(first)) === 'object' && second !== null) {
  411. if (Object.keys(first).length !== Object.keys(second).length) {
  412. return false;
  413. }
  414. for (var key in first) {
  415. if (!isEntryEqual(first[key], second[key])) {
  416. return false;
  417. }
  418. }
  419. return true;
  420. }
  421. return first === second || Number.isNaN(first) && Number.isNaN(second);
  422. }
  423. if (!(firstDest instanceof Array && secondDest instanceof Array)) {
  424. return false;
  425. }
  426. if (firstDest.length !== secondDest.length) {
  427. return false;
  428. }
  429. for (var i = 0, ii = firstDest.length; i < ii; i++) {
  430. if (!isEntryEqual(firstDest[i], secondDest[i])) {
  431. return false;
  432. }
  433. }
  434. return true;
  435. }
  436. exports.PDFHistory = PDFHistory;
  437. exports.isDestHashesEqual = isDestHashesEqual;
  438. exports.isDestArraysEqual = isDestArraysEqual;