pdf_history.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.PDFHistory = undefined;
  20. var _dom_events = require('pdfjs-web/dom_events');
  21. function PDFHistory(options) {
  22. this.linkService = options.linkService;
  23. this.eventBus = options.eventBus || _dom_events.domEvents.getGlobalEventBus();
  24. this.initialized = false;
  25. this.initialDestination = null;
  26. this.initialBookmark = null;
  27. }
  28. PDFHistory.prototype = {
  29. initialize: function pdfHistoryInitialize(fingerprint) {
  30. this.initialized = true;
  31. this.reInitialized = false;
  32. this.allowHashChange = true;
  33. this.historyUnlocked = true;
  34. this.isViewerInPresentationMode = false;
  35. this.previousHash = window.location.hash.substring(1);
  36. this.currentBookmark = '';
  37. this.currentPage = 0;
  38. this.updatePreviousBookmark = false;
  39. this.previousBookmark = '';
  40. this.previousPage = 0;
  41. this.nextHashParam = '';
  42. this.fingerprint = fingerprint;
  43. this.currentUid = this.uid = 0;
  44. this.current = {};
  45. var state = window.history.state;
  46. if (this._isStateObjectDefined(state)) {
  47. if (state.target.dest) {
  48. this.initialDestination = state.target.dest;
  49. } else {
  50. this.initialBookmark = state.target.hash;
  51. }
  52. this.currentUid = state.uid;
  53. this.uid = state.uid + 1;
  54. this.current = state.target;
  55. } else {
  56. if (state && state.fingerprint && this.fingerprint !== state.fingerprint) {
  57. this.reInitialized = true;
  58. }
  59. this._pushOrReplaceState({ fingerprint: this.fingerprint }, true);
  60. }
  61. var self = this;
  62. window.addEventListener('popstate', function pdfHistoryPopstate(evt) {
  63. if (!self.historyUnlocked) {
  64. return;
  65. }
  66. if (evt.state) {
  67. self._goTo(evt.state);
  68. return;
  69. }
  70. if (self.uid === 0) {
  71. var previousParams = self.previousHash && self.currentBookmark && self.previousHash !== self.currentBookmark ? {
  72. hash: self.currentBookmark,
  73. page: self.currentPage
  74. } : { page: 1 };
  75. replacePreviousHistoryState(previousParams, function () {
  76. updateHistoryWithCurrentHash();
  77. });
  78. } else {
  79. updateHistoryWithCurrentHash();
  80. }
  81. });
  82. function updateHistoryWithCurrentHash() {
  83. self.previousHash = window.location.hash.slice(1);
  84. self._pushToHistory({ hash: self.previousHash }, false, true);
  85. self._updatePreviousBookmark();
  86. }
  87. function replacePreviousHistoryState(params, callback) {
  88. self.historyUnlocked = false;
  89. self.allowHashChange = false;
  90. window.addEventListener('popstate', rewriteHistoryAfterBack);
  91. history.back();
  92. function rewriteHistoryAfterBack() {
  93. window.removeEventListener('popstate', rewriteHistoryAfterBack);
  94. window.addEventListener('popstate', rewriteHistoryAfterForward);
  95. self._pushToHistory(params, false, true);
  96. history.forward();
  97. }
  98. function rewriteHistoryAfterForward() {
  99. window.removeEventListener('popstate', rewriteHistoryAfterForward);
  100. self.allowHashChange = true;
  101. self.historyUnlocked = true;
  102. callback();
  103. }
  104. }
  105. function pdfHistoryBeforeUnload() {
  106. var previousParams = self._getPreviousParams(null, true);
  107. if (previousParams) {
  108. var replacePrevious = !self.current.dest && self.current.hash !== self.previousHash;
  109. self._pushToHistory(previousParams, false, replacePrevious);
  110. self._updatePreviousBookmark();
  111. }
  112. window.removeEventListener('beforeunload', pdfHistoryBeforeUnload);
  113. }
  114. window.addEventListener('beforeunload', pdfHistoryBeforeUnload);
  115. window.addEventListener('pageshow', function pdfHistoryPageShow(evt) {
  116. window.addEventListener('beforeunload', pdfHistoryBeforeUnload);
  117. });
  118. self.eventBus.on('presentationmodechanged', function (e) {
  119. self.isViewerInPresentationMode = e.active;
  120. });
  121. },
  122. clearHistoryState: function pdfHistory_clearHistoryState() {
  123. this._pushOrReplaceState(null, true);
  124. },
  125. _isStateObjectDefined: function pdfHistory_isStateObjectDefined(state) {
  126. return state && state.uid >= 0 && state.fingerprint && this.fingerprint === state.fingerprint && state.target && state.target.hash ? true : false;
  127. },
  128. _pushOrReplaceState: function pdfHistory_pushOrReplaceState(stateObj, replace) {
  129. if (replace) {
  130. window.history.replaceState(stateObj, '', document.URL);
  131. } else {
  132. window.history.pushState(stateObj, '', document.URL);
  133. }
  134. },
  135. get isHashChangeUnlocked() {
  136. if (!this.initialized) {
  137. return true;
  138. }
  139. return this.allowHashChange;
  140. },
  141. _updatePreviousBookmark: function pdfHistory_updatePreviousBookmark() {
  142. if (this.updatePreviousBookmark && this.currentBookmark && this.currentPage) {
  143. this.previousBookmark = this.currentBookmark;
  144. this.previousPage = this.currentPage;
  145. this.updatePreviousBookmark = false;
  146. }
  147. },
  148. updateCurrentBookmark: function pdfHistoryUpdateCurrentBookmark(bookmark, pageNum) {
  149. if (this.initialized) {
  150. this.currentBookmark = bookmark.substring(1);
  151. this.currentPage = pageNum | 0;
  152. this._updatePreviousBookmark();
  153. }
  154. },
  155. updateNextHashParam: function pdfHistoryUpdateNextHashParam(param) {
  156. if (this.initialized) {
  157. this.nextHashParam = param;
  158. }
  159. },
  160. push: function pdfHistoryPush(params, isInitialBookmark) {
  161. if (!(this.initialized && this.historyUnlocked)) {
  162. return;
  163. }
  164. if (params.dest && !params.hash) {
  165. params.hash = this.current.hash && this.current.dest && this.current.dest === params.dest ? this.current.hash : this.linkService.getDestinationHash(params.dest).split('#')[1];
  166. }
  167. if (params.page) {
  168. params.page |= 0;
  169. }
  170. if (isInitialBookmark) {
  171. var target = window.history.state.target;
  172. if (!target) {
  173. this._pushToHistory(params, false);
  174. this.previousHash = window.location.hash.substring(1);
  175. }
  176. this.updatePreviousBookmark = this.nextHashParam ? false : true;
  177. if (target) {
  178. this._updatePreviousBookmark();
  179. }
  180. return;
  181. }
  182. if (this.nextHashParam) {
  183. if (this.nextHashParam === params.hash) {
  184. this.nextHashParam = null;
  185. this.updatePreviousBookmark = true;
  186. return;
  187. }
  188. this.nextHashParam = null;
  189. }
  190. if (params.hash) {
  191. if (this.current.hash) {
  192. if (this.current.hash !== params.hash) {
  193. this._pushToHistory(params, true);
  194. } else {
  195. if (!this.current.page && params.page) {
  196. this._pushToHistory(params, false, true);
  197. }
  198. this.updatePreviousBookmark = true;
  199. }
  200. } else {
  201. this._pushToHistory(params, true);
  202. }
  203. } else if (this.current.page && params.page && this.current.page !== params.page) {
  204. this._pushToHistory(params, true);
  205. }
  206. },
  207. _getPreviousParams: function pdfHistory_getPreviousParams(onlyCheckPage, beforeUnload) {
  208. if (!(this.currentBookmark && this.currentPage)) {
  209. return null;
  210. } else if (this.updatePreviousBookmark) {
  211. this.updatePreviousBookmark = false;
  212. }
  213. if (this.uid > 0 && !(this.previousBookmark && this.previousPage)) {
  214. return null;
  215. }
  216. if (!this.current.dest && !onlyCheckPage || beforeUnload) {
  217. if (this.previousBookmark === this.currentBookmark) {
  218. return null;
  219. }
  220. } else if (this.current.page || onlyCheckPage) {
  221. if (this.previousPage === this.currentPage) {
  222. return null;
  223. }
  224. } else {
  225. return null;
  226. }
  227. var params = {
  228. hash: this.currentBookmark,
  229. page: this.currentPage
  230. };
  231. if (this.isViewerInPresentationMode) {
  232. params.hash = null;
  233. }
  234. return params;
  235. },
  236. _stateObj: function pdfHistory_stateObj(params) {
  237. return {
  238. fingerprint: this.fingerprint,
  239. uid: this.uid,
  240. target: params
  241. };
  242. },
  243. _pushToHistory: function pdfHistory_pushToHistory(params, addPrevious, overwrite) {
  244. if (!this.initialized) {
  245. return;
  246. }
  247. if (!params.hash && params.page) {
  248. params.hash = 'page=' + params.page;
  249. }
  250. if (addPrevious && !overwrite) {
  251. var previousParams = this._getPreviousParams();
  252. if (previousParams) {
  253. var replacePrevious = !this.current.dest && this.current.hash !== this.previousHash;
  254. this._pushToHistory(previousParams, false, replacePrevious);
  255. }
  256. }
  257. this._pushOrReplaceState(this._stateObj(params), overwrite || this.uid === 0);
  258. this.currentUid = this.uid++;
  259. this.current = params;
  260. this.updatePreviousBookmark = true;
  261. },
  262. _goTo: function pdfHistory_goTo(state) {
  263. if (!(this.initialized && this.historyUnlocked && this._isStateObjectDefined(state))) {
  264. return;
  265. }
  266. if (!this.reInitialized && state.uid < this.currentUid) {
  267. var previousParams = this._getPreviousParams(true);
  268. if (previousParams) {
  269. this._pushToHistory(this.current, false);
  270. this._pushToHistory(previousParams, false);
  271. this.currentUid = state.uid;
  272. window.history.back();
  273. return;
  274. }
  275. }
  276. this.historyUnlocked = false;
  277. if (state.target.dest) {
  278. this.linkService.navigateTo(state.target.dest);
  279. } else {
  280. this.linkService.setHash(state.target.hash);
  281. }
  282. this.currentUid = state.uid;
  283. if (state.uid > this.uid) {
  284. this.uid = state.uid;
  285. }
  286. this.current = state.target;
  287. this.updatePreviousBookmark = true;
  288. var currentHash = window.location.hash.substring(1);
  289. if (this.previousHash !== currentHash) {
  290. this.allowHashChange = false;
  291. }
  292. this.previousHash = currentHash;
  293. this.historyUnlocked = true;
  294. },
  295. back: function pdfHistoryBack() {
  296. this.go(-1);
  297. },
  298. forward: function pdfHistoryForward() {
  299. this.go(1);
  300. },
  301. go: function pdfHistoryGo(direction) {
  302. if (this.initialized && this.historyUnlocked) {
  303. var state = window.history.state;
  304. if (direction === -1 && state && state.uid > 0) {
  305. window.history.back();
  306. } else if (direction === 1 && state && state.uid < this.uid - 1) {
  307. window.history.forward();
  308. }
  309. }
  310. }
  311. };
  312. exports.PDFHistory = PDFHistory;