From 00a9a169679baa011c66b28152ff27dec2532907 Mon Sep 17 00:00:00 2001
From: monikahedman <monika_hedman@brown.edu>
Date: Fri, 12 Jul 2019 18:15:31 -0400
Subject: end of day 7/12

---
 src/client/views/search/SearchBox.tsx | 175 +++++++++++-----------------------
 1 file changed, 54 insertions(+), 121 deletions(-)

(limited to 'src/client/views/search/SearchBox.tsx')

diff --git a/src/client/views/search/SearchBox.tsx b/src/client/views/search/SearchBox.tsx
index 8e5d317f0..e5280fe8c 100644
--- a/src/client/views/search/SearchBox.tsx
+++ b/src/client/views/search/SearchBox.tsx
@@ -30,14 +30,12 @@ export class SearchBox extends React.Component {
     //temp
     @observable public _maxNum: number = 10;
     @observable private _visibleElements: JSX.Element[] = [];
-    @observable private _scrollY: number = 0;
+    // @observable private _sc rollY: number = 0;
 
     private _isSearch: ("search" | "placeholder" | undefined)[] = [];
-    private _currentIndex = 0;
     private _numTotalResults = -1;
     private _startIndex = -1;
     private _endIndex = -1;
-    private _fetchedIndices: number[] = [0];
 
     static Instance: SearchBox;
 
@@ -68,7 +66,6 @@ export class SearchBox extends React.Component {
         this._openNoResults = false;
         this._results = [];
         this._visibleElements = [];
-        this._currentIndex = 0;
         this._numTotalResults = -1;
         this._startIndex = -1;
         this._endIndex = -1;
@@ -97,120 +94,81 @@ export class SearchBox extends React.Component {
 
     @action
     submitSearch = async () => {
-        let query = this._searchString; // searchbox gets query
-        let results: Doc[];
-
+        let query = this._searchString;
         query = FilterBox.Instance.getFinalQuery(query);
-
-        // if (this._curRequest !== undefined) {
-        //     this._curRequest.then(() => {
-        //         this._curRequest = undefined;
-        //         this._results = [];
-        //     });
-        // }
-
-        // this._results = [];
+        this._results = [];
+        this._isSearch = [];
+        this._visibleElements = [];
 
         //if there is no query there should be no result
         if (query === "") {
-            // results = [];
             return;
         }
         else {
-            //gets json result into a list of documents that can be used
-            //these are filtered by type
-            // this._results = [];
-            this._currentIndex = 0;
             this._startIndex = 0;
             this._endIndex = 12;
-            this._results = [];
-            // this._curRequest = undefined;
-            // if (this._curRequest !== undefined) {
-            //     this._curRequest.then(() => {
-            //         this._curRequest = undefined;
-            //     });
-            // }
-
             this._maxSearchIndex = 0;
             this._numTotalResults = -1;
-
-            // results = await this.getResultsHelp(query);
-            await this.getResultsHelp(query);
+            await this.getResults(query);
         }
 
         runInAction(() => {
             this._resultsOpen = true;
-            // this._results = results;
             this._openNoResults = true;
             this.resultsScrolled();
         });
-    }
 
-    getResultsHelp = async (query: string) => {
-        // docs length = this._results.length --> number of docs that are shown (after filtering)
-        // stops looking once this._results.length >= maxDisplayIndex
-        // max search index = number of results looked through in solr (solr index) --> increments of 10
-        // max display index = number of documents that SHOULD be shown (should include buffer), this._endIndex + buffer (= 4)
-        // currentRequest = promise | undefined, if undefined, can run and look for more. If not undefined, then there is a request in progress and it cannot look for more yet
-
-        // let buffer = 4;
-        // let maxDisplayIndex: number = this._endIndex + buffer;
-        // console.log(`end index: ${this._endIndex}`)
-        // console.log(this._results.length)
-
-        while (this._results.length < this._endIndex && (this._numTotalResults === -1 || this._maxSearchIndex < this._numTotalResults)) {
-            console.log("looping");
-            //start at max search index, get 10, add 10 to max search index
-            // const { docs, numFound } = await SearchUtil.Search(query, true, this._maxSearchIndex, 10);
-
-            // happens at the beginning
-            // am i gonna need this?
-            // if (numFound !== this._numTotalResults && this._numTotalResults === 0) {
-            //     this._numTotalResults = numFound;
-            // }
-
-            let prom: Promise<any>;
-            if (this._curRequest) {
-                prom = this._curRequest;
-                return;
-            } else {
-                prom = SearchUtil.Search(query, true, this._maxSearchIndex, 10);
-                this._maxSearchIndex += 10;
-            }
-            prom.then(action((res: SearchUtil.DocSearchResult) => {
+        console.log(this._results)
+    }
 
-                // happens at the beginning
-                if (res.numFound !== this._numTotalResults && this._numTotalResults === -1) {
-                    this._numTotalResults = res.numFound;
-                }
+    getResults = async (query: string, all: boolean = false) => {
 
-                let filteredDocs = FilterBox.Instance.filterDocsByType(res.docs);
-                this._results.push(...filteredDocs);
+        if (all) {
+            // let { numFound, docs } = await SearchUtil.Search(query, true, this._maxSearchIndex, 100000);
+            let prom: Promise<any> = SearchUtil.Search(query, true, 0, 100000);
 
-                console.log(this._results);
-                if (prom === this._curRequest) {
-                    this._curRequest = undefined;
+            prom.then(({ docs, numFound }) => {
+                this._results = docs;
+            })
+        }
+        else {
+            while (this._results.length <= this._endIndex && (this._numTotalResults === -1 || this._maxSearchIndex < this._numTotalResults)) {
+
+                let prom: Promise<any>;
+                if (this._curRequest) {
+                    prom = this._curRequest;
+                    return;
+                } else {
+                    prom = SearchUtil.Search(query, true, this._maxSearchIndex, 10);
+                    this._maxSearchIndex += 10;
                 }
-                console.log("setting to undefined");
-            }));
+                prom.then(action((res: SearchUtil.DocSearchResult) => {
 
+                    // happens at the beginning
+                    if (res.numFound !== this._numTotalResults && this._numTotalResults === -1) {
+                        this._numTotalResults = res.numFound;
+                    }
 
+                    let filteredDocs = FilterBox.Instance.filterDocsByType(res.docs);
+                    this._results.push(...filteredDocs);
 
-            this._curRequest = prom;
+                    if (prom === this._curRequest) {
+                        this._curRequest = undefined;
+                    }
+                }));
 
-            await prom;
+                this._curRequest = prom;
 
-            //deals with if there are fewer results than can be scrolled through
-            // if (this._numTotalResults < this._endIndex) {
-            //     break;
-            // }
+                await prom;
+            }
         }
     }
 
     collectionRef = React.createRef<HTMLSpanElement>();
     startDragCollection = async () => {
-        // const results = await this.getResults(FilterBox.Instance.getFinalQuery(this._searchString), -1);
-        await this.getResultsHelp(FilterBox.Instance.getFinalQuery(this._searchString));
+        await this.getResults(FilterBox.Instance.getFinalQuery(this._searchString), true);
+        console.log(this._results)
+        // if (res !== undefined) {
         const docs = this._results.map(doc => {
             const isProto = Doc.GetT(doc, "isPrototype", "boolean", true);
             if (isProto) {
@@ -244,6 +202,7 @@ export class SearchBox extends React.Component {
             }
         }
         return Docs.FreeformDocument(docs, { width: 400, height: 400, panX: 175, panY: 175, backgroundColor: "grey", title: `Search Docs: "${this._searchString}"` });
+        // }
     }
 
     @action.bound
@@ -257,7 +216,6 @@ export class SearchBox extends React.Component {
 
     @action.bound
     closeSearch = () => {
-        console.log("closing search")
         FilterBox.Instance.closeFilter();
         this.closeResults();
     }
@@ -267,7 +225,6 @@ export class SearchBox extends React.Component {
         this._resultsOpen = false;
         this._results = [];
         this._visibleElements = [];
-        this._currentIndex = 0;
         this._numTotalResults = -1;
         this._startIndex = -1;
         this._endIndex = -1;
@@ -275,21 +232,11 @@ export class SearchBox extends React.Component {
     }
 
     resultsScrolled = flow(function* (this: SearchBox, e?: React.UIEvent<HTMLDivElement>) {
-        console.log("_________________________________________________________________________________________________________")
         let scrollY = e ? e.currentTarget.scrollTop : 0;
         let buffer = 4;
-        console.log(`start before: ${this._startIndex}`);
         let startIndex = Math.floor(Math.max(0, scrollY / 70 - buffer));
-        console.log(`end before: ${this._endIndex}`);
         let endIndex = Math.ceil(Math.min(this._numTotalResults - 1, startIndex + (560 / 70) + buffer));
 
-        // if (startIndex === this._startIndex && endIndex === this._endIndex && this._results.length > this._endIndex) {
-        //     console.log("returning")
-        //     return;
-        // }
-        console.log(`START: ${startIndex}`);
-        console.log(`END: ${endIndex}`);
-
         this._startIndex = startIndex === -1 ? 0 : startIndex;
         this._endIndex = endIndex === -1 ? 12 : endIndex;
 
@@ -298,6 +245,10 @@ export class SearchBox extends React.Component {
             return;
         }
 
+        if (this._numTotalResults <= this._maxSearchIndex) {
+            this._numTotalResults = this._results.length;
+        }
+
         // only hit right at the beginning
         // visibleElements is all of the elements (even the ones you can't see)
         else if (this._visibleElements.length !== this._numTotalResults) {
@@ -320,27 +271,12 @@ export class SearchBox extends React.Component {
                 if (this._isSearch[i] !== "search") {
                     let result: Doc | undefined = undefined;
                     if (i >= this._results.length) {
-                        // _________________________________________________________________________________________________
-                        // let results: Doc[] = yield this.getResults(this._searchString, i + 1 - this._results.length);
-
-                        // this updates this._results
-                        yield this.getResultsHelp(this._searchString);
-                        result = this._results[i];
+                        this.getResults(this._searchString);
+                        if (i < this._results.length) result = this._results[i];
                         if (result) {
                             this._visibleElements[i] = <SearchItem doc={result} key={result[Id]} />;
                             this._isSearch[i] = "search";
                         }
-                        // if (results.length !== 0) {
-                        //     runInAction(() => {
-                        //         // this._results.push(...results);
-                        //         result = this._results[i];
-                        //         if (result) {
-                        //             this._visibleElements[i] = <SearchItem doc={result} key={result[Id]} />;
-                        //             this._isSearch[i] = "search";
-                        //         }
-                        //     });
-                        // }
-                        // _________________________________________________________________________________________________
                     }
                     else {
                         result = this._results[i];
@@ -352,6 +288,10 @@ export class SearchBox extends React.Component {
                 }
             }
         }
+        if (this._maxSearchIndex >= this._numTotalResults) {
+            this._visibleElements.length = this._results.length;
+            this._isSearch.length = this._results.length;
+        }
     });
 
     render() {
@@ -367,15 +307,8 @@ export class SearchBox extends React.Component {
                     <button className="searchBox-barChild searchBox-filter" onClick={FilterBox.Instance.openFilter} onPointerDown={FilterBox.Instance.stopProp}>Filter</button>
                 </div>
                 <div className="searchBox-results" onScroll={this.resultsScrolled} style={this._resultsOpen ? { display: "flex" } : { display: "none" }}>
-                    {/* {(this._results.length !== 0) ? (
-                        this._results.map(result => <SearchItem doc={result} key={result[Id]} />)
-                    ) :
-                        this._openNoResults ? (<div className="no-result">No Search Results</div>) : null} */}
                     {this._visibleElements}
                 </div>
-                {/* <div style={this._results.length !== 0 ? { display: "flex" } : { display: "none" }}>
-                    <Pager />
-                </div> */}
             </div>
         );
     }
-- 
cgit v1.2.3-70-g09d2

