From 1be27e3c814518e71625c73eb4357c378237b3bf Mon Sep 17 00:00:00 2001 From: Karl Denninghoff Date: Thu, 16 Jan 2014 13:58:29 -0800 Subject: [PATCH] Fixes but 960409 and adresses review comments including removal of do-while construct --- web/pdf_find_controller.js | 88 +++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js index 99b9849dc..4274a125e 100644 --- a/web/pdf_find_controller.js +++ b/web/pdf_find_controller.js @@ -52,8 +52,6 @@ var PDFFindController = { resumePageIdx: null, - resumeCallback: null, - state: null, dirtyMatch: false, @@ -66,7 +64,7 @@ var PDFFindController = { initialize: function(options) { if(typeof PDFFindBar === 'undefined' || PDFFindBar === null) { - throw 'PDFFindController cannot be initialized ' + + throw 'PDFFindController cannot be initialized ' + 'without a PDFFindController instance'; } @@ -126,10 +124,8 @@ var PDFFindController = { this.pageMatches[pageIndex] = matches; this.updatePage(pageIndex); if (this.resumePageIdx === pageIndex) { - var callback = this.resumeCallback; this.resumePageIdx = null; - this.resumeCallback = null; - callback(); + this.nextPageMatch(); } }, @@ -220,7 +216,6 @@ var PDFFindController = { this.offset.pageIdx = currentPageIndex; this.offset.matchIdx = null; this.hadMatch = false; - this.resumeCallback = null; this.resumePageIdx = null; this.pageMatches = []; var self = this; @@ -247,7 +242,7 @@ var PDFFindController = { } // If we're waiting on a page, we return since we can't do anything else. - if (this.resumeCallback) { + if (this.resumePageIdx) { return; } @@ -273,48 +268,51 @@ var PDFFindController = { this.nextPageMatch(); }, + matchesReady: function(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; + offset.matchIdx = previous ? numMatches - 1 : 0; + this.updateMatch(true); + // matches were found + return true; + } else { + // No matches 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() { if (this.resumePageIdx !== null) console.error('There can only be one pending page.'); - - var matchesReady = function(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; - offset.matchIdx = previous ? numMatches - 1 : 0; - this.updateMatch(true); - } else { - // No matches 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); - return; - } - } - // Search the next page. - this.nextPageMatch(); + // done boolean to remove do-while construct per review of PR 4131 + var done = false; + while (!done) { + 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; } - }.bind(this); - - var pageIdx = this.offset.pageIdx; - var pageMatches = this.pageMatches; - if (!pageMatches[pageIdx]) { - // The matches aren't ready setup a callback so we can be notified, - // when they are ready. - this.resumeCallback = function() { - matchesReady(pageMatches[pageIdx]); - }; - this.resumePageIdx = pageIdx; - return; + done = this.matchesReady(matches); } - // The matches are finished already. - matchesReady(pageMatches[pageIdx]); }, advanceOffsetPage: function(previous) {