2
0

pdf_link_service.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 = [
  148. null,
  149. { name: 'XYZ' },
  150. zoomArgs.length > 1 ? zoomArgs[1] | 0 : null,
  151. zoomArgs.length > 2 ? zoomArgs[2] | 0 : null,
  152. zoomArgNumber ? zoomArgNumber / 100 : zoomArg
  153. ];
  154. } else {
  155. if (zoomArg === 'Fit' || zoomArg === 'FitB') {
  156. dest = [
  157. null,
  158. { name: zoomArg }
  159. ];
  160. } else if (zoomArg === 'FitH' || zoomArg === 'FitBH' || (zoomArg === 'FitV' || zoomArg === 'FitBV')) {
  161. dest = [
  162. null,
  163. { name: zoomArg },
  164. zoomArgs.length > 1 ? zoomArgs[1] | 0 : null
  165. ];
  166. } else if (zoomArg === 'FitR') {
  167. if (zoomArgs.length !== 5) {
  168. console.error('PDFLinkService_setHash: ' + 'Not enough parameters for \'FitR\'.');
  169. } else {
  170. dest = [
  171. null,
  172. { name: zoomArg },
  173. zoomArgs[1] | 0,
  174. zoomArgs[2] | 0,
  175. zoomArgs[3] | 0,
  176. zoomArgs[4] | 0
  177. ];
  178. }
  179. } else {
  180. console.error('PDFLinkService_setHash: \'' + zoomArg + '\' is not a valid zoom value.');
  181. }
  182. }
  183. }
  184. if (dest) {
  185. this.pdfViewer.scrollPageIntoView({
  186. pageNumber: pageNumber || this.page,
  187. destArray: dest,
  188. allowNegativeOffset: true
  189. });
  190. } else if (pageNumber) {
  191. this.page = pageNumber;
  192. }
  193. if ('pagemode' in params) {
  194. this.eventBus.dispatch('pagemode', {
  195. source: this,
  196. mode: params.pagemode
  197. });
  198. }
  199. } else {
  200. if (isPageNumber(hash) && hash <= this.pagesCount) {
  201. console.warn('PDFLinkService_setHash: specifying a page number ' + 'directly after the hash symbol (#) is deprecated, ' + 'please use the "#page=' + hash + '" form instead.');
  202. this.page = hash | 0;
  203. }
  204. dest = unescape(hash);
  205. try {
  206. dest = JSON.parse(dest);
  207. if (!(dest instanceof Array)) {
  208. dest = dest.toString();
  209. }
  210. } catch (ex) {
  211. }
  212. if (typeof dest === 'string' || isValidExplicitDestination(dest)) {
  213. if (this.pdfHistory) {
  214. this.pdfHistory.updateNextHashParam(dest);
  215. }
  216. this.navigateTo(dest);
  217. return;
  218. }
  219. console.error('PDFLinkService_setHash: \'' + unescape(hash) + '\' is not a valid destination.');
  220. }
  221. },
  222. executeNamedAction: function PDFLinkService_executeNamedAction(action) {
  223. switch (action) {
  224. case 'GoBack':
  225. if (this.pdfHistory) {
  226. this.pdfHistory.back();
  227. }
  228. break;
  229. case 'GoForward':
  230. if (this.pdfHistory) {
  231. this.pdfHistory.forward();
  232. }
  233. break;
  234. case 'NextPage':
  235. if (this.page < this.pagesCount) {
  236. this.page++;
  237. }
  238. break;
  239. case 'PrevPage':
  240. if (this.page > 1) {
  241. this.page--;
  242. }
  243. break;
  244. case 'LastPage':
  245. this.page = this.pagesCount;
  246. break;
  247. case 'FirstPage':
  248. this.page = 1;
  249. break;
  250. default:
  251. break;
  252. }
  253. this.eventBus.dispatch('namedaction', {
  254. source: this,
  255. action: action
  256. });
  257. },
  258. onFileAttachmentAnnotation: function (params) {
  259. this.eventBus.dispatch('fileattachmentannotation', {
  260. source: this,
  261. id: params.id,
  262. filename: params.filename,
  263. content: params.content
  264. });
  265. },
  266. cachePageRef: function PDFLinkService_cachePageRef(pageNum, pageRef) {
  267. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  268. this._pagesRefCache[refStr] = pageNum;
  269. },
  270. _cachedPageNumber: function PDFLinkService_cachedPageNumber(pageRef) {
  271. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  272. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  273. }
  274. };
  275. function isValidExplicitDestination(dest) {
  276. if (!(dest instanceof Array)) {
  277. return false;
  278. }
  279. var destLength = dest.length, allowNull = true;
  280. if (destLength < 2) {
  281. return false;
  282. }
  283. var page = dest[0];
  284. 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)) {
  285. return false;
  286. }
  287. var zoom = dest[1];
  288. if (!(typeof zoom === 'object' && typeof zoom.name === 'string')) {
  289. return false;
  290. }
  291. switch (zoom.name) {
  292. case 'XYZ':
  293. if (destLength !== 5) {
  294. return false;
  295. }
  296. break;
  297. case 'Fit':
  298. case 'FitB':
  299. return destLength === 2;
  300. case 'FitH':
  301. case 'FitBH':
  302. case 'FitV':
  303. case 'FitBV':
  304. if (destLength !== 3) {
  305. return false;
  306. }
  307. break;
  308. case 'FitR':
  309. if (destLength !== 6) {
  310. return false;
  311. }
  312. allowNull = false;
  313. break;
  314. default:
  315. return false;
  316. }
  317. for (var i = 2; i < destLength; i++) {
  318. var param = dest[i];
  319. if (!(typeof param === 'number' || allowNull && param === null)) {
  320. return false;
  321. }
  322. }
  323. return true;
  324. }
  325. return PDFLinkService;
  326. }();
  327. var SimpleLinkService = function SimpleLinkServiceClosure() {
  328. function SimpleLinkService() {
  329. }
  330. SimpleLinkService.prototype = {
  331. get page() {
  332. return 0;
  333. },
  334. set page(value) {
  335. },
  336. navigateTo: function (dest) {
  337. },
  338. getDestinationHash: function (dest) {
  339. return '#';
  340. },
  341. getAnchorUrl: function (hash) {
  342. return '#';
  343. },
  344. setHash: function (hash) {
  345. },
  346. executeNamedAction: function (action) {
  347. },
  348. onFileAttachmentAnnotation: function (params) {
  349. },
  350. cachePageRef: function (pageNum, pageRef) {
  351. }
  352. };
  353. return SimpleLinkService;
  354. }();
  355. exports.PDFLinkService = PDFLinkService;
  356. exports.SimpleLinkService = SimpleLinkService;