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