pdf_link_service.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. var uiUtils = require('./ui_utils.js');
  17. var domEvents = require('./dom_events.js');
  18. var parseQueryString = uiUtils.parseQueryString;
  19. var PageNumberRegExp = /^\d+$/;
  20. function isPageNumber(str) {
  21. return PageNumberRegExp.test(str);
  22. }
  23. var PDFLinkService = function PDFLinkServiceClosure() {
  24. function PDFLinkService(options) {
  25. options = options || {};
  26. this.eventBus = options.eventBus || domEvents.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 (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 PDFLinkService_getDestinationHash(dest) {
  109. if (typeof dest === 'string') {
  110. return this.getAnchorUrl('#' + (isPageNumber(dest) ? 'nameddest=' : '') + 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 = 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 (isPageNumber(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 (params) {
  238. this.eventBus.dispatch('fileattachmentannotation', {
  239. source: this,
  240. id: params.id,
  241. filename: params.filename,
  242. content: params.content
  243. });
  244. },
  245. cachePageRef: function PDFLinkService_cachePageRef(pageNum, pageRef) {
  246. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  247. this._pagesRefCache[refStr] = pageNum;
  248. },
  249. _cachedPageNumber: function PDFLinkService_cachedPageNumber(pageRef) {
  250. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  251. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  252. }
  253. };
  254. function isValidExplicitDestination(dest) {
  255. if (!(dest instanceof Array)) {
  256. return false;
  257. }
  258. var destLength = dest.length,
  259. allowNull = true;
  260. if (destLength < 2) {
  261. return false;
  262. }
  263. var page = dest[0];
  264. 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)) {
  265. return false;
  266. }
  267. var zoom = dest[1];
  268. if (!(typeof zoom === 'object' && typeof zoom.name === 'string')) {
  269. return false;
  270. }
  271. switch (zoom.name) {
  272. case 'XYZ':
  273. if (destLength !== 5) {
  274. return false;
  275. }
  276. break;
  277. case 'Fit':
  278. case 'FitB':
  279. return destLength === 2;
  280. case 'FitH':
  281. case 'FitBH':
  282. case 'FitV':
  283. case 'FitBV':
  284. if (destLength !== 3) {
  285. return false;
  286. }
  287. break;
  288. case 'FitR':
  289. if (destLength !== 6) {
  290. return false;
  291. }
  292. allowNull = false;
  293. break;
  294. default:
  295. return false;
  296. }
  297. for (var i = 2; i < destLength; i++) {
  298. var param = dest[i];
  299. if (!(typeof param === 'number' || allowNull && param === null)) {
  300. return false;
  301. }
  302. }
  303. return true;
  304. }
  305. return PDFLinkService;
  306. }();
  307. var SimpleLinkService = function SimpleLinkServiceClosure() {
  308. function SimpleLinkService() {}
  309. SimpleLinkService.prototype = {
  310. get page() {
  311. return 0;
  312. },
  313. set page(value) {},
  314. navigateTo: function (dest) {},
  315. getDestinationHash: function (dest) {
  316. return '#';
  317. },
  318. getAnchorUrl: function (hash) {
  319. return '#';
  320. },
  321. setHash: function (hash) {},
  322. executeNamedAction: function (action) {},
  323. onFileAttachmentAnnotation: function (params) {},
  324. cachePageRef: function (pageNum, pageRef) {}
  325. };
  326. return SimpleLinkService;
  327. }();
  328. exports.PDFLinkService = PDFLinkService;
  329. exports.SimpleLinkService = SimpleLinkService;