From 54dfe05e306702ef4e8f7d03c1094900d23d83f5 Mon Sep 17 00:00:00 2001
From: Jonas <Snuffleupagus@server.fake>
Date: Thu, 5 Sep 2013 00:25:19 +0200
Subject: [PATCH] Prevent searching during load from breaking the find
 functionality

---
 web/pdf_find_controller.js | 20 ++++++++++++--------
 web/viewer.js              |  4 ++--
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js
index b55acbe23..4778a565e 100644
--- a/web/pdf_find_controller.js
+++ b/web/pdf_find_controller.js
@@ -64,6 +64,8 @@ var PDFFindController = {
 
   integratedFind: false,
 
+  firstPagePromise: new PDFJS.Promise(),
+
   initialize: function(options) {
     if(typeof PDFFindBar === 'undefined' || PDFFindBar === null) {
         throw 'PDFFindController cannot be initialized ' +
@@ -172,15 +174,17 @@ var PDFFindController = {
     this.state = e.detail;
     this.updateUIState(FindStates.FIND_PENDING);
 
-    this.extractText();
+    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();
-    }
+      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));
   },
 
   updatePage: function(idx) {
diff --git a/web/viewer.js b/web/viewer.js
index 843bd2625..003e549f9 100644
--- a/web/viewer.js
+++ b/web/viewer.js
@@ -162,8 +162,6 @@ var Settings = (function SettingsClosure() {
 var cache = new Cache(CACHE_SIZE);
 var currentPageNumber = 1;
 
-// TODO: Enable the FindBar *AFTER* the pagesPromise in the load function
-// got resolved
 //#include pdf_find_bar.js
 //#include pdf_find_controller.js
 //#include pdf_history.js
@@ -941,6 +939,8 @@ var PDFView = {
         pagePromises.push(pagePromise);
       }
 
+      PDFFindController.firstPagePromise.resolve();
+
       PDFJS.Promise.all(pagePromises).then(function(pages) {
         pagesPromise.resolve(pages);
       });