pdf_history.js 15 KB

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