ELOS-License-Builder/src/Main.elm

134 lines
2.8 KiB
Elm
Raw Normal View History

2022-02-01 22:33:58 +00:00
module Main exposing (main)
import Browser
import Sort.Set exposing (Set)
2022-02-03 00:51:31 +00:00
import Html exposing (Html, div, input, label, p, section, text, h3)
import Html.Events exposing (onCheck, onClick)
2022-02-02 19:31:07 +00:00
import Html.Attributes exposing (class, type_, id)
2022-02-01 22:33:58 +00:00
import Modules exposing
( Module
, all_modules
2022-02-03 00:51:31 +00:00
, download_license
, DownloadType(..)
2022-02-01 22:33:58 +00:00
, module_property
, module_desc
, module_sorter
)
2022-02-03 00:51:31 +00:00
import Html.Attributes exposing (attribute)
2022-02-01 22:33:58 +00:00
main: Program () Model Msg
main =
2022-02-03 00:51:31 +00:00
Browser.element
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions _ = Sub.none
2022-02-01 22:33:58 +00:00
-- INIT + Model
type alias Model =
{ enabled_modules : Set Module
}
2022-02-03 00:51:31 +00:00
init : () -> (Model, Cmd Msg)
init () =
( { enabled_modules = Sort.Set.empty module_sorter }
, Cmd.none
)
2022-02-01 22:33:58 +00:00
-- UPDATE
type Msg
= ToggleModule Module Bool
2022-02-03 00:51:31 +00:00
| Download DownloadType
2022-02-01 22:33:58 +00:00
add_or_remove : Bool -> (a -> Set a -> Set a)
add_or_remove active =
if active then
Sort.Set.insert
else
Sort.Set.remove
2022-02-03 00:51:31 +00:00
update : Msg -> Model -> (Model, Cmd Msg)
2022-02-01 22:33:58 +00:00
update msg model =
case msg of
ToggleModule mod enabled ->
2022-02-03 00:51:31 +00:00
( { model | enabled_modules = add_or_remove enabled mod model.enabled_modules }
, Cmd.none
)
Download dtype ->
( model , download_license dtype (Sort.Set.toList model.enabled_modules) )
2022-02-01 22:33:58 +00:00
-- VIEW
view_module_button : Set Module -> Module -> Html Msg
view_module_button enabled mod =
label
2022-02-02 23:27:11 +00:00
(class "module-button"
:: (if Sort.Set.memberOf enabled mod then [class "active"] else [])
)
[ div []
[ div [class "cooler-checkbox"]
[ input
[ type_ "checkbox"
, onCheck (ToggleModule mod)
] []
]
, h3 [] [ text (module_property .name mod) ]
, p [] (module_desc mod)
]
2022-02-01 22:33:58 +00:00
]
2022-02-03 00:51:31 +00:00
view_modules : Set Module -> Html Msg
view_modules modules =
section [id "modules"] (
2022-02-01 22:33:58 +00:00
all_modules
2022-02-03 00:51:31 +00:00
|> List.map (view_module_button modules)
2022-02-01 22:33:58 +00:00
)
2022-02-03 00:51:31 +00:00
view_cooler_button : String -> msg -> Html msg
view_cooler_button name message =
div
[ class "cool-button"
, onClick message
]
[ div
[ class "bevel"
, class "tr"
, attribute "aria-hidden" "true"
] []
, div [] [ text name ]
, div
[ class "bevel"
, class "bl"
, attribute "aria-hidden" "true"
] []
]
view_download_button : (String, DownloadType) -> Html Msg
view_download_button (name, dtype) =
view_cooler_button name (Download dtype)
downloads : Html Msg
downloads =
section [id "downloads"]
( List.map view_download_button
[ ("Markdown", Markdown)
, ("Plaintext", Plain)
, ("HTML", Html)
]
)
view : Model -> Html Msg
view model =
div [id "elm-area"]
[ view_modules model.enabled_modules
, downloads
]