pdf_link_service.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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.SimpleLinkService = exports.PDFLinkService = 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 _dom_events = require('./dom_events');
  22. var _ui_utils = require('./ui_utils');
  23. var PDFLinkService = function PDFLinkServiceClosure() {
  24. function PDFLinkService(options) {
  25. options = options || {};
  26. this.eventBus = options.eventBus || (0, _dom_events.getGlobalEventBus)();
  27. this.baseUrl = null;
  28. this.pdfDocument = null;
  29. this.pdfViewer = null;
  30. this.pdfHistory = null;
  31. this._pagesRefCache = null;
  32. }
  33. PDFLinkService.prototype = {
  34. setDocument: function PDFLinkService_setDocument(pdfDocument, baseUrl) {
  35. this.baseUrl = baseUrl;
  36. this.pdfDocument = pdfDocument;
  37. this._pagesRefCache = Object.create(null);
  38. },
  39. setViewer: function PDFLinkService_setViewer(pdfViewer) {
  40. this.pdfViewer = pdfViewer;
  41. },
  42. setHistory: function PDFLinkService_setHistory(pdfHistory) {
  43. this.pdfHistory = pdfHistory;
  44. },
  45. get pagesCount() {
  46. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  47. },
  48. get page() {
  49. return this.pdfViewer.currentPageNumber;
  50. },
  51. set page(value) {
  52. this.pdfViewer.currentPageNumber = value;
  53. },
  54. navigateTo: function PDFLinkService_navigateTo(dest) {
  55. var destString = '';
  56. var self = this;
  57. var goToDestination = function goToDestination(destRef) {
  58. var pageNumber;
  59. if (destRef instanceof Object) {
  60. pageNumber = self._cachedPageNumber(destRef);
  61. } else if ((destRef | 0) === destRef) {
  62. pageNumber = destRef + 1;
  63. } else {
  64. console.error('PDFLinkService_navigateTo: "' + destRef + '" is not a valid destination reference.');
  65. return;
  66. }
  67. if (pageNumber) {
  68. if (pageNumber < 1 || pageNumber > self.pagesCount) {
  69. console.error('PDFLinkService_navigateTo: "' + pageNumber + '" is a non-existent page number.');
  70. return;
  71. }
  72. self.pdfViewer.scrollPageIntoView({
  73. pageNumber: pageNumber,
  74. destArray: dest
  75. });
  76. if (self.pdfHistory) {
  77. self.pdfHistory.push({
  78. dest: dest,
  79. hash: destString,
  80. page: pageNumber
  81. });
  82. }
  83. } else {
  84. self.pdfDocument.getPageIndex(destRef).then(function (pageIndex) {
  85. self.cachePageRef(pageIndex + 1, destRef);
  86. goToDestination(destRef);
  87. }).catch(function () {
  88. console.error('PDFLinkService_navigateTo: "' + destRef + '" is not a valid page reference.');
  89. });
  90. }
  91. };
  92. var destinationPromise;
  93. if (typeof dest === 'string') {
  94. destString = dest;
  95. destinationPromise = this.pdfDocument.getDestination(dest);
  96. } else {
  97. destinationPromise = Promise.resolve(dest);
  98. }
  99. destinationPromise.then(function (destination) {
  100. dest = destination;
  101. if (!(destination instanceof Array)) {
  102. console.error('PDFLinkService_navigateTo: "' + destination + '" is not a valid destination array.');
  103. return;
  104. }
  105. goToDestination(destination[0]);
  106. });
  107. },
  108. getDestinationHash: function getDestinationHash(dest) {
  109. if (typeof dest === 'string') {
  110. return this.getAnchorUrl('#' + escape(dest));
  111. }
  112. if (dest instanceof Array) {
  113. var str = JSON.stringify(dest);
  114. return this.getAnchorUrl('#' + escape(str));
  115. }
  116. return this.getAnchorUrl('');
  117. },
  118. getAnchorUrl: function PDFLinkService_getAnchorUrl(anchor) {
  119. return (this.baseUrl || '') + anchor;
  120. },
  121. setHash: function PDFLinkService_setHash(hash) {
  122. var pageNumber, dest;
  123. if (hash.indexOf('=') >= 0) {
  124. var params = (0, _ui_utils.parseQueryString)(hash);
  125. if ('search' in params) {
  126. this.eventBus.dispatch('findfromurlhash', {
  127. source: this,
  128. query: params['search'].replace(/"/g, ''),
  129. phraseSearch: params['phrase'] === 'true'
  130. });
  131. }
  132. if ('nameddest' in params) {
  133. if (this.pdfHistory) {
  134. this.pdfHistory.updateNextHashParam(params.nameddest);
  135. }
  136. this.navigateTo(params.nameddest);
  137. return;
  138. }
  139. if ('page' in params) {
  140. pageNumber = params.page | 0 || 1;
  141. }
  142. if ('zoom' in params) {
  143. var zoomArgs = params.zoom.split(',');
  144. var zoomArg = zoomArgs[0];
  145. var zoomArgNumber = parseFloat(zoomArg);
  146. if (zoomArg.indexOf('Fit') === -1) {
  147. dest = [null, { name: 'XYZ' }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null, zoomArgs.length > 2 ? zoomArgs[2] | 0 : null, zoomArgNumber ? zoomArgNumber / 100 : zoomArg];
  148. } else {
  149. if (zoomArg === 'Fit' || zoomArg === 'FitB') {
  150. dest = [null, { name: zoomArg }];
  151. } else if (zoomArg === 'FitH' || zoomArg === 'FitBH' || zoomArg === 'FitV' || zoomArg === 'FitBV') {
  152. dest = [null, { name: zoomArg }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null];
  153. } else if (zoomArg === 'FitR') {
  154. if (zoomArgs.length !== 5) {
  155. console.error('PDFLinkService_setHash: ' + 'Not enough parameters for \'FitR\'.');
  156. } else {
  157. dest = [null, { name: zoomArg }, zoomArgs[1] | 0, zoomArgs[2] | 0, zoomArgs[3] | 0, zoomArgs[4] | 0];
  158. }
  159. } else {
  160. console.error('PDFLinkService_setHash: \'' + zoomArg + '\' is not a valid zoom value.');
  161. }
  162. }
  163. }
  164. if (dest) {
  165. this.pdfViewer.scrollPageIntoView({
  166. pageNumber: pageNumber || this.page,
  167. destArray: dest,
  168. allowNegativeOffset: true
  169. });
  170. } else if (pageNumber) {
  171. this.page = pageNumber;
  172. }
  173. if ('pagemode' in params) {
  174. this.eventBus.dispatch('pagemode', {
  175. source: this,
  176. mode: params.pagemode
  177. });
  178. }
  179. } else {
  180. if (/^\d+$/.test(hash) && hash <= this.pagesCount) {
  181. console.warn('PDFLinkService_setHash: specifying a page number ' + 'directly after the hash symbol (#) is deprecated, ' + 'please use the "#page=' + hash + '" form instead.');
  182. this.page = hash | 0;
  183. }
  184. dest = unescape(hash);
  185. try {
  186. dest = JSON.parse(dest);
  187. if (!(dest instanceof Array)) {
  188. dest = dest.toString();
  189. }
  190. } catch (ex) {}
  191. if (typeof dest === 'string' || isValidExplicitDestination(dest)) {
  192. if (this.pdfHistory) {
  193. this.pdfHistory.updateNextHashParam(dest);
  194. }
  195. this.navigateTo(dest);
  196. return;
  197. }
  198. console.error('PDFLinkService_setHash: \'' + unescape(hash) + '\' is not a valid destination.');
  199. }
  200. },
  201. executeNamedAction: function PDFLinkService_executeNamedAction(action) {
  202. switch (action) {
  203. case 'GoBack':
  204. if (this.pdfHistory) {
  205. this.pdfHistory.back();
  206. }
  207. break;
  208. case 'GoForward':
  209. if (this.pdfHistory) {
  210. this.pdfHistory.forward();
  211. }
  212. break;
  213. case 'NextPage':
  214. if (this.page < this.pagesCount) {
  215. this.page++;
  216. }
  217. break;
  218. case 'PrevPage':
  219. if (this.page > 1) {
  220. this.page--;
  221. }
  222. break;
  223. case 'LastPage':
  224. this.page = this.pagesCount;
  225. break;
  226. case 'FirstPage':
  227. this.page = 1;
  228. break;
  229. default:
  230. break;
  231. }
  232. this.eventBus.dispatch('namedaction', {
  233. source: this,
  234. action: action
  235. });
  236. },
  237. onFileAttachmentAnnotation: function onFileAttachmentAnnotation() {
  238. var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  239. this.eventBus.dispatch('fileattachmentannotation', {
  240. source: this,
  241. id: params.id,
  242. filename: params.filename,
  243. content: params.content
  244. });
  245. },
  246. cachePageRef: function PDFLinkService_cachePageRef(pageNum, pageRef) {
  247. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  248. this._pagesRefCache[refStr] = pageNum;
  249. },
  250. _cachedPageNumber: function PDFLinkService_cachedPageNumber(pageRef) {
  251. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  252. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  253. }
  254. };
  255. function isValidExplicitDestination(dest) {
  256. if (!(dest instanceof Array)) {
  257. return false;
  258. }
  259. var destLength = dest.length,
  260. allowNull = true;
  261. if (destLength < 2) {
  262. return false;
  263. }
  264. var page = dest[0];
  265. if (!((typeof page === 'undefined' ? 'undefined' : _typeof(page)) === 'object' && typeof page.num === 'number' && (page.num | 0) === page.num && typeof page.gen === 'number' && (page.gen | 0) === page.gen) && !(typeof page === 'number' && (page | 0) === page && page >= 0)) {
  266. return false;
  267. }
  268. var zoom = dest[1];
  269. if (!((typeof zoom === 'undefined' ? 'undefined' : _typeof(zoom)) === 'object' && typeof zoom.name === 'string')) {
  270. return false;
  271. }
  272. switch (zoom.name) {
  273. case 'XYZ':
  274. if (destLength !== 5) {
  275. return false;
  276. }
  277. break;
  278. case 'Fit':
  279. case 'FitB':
  280. return destLength === 2;
  281. case 'FitH':
  282. case 'FitBH':
  283. case 'FitV':
  284. case 'FitBV':
  285. if (destLength !== 3) {
  286. return false;
  287. }
  288. break;
  289. case 'FitR':
  290. if (destLength !== 6) {
  291. return false;
  292. }
  293. allowNull = false;
  294. break;
  295. default:
  296. return false;
  297. }
  298. for (var i = 2; i < destLength; i++) {
  299. var param = dest[i];
  300. if (!(typeof param === 'number' || allowNull && param === null)) {
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. return PDFLinkService;
  307. }();
  308. var SimpleLinkService = function SimpleLinkServiceClosure() {
  309. function SimpleLinkService() {}
  310. SimpleLinkService.prototype = {
  311. get page() {
  312. return 0;
  313. },
  314. set page(value) {},
  315. navigateTo: function navigateTo(dest) {},
  316. getDestinationHash: function getDestinationHash(dest) {
  317. return '#';
  318. },
  319. getAnchorUrl: function getAnchorUrl(hash) {
  320. return '#';
  321. },
  322. setHash: function setHash(hash) {},
  323. executeNamedAction: function executeNamedAction(action) {},
  324. onFileAttachmentAnnotation: function onFileAttachmentAnnotation(params) {},
  325. cachePageRef: function cachePageRef(pageNum, pageRef) {}
  326. };
  327. return SimpleLinkService;
  328. }();
  329. exports.PDFLinkService = PDFLinkService;
  330. exports.SimpleLinkService = SimpleLinkService;