From aa366aa31c0c6deef5a3d149f3467a6058948159 Mon Sep 17 00:00:00 2001 From: Marek Fajkus Date: Mon, 28 Dec 2020 23:18:56 +0100 Subject: [PATCH] Implement scroll to package (#257) --- src/Page/Options.elm | 5 ++++- src/Page/Packages.elm | 5 ++++- src/Search.elm | 19 ++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Page/Options.elm b/src/Page/Options.elm index ce9ed4f..725a51d 100644 --- a/src/Page/Options.elm +++ b/src/Page/Options.elm @@ -174,7 +174,10 @@ viewResultItem channel show item = -- DEBUG: ] -- DEBUG: ] |> List.append - (tr [ onClick (SearchMsg (Search.ShowDetails item.source.name)) ] + (tr + [ onClick (SearchMsg (Search.ShowDetails item.source.name)) + , Search.elementId item.source.name + ] [ td [] [ text item.source.name ] ] :: packageDetails diff --git a/src/Page/Packages.elm b/src/Page/Packages.elm index da030b7..5ba928c 100644 --- a/src/Page/Packages.elm +++ b/src/Page/Packages.elm @@ -221,7 +221,10 @@ viewResultItem channel show item = -- DEBUG: ] -- DEBUG: ] |> List.append - (tr [ onClick (SearchMsg (Search.ShowDetails item.source.attr_name)) ] + (tr + [ onClick (SearchMsg (Search.ShowDetails item.source.attr_name)) + , Search.elementId item.source.attr_name + ] [ td [] [ text <| item.source.attr_name ] , td [] [ text item.source.pname ] , td [] [ text item.source.pversion ] diff --git a/src/Search.elm b/src/Search.elm index 38359f4..d668c68 100644 --- a/src/Search.elm +++ b/src/Search.elm @@ -8,6 +8,7 @@ module Search exposing , channelDetailsFromId , channels , decodeResult + , elementId , fromSortId , init , makeRequest @@ -168,6 +169,11 @@ ensureLoading model = model +elementId : String -> Html.Attribute msg +elementId str = + Html.Attributes.id <| "result-" ++ str + + -- --------------------------- -- UPDATE @@ -185,6 +191,17 @@ type Msg a | ChangePage Int +scrollToEntry : Maybe String -> Cmd (Msg a) +scrollToEntry val = + let + doScroll id = + Browser.Dom.getElement ("result-" ++ id) + |> Task.andThen (\{ element } -> Browser.Dom.setViewport element.x element.y) + |> Task.attempt (always NoOp) + in + Maybe.withDefault Cmd.none <| Maybe.map doScroll val + + update : Route.SearchRoute -> Browser.Navigation.Key @@ -226,7 +243,7 @@ update toRoute navKey msg model = QueryResponse result -> ( { model | result = result } - , Cmd.none + , scrollToEntry model.show ) ShowDetails selected ->