pdf_link_service.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. _classCallCheck(this, PDFLinkService);
  37. this.eventBus = eventBus || (0, _dom_events.getGlobalEventBus)();
  38. this.baseUrl = null;
  39. this.pdfDocument = null;
  40. this.pdfViewer = null;
  41. this.pdfHistory = null;
  42. this._pagesRefCache = null;
  43. }
  44. _createClass(PDFLinkService, [{
  45. key: 'setDocument',
  46. value: function setDocument(pdfDocument, baseUrl) {
  47. this.baseUrl = baseUrl;
  48. this.pdfDocument = pdfDocument;
  49. this._pagesRefCache = Object.create(null);
  50. }
  51. }, {
  52. key: 'setViewer',
  53. value: function setViewer(pdfViewer) {
  54. this.pdfViewer = pdfViewer;
  55. }
  56. }, {
  57. key: 'setHistory',
  58. value: function setHistory(pdfHistory) {
  59. this.pdfHistory = pdfHistory;
  60. }
  61. }, {
  62. key: 'navigateTo',
  63. value: function navigateTo(dest) {
  64. var _this = this;
  65. var goToDestination = function goToDestination(_ref2) {
  66. var namedDest = _ref2.namedDest,
  67. explicitDest = _ref2.explicitDest;
  68. var destRef = explicitDest[0],
  69. pageNumber = void 0;
  70. if (destRef instanceof Object) {
  71. pageNumber = _this._cachedPageNumber(destRef);
  72. if (pageNumber === null) {
  73. _this.pdfDocument.getPageIndex(destRef).then(function (pageIndex) {
  74. _this.cachePageRef(pageIndex + 1, destRef);
  75. goToDestination({
  76. namedDest: namedDest,
  77. explicitDest: explicitDest
  78. });
  79. }).catch(function () {
  80. console.error('PDFLinkService.navigateTo: "' + destRef + '" is not ' + ('a valid page reference, for dest="' + dest + '".'));
  81. });
  82. return;
  83. }
  84. } else if (Number.isInteger(destRef)) {
  85. pageNumber = destRef + 1;
  86. } else {
  87. console.error('PDFLinkService.navigateTo: "' + destRef + '" is not ' + ('a valid destination reference, for dest="' + dest + '".'));
  88. return;
  89. }
  90. if (!pageNumber || pageNumber < 1 || pageNumber > _this.pagesCount) {
  91. console.error('PDFLinkService.navigateTo: "' + pageNumber + '" is not ' + ('a valid page number, for dest="' + dest + '".'));
  92. return;
  93. }
  94. if (_this.pdfHistory) {
  95. _this.pdfHistory.pushCurrentPosition();
  96. _this.pdfHistory.push({
  97. namedDest: namedDest,
  98. explicitDest: explicitDest,
  99. pageNumber: pageNumber
  100. });
  101. }
  102. _this.pdfViewer.scrollPageIntoView({
  103. pageNumber: pageNumber,
  104. destArray: explicitDest
  105. });
  106. };
  107. new Promise(function (resolve, reject) {
  108. if (typeof dest === 'string') {
  109. _this.pdfDocument.getDestination(dest).then(function (destArray) {
  110. resolve({
  111. namedDest: dest,
  112. explicitDest: destArray
  113. });
  114. });
  115. return;
  116. }
  117. resolve({
  118. namedDest: '',
  119. explicitDest: dest
  120. });
  121. }).then(function (data) {
  122. if (!(data.explicitDest instanceof Array)) {
  123. console.error('PDFLinkService.navigateTo: "' + data.explicitDest + '" is' + (' not a valid destination array, for dest="' + dest + '".'));
  124. return;
  125. }
  126. goToDestination(data);
  127. });
  128. }
  129. }, {
  130. key: 'getDestinationHash',
  131. value: function getDestinationHash(dest) {
  132. if (typeof dest === 'string') {
  133. return this.getAnchorUrl('#' + escape(dest));
  134. }
  135. if (dest instanceof Array) {
  136. var str = JSON.stringify(dest);
  137. return this.getAnchorUrl('#' + escape(str));
  138. }
  139. return this.getAnchorUrl('');
  140. }
  141. }, {
  142. key: 'getAnchorUrl',
  143. value: function getAnchorUrl(anchor) {
  144. return (this.baseUrl || '') + anchor;
  145. }
  146. }, {
  147. key: 'setHash',
  148. value: function setHash(hash) {
  149. var pageNumber = void 0,
  150. dest = void 0;
  151. if (hash.indexOf('=') >= 0) {
  152. var params = (0, _ui_utils.parseQueryString)(hash);
  153. if ('search' in params) {
  154. this.eventBus.dispatch('findfromurlhash', {
  155. source: this,
  156. query: params['search'].replace(/"/g, ''),
  157. phraseSearch: params['phrase'] === 'true'
  158. });
  159. }
  160. if ('nameddest' in params) {
  161. this.navigateTo(params.nameddest);
  162. return;
  163. }
  164. if ('page' in params) {
  165. pageNumber = params.page | 0 || 1;
  166. }
  167. if ('zoom' in params) {
  168. var zoomArgs = params.zoom.split(',');
  169. var zoomArg = zoomArgs[0];
  170. var zoomArgNumber = parseFloat(zoomArg);
  171. if (zoomArg.indexOf('Fit') === -1) {
  172. dest = [null, { name: 'XYZ' }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null, zoomArgs.length > 2 ? zoomArgs[2] | 0 : null, zoomArgNumber ? zoomArgNumber / 100 : zoomArg];
  173. } else {
  174. if (zoomArg === 'Fit' || zoomArg === 'FitB') {
  175. dest = [null, { name: zoomArg }];
  176. } else if (zoomArg === 'FitH' || zoomArg === 'FitBH' || zoomArg === 'FitV' || zoomArg === 'FitBV') {
  177. dest = [null, { name: zoomArg }, zoomArgs.length > 1 ? zoomArgs[1] | 0 : null];
  178. } else if (zoomArg === 'FitR') {
  179. if (zoomArgs.length !== 5) {
  180. console.error('PDFLinkService.setHash: Not enough parameters for "FitR".');
  181. } else {
  182. dest = [null, { name: zoomArg }, zoomArgs[1] | 0, zoomArgs[2] | 0, zoomArgs[3] | 0, zoomArgs[4] | 0];
  183. }
  184. } else {
  185. console.error('PDFLinkService.setHash: "' + zoomArg + '" is not ' + 'a valid zoom value.');
  186. }
  187. }
  188. }
  189. if (dest) {
  190. this.pdfViewer.scrollPageIntoView({
  191. pageNumber: pageNumber || this.page,
  192. destArray: dest,
  193. allowNegativeOffset: true
  194. });
  195. } else if (pageNumber) {
  196. this.page = pageNumber;
  197. }
  198. if ('pagemode' in params) {
  199. this.eventBus.dispatch('pagemode', {
  200. source: this,
  201. mode: params.pagemode
  202. });
  203. }
  204. } else {
  205. dest = unescape(hash);
  206. try {
  207. dest = JSON.parse(dest);
  208. if (!(dest instanceof Array)) {
  209. dest = dest.toString();
  210. }
  211. } catch (ex) {}
  212. if (typeof dest === 'string' || isValidExplicitDestination(dest)) {
  213. this.navigateTo(dest);
  214. return;
  215. }
  216. console.error('PDFLinkService.setHash: "' + unescape(hash) + '" is not ' + 'a valid destination.');
  217. }
  218. }
  219. }, {
  220. key: 'executeNamedAction',
  221. value: function executeNamedAction(action) {
  222. switch (action) {
  223. case 'GoBack':
  224. if (this.pdfHistory) {
  225. this.pdfHistory.back();
  226. }
  227. break;
  228. case 'GoForward':
  229. if (this.pdfHistory) {
  230. this.pdfHistory.forward();
  231. }
  232. break;
  233. case 'NextPage':
  234. if (this.page < this.pagesCount) {
  235. this.page++;
  236. }
  237. break;
  238. case 'PrevPage':
  239. if (this.page > 1) {
  240. this.page--;
  241. }
  242. break;
  243. case 'LastPage':
  244. this.page = this.pagesCount;
  245. break;
  246. case 'FirstPage':
  247. this.page = 1;
  248. break;
  249. default:
  250. break;
  251. }
  252. this.eventBus.dispatch('namedaction', {
  253. source: this,
  254. action: action
  255. });
  256. }
  257. }, {
  258. key: 'onFileAttachmentAnnotation',
  259. value: function onFileAttachmentAnnotation(_ref3) {
  260. var id = _ref3.id,
  261. filename = _ref3.filename,
  262. content = _ref3.content;
  263. this.eventBus.dispatch('fileattachmentannotation', {
  264. source: this,
  265. id: id,
  266. filename: filename,
  267. content: content
  268. });
  269. }
  270. }, {
  271. key: 'cachePageRef',
  272. value: function cachePageRef(pageNum, pageRef) {
  273. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  274. this._pagesRefCache[refStr] = pageNum;
  275. }
  276. }, {
  277. key: '_cachedPageNumber',
  278. value: function _cachedPageNumber(pageRef) {
  279. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  280. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  281. }
  282. }, {
  283. key: 'pagesCount',
  284. get: function get() {
  285. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  286. }
  287. }, {
  288. key: 'page',
  289. get: function get() {
  290. return this.pdfViewer.currentPageNumber;
  291. },
  292. set: function set(value) {
  293. this.pdfViewer.currentPageNumber = value;
  294. }
  295. }, {
  296. key: 'rotation',
  297. get: function get() {
  298. return this.pdfViewer.pagesRotation;
  299. },
  300. set: function set(value) {
  301. this.pdfViewer.pagesRotation = value;
  302. }
  303. }]);
  304. return PDFLinkService;
  305. }();
  306. function isValidExplicitDestination(dest) {
  307. if (!(dest instanceof Array)) {
  308. return false;
  309. }
  310. var destLength = dest.length,
  311. allowNull = true;
  312. if (destLength < 2) {
  313. return false;
  314. }
  315. var page = dest[0];
  316. if (!((typeof page === 'undefined' ? 'undefined' : _typeof(page)) === 'object' && Number.isInteger(page.num) && Number.isInteger(page.gen)) && !(Number.isInteger(page) && page >= 0)) {
  317. return false;
  318. }
  319. var zoom = dest[1];
  320. if (!((typeof zoom === 'undefined' ? 'undefined' : _typeof(zoom)) === 'object' && typeof zoom.name === 'string')) {
  321. return false;
  322. }
  323. switch (zoom.name) {
  324. case 'XYZ':
  325. if (destLength !== 5) {
  326. return false;
  327. }
  328. break;
  329. case 'Fit':
  330. case 'FitB':
  331. return destLength === 2;
  332. case 'FitH':
  333. case 'FitBH':
  334. case 'FitV':
  335. case 'FitBV':
  336. if (destLength !== 3) {
  337. return false;
  338. }
  339. break;
  340. case 'FitR':
  341. if (destLength !== 6) {
  342. return false;
  343. }
  344. allowNull = false;
  345. break;
  346. default:
  347. return false;
  348. }
  349. for (var i = 2; i < destLength; i++) {
  350. var param = dest[i];
  351. if (!(typeof param === 'number' || allowNull && param === null)) {
  352. return false;
  353. }
  354. }
  355. return true;
  356. }
  357. var SimpleLinkService = function () {
  358. function SimpleLinkService() {
  359. _classCallCheck(this, SimpleLinkService);
  360. }
  361. _createClass(SimpleLinkService, [{
  362. key: 'navigateTo',
  363. value: function navigateTo(dest) {}
  364. }, {
  365. key: 'getDestinationHash',
  366. value: function getDestinationHash(dest) {
  367. return '#';
  368. }
  369. }, {
  370. key: 'getAnchorUrl',
  371. value: function getAnchorUrl(hash) {
  372. return '#';
  373. }
  374. }, {
  375. key: 'setHash',
  376. value: function setHash(hash) {}
  377. }, {
  378. key: 'executeNamedAction',
  379. value: function executeNamedAction(action) {}
  380. }, {
  381. key: 'onFileAttachmentAnnotation',
  382. value: function onFileAttachmentAnnotation(_ref4) {
  383. var id = _ref4.id,
  384. filename = _ref4.filename,
  385. content = _ref4.content;
  386. }
  387. }, {
  388. key: 'cachePageRef',
  389. value: function cachePageRef(pageNum, pageRef) {}
  390. }, {
  391. key: 'page',
  392. get: function get() {
  393. return 0;
  394. },
  395. set: function set(value) {}
  396. }, {
  397. key: 'rotation',
  398. get: function get() {
  399. return 0;
  400. },
  401. set: function set(value) {}
  402. }]);
  403. return SimpleLinkService;
  404. }();
  405. exports.PDFLinkService = PDFLinkService;
  406. exports.SimpleLinkService = SimpleLinkService;