2
0

pdf_history.js 14 KB

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