pdf_link_service.js 11 KB

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