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