Refactors FindController dependencies.

This commit is contained in:
Yury Delendik 2016-04-13 14:39:29 -05:00
parent 3d49879211
commit 81fc46e666
5 changed files with 60 additions and 43 deletions

View File

@ -30,6 +30,7 @@ PDFJS.workerSrc = '../../build/pdf.worker.js';
// PDFJS.cMapPacked = true;
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
var SEARCH_FOR = ''; // try 'Mozilla';
var container = document.getElementById('viewerContainer');
@ -42,9 +43,19 @@ var pdfViewer = new PDFJS.PDFViewer({
});
pdfLinkService.setViewer(pdfViewer);
// (Optionally) enable find controller.
var pdfFindController = new PDFJS.PDFFindController({
pdfViewer: pdfViewer
});
pdfViewer.setFindController(pdfFindController);
container.addEventListener('pagesinit', function () {
// We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = 'page-width';
if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
}
});
// Loading document.

View File

@ -179,6 +179,7 @@ target.components = function() {
var bundleFiles = [
'pdf_viewer.js',
'pdf_history.js',
'pdf_find_controller.js',
'download_manager.js'
];
bundle('pdf_viewer.component.js', ROOT_DIR + TMP_PDF_VIEWER, bundleFiles,

View File

@ -204,9 +204,24 @@ var PDFViewerApplication = {
pdfLinkService.setHistory(this.pdfHistory);
this.findController = new PDFFindController({
pdfViewer: this.pdfViewer,
integratedFind: this.supportsIntegratedFind
pdfViewer: this.pdfViewer
});
this.findController.onUpdateResultsCount = function (matchCount) {
if (this.supportsIntegratedFind) {
return;
}
this.findBar.updateResultsCount(matchCount);
}.bind(this);
this.findController.onUpdateState = function (state, previous, matchCount) {
if (this.supportsIntegratedFind) {
FirefoxCom.request('updateFindControlState',
{result: state, findPrevious: previous});
} else {
this.findBar.updateUIState(state, previous, matchCount);
}
}.bind(this);
this.findController.listenWindowEvents();
this.pdfViewer.setFindController(this.findController);
// FIXME better PDFFindBar constructor parameters
@ -214,8 +229,6 @@ var PDFViewerApplication = {
findBarConfig.findController = this.findController;
this.findBar = new PDFFindBar(findBarConfig);
this.findController.setFindBar(this.findBar);
this.overlayManager = OverlayManager;
this.handTool = new HandTool({

View File

@ -17,18 +17,16 @@
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('pdfjs-web/pdf_find_controller', ['exports',
'pdfjs-web/ui_utils', 'pdfjs-web/firefoxcom'], factory);
define('pdfjs-web/pdf_find_controller', ['exports', 'pdfjs-web/ui_utils'],
factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('./ui_utils.js'), require('./firefoxcom.js'));
factory(exports, require('./ui_utils.js'));
} else {
factory((root.pdfjsWebPDFFindController = {}), root.pdfjsWebUIUtils,
root.pdfjsWebFirefoxCom);
factory((root.pdfjsWebPDFFindController = {}), root.pdfjsWebUIUtils);
}
}(this, function (exports, uiUtils, firefoxCom) {
var scrollIntoView = uiUtils.scrollIntoView;
var FirefoxCom = firefoxCom.FirefoxCom;
var FindStates = {
FIND_FOUND: 0,
@ -61,31 +59,32 @@ var CHARACTERS_TO_NORMALIZE = {
var PDFFindController = (function PDFFindControllerClosure() {
function PDFFindController(options) {
this.pdfViewer = options.pdfViewer || null;
this.integratedFind = options.integratedFind || false;
this.findBar = options.findBar || null;
this.onUpdateResultsCount = null;
this.onUpdateState = null;
this.reset();
// Compile the regular expression for text normalization once.
var replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
this.normalizationRegex = new RegExp('[' + replace + ']', 'g');
var events = [
'find',
'findagain',
'findhighlightallchange',
'findcasesensitivitychange'
];
this.handleEvent = this.handleEvent.bind(this);
for (var i = 0, len = events.length; i < len; i++) {
window.addEventListener(events[i], this.handleEvent);
}
}
PDFFindController.prototype = {
setFindBar: function PDFFindController_setFindBar(findBar) {
this.findBar = findBar;
listenWindowEvents: function PDFFindController_listenWindowEvents() {
var events = [
'find',
'findagain',
'findhighlightallchange',
'findcasesensitivitychange'
];
var handleEvent = function (e) {
this.executeCommand(e.type, e.detail);
}.bind(this);
for (var i = 0, len = events.length; i < len; i++) {
window.addEventListener(events[i], handleEvent);
}
},
reset: function PDFFindController_reset() {
@ -199,18 +198,18 @@ var PDFFindController = (function PDFFindControllerClosure() {
extractPageText(0);
},
handleEvent: function PDFFindController_handleEvent(e) {
if (this.state === null || e.type !== 'findagain') {
executeCommand: function PDFFindController_executeCommand(cmd, state) {
if (this.state === null || cmd !== 'findagain') {
this.dirtyMatch = true;
}
this.state = e.detail;
this.state = state;
this.updateUIState(FindStates.FIND_PENDING);
this.firstPagePromise.then(function() {
this.extractText();
clearTimeout(this.findTimeout);
if (e.type === 'find') {
if (cmd === 'find') {
// Only trigger the find action after 250ms of silence.
this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
} else {
@ -408,24 +407,15 @@ var PDFFindController = (function PDFFindControllerClosure() {
updateUIResultsCount:
function PDFFindController_updateUIResultsCount() {
if (this.findBar === null) {
throw new Error('PDFFindController is not initialized with a ' +
'PDFFindBar instance.');
if (this.onUpdateResultsCount) {
this.onUpdateResultsCount(this.matchCount);
}
this.findBar.updateResultsCount(this.matchCount);
},
updateUIState: function PDFFindController_updateUIState(state, previous) {
if (this.integratedFind) {
FirefoxCom.request('updateFindControlState',
{ result: state, findPrevious: previous });
return;
if (this.onUpdateState) {
this.onUpdateState(state, previous, this.matchCount);
}
if (this.findBar === null) {
throw new Error('PDFFindController is not initialized with a ' +
'PDFFindBar instance.');
}
this.findBar.updateUIState(state, previous, this.matchCount);
}
};
return PDFFindController;

View File

@ -50,6 +50,8 @@
PDFJS.DefaultAnnotationLayerFactory =
pdfViewerLibs.pdfjsWebAnnotationLayerBuilder.DefaultAnnotationLayerFactory;
PDFJS.PDFHistory = pdfViewerLibs.pdfjsWebPDFHistory.PDFHistory;
PDFJS.PDFFindController =
pdfViewerLibs.pdfjsWebPDFFindController.PDFFindController;
PDFJS.DownloadManager = pdfViewerLibs.pdfjsWebDownloadManager.DownloadManager;
PDFJS.ProgressBar = pdfViewerLibs.pdfjsWebUIUtils.ProgressBar;