From 5b1305a1d33a61909322564110866032019d0f86 Mon Sep 17 00:00:00 2001 From: Taka Date: Sat, 21 Mar 2020 05:31:37 +0000 Subject: [PATCH] Scroll to an article when clicking on them within a publication. --- web/src/App.js | 1 + web/src/PublicationSearchResult.js | 19 +++++++++++++++---- web/src/SearchResults.js | 29 +++++++++++++++++++++++------ web/src/index.js | 18 +++++++++--------- 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/web/src/App.js b/web/src/App.js index 90661f2..ba4197e 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -112,6 +112,7 @@ export class App extends React.Component ) ; } diff --git a/web/src/PublicationSearchResult.js b/web/src/PublicationSearchResult.js index 3f37d1c..55c365a 100644 --- a/web/src/PublicationSearchResult.js +++ b/web/src/PublicationSearchResult.js @@ -55,10 +55,21 @@ export class PublicationSearchResult extends React.Component if ( this.props.data.articles ) { for ( let i=0 ; i < this.props.data.articles.length ; ++i ) { const article = this.props.data.articles[ i ] ; - articles.push( ) ; + if ( this.props.onArticleClick ) { + // forward clicks on the article to the parent + articles.push(
this.props.onArticleClick( article.article_id ) } + style = {{ cursor: "pointer" }} + title = "Go to this article." + /> ) ; + } else { + // handle clicks on the article normally + articles.push( ) ; + } } } diff --git a/web/src/SearchResults.js b/web/src/SearchResults.js index 03fa804..75ef031 100644 --- a/web/src/SearchResults.js +++ b/web/src/SearchResults.js @@ -1,4 +1,5 @@ import React from "react" ; +import ReactDOM from "react-dom" ; import "./SearchResults.css" ; import { PublisherSearchResult } from "./PublisherSearchResult" ; import { PublicationSearchResult } from "./PublicationSearchResult" ; @@ -11,12 +12,16 @@ export class SearchResults extends React.Component { render() { + let results ; - if ( this.props.searchResults && this.props.searchResults.error !== undefined ) + if ( this.props.searchResults && this.props.searchResults.error !== undefined ) { + // show the error message results = "ERROR: " + this.props.searchResults.error ; - else if ( ! this.props.searchResults || this.props.searchResults.length === 0 ) + } else if ( ! this.props.searchResults || this.props.searchResults.length === 0 ) { + // show that no search results were found results = (this.props.seqNo === 0) ? null :
No results.
; - else if ( this.props.searchResults === "(loading)" ) { + } else if ( this.props.searchResults === "(loading)" ) { + // show the loading spinner results = (
Loading...
) ; @@ -26,19 +31,31 @@ export class SearchResults extends React.Component elem.style.display = "block" ; }, 500 ) ; } else { + // track articles + let articleRefs = {} ; + function scrollToArticle( article_id ) { + const node = ReactDOM.findDOMNode( articleRefs[article_id] ) ; + if ( node ) + node.scrollIntoView() ; + else + document.location = gAppRef.makeAppUrl( "/article/" + article_id ) ; + } + // render the search results results = [] ; this.props.searchResults.forEach( sr => { if ( sr.type === "publisher" ) { results.push( this.onDeleteSearchResult( n, v ) } /> ) ; } else if ( sr.type === "publication" ) { results.push( this.onDeleteSearchResult( n, v ) } + onArticleClick = { this.props.type === "publication" ? (a) => scrollToArticle(a) : null } /> ) ; } else if ( sr.type === "article" ) { results.push( this.onDeleteSearchResult( n, v ) } + ref = { r => articleRefs[sr.article_id] = r } /> ) ; } else { gAppRef.logInternalError( "Unknown search result type.", "srType = "+sr.type ) ; diff --git a/web/src/index.js b/web/src/index.js index 6187d0a..0d0da57 100644 --- a/web/src/index.js +++ b/web/src/index.js @@ -10,32 +10,32 @@ import "./index.css" ; ReactDOM.render( - gAppRef._showPublishers() } /> } /> - gAppRef._showTechniqueArticles() } /> } /> - gAppRef._showTipsArticles() } /> } /> - gAppRef.runSpecialSearch( "/search/publisher/"+gAppRef.props.match.params.publId, null, () => gAppRef.setWindowTitleFromSearchResults( "publisher", "publ_id", gAppRef.props.match.params.publId, "publ_name" ) ) } /> } /> - gAppRef.runSpecialSearch( "/search/publication/"+gAppRef.props.match.params.pubId, null, () => gAppRef.setWindowTitleFromSearchResults( "publication", "pub_id", gAppRef.props.match.params.pubId, sr => { return PublicationSearchResult.makeDisplayName( sr ) } ) ) } /> } /> - gAppRef.runSpecialSearch( "/search/article/"+gAppRef.props.match.params.articleId, null, () => gAppRef.setWindowTitleFromSearchResults( "article", "article_id", gAppRef.props.match.params.articleId, "article_title" ) ) } /> } /> - gAppRef.runSpecialSearch( "/search/author/"+gAppRef.props.match.params.authorId, null, () => { const author = gAppRef.caches.authors[ gAppRef.props.match.params.authorId ] ; @@ -43,13 +43,13 @@ ReactDOM.render( } ) } /> } /> - gAppRef.runSpecialSearch( "/search/tag/"+gAppRef.props.match.params.tag, null, () => gAppRef.setWindowTitle( gAppRef.props.match.params.tag ) ) } /> } /> - } /> + } /> , document.getElementById( "app" )