Add part14

This commit is contained in:
Richard Feldman
2016-09-07 07:38:57 -05:00
parent 9d85e8f35a
commit 9238659d69
10 changed files with 756 additions and 0 deletions

319
part14/ElmHub.elm Normal file
View File

@@ -0,0 +1,319 @@
port module ElmHub exposing (..)
import Html exposing (..)
import Html.Attributes exposing (class, target, href, defaultValue, type', checked, placeholder, value)
import Html.Events exposing (..)
import Html.App as Html
import Html.Lazy exposing (lazy, lazy3)
import Auth
import Json.Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (..)
import String
import Table
responseDecoder : Decoder (List SearchResult)
responseDecoder =
Json.Decode.at [ "items" ] (Json.Decode.list searchResultDecoder)
searchResultDecoder : Decoder SearchResult
searchResultDecoder =
decode SearchResult
|> required "id" Json.Decode.int
|> required "full_name" Json.Decode.string
|> required "stargazers_count" Json.Decode.int
type alias Model =
{ query : String
, results : List SearchResult
, errorMessage : Maybe String
, options : SearchOptions
, tableState : Table.State
}
type alias SearchOptions =
{ minStars : Int
, minStarsError : Maybe String
, searchIn : String
, userFilter : String
}
type alias SearchResult =
{ id : Int
, name : String
, stars : Int
}
initialModel : Model
initialModel =
{ query = "tutorial"
, results = []
, errorMessage = Nothing
, options =
{ minStars = 0
, minStarsError = Nothing
, searchIn = "name"
, userFilter = ""
}
, tableState = Table.initialSort "Stars"
}
init : ( Model, Cmd Msg )
init =
( initialModel, githubSearch (getQueryString initialModel) )
subscriptions : Model -> Sub Msg
subscriptions _ =
githubResponse decodeResponse
viewOptions : SearchOptions -> Html OptionsMsg
viewOptions opts =
div [ class "search-options" ]
[ div [ class "search-option" ]
[ label [ class "top-label" ] [ text "Search in" ]
, select [ onChange SetSearchIn, value opts.searchIn ]
[ option [ value "name" ] [ text "Name" ]
, option [ value "description" ] [ text "Description" ]
, option [ value "name,description" ] [ text "Name and Description" ]
]
]
, div [ class "search-option" ]
[ label [ class "top-label" ] [ text "Owned by" ]
, input
[ type' "text"
, placeholder "Enter a username"
, defaultValue (Debug.log "username" opts.userFilter)
, onInput SetUserFilter
]
[]
]
, div [ class "search-option" ]
[ label [ class "top-label" ] [ text "Minimum Stars" ]
, input
[ type' "text"
, onBlurWithTargetValue SetMinStars
, defaultValue (toString opts.minStars)
]
[]
, viewMinStarsError opts.minStarsError
]
]
viewMinStarsError : Maybe String -> Html msg
viewMinStarsError message =
case message of
Nothing ->
text " "
Just errorMessage ->
div [ class "stars-error" ] [ text errorMessage ]
type Msg
= Search
| Options OptionsMsg
| SetQuery String
| DeleteById Int
| HandleSearchResponse (List SearchResult)
| HandleSearchError (Maybe String)
| SetTableState Table.State
| DoNothing
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Options optionsMsg ->
( { model | options = updateOptions optionsMsg model.options }, Cmd.none )
Search ->
( model, githubSearch (getQueryString model) )
SetQuery query ->
( { model | query = query }, Cmd.none )
HandleSearchResponse results ->
( { model | results = results }, Cmd.none )
HandleSearchError error ->
( { model | errorMessage = error }, Cmd.none )
DeleteById idToHide ->
let
newResults =
model.results
|> List.filter (\{ id } -> id /= idToHide)
newModel =
{ model | results = newResults }
in
( newModel, Cmd.none )
SetTableState tableState ->
( { model | tableState = tableState }, Cmd.none )
DoNothing ->
( model, Cmd.none )
onBlurWithTargetValue : (String -> msg) -> Attribute msg
onBlurWithTargetValue toMsg =
on "blur" (Json.Decode.map toMsg targetValue)
tableConfig : Table.Config SearchResult Msg
tableConfig =
Table.config
{ toId = .id >> toString
, toMsg = SetTableState
, columns = [ starsColumn, nameColumn ]
}
starsColumn : Table.Column SearchResult Msg
starsColumn =
Table.veryCustomColumn
{ name = "Stars"
, viewData = viewStars
, sorter = Table.increasingOrDecreasingBy (negate << .stars)
}
nameColumn : Table.Column SearchResult Msg
nameColumn =
Table.veryCustomColumn
{ name = "Name"
, viewData = viewSearchResult
, sorter = Table.increasingOrDecreasingBy .name
}
updateOptions : OptionsMsg -> SearchOptions -> SearchOptions
updateOptions optionsMsg options =
case optionsMsg of
SetMinStars minStarsStr ->
case String.toInt minStarsStr of
Ok minStars ->
{ options | minStars = minStars, minStarsError = Nothing }
Err _ ->
{ options
| minStarsError =
Just "Must be an integer!"
}
SetSearchIn searchIn ->
{ options | searchIn = searchIn }
SetUserFilter userFilter ->
{ options | userFilter = userFilter }
view : Model -> Html Msg
view model =
div [ class "content" ]
[ header []
[ h1 [] [ text "ElmHub" ]
, span [ class "tagline" ] [ text "Like GitHub, but for Elm things." ]
]
, div [ class "search" ]
[ Html.map Options (lazy viewOptions model.options)
, div [ class "search-input" ]
[ input [ class "search-query", onInput SetQuery, defaultValue model.query ] []
, button [ class "search-button", onClick Search ] [ text "Search" ]
]
]
, viewErrorMessage model.errorMessage
, lazy3 Table.view tableConfig model.tableState model.results
]
viewErrorMessage : Maybe String -> Html msg
viewErrorMessage errorMessage =
case errorMessage of
Just message ->
div [ class "error" ] [ text message ]
Nothing ->
text ""
viewStars : SearchResult -> Table.HtmlDetails Msg
viewStars result =
Table.HtmlDetails []
[ span [ class "star-count" ] [ text (toString result.stars) ] ]
viewSearchResult : SearchResult -> Table.HtmlDetails Msg
viewSearchResult result =
Table.HtmlDetails []
[ a [ href ("https://github.com/" ++ result.name), target "_blank" ]
[ text result.name ]
, button [ class "hide-result", onClick (DeleteById result.id) ]
[ text "X" ]
]
type OptionsMsg
= SetMinStars String
| SetSearchIn String
| SetUserFilter String
decodeGithubResponse : Json.Decode.Value -> Msg
decodeGithubResponse value =
case Json.Decode.decodeValue responseDecoder value of
Ok results ->
HandleSearchResponse results
Err err ->
HandleSearchError (Just err)
onChange : (String -> msg) -> Attribute msg
onChange toMsg =
on "change" (Json.Decode.map toMsg Html.Events.targetValue)
decodeResponse : Json.Decode.Value -> Msg
decodeResponse json =
case Json.Decode.decodeValue responseDecoder json of
Err err ->
HandleSearchError (Just err)
Ok results ->
HandleSearchResponse results
port githubSearch : String -> Cmd msg
port githubResponse : (Json.Decode.Value -> msg) -> Sub msg
getQueryString : Model -> String
getQueryString model =
-- See https://developer.github.com/v3/search/#example for how to customize!
"access_token="
++ Auth.token
++ "&q="
++ model.query
++ "+in:"
++ model.options.searchIn
++ "+stars:>="
++ (toString model.options.minStars)
++ "+language:elm"
++ (if String.isEmpty model.options.userFilter then
""
else
"+user:" ++ model.options.userFilter
)

158
part14/ElmHubCss.elm Normal file
View File

@@ -0,0 +1,158 @@
module ElmHubCss exposing (..)
import Css exposing (..)
import Css.Elements exposing (..)
-- Documentation:
-- http://package.elm-lang.org/packages/rtfeldman/elm-css/latest
css : Stylesheet
css =
stylesheet
[ (.) "hide-result"
[ backgroundColor transparent
, fontWeight bold
, fontSize (px 18)
, marginLeft (px 18)
, border zero
, cursor pointer
, hover
[ color (rgb 96 181 204)
]
]
, (.) "content"
[ width (px 960)
, margin2 zero auto
, padding (px 30)
, fontFamilies [ "Helvetica", "Arial", "serif" ]
]
, header
[ position relative
, padding2 (px 6) (px 12)
, height (px 36)
, backgroundColor (rgb 96 181 204)
]
, h1
[ color (hex "ffffff")
, margin zero
]
, (.) "tagline"
[ color (hex "eeeeee")
, position absolute
, right (px 16)
, top (px 12)
, fontSize (px 24)
, fontStyle italic
]
, (.) "results"
[ property "list-style-image" "url('http://img-cache.cdn.gaiaonline.com/76bd5c99d8f2236e9d3672510e933fdf/http://i278.photobucket.com/albums/kk81/d3m3nt3dpr3p/Tiny-Star-Icon.png')"
, property "list-style-position" "inside"
, padding zero
]
, (.) "results"
[ descendants
[ li
[ fontSize (px 18)
, marginBottom (px 16)
]
]
]
, (.) "star-count"
[ fontWeight bold
, marginRight (px 16)
]
, a
[ color (rgb 96 181 204)
, textDecoration none
, hover
[ textDecoration underline ]
]
, (.) "search-query"
[ padding (px 8)
, fontSize (px 24)
, marginBottom (px 18)
, marginTop (px 36)
]
, (.) "search-button"
[ padding2 (px 8) (px 16)
, fontSize (px 24)
, color (hex "ffffff")
, border3 (px 1) solid (hex "cccccc")
, backgroundColor (rgb 96 181 204)
, marginLeft (px 12)
, hover
[ color (rgb 96 181 204)
, backgroundColor (hex "ffffff")
]
]
, (.) "search-option"
[ descendants
[ selector "input[type=\"text\"]"
[ padding (px 5)
, boxSizing borderBox
, width (pct 90)
]
]
]
, each [ button, input ]
[ focus [ outline none ]
]
, (.) "search"
[ after
[ property "content" "\"\""
, property "display" "table"
, property "clear" "both"
]
]
, (.) "error"
[ backgroundColor (hex "FF9632")
, padding (px 20)
, boxSizing borderBox
, overflowX auto
, fontFamily monospace
, fontSize (px 18)
]
, (.) "top-label"
[ display block
, color (hex "555555")
]
, th
[ textAlign left
, cursor pointer
, hover [ color (rgb 96 181 204) ]
]
, each
[ th, td ]
[ fontSize (px 18)
, paddingRight (px 20)
]
, (.) "stars-error"
[ backgroundColor (hex "FF9632")
, fontSize (px 16)
, padding (px 10)
, marginRight (px 24)
, borderRadius (px 10)
, marginTop (px 10)
]
, (.) "search-input"
[ display block
, property "float" "left"
, width (pct 42)
]
, (.) "search-options"
[ position relative
, property "float" "right"
, width (pct 58)
, boxSizing borderBox
, paddingTop (px 20)
]
, (.) "search-option"
[ display block
, property "float" "left"
, width (pct 30)
, marginLeft (px 16)
, boxSizing borderBox
]
]

14
part14/Main.elm Normal file
View File

@@ -0,0 +1,14 @@
module Main exposing (main)
import ElmHub
import Html.App as Html
main : Program Never
main =
Html.program
{ view = ElmHub.view
, update = ElmHub.update
, init = ElmHub.init
, subscriptions = ElmHub.subscriptions
}

27
part14/README.md Normal file
View File

@@ -0,0 +1,27 @@
Part 13
=======
## Installation
```bash
elm-package install
```
(Answer `y` when prompted.)
## Building
```bash
elm-live Main.elm --open --pushstate --output=elm.js
```
## Compiling CSS
```bash
elm-css Stylesheets.elm
```
## References
* [Elm CSS documentation](http://package.elm-lang.org/packages/rtfeldman/elm-css/latest)

24
part14/Stylesheets.elm Normal file
View File

@@ -0,0 +1,24 @@
port module Stylesheets exposing (..)
import Css.File exposing (..)
import ElmHubCss
import Html exposing (div)
import Html.App as Html
port files : CssFileStructure -> Cmd msg
cssFiles : CssFileStructure
cssFiles =
toFileStructure [ ( "style.css", compile [ ElmHubCss.css ] ) ]
main : Program Never
main =
Html.program
{ init = ( (), files cssFiles )
, view = \_ -> (div [] [])
, update = \_ _ -> ( (), Cmd.none )
, subscriptions = \_ -> Sub.none
}

BIN
part14/elm-hub.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

19
part14/elm-package.json Normal file
View File

@@ -0,0 +1,19 @@
{
"version": "1.0.0",
"summary": "Like GitHub, but for Elm stuff.",
"repository": "https://github.com/rtfeldman/elm-workshop.git",
"license": "BSD-3-Clause",
"source-directories": [
".",
".."
],
"exposed-modules": [],
"dependencies": {
"NoRedInk/elm-decode-pipeline": "1.1.2 <= v < 2.0.0",
"elm-lang/core": "4.0.1 <= v < 5.0.0",
"elm-lang/html": "1.0.0 <= v < 2.0.0",
"evancz/elm-sortable-table": "1.0.0 <= v < 2.0.0",
"rtfeldman/elm-css": "5.0.0 <= v < 6.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}

2
part14/github.js Normal file

File diff suppressed because one or more lines are too long

32
part14/index.html Normal file
View File

@@ -0,0 +1,32 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>ElmHub</title>
<script type="text/javascript" src="/github.js"></script>
<script type="text/javascript" src="/elm.js"></script>
<link rel="stylesheet" href="/style.css">
<link rel="icon" type="image/png" href="/elm-hub.png">
</head>
<body>
<div id="elm-landing-pad"></div>
</body>
<script type="text/javascript">
// documentation: https://github.com/michael/github
var github = new Github();
var app = Elm.Main.embed(document.getElementById("elm-landing-pad"));
function searchGithub(query) {
github.getSearch(query).repositories({}, function (err, repositories) {
app.ports.githubResponse.send(repositories);
});
}
app.ports.githubSearch.subscribe(searchGithub);
</script>
</html>

161
part14/style.css Normal file
View File

@@ -0,0 +1,161 @@
.hide-result {
background-color: transparent;
font-weight: bold;
font-size: 18px;
margin-left: 18px;
border: 0;
cursor: pointer;
}
.hide-result:hover {
color: rgb(96, 181, 204);
}
.content {
width: 960px;
margin: 0 auto;
padding: 30px;
font-family: Helvetica, Arial, serif;
}
header {
position: relative;
padding: 6px 12px;
height: 36px;
background-color: rgb(96, 181, 204);
}
h1 {
color: #ffffff;
margin: 0;
}
.tagline {
color: #eeeeee;
position: absolute;
right: 16px;
top: 12px;
font-size: 24px;
font-style: italic;
}
.results {
list-style-image: url('http://img-cache.cdn.gaiaonline.com/76bd5c99d8f2236e9d3672510e933fdf/http://i278.photobucket.com/albums/kk81/d3m3nt3dpr3p/Tiny-Star-Icon.png');
list-style-position: inside;
padding: 0;
}
.results li {
font-size: 18px;
margin-bottom: 16px;
}
.star-count {
font-weight: bold;
margin-right: 16px;
}
a {
color: rgb(96, 181, 204);
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.search-query {
padding: 8px;
font-size: 24px;
margin-bottom: 18px;
margin-top: 36px;
}
.search-button {
padding: 8px 16px;
font-size: 24px;
color: #ffffff;
border: 1px solid #cccccc;
background-color: rgb(96, 181, 204);
margin-left: 12px;
}
.search-button:hover {
color: rgb(96, 181, 204);
background-color: #ffffff;
}
.search-option input[type="text"] {
padding: 5px;
box-sizing: border-box;
width: 90%;
}
button:focus, input:focus {
outline: none;
}
.search::after {
content: "";
display: table;
clear: both;
}
.error {
background-color: #FF9632;
padding: 20px;
box-sizing: border-box;
overflow-x: auto;
font-family: monospace;
font-size: 18px;
}
.top-label {
display: block;
color: #555555;
}
th {
text-align: left;
cursor: pointer;
}
th:hover {
color: rgb(96, 181, 204);
}
th, td {
font-size: 18px;
padding-right: 20px;
}
.stars-error {
background-color: #FF9632;
font-size: 16px;
padding: 10px;
margin-right: 24px;
border-radius: 10px;
margin-top: 10px;
}
.search-input {
display: block;
float: left;
width: 42%;
}
.search-options {
position: relative;
float: right;
width: 58%;
box-sizing: border-box;
padding-top: 20px;
}
.search-option {
display: block;
float: left;
width: 30%;
margin-left: 16px;
box-sizing: border-box;
}