Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add links for types from dependencies #358

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 74 additions & 18 deletions src/frontend/Page/Docs.elm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type alias Model =
, readme : Status String
, docs : Status (List Docs.Module)
, outline : Status Outline.PackageInfo
, resolvedDeps : List Session.ResolvedDep
}


Expand Down Expand Up @@ -79,25 +80,25 @@ init session author project version focus =
case Session.getReleases session author project of
Just releases ->
getInfo (Release.getLatestVersion releases) <|
Model session author project version focus "" (Success releases) Loading Loading Loading
Model session author project version focus "" (Success releases) Loading Loading Loading []

Nothing ->
( Model session author project version focus "" Loading Loading Loading Loading
( Model session author project version focus "" Loading Loading Loading Loading []
, Http.send GotReleases (Session.fetchReleases author project)
)


getInfo : V.Version -> Model -> ( Model, Cmd Msg )
getInfo latest model =
let
author = model.author
project = model.project
version = Maybe.withDefault latest model.version
maybeInfo =
Maybe.map3 (\a b c -> (a,b,c))
Maybe.map4 (\a b c d -> ((a,b),(c,d)))
(Session.getReadme model.session author project version)
(Session.getDocs model.session author project version)
(Session.getOutline model.session author project version)
(Session.getResolvedDeps model.session author project version)
in
case maybeInfo of
Nothing ->
Expand All @@ -109,16 +110,33 @@ getInfo latest model =
]
)

Just (readme, docs, outline) ->
Just ((readme, docs), (outline, resolvedDeps)) ->
( { model
| readme = Success readme
, docs = Success docs
, outline = Success outline
, resolvedDeps = resolvedDeps
}
, scrollIfNeeded model.focus
, Cmd.batch
[ scrollIfNeeded model.focus
, getDepsDocs model.session resolvedDeps
]
)


getDepsDocs : Session.Data -> List Session.ResolvedDep -> Cmd Msg
getDepsDocs session resolvedDeps =
resolvedDeps
|> List.filter (\resolvedDep ->
case Session.getDocs session resolvedDep.author resolvedDep.project resolvedDep.version of
Just _ -> False -- No need to fetch docs once again
Maybe.Nothing -> True)
|> List.map (\resolvedDep ->
Session.fetchDocs resolvedDep.author resolvedDep.project resolvedDep.version
|> Http.send (GotDepDocs resolvedDep))
|> Cmd.batch


scrollIfNeeded : Focus -> Cmd Msg
scrollIfNeeded focus =
case focus of
Expand All @@ -143,6 +161,8 @@ type Msg
| GotReadme V.Version (Result Http.Error String)
| GotDocs V.Version (Result Http.Error (List Docs.Module))
| GotOutline V.Version (Result Http.Error Outline.PackageInfo)
| GotResolvedDeps V.Version (List Session.ResolvedDep)
| GotDepDocs Session.ResolvedDep (Result Http.Error (List Docs.Module))


update : Msg -> Model -> ( Model, Cmd Msg )
Expand Down Expand Up @@ -207,6 +227,21 @@ update msg model =
, scrollIfNeeded model.focus
)

GotDepDocs resolvedDep result ->
case result of
Ok docs ->
( { model
| session = Session.addDocs resolvedDep.author resolvedDep.project resolvedDep.version docs model.session
}
, Cmd.none
)

_ ->
( model
, Cmd.none
)


GotOutline version result ->
case result of
Err _ ->
Expand All @@ -219,9 +254,16 @@ update msg model =
| outline = Success outline
, session = Session.addOutline model.author model.project version outline model.session
}
, Cmd.none
, Task.perform (GotResolvedDeps version) (Session.fetchResolvedDeps outline)
)

GotResolvedDeps version resolvedDeps ->
( { model
| resolvedDeps = resolvedDeps
, session = Session.addResolvedDeps model.author model.project version resolvedDeps model.session
}
, getDepsDocs model.session resolvedDeps
)


-- VIEW
Expand Down Expand Up @@ -394,10 +436,16 @@ viewContent model =
lazy viewReadme model.readme

About ->
lazy2 viewAbout model.outline model.releases
lazy3 viewAbout model.outline model.releases model.resolvedDeps

Module name tag ->
lazy5 viewModule model.author model.project model.version name model.docs
let depsDocs =
model.resolvedDeps
|> List.filterMap (\resolvedDep ->
Session.getDocs model.session resolvedDep.author resolvedDep.project resolvedDep.version
|> Maybe.map (Tuple.pair resolvedDep))
in
lazy6 viewModule model.author model.project model.version name model.docs depsDocs



Expand All @@ -423,15 +471,15 @@ viewReadme status =
-- VIEW MODULE


viewModule : String -> String -> Maybe V.Version -> String -> Status (List Docs.Module) -> Html msg
viewModule author project version name status =
viewModule : String -> String -> Maybe V.Version -> String -> Status (List Docs.Module) -> List (Session.ResolvedDep, List Docs.Module) -> Html msg
viewModule author project version name status depsDocs =
case status of
Success allDocs ->
case findModule name allDocs of
Just docs ->
let
header = h1 [class "block-list-title"] [ text name ]
info = Block.makeInfo author project version name allDocs
info = Block.makeInfo author project version name allDocs depsDocs
blocks = List.map (Block.view info) (Docs.toBlocks docs)
in
div [ class "block-list" ] (header :: blocks)
Expand Down Expand Up @@ -663,8 +711,8 @@ viewValueItem { author, project, version } moduleName ownerName valueName =
-- VIEW ABOUT


viewAbout : Status Outline.PackageInfo -> Status (OneOrMore Release.Release) -> Html msg
viewAbout outlineStatus releases =
viewAbout : Status Outline.PackageInfo -> Status (OneOrMore Release.Release) -> List Session.ResolvedDep -> Html msg
viewAbout outlineStatus releases resolvedDeps =
case outlineStatus of
Success outline ->
div [ class "block-list pkg-about" ]
Expand All @@ -689,7 +737,7 @@ viewAbout outlineStatus releases =
_ :: _ ->
div []
[ h1 [ style "margin-top" "2em", style "margin-bottom" "0.5em" ] [ text "Dependencies" ]
, table [] (List.map viewDependency outline.deps)
, table [] (List.map (viewDependency resolvedDeps) outline.deps)
]
]

Expand Down Expand Up @@ -745,13 +793,21 @@ toLicenseUrl outline =
[]


viewDependency : (Pkg.Name, C.Constraint) -> Html msg
viewDependency (pkg, constraint) =
viewDependency : List Session.ResolvedDep -> (Pkg.Name, C.Constraint) -> Html msg
viewDependency resolvedDeps (pkg, constraint) =
tr []
[ td []
[ case String.split "/" (Pkg.toString pkg) of
[author,project] ->
a [ href (Href.toVersion author project Nothing) ]
let depVersion =
resolvedDeps
|> List.filterMap (\resolvedDep ->
if resolvedDep.author == author && resolvedDep.project == project
then Just resolvedDep.version
else Nothing)
|> List.head
in
a [ href (Href.toVersion author project depVersion) ]
[ span [ class "light" ] [ text (author ++ "/") ]
, text project
]
Expand Down
73 changes: 61 additions & 12 deletions src/frontend/Page/Docs/Block.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Elm.Version as V
import Html exposing (..)
import Html.Attributes exposing (..)
import Href
import Session
import Utils.Markdown as Markdown


Expand Down Expand Up @@ -168,32 +169,57 @@ unionMore info =

-- INFO

type alias LinkInfo r =
{ r
| author : String
, project : String
, version : Maybe V.Version
, moduleName : String
}


type alias DepInfo =
{ resolvedDep : Session.ResolvedDep
, typeNameDict : TypeNameDict
}


type alias Info =
{ author : String
, project : String
, version : Maybe V.Version
, moduleName : String
, typeNameDict : TypeNameDict
, depsInfo : List DepInfo
}


type alias TypeNameDict =
Dict.Dict String (String, String)


makeInfo : String -> String -> Maybe V.Version -> String -> List Docs.Module -> Info
makeInfo author project version moduleName docsList =
typeNameDictFromDocs : List Docs.Module -> TypeNameDict
typeNameDictFromDocs docsList =
let
addUnion home union docs =
Dict.insert (home ++ "." ++ union.name) (home, union.name) docs
addEntry home entry docs =
Dict.insert (home ++ "." ++ entry.name) (home, entry.name) docs

addModule docs dict =
List.foldl (addUnion docs.name) dict docs.unions
dict
|> \unions -> List.foldl (addEntry docs.name) unions docs.unions
|> \aliases -> List.foldl (addEntry docs.name) aliases docs.aliases
in
Info author project version moduleName <|
List.foldl addModule Dict.empty docsList
List.foldl addModule Dict.empty docsList


makeInfo : String -> String -> Maybe V.Version -> String -> List Docs.Module -> List (Session.ResolvedDep, List Docs.Module) -> Info
makeInfo author project version moduleName docsList depsDocs =
let depsInfo =
depsDocs
|> List.map (\(resolvedDep, depDocs) ->
DepInfo resolvedDep (typeNameDictFromDocs depDocs))
in
Info author project version moduleName (typeNameDictFromDocs docsList) depsInfo


-- CREATE LINKS
Expand All @@ -209,7 +235,7 @@ bold =
style "font-weight" "bold"


makeLink : Info -> List (Attribute msg) -> String -> String -> Html msg
makeLink : LinkInfo r -> List (Attribute msg) -> String -> String -> Html msg
makeLink {author, project, version, moduleName} attrs tagName humanName =
let
url =
Expand All @@ -222,11 +248,34 @@ toLinkLine : Info -> String -> Lines (Line msg)
toLinkLine info qualifiedName =
case Dict.get qualifiedName info.typeNameDict of
Nothing ->
let
shortName =
last qualifiedName (String.split "." qualifiedName)
let depRef =
info.depsInfo
|> List.filterMap (\i ->
i.typeNameDict
|> Dict.get qualifiedName
|> Maybe.map (Tuple.pair i))
|> List.head
in
One (String.length shortName) [ span [ title qualifiedName ] [ text shortName ] ]
case depRef of
Nothing ->
let
shortName =
last qualifiedName (String.split "." qualifiedName)
in
One (String.length shortName) [ span [ title qualifiedName ] [ text shortName ] ]

Just (depInfo, (moduleName, name)) ->
One (String.length name)
[ makeLink
{ author = depInfo.resolvedDep.author
, project = depInfo.resolvedDep.project
, version = Just depInfo.resolvedDep.version
, moduleName = moduleName
}
[]
name
name
]

Just (moduleName, name) ->
One (String.length name) [ makeLink { info | moduleName = moduleName } [] name name ]
Expand Down
Loading