pdf_link_service.js 13 KB

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