pdf_link_service.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.SimpleLinkService = exports.PDFLinkService = undefined;
  27. 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; };
  28. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  29. var _dom_events = require('./dom_events');
  30. var _ui_utils = require('./ui_utils');
  31. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  32. var PDFLinkService = function () {
  33. function PDFLinkService() {
  34. var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
  35. eventBus = _ref.eventBus,
  36. _ref$externalLinkTarg = _ref.externalLinkTarget,
  37. externalLinkTarget = _ref$externalLinkTarg === undefined ? null : _ref$externalLinkTarg,
  38. _ref$externalLinkRel = _ref.externalLinkRel,
  39. externalLinkRel = _ref$externalLinkRel === undefined ? null : _ref$externalLinkRel;
  40. _classCallCheck(this, PDFLinkService);
  41. this.eventBus = eventBus || (0, _dom_events.getGlobalEventBus)();
  42. this.externalLinkTarget = externalLinkTarget;
  43. this.externalLinkRel = externalLinkRel;
  44. this.baseUrl = null;
  45. this.pdfDocument = null;
  46. this.pdfViewer = null;
  47. this.pdfHistory = null;
  48. this._pagesRefCache = null;
  49. }
  50. _createClass(PDFLinkService, [{
  51. key: 'setDocument',
  52. value: function setDocument(pdfDocument, baseUrl) {
  53. this.baseUrl = baseUrl;
  54. this.pdfDocument = pdfDocument;
  55. this._pagesRefCache = Object.create(null);
  56. }
  57. }, {
  58. key: 'setViewer',
  59. value: function setViewer(pdfViewer) {
  60. this.pdfViewer = pdfViewer;
  61. }
  62. }, {
  63. key: 'setHistory',
  64. value: function setHistory(pdfHistory) {
  65. this.pdfHistory = pdfHistory;
  66. }
  67. }, {
  68. key: 'navigateTo',
  69. value: function navigateTo(dest) {
  70. var _this = this;
  71. var goToDestination = function goToDestination(_ref2) {
  72. var namedDest = _ref2.namedDest,
  73. explicitDest = _ref2.explicitDest;
  74. var destRef = explicitDest[0],
  75. pageNumber = void 0;
  76. if (destRef instanceof Object) {
  77. pageNumber = _this._cachedPageNumber(destRef);
  78. if (pageNumber === null) {
  79. _this.pdfDocument.getPageIndex(destRef).then(function (pageIndex) {
  80. _this.cachePageRef(pageIndex + 1, destRef);
  81. goToDestination({
  82. namedDest: namedDest,
  83. explicitDest: explicitDest
  84. });
  85. }).catch(function () {
  86. console.error('PDFLinkService.navigateTo: "' + destRef + '" is not ' + ('a valid page reference, for dest="' + dest + '".'));
  87. });
  88. return;
  89. }
  90. } else if (Number.isInteger(destRef)) {
  91. pageNumber = destRef + 1;
  92. } else {
  93. console.error('PDFLinkService.navigateTo: "' + destRef + '" is not ' + ('a valid destination reference, for dest="' + dest + '".'));
  94. return;
  95. }
  96. if (!pageNumber || pageNumber < 1 || pageNumber > _this.pagesCount) {
  97. console.error('PDFLinkService.navigateTo: "' + pageNumber + '" is not ' + ('a valid page number, for dest="' + dest + '".'));
  98. return;
  99. }
  100. if (_this.pdfHistory) {
  101. _this.pdfHistory.pushCurrentPosition();
  102. _this.pdfHistory.push({
  103. namedDest: namedDest,
  104. explicitDest: explicitDest,
  105. pageNumber: pageNumber
  106. });
  107. }
  108. _this.pdfViewer.scrollPageIntoView({
  109. pageNumber: pageNumber,
  110. destArray: explicitDest
  111. });
  112. };
  113. new Promise(function (resolve, reject) {
  114. if (typeof dest === 'string') {
  115. _this.pdfDocument.getDestination(dest).then(function (destArray) {
  116. resolve({
  117. namedDest: dest,
  118. explicitDest: destArray
  119. });
  120. });
  121. return;
  122. }
  123. resolve({
  124. namedDest: '',
  125. explicitDest: dest
  126. });
  127. }).then(function (data) {
  128. if (!(data.explicitDest instanceof Array)) {
  129. console.error('PDFLinkService.navigateTo: "' + data.explicitDest + '" is' + (' not a valid destination array, for dest="' + dest + '".'));
  130. return;
  131. }
  132. goToDestination(data);
  133. });
  134. }
  135. }, {
  136. key: 'getDestinationHash',
  137. value: function getDestinationHash(dest) {
  138. if (typeof dest === 'string') {
  139. return this.getAnchorUrl('#' + escape(dest));
  140. }
  141. if (dest instanceof Array) {
  142. var str = JSON.stringify(dest);
  143. return this.getAnchorUrl('#' + escape(str));
  144. }
  145. return this.getAnchorUrl('');
  146. }
  147. }, {
  148. key: 'getAnchorUrl',
  149. value: function getAnchorUrl(anchor) {
  150. return (this.baseUrl || '') + anchor;
  151. }
  152. }, {
  153. key: 'setHash',
  154. value: function setHash(hash) {
  155. var pageNumber = void 0,
  156. dest = void 0;
  157. if (hash.includes('=')) {
  158. var params = (0, _ui_utils.parseQueryString)(hash);
  159. if ('search' in params) {
  160. this.eventBus.dispatch('findfromurlhash', {
  161. source: this,
  162. query: params['search'].replace(/"/g, ''),
  163. phraseSearch: params['phrase'] === 'true'
  164. });
  165. }
  166. if ('nameddest' in params) {
  167. this.navigateTo(params.nameddest);
  168. return;
  169. }
  170. if ('page' in params) {
  171. pageNumber = params.page | 0 || 1;
  172. }
  173. if ('zoom' in params) {
  174. var zoomArgs = params.zoom.split(',');
  175. var zoomArg = zoomArgs[0];
  176. var zoomArgNumber = parseFloat(zoomArg);
  177. if (!zoomArg.includes('Fit')) {
  178. dest = [null, { name: 'XYZ' }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null, zoomArgs.length > 2 ? zoomArgs[2] | 0 : null, zoomArgNumber ? zoomArgNumber / 100 : zoomArg];
  179. } else {
  180. if (zoomArg === 'Fit' || zoomArg === 'FitB') {
  181. dest = [null, { name: zoomArg }];
  182. } else if (zoomArg === 'FitH' || zoomArg === 'FitBH' || zoomArg === 'FitV' || zoomArg === 'FitBV') {
  183. dest = [null, { name: zoomArg }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null];
  184. } else if (zoomArg === 'FitR') {
  185. if (zoomArgs.length !== 5) {
  186. console.error('PDFLinkService.setHash: Not enough parameters for "FitR".');
  187. } else {
  188. dest = [null, { name: zoomArg }, zoomArgs[1] | 0, zoomArgs[2] | 0, zoomArgs[3] | 0, zoomArgs[4] | 0];
  189. }
  190. } else {
  191. console.error('PDFLinkService.setHash: "' + zoomArg + '" is not ' + 'a valid zoom value.');
  192. }
  193. }
  194. }
  195. if (dest) {
  196. this.pdfViewer.scrollPageIntoView({
  197. pageNumber: pageNumber || this.page,
  198. destArray: dest,
  199. allowNegativeOffset: true
  200. });
  201. } else if (pageNumber) {
  202. this.page = pageNumber;
  203. }
  204. if ('pagemode' in params) {
  205. this.eventBus.dispatch('pagemode', {
  206. source: this,
  207. mode: params.pagemode
  208. });
  209. }
  210. } else {
  211. dest = unescape(hash);
  212. try {
  213. dest = JSON.parse(dest);
  214. if (!(dest instanceof Array)) {
  215. dest = dest.toString();
  216. }
  217. } catch (ex) {}
  218. if (typeof dest === 'string' || isValidExplicitDestination(dest)) {
  219. this.navigateTo(dest);
  220. return;
  221. }
  222. console.error('PDFLinkService.setHash: "' + unescape(hash) + '" is not ' + 'a valid destination.');
  223. }
  224. }
  225. }, {
  226. key: 'executeNamedAction',
  227. value: function executeNamedAction(action) {
  228. switch (action) {
  229. case 'GoBack':
  230. if (this.pdfHistory) {
  231. this.pdfHistory.back();
  232. }
  233. break;
  234. case 'GoForward':
  235. if (this.pdfHistory) {
  236. this.pdfHistory.forward();
  237. }
  238. break;
  239. case 'NextPage':
  240. if (this.page < this.pagesCount) {
  241. this.page++;
  242. }
  243. break;
  244. case 'PrevPage':
  245. if (this.page > 1) {
  246. this.page--;
  247. }
  248. break;
  249. case 'LastPage':
  250. this.page = this.pagesCount;
  251. break;
  252. case 'FirstPage':
  253. this.page = 1;
  254. break;
  255. default:
  256. break;
  257. }
  258. this.eventBus.dispatch('namedaction', {
  259. source: this,
  260. action: action
  261. });
  262. }
  263. }, {
  264. key: 'onFileAttachmentAnnotation',
  265. value: function onFileAttachmentAnnotation(_ref3) {
  266. var id = _ref3.id,
  267. filename = _ref3.filename,
  268. content = _ref3.content;
  269. this.eventBus.dispatch('fileattachmentannotation', {
  270. source: this,
  271. id: id,
  272. filename: filename,
  273. content: content
  274. });
  275. }
  276. }, {
  277. key: 'cachePageRef',
  278. value: function cachePageRef(pageNum, pageRef) {
  279. if (!pageRef) {
  280. return;
  281. }
  282. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  283. this._pagesRefCache[refStr] = pageNum;
  284. }
  285. }, {
  286. key: '_cachedPageNumber',
  287. value: function _cachedPageNumber(pageRef) {
  288. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  289. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  290. }
  291. }, {
  292. key: 'pagesCount',
  293. get: function get() {
  294. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  295. }
  296. }, {
  297. key: 'page',
  298. get: function get() {
  299. return this.pdfViewer.currentPageNumber;
  300. },
  301. set: function set(value) {
  302. this.pdfViewer.currentPageNumber = value;
  303. }
  304. }, {
  305. key: 'rotation',
  306. get: function get() {
  307. return this.pdfViewer.pagesRotation;
  308. },
  309. set: function set(value) {
  310. this.pdfViewer.pagesRotation = value;
  311. }
  312. }]);
  313. return PDFLinkService;
  314. }();
  315. function isValidExplicitDestination(dest) {
  316. if (!(dest instanceof Array)) {
  317. return false;
  318. }
  319. var destLength = dest.length,
  320. allowNull = true;
  321. if (destLength < 2) {
  322. return false;
  323. }
  324. var page = dest[0];
  325. if (!((typeof page === 'undefined' ? 'undefined' : _typeof(page)) === 'object' && Number.isInteger(page.num) && Number.isInteger(page.gen)) && !(Number.isInteger(page) && page >= 0)) {
  326. return false;
  327. }
  328. var zoom = dest[1];
  329. if (!((typeof zoom === 'undefined' ? 'undefined' : _typeof(zoom)) === 'object' && typeof zoom.name === 'string')) {
  330. return false;
  331. }
  332. switch (zoom.name) {
  333. case 'XYZ':
  334. if (destLength !== 5) {
  335. return false;
  336. }
  337. break;
  338. case 'Fit':
  339. case 'FitB':
  340. return destLength === 2;
  341. case 'FitH':
  342. case 'FitBH':
  343. case 'FitV':
  344. case 'FitBV':
  345. if (destLength !== 3) {
  346. return false;
  347. }
  348. break;
  349. case 'FitR':
  350. if (destLength !== 6) {
  351. return false;
  352. }
  353. allowNull = false;
  354. break;
  355. default:
  356. return false;
  357. }
  358. for (var i = 2; i < destLength; i++) {
  359. var param = dest[i];
  360. if (!(typeof param === 'number' || allowNull && param === null)) {
  361. return false;
  362. }
  363. }
  364. return true;
  365. }
  366. var SimpleLinkService = function () {
  367. function SimpleLinkService() {
  368. _classCallCheck(this, SimpleLinkService);
  369. this.externalLinkTarget = null;
  370. this.externalLinkRel = null;
  371. }
  372. _createClass(SimpleLinkService, [{
  373. key: 'navigateTo',
  374. value: function navigateTo(dest) {}
  375. }, {
  376. key: 'getDestinationHash',
  377. value: function getDestinationHash(dest) {
  378. return '#';
  379. }
  380. }, {
  381. key: 'getAnchorUrl',
  382. value: function getAnchorUrl(hash) {
  383. return '#';
  384. }
  385. }, {
  386. key: 'setHash',
  387. value: function setHash(hash) {}
  388. }, {
  389. key: 'executeNamedAction',
  390. value: function executeNamedAction(action) {}
  391. }, {
  392. key: 'onFileAttachmentAnnotation',
  393. value: function onFileAttachmentAnnotation(_ref4) {
  394. var id = _ref4.id,
  395. filename = _ref4.filename,
  396. content = _ref4.content;
  397. }
  398. }, {
  399. key: 'cachePageRef',
  400. value: function cachePageRef(pageNum, pageRef) {}
  401. }, {
  402. key: 'page',
  403. get: function get() {
  404. return 0;
  405. },
  406. set: function set(value) {}
  407. }, {
  408. key: 'rotation',
  409. get: function get() {
  410. return 0;
  411. },
  412. set: function set(value) {}
  413. }]);
  414. return SimpleLinkService;
  415. }();
  416. exports.PDFLinkService = PDFLinkService;
  417. exports.SimpleLinkService = SimpleLinkService;