module Main exposing (main) import Browser import Sort.Set exposing (Set) import Html exposing (Html, div, input, label, p, section, text, h3) import Html.Events exposing (onCheck, onClick) import Html.Attributes exposing (class, type_, id) import Modules exposing ( Module , all_modules , download_license , DownloadType(..) , module_property , module_desc , module_sorter ) import Html.Attributes exposing (attribute) main: Program () Model Msg main = Browser.element { init = init , update = update , view = view , subscriptions = subscriptions } subscriptions : Model -> Sub Msg subscriptions _ = Sub.none -- INIT + Model type alias Model = { enabled_modules : Set Module } init : () -> (Model, Cmd Msg) init () = ( { enabled_modules = Sort.Set.empty module_sorter } , Cmd.none ) -- UPDATE type Msg = ToggleModule Module Bool | Download DownloadType add_or_remove : Bool -> (a -> Set a -> Set a) add_or_remove active = if active then Sort.Set.insert else Sort.Set.remove update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of ToggleModule mod enabled -> ( { 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) ) -- VIEW view_module_button : Set Module -> Module -> Html Msg view_module_button enabled mod = label (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) ] ] view_modules : Set Module -> Html Msg view_modules modules = section [id "modules"] ( all_modules |> List.map (view_module_button modules) ) 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 ]