internal,static: more results link jumps to new results

Clicking the more results link will load the
additional results with the document scrolled
to the last result from the previous page.

Change-Id: I58d6d2a608b9069ba6f2e6c604d5acdf8c16d92f
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/347407
Trust: Jamal Carvalho <jamal@golang.org>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/frontend/paginate.go b/internal/frontend/paginate.go
index 37ac83d..c7746a6 100644
--- a/internal/frontend/paginate.go
+++ b/internal/frontend/paginate.go
@@ -12,8 +12,6 @@
 	"golang.org/x/pkgsite/internal/log"
 )
 
-const maxLimit = 100
-
 // pagination holds information related to paginated display. It is intended to
 // be part of a view model struct.
 //
@@ -21,18 +19,19 @@
 // database query), we paginate it by dividing it into numbered pages
 // 1, 2, 3, .... Each page except possibly the last has the same number of results.
 type pagination struct {
-	baseURL     *url.URL // URL common to all pages
-	Limit       int      // the number of results requested on a page
-	MaxLimit    int      // the maximum number of results allowed for any request
-	ResultCount int      // number of results on this page
-	TotalCount  int      // total number of results
-	Approximate bool     // whether or not the total count is approximate
-	Page        int      // number of the current page
-	PrevPage    int      //   "    "   "  previous page, usually Page-1 but zero if Page == 1
-	NextPage    int      //   "    "   "  next page, usually Page+1, but zero on the last page
-	Offset      int      // offset of the first item on the current page
-	Pages       []int    // consecutive page numbers to be displayed for navigation
-	Limits      []int    // limits to be displayed
+	baseURL      *url.URL // URL common to all pages
+	Limit        int      // the number of results requested on a page
+	DefaultLimit int      // the default search limit
+	MaxLimit     int      // the maximum number of results allowed for any request
+	ResultCount  int      // number of results on this page
+	TotalCount   int      // total number of results
+	Approximate  bool     // whether or not the total count is approximate
+	Page         int      // number of the current page
+	PrevPage     int      //   "    "   "  previous page, usually Page-1 but zero if Page == 1
+	NextPage     int      //   "    "   "  next page, usually Page+1, but zero on the last page
+	Offset       int      // offset of the first item on the current page
+	Pages        []int    // consecutive page numbers to be displayed for navigation
+	Limits       []int    // limits to be displayed
 }
 
 // PageURL constructs a URL that displays the given page.
@@ -67,17 +66,17 @@
 // totalCount is the total number of results.
 func newPagination(params paginationParams, resultCount, totalCount int) pagination {
 	return pagination{
-		baseURL:     params.baseURL,
-		TotalCount:  totalCount,
-		ResultCount: resultCount,
-		Offset:      params.offset(),
-		Limit:       params.limit,
-		MaxLimit:    maxLimit,
-		Page:        params.page,
-		PrevPage:    prev(params.page),
-		NextPage:    next(params.page, params.limit, totalCount),
-		Pages:       pagesToLink(params.page, numPages(params.limit, totalCount), defaultNumPagesToLink),
-		Limits:      []int{10, 30, maxSearchPageSize},
+		baseURL:      params.baseURL,
+		TotalCount:   totalCount,
+		ResultCount:  resultCount,
+		Offset:       params.offset(),
+		Limit:        params.limit,
+		DefaultLimit: defaultSearchLimit,
+		MaxLimit:     maxSearchPageSize,
+		Page:         params.page,
+		PrevPage:     prev(params.page),
+		NextPage:     next(params.page, params.limit, totalCount),
+		Pages:        pagesToLink(params.page, numPages(params.limit, totalCount), defaultNumPagesToLink),
 	}
 }
 
diff --git a/internal/frontend/search_test.go b/internal/frontend/search_test.go
index 415119b..1d076d1 100644
--- a/internal/frontend/search_test.go
+++ b/internal/frontend/search_test.go
@@ -171,15 +171,15 @@
 			query: "foo bar",
 			wantSearchPage: &SearchPage{
 				Pagination: pagination{
-					TotalCount:  1,
-					ResultCount: 1,
-					PrevPage:    0,
-					NextPage:    0,
-					Limit:       20,
-					MaxLimit:    100,
-					Page:        1,
-					Pages:       []int{1},
-					Limits:      []int{10, 30, 100},
+					TotalCount:   1,
+					ResultCount:  1,
+					PrevPage:     0,
+					NextPage:     0,
+					Limit:        20,
+					DefaultLimit: 10,
+					MaxLimit:     100,
+					Page:         1,
+					Pages:        []int{1},
 				},
 				Results: []*SearchResult{
 					{
@@ -199,15 +199,15 @@
 			query: "package",
 			wantSearchPage: &SearchPage{
 				Pagination: pagination{
-					TotalCount:  1,
-					ResultCount: 1,
-					PrevPage:    0,
-					NextPage:    0,
-					Limit:       20,
-					MaxLimit:    100,
-					Page:        1,
-					Pages:       []int{1},
-					Limits:      []int{10, 30, 100},
+					TotalCount:   1,
+					ResultCount:  1,
+					PrevPage:     0,
+					NextPage:     0,
+					Limit:        20,
+					DefaultLimit: 10,
+					MaxLimit:     100,
+					Page:         1,
+					Pages:        []int{1},
 				},
 				Results: []*SearchResult{
 					{
diff --git a/static/frontend/search/search.tmpl b/static/frontend/search/search.tmpl
index fd7f596..8ecb5f5 100644
--- a/static/frontend/search/search.tmpl
+++ b/static/frontend/search/search.tmpl
@@ -52,7 +52,8 @@
   <div>
     {{$query := .Query}}
     {{range $i, $r := .Results}}
-      <div class="SearchSnippet">
+      {{$moreLink := eq $i (subtract $.Pagination.DefaultLimit 1)}}
+      <div class="SearchSnippet" {{if $moreLink}}id="more-results"{{end}}>
         <div class="SearchSnippet-headerContainer">
           <h2>
             <a href="{{.SymbolLink}}" data-gtmc="symbol search result symbol" data-gtmv="{{$i}}"
@@ -99,7 +100,8 @@
   <div>
     {{$query := .Query}}
     {{range $i, $v := .Results}}
-      <div class="SearchSnippet">
+      {{$moreLink := eq $i (subtract $.Pagination.DefaultLimit 1)}}
+      <div class="SearchSnippet" {{if eq $i 9}}id="more-results"{{end}}>
         <div class="SearchSnippet-headerContainer">
           <h2>
             <a href="/{{$v.PackagePath}}" data-gtmc="search result" data-gtmv="{{$i}}"
@@ -180,7 +182,7 @@
     Didn't find what you were looking for?
     {{$m := or $.SearchMode .SearchModePackage}}
     {{- if and (lt $p.Limit $p.MaxLimit) (eq $p.Limit (len .Results)) -}}
-      <a href="{{$p.URL $p.MaxLimit $m ""}}" data-gtmc="search more results">Show more results.</a>
+      <a href="{{$p.URL $p.MaxLimit $m ""}}#more-results" data-gtmc="search more results">Show more results.</a>
     {{- else -}}
       See <a href="/search-help" data-gtmc="search help"> search help.</a>
     {{- end -}}