pdf_link_service.js 12 KB

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