pdf_history.js 14 KB

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