Browse Source

Merge pull request #5007 from timvandermeij/pdfview-tlb

Converting PDFFindBar and PDFFindController to classes
Yury Delendik 11 years ago
parent
commit
df8d2573dd
  1. 10
      web/page_view.js
  2. 210
      web/pdf_find_bar.js
  3. 581
      web/pdf_find_controller.js
  4. 4
      web/text_layer_builder.js
  5. 41
      web/viewer.js

10
web/page_view.js

@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals RenderingStates, PDFView, PDFHistory, PDFFindBar, PDFJS, mozL10n, /* globals RenderingStates, PDFView, PDFHistory, PDFJS, mozL10n, CustomStyle,
CustomStyle, PresentationMode, scrollIntoView, SCROLLBAR_PADDING, PresentationMode, scrollIntoView, SCROLLBAR_PADDING, CSS_UNITS,
CSS_UNITS, UNKNOWN_SCALE, DEFAULT_SCALE, getOutputScale, UNKNOWN_SCALE, DEFAULT_SCALE, getOutputScale, TextLayerBuilder,
TextLayerBuilder, cache, Stats */ cache, Stats */
'use strict'; 'use strict';
@ -272,7 +272,7 @@ var PageView = function pageView(container, id, scale,
case 'Find': case 'Find':
if (!PDFView.supportsIntegratedFind) { if (!PDFView.supportsIntegratedFind) {
PDFFindBar.toggle(); PDFView.findBar.toggle();
} }
break; break;

210
web/pdf_find_bar.js

@ -13,45 +13,36 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals PDFFindController, FindStates, mozL10n */ /* globals FindStates, mozL10n */
'use strict'; 'use strict';
/** /**
* Creates a "search bar" given set of DOM elements * Creates a "search bar" given a set of DOM elements that act as controls
* that act as controls for searching, or for setting * for searching or for setting search preferences in the UI. This object
* search preferences in the UI. This object also sets * also sets up the appropriate events for the controls. Actual searching
* up the appropriate events for the controls. Actual * is done by PDFFindController.
* searching is done by PDFFindController
*/ */
var PDFFindBar = { var PDFFindBar = (function PDFFindBarClosure() {
opened: false, function PDFFindBar(options) {
bar: null, this.opened = false;
toggleButton: null, this.bar = options.bar || null;
findField: null, this.toggleButton = options.toggleButton || null;
highlightAll: null, this.findField = options.findField || null;
caseSensitive: null, this.highlightAll = options.highlightAllCheckbox || null;
findMsg: null, this.caseSensitive = options.caseSensitiveCheckbox || null;
findStatusIcon: null, this.findMsg = options.findMsg || null;
findPreviousButton: null, this.findStatusIcon = options.findStatusIcon || null;
findNextButton: null, this.findPreviousButton = options.findPreviousButton || null;
this.findNextButton = options.findNextButton || null;
initialize: function(options) { this.findController = options.findController || null;
if(typeof PDFFindController === 'undefined' || PDFFindController === null) {
throw 'PDFFindBar cannot be initialized ' + if (this.findController === null) {
'without a PDFFindController instance.'; throw new Error('PDFFindBar cannot be used without a ' +
'PDFFindController instance.');
} }
this.bar = options.bar; // Add event listeners to the DOM elements.
this.toggleButton = options.toggleButton;
this.findField = options.findField;
this.highlightAll = options.highlightAllCheckbox;
this.caseSensitive = options.caseSensitiveCheckbox;
this.findMsg = options.findMsg;
this.findStatusIcon = options.findStatusIcon;
this.findPreviousButton = options.findPreviousButton;
this.findNextButton = options.findNextButton;
var self = this; var self = this;
this.toggleButton.addEventListener('click', function() { this.toggleButton.addEventListener('click', function() {
self.toggle(); self.toggle();
@ -74,9 +65,9 @@ var PDFFindBar = {
} }
}); });
this.findPreviousButton.addEventListener('click', this.findPreviousButton.addEventListener('click', function() {
function() { self.dispatchEvent('again', true); } self.dispatchEvent('again', true);
); });
this.findNextButton.addEventListener('click', function() { this.findNextButton.addEventListener('click', function() {
self.dispatchEvent('again', false); self.dispatchEvent('again', false);
@ -89,86 +80,87 @@ var PDFFindBar = {
this.caseSensitive.addEventListener('click', function() { this.caseSensitive.addEventListener('click', function() {
self.dispatchEvent('casesensitivitychange'); self.dispatchEvent('casesensitivitychange');
}); });
}, }
dispatchEvent: function(aType, aFindPrevious) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent('find' + aType, true, true, {
query: this.findField.value,
caseSensitive: this.caseSensitive.checked,
highlightAll: this.highlightAll.checked,
findPrevious: aFindPrevious
});
return window.dispatchEvent(event);
},
updateUIState: function(state, previous) {
var notFound = false;
var findMsg = '';
var status = '';
switch (state) {
case FindStates.FIND_FOUND:
break;
case FindStates.FIND_PENDING:
status = 'pending';
break;
case FindStates.FIND_NOTFOUND:
findMsg = mozL10n.get('find_not_found', null, 'Phrase not found');
notFound = true;
break;
case FindStates.FIND_WRAPPED:
if (previous) {
findMsg = mozL10n.get('find_reached_top', null,
'Reached top of document, continued from bottom');
} else {
findMsg = mozL10n.get('find_reached_bottom', null,
'Reached end of document, continued from top');
}
break;
}
if (notFound) { PDFFindBar.prototype = {
this.findField.classList.add('notFound'); dispatchEvent: function PDFFindBar_dispatchEvent(type, findPrev) {
} else { var event = document.createEvent('CustomEvent');
this.findField.classList.remove('notFound'); event.initCustomEvent('find' + type, true, true, {
} query: this.findField.value,
caseSensitive: this.caseSensitive.checked,
highlightAll: this.highlightAll.checked,
findPrevious: findPrev
});
return window.dispatchEvent(event);
},
updateUIState: function PDFFindBar_updateUIState(state, previous) {
var notFound = false;
var findMsg = '';
var status = '';
switch (state) {
case FindStates.FIND_FOUND:
break;
this.findField.setAttribute('data-status', status); case FindStates.FIND_PENDING:
this.findMsg.textContent = findMsg; status = 'pending';
}, break;
open: function() { case FindStates.FIND_NOTFOUND:
if (!this.opened) { findMsg = mozL10n.get('find_not_found', null, 'Phrase not found');
this.opened = true; notFound = true;
this.toggleButton.classList.add('toggled'); break;
this.bar.classList.remove('hidden');
}
this.findField.select(); case FindStates.FIND_WRAPPED:
this.findField.focus(); if (previous) {
}, findMsg = mozL10n.get('find_reached_top', null,
'Reached top of document, continued from bottom');
} else {
findMsg = mozL10n.get('find_reached_bottom', null,
'Reached end of document, continued from top');
}
break;
}
close: function() { if (notFound) {
if (!this.opened) { this.findField.classList.add('notFound');
return; } else {
} this.findField.classList.remove('notFound');
this.opened = false; }
this.toggleButton.classList.remove('toggled');
this.bar.classList.add('hidden');
PDFFindController.active = false; this.findField.setAttribute('data-status', status);
}, this.findMsg.textContent = findMsg;
},
toggle: function() { open: function PDFFindBar_open() {
if (this.opened) { if (!this.opened) {
this.close(); this.opened = true;
} else { this.toggleButton.classList.add('toggled');
this.open(); this.bar.classList.remove('hidden');
}
this.findField.select();
this.findField.focus();
},
close: function PDFFindBar_close() {
if (!this.opened) {
return;
}
this.opened = false;
this.toggleButton.classList.remove('toggled');
this.bar.classList.add('hidden');
this.findController.active = false;
},
toggle: function PDFFindBar_toggle() {
if (this.opened) {
this.close();
} else {
this.open();
}
} }
} };
}; return PDFFindBar;
})();

581
web/pdf_find_controller.js

@ -13,58 +13,50 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals PDFFindBar, PDFJS, FindStates, FirefoxCom, Promise */ /* globals PDFJS, FindStates, FirefoxCom, Promise */
'use strict'; 'use strict';
/** /**
* Provides a "search" or "find" functionality for the PDF. * Provides "search" or "find" functionality for the PDF.
* This object actually performs the search for a given string. * This object actually performs the search for a given string.
*/ */
var PDFFindController = (function PDFFindControllerClosure() {
var PDFFindController = { function PDFFindController(options) {
startedTextExtraction: false, this.startedTextExtraction = false;
extractTextPromises: [], this.extractTextPromises = [];
pendingFindMatches: {}, this.pendingFindMatches = {};
active: false, // If active, find results will be highlighted. this.active = false; // If active, find results will be highlighted.
pageContents: [], // Stores the text for each page. this.pageContents = []; // Stores the text for each page.
pageMatches: [], this.pageMatches = [];
selected: { // Currently selected match. this.selected = { // Currently selected match.
pageIdx: -1, pageIdx: -1,
matchIdx: -1 matchIdx: -1
}, };
offset: { // Where the find algorithm currently is in the document. this.offset = { // Where the find algorithm currently is in the document.
pageIdx: null, pageIdx: null,
matchIdx: null matchIdx: null
}, };
resumePageIdx: null, this.resumePageIdx = null;
state: null, this.state = null;
dirtyMatch: false, this.dirtyMatch = false;
findTimeout: null, this.findTimeout = null;
pdfPageSource: null, this.pdfPageSource = options.pdfPageSource || null;
integratedFind: false, this.integratedFind = options.integratedFind || false;
charactersToNormalize: { this.charactersToNormalize = {
'\u2018': '\'', // Left single quotation mark '\u2018': '\'', // Left single quotation mark
'\u2019': '\'', // Right single quotation mark '\u2019': '\'', // Right single quotation mark
'\u201A': '\'', // Single low-9 quotation mark '\u201A': '\'', // Single low-9 quotation mark
'\u201B': '\'', // Single high-reversed-9 quotation mark '\u201B': '\'', // Single high-reversed-9 quotation mark
'\u201C': '"', // Left double quotation mark '\u201C': '"', // Left double quotation mark
'\u201D': '"', // Right double quotation mark '\u201D': '"', // Right double quotation mark
'\u201E': '"', // Double low-9 quotation mark '\u201E': '"', // Double low-9 quotation mark
'\u201F': '"', // Double high-reversed-9 quotation mark '\u201F': '"', // Double high-reversed-9 quotation mark
'\u00BC': '1/4', // Vulgar fraction one quarter '\u00BC': '1/4', // Vulgar fraction one quarter
'\u00BD': '1/2', // Vulgar fraction one half '\u00BD': '1/2', // Vulgar fraction one half
'\u00BE': '3/4' // Vulgar fraction three quarters '\u00BE': '3/4' // Vulgar fraction three quarters
}, };
this.findBar = options.findBar || null;
initialize: function(options) {
if (typeof PDFFindBar === 'undefined' || PDFFindBar === null) {
throw 'PDFFindController cannot be initialized ' +
'without a PDFFindBar instance';
}
this.pdfPageSource = options.pdfPageSource;
this.integratedFind = options.integratedFind;
// Compile the regular expression for text normalization once // Compile the regular expression for text normalization once
var replace = Object.keys(this.charactersToNormalize).join(''); var replace = Object.keys(this.charactersToNormalize).join('');
@ -85,277 +77,292 @@ var PDFFindController = {
for (var i = 0, len = events.length; i < len; i++) { for (var i = 0, len = events.length; i < len; i++) {
window.addEventListener(events[i], this.handleEvent); window.addEventListener(events[i], this.handleEvent);
} }
}, }
reset: function pdfFindControllerReset() { PDFFindController.prototype = {
this.startedTextExtraction = false; setFindBar: function PDFFindController_setFindBar(findBar) {
this.extractTextPromises = []; this.findBar = findBar;
this.active = false; },
},
normalize: function pdfFindControllerNormalize(text) {
return text.replace(this.normalizationRegex, function (ch) {
return PDFFindController.charactersToNormalize[ch];
});
},
calcFindMatch: function(pageIndex) {
var pageContent = this.normalize(this.pageContents[pageIndex]);
var query = this.normalize(this.state.query);
var caseSensitive = this.state.caseSensitive;
var queryLen = query.length;
if (queryLen === 0) {
// Do nothing: the matches should be wiped out already.
return;
}
if (!caseSensitive) { reset: function PDFFindController_reset() {
pageContent = pageContent.toLowerCase(); this.startedTextExtraction = false;
query = query.toLowerCase(); this.extractTextPromises = [];
} this.active = false;
},
var matches = []; normalize: function PDFFindController_normalize(text) {
var matchIdx = -queryLen; var self = this;
while (true) { return text.replace(this.normalizationRegex, function (ch) {
matchIdx = pageContent.indexOf(query, matchIdx + queryLen); return self.charactersToNormalize[ch];
if (matchIdx === -1) { });
break; },
calcFindMatch: function PDFFindController_calcFindMatch(pageIndex) {
var pageContent = this.normalize(this.pageContents[pageIndex]);
var query = this.normalize(this.state.query);
var caseSensitive = this.state.caseSensitive;
var queryLen = query.length;
if (queryLen === 0) {
return; // Do nothing: the matches should be wiped out already.
} }
matches.push(matchIdx);
}
this.pageMatches[pageIndex] = matches;
this.updatePage(pageIndex);
if (this.resumePageIdx === pageIndex) {
this.resumePageIdx = null;
this.nextPageMatch();
}
},
extractText: function() { if (!caseSensitive) {
if (this.startedTextExtraction) { pageContent = pageContent.toLowerCase();
return; query = query.toLowerCase();
} }
this.startedTextExtraction = true;
this.pageContents = [];
var extractTextPromisesResolves = [];
var numPages = this.pdfPageSource.pdfDocument.numPages;
for (var i = 0; i < numPages; i++) {
this.extractTextPromises.push(new Promise(function (resolve) {
extractTextPromisesResolves.push(resolve);
}));
}
var self = this; var matches = [];
function extractPageText(pageIndex) { var matchIdx = -queryLen;
self.pdfPageSource.pages[pageIndex].getTextContent().then( while (true) {
function textContentResolved(textContent) { matchIdx = pageContent.indexOf(query, matchIdx + queryLen);
var textItems = textContent.items; if (matchIdx === -1) {
var str = []; break;
}
matches.push(matchIdx);
}
this.pageMatches[pageIndex] = matches;
this.updatePage(pageIndex);
if (this.resumePageIdx === pageIndex) {
this.resumePageIdx = null;
this.nextPageMatch();
}
},
for (var i = 0, len = textItems.length; i < len; i++) { extractText: function PDFFindController_extractText() {
str.push(textItems[i].str); if (this.startedTextExtraction) {
} return;
}
this.startedTextExtraction = true;
// Store the pageContent as a string. this.pageContents = [];
self.pageContents.push(str.join('')); var extractTextPromisesResolves = [];
var numPages = this.pdfPageSource.pdfDocument.numPages;
for (var i = 0; i < numPages; i++) {
this.extractTextPromises.push(new Promise(function (resolve) {
extractTextPromisesResolves.push(resolve);
}));
}
extractTextPromisesResolves[pageIndex](pageIndex); var self = this;
if ((pageIndex + 1) < self.pdfPageSource.pages.length) { function extractPageText(pageIndex) {
extractPageText(pageIndex + 1); self.pdfPageSource.pages[pageIndex].getTextContent().then(
function textContentResolved(textContent) {
var textItems = textContent.items;
var str = [];
for (var i = 0, len = textItems.length; i < len; i++) {
str.push(textItems[i].str);
}
// Store the pageContent as a string.
self.pageContents.push(str.join(''));
extractTextPromisesResolves[pageIndex](pageIndex);
if ((pageIndex + 1) < self.pdfPageSource.pages.length) {
extractPageText(pageIndex + 1);
}
} }
} );
); }
} extractPageText(0);
extractPageText(0); },
},
handleEvent: function(e) { handleEvent: function PDFFindController_handleEvent(e) {
if (this.state === null || e.type !== 'findagain') { if (this.state === null || e.type !== 'findagain') {
this.dirtyMatch = true; this.dirtyMatch = true;
} }
this.state = e.detail; this.state = e.detail;
this.updateUIState(FindStates.FIND_PENDING); this.updateUIState(FindStates.FIND_PENDING);
this.firstPagePromise.then(function() {
this.extractText();
clearTimeout(this.findTimeout);
if (e.type === 'find') {
// Only trigger the find action after 250ms of silence.
this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
} else {
this.nextMatch();
}
}.bind(this));
},
this.firstPagePromise.then(function() { updatePage: function PDFFindController_updatePage(index) {
this.extractText(); var page = this.pdfPageSource.pages[index];
clearTimeout(this.findTimeout); if (this.selected.pageIdx === index) {
if (e.type === 'find') { // If the page is selected, scroll the page into view, which triggers
// Only trigger the find action after 250ms of silence. // rendering the page, which adds the textLayer. Once the textLayer is
this.findTimeout = setTimeout(this.nextMatch.bind(this), 250); // build, it will scroll onto the selected match.
} else { page.scrollIntoView();
this.nextMatch();
} }
}.bind(this));
},
updatePage: function(idx) { if (page.textLayer) {
var page = this.pdfPageSource.pages[idx]; page.textLayer.updateMatches();
}
},
nextMatch: function PDFFindController_nextMatch() {
var previous = this.state.findPrevious;
var currentPageIndex = this.pdfPageSource.page - 1;
var numPages = this.pdfPageSource.pages.length;
this.active = true;
if (this.dirtyMatch) {
// Need to recalculate the matches, reset everything.
this.dirtyMatch = false;
this.selected.pageIdx = this.selected.matchIdx = -1;
this.offset.pageIdx = currentPageIndex;
this.offset.matchIdx = null;
this.hadMatch = false;
this.resumePageIdx = null;
this.pageMatches = [];
var self = this;
for (var i = 0; i < numPages; i++) {
// Wipe out any previous highlighted matches.
this.updatePage(i);
// As soon as the text is extracted start finding the matches.
if (!(i in this.pendingFindMatches)) {
this.pendingFindMatches[i] = true;
this.extractTextPromises[i].then(function(pageIdx) {
delete self.pendingFindMatches[pageIdx];
self.calcFindMatch(pageIdx);
});
}
}
}
if (this.selected.pageIdx === idx) { // If there's no query there's no point in searching.
// If the page is selected, scroll the page into view, which triggers if (this.state.query === '') {
// rendering the page, which adds the textLayer. Once the textLayer is this.updateUIState(FindStates.FIND_FOUND);
// build, it will scroll onto the selected match. return;
page.scrollIntoView(); }
}
if (page.textLayer) { // If we're waiting on a page, we return since we can't do anything else.
page.textLayer.updateMatches(); if (this.resumePageIdx) {
} return;
}, }
nextMatch: function() {
var previous = this.state.findPrevious;
var currentPageIndex = this.pdfPageSource.page - 1;
var numPages = this.pdfPageSource.pages.length;
this.active = true;
if (this.dirtyMatch) {
// Need to recalculate the matches, reset everything.
this.dirtyMatch = false;
this.selected.pageIdx = this.selected.matchIdx = -1;
this.offset.pageIdx = currentPageIndex;
this.offset.matchIdx = null;
this.hadMatch = false;
this.resumePageIdx = null;
this.pageMatches = [];
var self = this;
for (var i = 0; i < numPages; i++) { var offset = this.offset;
// Wipe out any previous highlighted matches. // If there's already a matchIdx that means we are iterating through a
this.updatePage(i); // page's matches.
if (offset.matchIdx !== null) {
// As soon as the text is extracted start finding the matches. var numPageMatches = this.pageMatches[offset.pageIdx].length;
if (!(i in this.pendingFindMatches)) { if ((!previous && offset.matchIdx + 1 < numPageMatches) ||
this.pendingFindMatches[i] = true; (previous && offset.matchIdx > 0)) {
this.extractTextPromises[i].then(function(pageIdx) { // The simple case; we just have advance the matchIdx to select
delete self.pendingFindMatches[pageIdx]; // the next match on the page.
self.calcFindMatch(pageIdx); this.hadMatch = true;
}); offset.matchIdx = (previous ? offset.matchIdx - 1 :
offset.matchIdx + 1);
this.updateMatch(true);
return;
} }
// We went beyond the current page's matches, so we advance to
// the next page.
this.advanceOffsetPage(previous);
} }
} // Start searching through the page.
this.nextPageMatch();
// If there's no query there's no point in searching. },
if (this.state.query === '') {
this.updateUIState(FindStates.FIND_FOUND);
return;
}
// If we're waiting on a page, we return since we can't do anything else. matchesReady: function PDFFindController_matchesReady(matches) {
if (this.resumePageIdx) { var offset = this.offset;
return; var numMatches = matches.length;
} var previous = this.state.findPrevious;
var offset = this.offset; if (numMatches) {
// If there's already a matchIdx that means we are iterating through a // There were matches for the page, so initialize the matchIdx.
// page's matches.
if (offset.matchIdx !== null) {
var numPageMatches = this.pageMatches[offset.pageIdx].length;
if ((!previous && offset.matchIdx + 1 < numPageMatches) ||
(previous && offset.matchIdx > 0)) {
// The simple case; we just have advance the matchIdx to select
// the next match on the page.
this.hadMatch = true; this.hadMatch = true;
offset.matchIdx = (previous ? offset.matchIdx - 1 : offset.matchIdx = (previous ? numMatches - 1 : 0);
offset.matchIdx + 1);
this.updateMatch(true); this.updateMatch(true);
return true;
} else {
// No matches, so attempt to search the next page.
this.advanceOffsetPage(previous);
if (offset.wrapped) {
offset.matchIdx = null;
if (!this.hadMatch) {
// No point in wrapping, there were no matches.
this.updateMatch(false);
// while matches were not found, searching for a page
// with matches should nevertheless halt.
return true;
}
}
// Matches were not found (and searching is not done).
return false;
}
},
nextPageMatch: function PDFFindController_nextPageMatch() {
if (this.resumePageIdx !== null) {
console.error('There can only be one pending page.');
}
do {
var pageIdx = this.offset.pageIdx;
var matches = this.pageMatches[pageIdx];
if (!matches) {
// The matches don't exist yet for processing by "matchesReady",
// so set a resume point for when they do exist.
this.resumePageIdx = pageIdx;
break;
}
} while (!this.matchesReady(matches));
},
advanceOffsetPage: function PDFFindController_advanceOffsetPage(previous) {
var offset = this.offset;
var numPages = this.extractTextPromises.length;
offset.pageIdx = (previous ? offset.pageIdx - 1 : offset.pageIdx + 1);
offset.matchIdx = null;
if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
offset.pageIdx = (previous ? numPages - 1 : 0);
offset.wrapped = true;
return; return;
} }
// We went beyond the current page's matches, so we advance to },
// the next page.
this.advanceOffsetPage(previous); updateMatch: function PDFFindController_updateMatch(found) {
} var state = FindStates.FIND_NOTFOUND;
// Start searching through the page. var wrapped = this.offset.wrapped;
this.nextPageMatch(); this.offset.wrapped = false;
},
if (found) {
matchesReady: function(matches) { var previousPage = this.selected.pageIdx;
var offset = this.offset; this.selected.pageIdx = this.offset.pageIdx;
var numMatches = matches.length; this.selected.matchIdx = this.offset.matchIdx;
var previous = this.state.findPrevious; state = (wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND);
if (numMatches) { // Update the currently selected page to wipe out any selected matches.
// There were matches for the page, so initialize the matchIdx. if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
this.hadMatch = true; this.updatePage(previousPage);
offset.matchIdx = (previous ? numMatches - 1 : 0);
this.updateMatch(true);
return true;
} else {
// No matches, so attempt to search the next page.
this.advanceOffsetPage(previous);
if (offset.wrapped) {
offset.matchIdx = null;
if (!this.hadMatch) {
// No point in wrapping, there were no matches.
this.updateMatch(false);
// while matches were not found, searching for a page
// with matches should nevertheless halt.
return true;
} }
} }
// Matches were not found (and searching is not done).
return false;
}
},
nextPageMatch: function() { this.updateUIState(state, this.state.findPrevious);
if (this.resumePageIdx !== null) { if (this.selected.pageIdx !== -1) {
console.error('There can only be one pending page.'); this.updatePage(this.selected.pageIdx, true);
}
do {
var pageIdx = this.offset.pageIdx;
var matches = this.pageMatches[pageIdx];
if (!matches) {
// The matches don't exist yet for processing by "matchesReady",
// so set a resume point for when they do exist.
this.resumePageIdx = pageIdx;
break;
} }
} while (!this.matchesReady(matches)); },
},
advanceOffsetPage: function(previous) {
var offset = this.offset;
var numPages = this.extractTextPromises.length;
offset.pageIdx = (previous ? offset.pageIdx - 1 : offset.pageIdx + 1);
offset.matchIdx = null;
if (offset.pageIdx >= numPages || offset.pageIdx < 0) {
offset.pageIdx = (previous ? numPages - 1 : 0);
offset.wrapped = true;
return;
}
},
updateMatch: function(found) {
var state = FindStates.FIND_NOTFOUND;
var wrapped = this.offset.wrapped;
this.offset.wrapped = false;
if (found) {
var previousPage = this.selected.pageIdx;
this.selected.pageIdx = this.offset.pageIdx;
this.selected.matchIdx = this.offset.matchIdx;
state = (wrapped ? FindStates.FIND_WRAPPED : FindStates.FIND_FOUND);
// Update the currently selected page to wipe out any selected matches.
if (previousPage !== -1 && previousPage !== this.selected.pageIdx) {
this.updatePage(previousPage);
}
}
this.updateUIState(state, this.state.findPrevious);
if (this.selected.pageIdx !== -1) {
this.updatePage(this.selected.pageIdx, true);
}
},
updateUIState: function(state, previous) { updateUIState: function PDFFindController_updateUIState(state, previous) {
if (this.integratedFind) { if (this.integratedFind) {
FirefoxCom.request('updateFindControlState', FirefoxCom.request('updateFindControlState',
{ result: state, findPrevious: previous }); { result: state, findPrevious: previous });
return; return;
}
if (this.findBar === null) {
throw new Error('PDFFindController is not initialized with a ' +
'PDFFindBar instance.');
}
this.findBar.updateUIState(state, previous);
} }
PDFFindBar.updateUIState(state, previous); };
} return PDFFindController;
}; })();

4
web/text_layer_builder.js

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals CustomStyle, PDFFindController, scrollIntoView, PDFJS */ /* globals PDFView, CustomStyle, scrollIntoView, PDFJS */
'use strict'; 'use strict';
@ -39,7 +39,7 @@ var TextLayerBuilder = (function TextLayerBuilderClosure() {
this.viewport = options.viewport; this.viewport = options.viewport;
this.isViewerInPresentationMode = options.isViewerInPresentationMode; this.isViewerInPresentationMode = options.isViewerInPresentationMode;
this.textDivs = []; this.textDivs = [];
this.findController = window.PDFFindController || null; this.findController = PDFView.findController || null;
} }
TextLayerBuilder.prototype = { TextLayerBuilder.prototype = {

41
web/viewer.js

@ -14,13 +14,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals PDFJS, PDFBug, FirefoxCom, Stats, Cache, PDFFindBar, CustomStyle, /* globals PDFJS, PDFBug, FirefoxCom, Stats, Cache, CustomStyle, ProgressBar,
PDFFindController, ProgressBar, TextLayerBuilder, DownloadManager, DownloadManager, getFileName, scrollIntoView, getPDFFileNameFromURL,
getFileName, scrollIntoView, getPDFFileNameFromURL, PDFHistory, PDFHistory, Preferences, SidebarView, ViewHistory, PageView,
Preferences, SidebarView, ViewHistory, PageView, ThumbnailView, URL, ThumbnailView, URL, noContextMenuHandler, SecondaryToolbar,
noContextMenuHandler, SecondaryToolbar, PasswordPrompt, PasswordPrompt, PresentationMode, HandTool, Promise,
PresentationMode, HandTool, Promise, DocumentProperties, DocumentProperties, DocumentOutlineView, DocumentAttachmentsView,
DocumentOutlineView, DocumentAttachmentsView, OverlayManager */ OverlayManager, PDFFindController, PDFFindBar */
'use strict'; 'use strict';
@ -145,7 +145,12 @@ var PDFView = {
Preferences.initialize(); Preferences.initialize();
PDFFindBar.initialize({ this.findController = new PDFFindController({
pdfPageSource: this,
integratedFind: this.supportsIntegratedFind
});
this.findBar = new PDFFindBar({
bar: document.getElementById('findbar'), bar: document.getElementById('findbar'),
toggleButton: document.getElementById('viewFind'), toggleButton: document.getElementById('viewFind'),
findField: document.getElementById('findInput'), findField: document.getElementById('findInput'),
@ -154,13 +159,11 @@ var PDFView = {
findMsg: document.getElementById('findMsg'), findMsg: document.getElementById('findMsg'),
findStatusIcon: document.getElementById('findStatusIcon'), findStatusIcon: document.getElementById('findStatusIcon'),
findPreviousButton: document.getElementById('findPrevious'), findPreviousButton: document.getElementById('findPrevious'),
findNextButton: document.getElementById('findNext') findNextButton: document.getElementById('findNext'),
findController: this.findController
}); });
PDFFindController.initialize({ this.findController.setFindBar(this.findBar);
pdfPageSource: this,
integratedFind: this.supportsIntegratedFind
});
HandTool.initialize({ HandTool.initialize({
container: container, container: container,
@ -939,7 +942,7 @@ var PDFView = {
}; };
} }
PDFFindController.reset(); PDFView.findController.reset();
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
@ -1028,7 +1031,7 @@ var PDFView = {
PDFView.loadingBar.setWidth(container); PDFView.loadingBar.setWidth(container);
PDFFindController.resolveFirstPage(); PDFView.findController.resolveFirstPage();
// Initialize the browsing history. // Initialize the browsing history.
PDFHistory.initialize(self.documentFingerprint); PDFHistory.initialize(self.documentFingerprint);
@ -2237,13 +2240,13 @@ window.addEventListener('keydown', function keydown(evt) {
switch (evt.keyCode) { switch (evt.keyCode) {
case 70: // f case 70: // f
if (!PDFView.supportsIntegratedFind) { if (!PDFView.supportsIntegratedFind) {
PDFFindBar.open(); PDFView.findBar.open();
handled = true; handled = true;
} }
break; break;
case 71: // g case 71: // g
if (!PDFView.supportsIntegratedFind) { if (!PDFView.supportsIntegratedFind) {
PDFFindBar.dispatchEvent('again', cmd === 5 || cmd === 12); PDFView.findBar.dispatchEvent('again', cmd === 5 || cmd === 12);
handled = true; handled = true;
} }
break; break;
@ -2344,8 +2347,8 @@ window.addEventListener('keydown', function keydown(evt) {
SecondaryToolbar.close(); SecondaryToolbar.close();
handled = true; handled = true;
} }
if (!PDFView.supportsIntegratedFind && PDFFindBar.opened) { if (!PDFView.supportsIntegratedFind && PDFView.findBar.opened) {
PDFFindBar.close(); PDFView.findBar.close();
handled = true; handled = true;
} }
break; break;

Loading…
Cancel
Save