pdf_link_service.js 13 KB

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