Converting PDFFindBar and PDFFindController to classes

This commit is contained in:
Tim van der Meij 2014-06-26 00:15:22 +02:00
parent 2e98f9095e
commit dbe22475e1
5 changed files with 442 additions and 440 deletions

View File

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

View File

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

View File

@ -13,37 +13,37 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFFindBar, PDFJS, FindStates, FirefoxCom, Promise */
/* globals PDFJS, FindStates, FirefoxCom, Promise */
'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.
*/
var PDFFindController = {
startedTextExtraction: false,
extractTextPromises: [],
pendingFindMatches: {},
active: false, // If active, find results will be highlighted.
pageContents: [], // Stores the text for each page.
pageMatches: [],
selected: { // Currently selected match.
var PDFFindController = (function PDFFindControllerClosure() {
function PDFFindController(options) {
this.startedTextExtraction = false;
this.extractTextPromises = [];
this.pendingFindMatches = {};
this.active = false; // If active, find results will be highlighted.
this.pageContents = []; // Stores the text for each page.
this.pageMatches = [];
this.selected = { // Currently selected match.
pageIdx: -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,
matchIdx: null
},
resumePageIdx: null,
state: null,
dirtyMatch: false,
findTimeout: null,
pdfPageSource: null,
integratedFind: false,
charactersToNormalize: {
};
this.resumePageIdx = null;
this.state = null;
this.dirtyMatch = false;
this.findTimeout = null;
this.pdfPageSource = options.pdfPageSource || null;
this.integratedFind = options.integratedFind || false;
this.charactersToNormalize = {
'\u2018': '\'', // Left single quotation mark
'\u2019': '\'', // Right single quotation mark
'\u201A': '\'', // Single low-9 quotation mark
@ -55,16 +55,8 @@ var PDFFindController = {
'\u00BC': '1/4', // Vulgar fraction one quarter
'\u00BD': '1/2', // Vulgar fraction one half
'\u00BE': '3/4' // Vulgar fraction three quarters
},
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;
};
this.findBar = options.findBar || null;
// Compile the regular expression for text normalization once
var replace = Object.keys(this.charactersToNormalize).join('');
@ -85,29 +77,34 @@ var PDFFindController = {
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;
},
reset: function pdfFindControllerReset() {
reset: function PDFFindController_reset() {
this.startedTextExtraction = false;
this.extractTextPromises = [];
this.active = false;
},
normalize: function pdfFindControllerNormalize(text) {
normalize: function PDFFindController_normalize(text) {
var self = this;
return text.replace(this.normalizationRegex, function (ch) {
return PDFFindController.charactersToNormalize[ch];
return self.charactersToNormalize[ch];
});
},
calcFindMatch: function(pageIndex) {
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) {
// Do nothing: the matches should be wiped out already.
return;
return; // Do nothing: the matches should be wiped out already.
}
if (!caseSensitive) {
@ -132,7 +129,7 @@ var PDFFindController = {
}
},
extractText: function() {
extractText: function PDFFindController_extractText() {
if (this.startedTextExtraction) {
return;
}
@ -171,7 +168,7 @@ var PDFFindController = {
extractPageText(0);
},
handleEvent: function(e) {
handleEvent: function PDFFindController_handleEvent(e) {
if (this.state === null || e.type !== 'findagain') {
this.dirtyMatch = true;
}
@ -191,10 +188,10 @@ var PDFFindController = {
}.bind(this));
},
updatePage: function(idx) {
var page = this.pdfPageSource.pages[idx];
updatePage: function PDFFindController_updatePage(index) {
var page = this.pdfPageSource.pages[index];
if (this.selected.pageIdx === idx) {
if (this.selected.pageIdx === index) {
// If the page is selected, scroll the page into view, which triggers
// rendering the page, which adds the textLayer. Once the textLayer is
// build, it will scroll onto the selected match.
@ -206,7 +203,7 @@ var PDFFindController = {
}
},
nextMatch: function() {
nextMatch: function PDFFindController_nextMatch() {
var previous = this.state.findPrevious;
var currentPageIndex = this.pdfPageSource.page - 1;
var numPages = this.pdfPageSource.pages.length;
@ -273,10 +270,11 @@ var PDFFindController = {
this.nextPageMatch();
},
matchesReady: function(matches) {
matchesReady: function PDFFindController_matchesReady(matches) {
var offset = this.offset;
var numMatches = matches.length;
var previous = this.state.findPrevious;
if (numMatches) {
// There were matches for the page, so initialize the matchIdx.
this.hadMatch = true;
@ -301,7 +299,7 @@ var PDFFindController = {
}
},
nextPageMatch: function() {
nextPageMatch: function PDFFindController_nextPageMatch() {
if (this.resumePageIdx !== null) {
console.error('There can only be one pending page.');
}
@ -317,11 +315,12 @@ var PDFFindController = {
} while (!this.matchesReady(matches));
},
advanceOffsetPage: function(previous) {
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;
@ -329,10 +328,11 @@ var PDFFindController = {
}
},
updateMatch: function(found) {
updateMatch: function PDFFindController_updateMatch(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;
@ -343,19 +343,26 @@ var PDFFindController = {
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) {
FirefoxCom.request('updateFindControlState',
{ result: state, findPrevious: previous });
return;
}
PDFFindBar.updateUIState(state, previous);
if (this.findBar === null) {
throw new Error('PDFFindController is not initialized with a ' +
'PDFFindBar instance.');
}
};
this.findBar.updateUIState(state, previous);
}
};
return PDFFindController;
})();

View File

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

View File

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