Convert PDFLinkService
to an ES6 class
This commit is contained in:
parent
ca3c08f12b
commit
8ba8072937
@ -57,6 +57,11 @@ class IPDFLinkService {
|
||||
*/
|
||||
executeNamedAction(action) {}
|
||||
|
||||
/**
|
||||
* @param {Object} params
|
||||
*/
|
||||
onFileAttachmentAnnotation({ id, filename, content, }) {}
|
||||
|
||||
/**
|
||||
* @param {number} pageNum - page number.
|
||||
* @param {Object} pageRef - reference to the page.
|
||||
|
@ -24,17 +24,14 @@ import { parseQueryString } from './ui_utils';
|
||||
/**
|
||||
* Performs navigation functions inside PDF, such as opening specified page,
|
||||
* or destination.
|
||||
* @class
|
||||
* @implements {IPDFLinkService}
|
||||
*/
|
||||
var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
class PDFLinkService {
|
||||
/**
|
||||
* @constructs PDFLinkService
|
||||
* @param {PDFLinkServiceOptions} options
|
||||
*/
|
||||
function PDFLinkService(options) {
|
||||
options = options || {};
|
||||
this.eventBus = options.eventBus || getGlobalEventBus();
|
||||
constructor({ eventBus, } = {}) {
|
||||
this.eventBus = eventBus || getGlobalEventBus();
|
||||
this.baseUrl = null;
|
||||
this.pdfDocument = null;
|
||||
this.pdfViewer = null;
|
||||
@ -43,41 +40,40 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
this._pagesRefCache = null;
|
||||
}
|
||||
|
||||
PDFLinkService.prototype = {
|
||||
setDocument: function PDFLinkService_setDocument(pdfDocument, baseUrl) {
|
||||
setDocument(pdfDocument, baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.pdfDocument = pdfDocument;
|
||||
this._pagesRefCache = Object.create(null);
|
||||
},
|
||||
}
|
||||
|
||||
setViewer: function PDFLinkService_setViewer(pdfViewer) {
|
||||
setViewer(pdfViewer) {
|
||||
this.pdfViewer = pdfViewer;
|
||||
},
|
||||
}
|
||||
|
||||
setHistory: function PDFLinkService_setHistory(pdfHistory) {
|
||||
setHistory(pdfHistory) {
|
||||
this.pdfHistory = pdfHistory;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
get pagesCount() {
|
||||
return this.pdfDocument ? this.pdfDocument.numPages : 0;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
get page() {
|
||||
return this.pdfViewer.currentPageNumber;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} value
|
||||
*/
|
||||
set page(value) {
|
||||
this.pdfViewer.currentPageNumber = value;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string|Array} dest - The named, or explicit, PDF destination.
|
||||
@ -151,7 +147,7 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
}
|
||||
goToDestination(data);
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string|Array} dest - The PDF destination object.
|
||||
@ -166,7 +162,7 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
return this.getAnchorUrl('#' + escape(str));
|
||||
}
|
||||
return this.getAnchorUrl('');
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefix the full url on anchor links to make sure that links are resolved
|
||||
@ -174,17 +170,17 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
* @param {String} anchor The anchor hash, including the #.
|
||||
* @returns {string} The hyperlink to the PDF object.
|
||||
*/
|
||||
getAnchorUrl: function PDFLinkService_getAnchorUrl(anchor) {
|
||||
getAnchorUrl(anchor) {
|
||||
return (this.baseUrl || '') + anchor;
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} hash
|
||||
*/
|
||||
setHash: function PDFLinkService_setHash(hash) {
|
||||
var pageNumber, dest;
|
||||
setHash(hash) {
|
||||
let pageNumber, dest;
|
||||
if (hash.indexOf('=') >= 0) {
|
||||
var params = parseQueryString(hash);
|
||||
let params = parseQueryString(hash);
|
||||
if ('search' in params) {
|
||||
this.eventBus.dispatch('findfromurlhash', {
|
||||
source: this,
|
||||
@ -205,9 +201,9 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
}
|
||||
if ('zoom' in params) {
|
||||
// Build the destination array.
|
||||
var zoomArgs = params.zoom.split(','); // scale,left,top
|
||||
var zoomArg = zoomArgs[0];
|
||||
var zoomArgNumber = parseFloat(zoomArg);
|
||||
let zoomArgs = params.zoom.split(','); // scale,left,top
|
||||
let zoomArg = zoomArgs[0];
|
||||
let zoomArgNumber = parseFloat(zoomArg);
|
||||
|
||||
if (zoomArg.indexOf('Fit') === -1) {
|
||||
// If the zoomArg is a number, it has to get divided by 100. If it's
|
||||
@ -225,16 +221,16 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
zoomArgs.length > 1 ? (zoomArgs[1] | 0) : null];
|
||||
} else if (zoomArg === 'FitR') {
|
||||
if (zoomArgs.length !== 5) {
|
||||
console.error('PDFLinkService_setHash: ' +
|
||||
'Not enough parameters for \'FitR\'.');
|
||||
console.error(
|
||||
'PDFLinkService.setHash: Not enough parameters for "FitR".');
|
||||
} else {
|
||||
dest = [null, { name: zoomArg, },
|
||||
(zoomArgs[1] | 0), (zoomArgs[2] | 0),
|
||||
(zoomArgs[3] | 0), (zoomArgs[4] | 0)];
|
||||
}
|
||||
} else {
|
||||
console.error('PDFLinkService_setHash: \'' + zoomArg +
|
||||
'\' is not a valid zoom value.');
|
||||
console.error(`PDFLinkService.setHash: "${zoomArg}" is not ` +
|
||||
'a valid zoom value.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -258,7 +254,7 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
/^\d+$/.test(hash) && hash <= this.pagesCount) {
|
||||
console.warn('PDFLinkService_setHash: specifying a page number ' +
|
||||
'directly after the hash symbol (#) is deprecated, ' +
|
||||
'please use the "#page=' + hash + '" form instead.');
|
||||
`please use the "#page=${hash}" form instead.`);
|
||||
this.page = hash | 0;
|
||||
}
|
||||
|
||||
@ -280,15 +276,15 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
this.navigateTo(dest);
|
||||
return;
|
||||
}
|
||||
console.error('PDFLinkService_setHash: \'' + unescape(hash) +
|
||||
'\' is not a valid destination.');
|
||||
console.error(`PDFLinkService.setHash: "${unescape(hash)}" is not ` +
|
||||
'a valid destination.');
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} action
|
||||
*/
|
||||
executeNamedAction: function PDFLinkService_executeNamedAction(action) {
|
||||
executeNamedAction(action) {
|
||||
// See PDF reference, table 8.45 - Named action
|
||||
switch (action) {
|
||||
case 'GoBack':
|
||||
@ -331,51 +327,51 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
source: this,
|
||||
action,
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} params
|
||||
*/
|
||||
onFileAttachmentAnnotation(params = {}) {
|
||||
onFileAttachmentAnnotation({ id, filename, content, }) {
|
||||
this.eventBus.dispatch('fileattachmentannotation', {
|
||||
source: this,
|
||||
id: params.id,
|
||||
filename: params.filename,
|
||||
content: params.content,
|
||||
id,
|
||||
filename,
|
||||
content,
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} pageNum - page number.
|
||||
* @param {Object} pageRef - reference to the page.
|
||||
*/
|
||||
cachePageRef: function PDFLinkService_cachePageRef(pageNum, pageRef) {
|
||||
var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
|
||||
cachePageRef(pageNum, pageRef) {
|
||||
let refStr = pageRef.num + ' ' + pageRef.gen + ' R';
|
||||
this._pagesRefCache[refStr] = pageNum;
|
||||
},
|
||||
}
|
||||
|
||||
_cachedPageNumber: function PDFLinkService_cachedPageNumber(pageRef) {
|
||||
var refStr = pageRef.num + ' ' + pageRef.gen + ' R';
|
||||
_cachedPageNumber(pageRef) {
|
||||
let refStr = pageRef.num + ' ' + pageRef.gen + ' R';
|
||||
return (this._pagesRefCache && this._pagesRefCache[refStr]) || null;
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function isValidExplicitDestination(dest) {
|
||||
function isValidExplicitDestination(dest) {
|
||||
if (!(dest instanceof Array)) {
|
||||
return false;
|
||||
}
|
||||
var destLength = dest.length, allowNull = true;
|
||||
let destLength = dest.length, allowNull = true;
|
||||
if (destLength < 2) {
|
||||
return false;
|
||||
}
|
||||
var page = dest[0];
|
||||
let page = dest[0];
|
||||
if (!(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)) {
|
||||
return false;
|
||||
}
|
||||
var zoom = dest[1];
|
||||
let zoom = dest[1];
|
||||
if (!(typeof zoom === 'object' && typeof zoom.name === 'string')) {
|
||||
return false;
|
||||
}
|
||||
@ -405,70 +401,62 @@ var PDFLinkService = (function PDFLinkServiceClosure() {
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
for (var i = 2; i < destLength; i++) {
|
||||
var param = dest[i];
|
||||
for (let i = 2; i < destLength; i++) {
|
||||
let param = dest[i];
|
||||
if (!(typeof param === 'number' || (allowNull && param === null))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return PDFLinkService;
|
||||
})();
|
||||
|
||||
var SimpleLinkService = (function SimpleLinkServiceClosure() {
|
||||
function SimpleLinkService() {}
|
||||
|
||||
SimpleLinkService.prototype = {
|
||||
class SimpleLinkService {
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
get page() {
|
||||
return 0;
|
||||
},
|
||||
}
|
||||
/**
|
||||
* @param {number} value
|
||||
*/
|
||||
set page(value) {},
|
||||
set page(value) {}
|
||||
/**
|
||||
* @param dest - The PDF destination object.
|
||||
*/
|
||||
navigateTo(dest) {},
|
||||
navigateTo(dest) {}
|
||||
/**
|
||||
* @param dest - The PDF destination object.
|
||||
* @returns {string} The hyperlink to the PDF object.
|
||||
*/
|
||||
getDestinationHash(dest) {
|
||||
return '#';
|
||||
},
|
||||
}
|
||||
/**
|
||||
* @param hash - The PDF parameters/hash.
|
||||
* @returns {string} The hyperlink to the PDF object.
|
||||
*/
|
||||
getAnchorUrl(hash) {
|
||||
return '#';
|
||||
},
|
||||
}
|
||||
/**
|
||||
* @param {string} hash
|
||||
*/
|
||||
setHash(hash) {},
|
||||
setHash(hash) {}
|
||||
/**
|
||||
* @param {string} action
|
||||
*/
|
||||
executeNamedAction(action) {},
|
||||
executeNamedAction(action) {}
|
||||
/**
|
||||
* @param {Object} params
|
||||
*/
|
||||
onFileAttachmentAnnotation(params) {},
|
||||
onFileAttachmentAnnotation({ id, filename, content, }) {}
|
||||
/**
|
||||
* @param {number} pageNum - page number.
|
||||
* @param {Object} pageRef - reference to the page.
|
||||
*/
|
||||
cachePageRef(pageNum, pageRef) {},
|
||||
};
|
||||
return SimpleLinkService;
|
||||
})();
|
||||
cachePageRef(pageNum, pageRef) {}
|
||||
}
|
||||
|
||||
export {
|
||||
PDFLinkService,
|
||||
|
Loading…
Reference in New Issue
Block a user