pdf_link_service.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. dest = unescape(hash);
  199. try {
  200. dest = JSON.parse(dest);
  201. if (!(dest instanceof Array)) {
  202. dest = dest.toString();
  203. }
  204. } catch (ex) {}
  205. if (typeof dest === 'string' || isValidExplicitDestination(dest)) {
  206. this.navigateTo(dest);
  207. return;
  208. }
  209. console.error('PDFLinkService.setHash: "' + unescape(hash) + '" is not ' + 'a valid destination.');
  210. }
  211. }
  212. }, {
  213. key: 'executeNamedAction',
  214. value: function executeNamedAction(action) {
  215. switch (action) {
  216. case 'GoBack':
  217. if (this.pdfHistory) {
  218. this.pdfHistory.back();
  219. }
  220. break;
  221. case 'GoForward':
  222. if (this.pdfHistory) {
  223. this.pdfHistory.forward();
  224. }
  225. break;
  226. case 'NextPage':
  227. if (this.page < this.pagesCount) {
  228. this.page++;
  229. }
  230. break;
  231. case 'PrevPage':
  232. if (this.page > 1) {
  233. this.page--;
  234. }
  235. break;
  236. case 'LastPage':
  237. this.page = this.pagesCount;
  238. break;
  239. case 'FirstPage':
  240. this.page = 1;
  241. break;
  242. default:
  243. break;
  244. }
  245. this.eventBus.dispatch('namedaction', {
  246. source: this,
  247. action: action
  248. });
  249. }
  250. }, {
  251. key: 'onFileAttachmentAnnotation',
  252. value: function onFileAttachmentAnnotation(_ref3) {
  253. var id = _ref3.id,
  254. filename = _ref3.filename,
  255. content = _ref3.content;
  256. this.eventBus.dispatch('fileattachmentannotation', {
  257. source: this,
  258. id: id,
  259. filename: filename,
  260. content: content
  261. });
  262. }
  263. }, {
  264. key: 'cachePageRef',
  265. value: function cachePageRef(pageNum, pageRef) {
  266. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  267. this._pagesRefCache[refStr] = pageNum;
  268. }
  269. }, {
  270. key: '_cachedPageNumber',
  271. value: function _cachedPageNumber(pageRef) {
  272. var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
  273. return this._pagesRefCache && this._pagesRefCache[refStr] || null;
  274. }
  275. }, {
  276. key: 'pagesCount',
  277. get: function get() {
  278. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  279. }
  280. }, {
  281. key: 'page',
  282. get: function get() {
  283. return this.pdfViewer.currentPageNumber;
  284. },
  285. set: function set(value) {
  286. this.pdfViewer.currentPageNumber = value;
  287. }
  288. }, {
  289. key: 'rotation',
  290. get: function get() {
  291. return this.pdfViewer.pagesRotation;
  292. },
  293. set: function set(value) {
  294. this.pdfViewer.pagesRotation = value;
  295. }
  296. }]);
  297. return PDFLinkService;
  298. }();
  299. function isValidExplicitDestination(dest) {
  300. if (!(dest instanceof Array)) {
  301. return false;
  302. }
  303. var destLength = dest.length,
  304. allowNull = true;
  305. if (destLength < 2) {
  306. return false;
  307. }
  308. var page = dest[0];
  309. if (!((typeof page === 'undefined' ? 'undefined' : _typeof(page)) === 'object' && Number.isInteger(page.num) && Number.isInteger(page.gen)) && !(Number.isInteger(page) && page >= 0)) {
  310. return false;
  311. }
  312. var zoom = dest[1];
  313. if (!((typeof zoom === 'undefined' ? 'undefined' : _typeof(zoom)) === 'object' && typeof zoom.name === 'string')) {
  314. return false;
  315. }
  316. switch (zoom.name) {
  317. case 'XYZ':
  318. if (destLength !== 5) {
  319. return false;
  320. }
  321. break;
  322. case 'Fit':
  323. case 'FitB':
  324. return destLength === 2;
  325. case 'FitH':
  326. case 'FitBH':
  327. case 'FitV':
  328. case 'FitBV':
  329. if (destLength !== 3) {
  330. return false;
  331. }
  332. break;
  333. case 'FitR':
  334. if (destLength !== 6) {
  335. return false;
  336. }
  337. allowNull = false;
  338. break;
  339. default:
  340. return false;
  341. }
  342. for (var i = 2; i < destLength; i++) {
  343. var param = dest[i];
  344. if (!(typeof param === 'number' || allowNull && param === null)) {
  345. return false;
  346. }
  347. }
  348. return true;
  349. }
  350. var SimpleLinkService = function () {
  351. function SimpleLinkService() {
  352. _classCallCheck(this, SimpleLinkService);
  353. }
  354. _createClass(SimpleLinkService, [{
  355. key: 'navigateTo',
  356. value: function navigateTo(dest) {}
  357. }, {
  358. key: 'getDestinationHash',
  359. value: function getDestinationHash(dest) {
  360. return '#';
  361. }
  362. }, {
  363. key: 'getAnchorUrl',
  364. value: function getAnchorUrl(hash) {
  365. return '#';
  366. }
  367. }, {
  368. key: 'setHash',
  369. value: function setHash(hash) {}
  370. }, {
  371. key: 'executeNamedAction',
  372. value: function executeNamedAction(action) {}
  373. }, {
  374. key: 'onFileAttachmentAnnotation',
  375. value: function onFileAttachmentAnnotation(_ref4) {
  376. var id = _ref4.id,
  377. filename = _ref4.filename,
  378. content = _ref4.content;
  379. }
  380. }, {
  381. key: 'cachePageRef',
  382. value: function cachePageRef(pageNum, pageRef) {}
  383. }, {
  384. key: 'page',
  385. get: function get() {
  386. return 0;
  387. },
  388. set: function set(value) {}
  389. }, {
  390. key: 'rotation',
  391. get: function get() {
  392. return 0;
  393. },
  394. set: function set(value) {}
  395. }]);
  396. return SimpleLinkService;
  397. }();
  398. exports.PDFLinkService = PDFLinkService;
  399. exports.SimpleLinkService = SimpleLinkService;