module CacheSim.View exposing (..) import CacheSim.Raw exposing (..) import CacheSim.Model exposing (..) import Html exposing (Html, input, text, div, label, span, h2, h3) import Html.Attributes exposing (type_, class, value, for, classList, disabled) import Html.Events exposing (onInput, onClick) optionalButton : Bool -> String -> Msg -> Html Msg optionalButton e s m = let events = if e then [ onClick m ] else [ disabled (not e) ] in input ([ type_ "button", value s ] ++ events) [] button : String -> Msg -> Html Msg button s m = input [ type_ "button", onClick m, value s] [] buttonWrapper : List (Html Msg) -> Html Msg buttonWrapper = div [ class "button-wrapper" ] labeledInput : String -> String -> (String -> Msg) -> Html Msg labeledInput s val f = div [ class "input-group" ] [ span [] [ text s ] , input [ value val, type_ "text", onInput f ] [] ] viewRawCacheModel : Int -> RawCacheModel -> Html Msg viewRawCacheModel level rcm = let updateBlockSize s cm = { cm | blockSize = s} updateSetCount s cm = { cm | setCount = s} updateSetSize s cm = { cm | setSize = s} wrapUpdate f s = ChangeRawModel level (f s) deleteButton = button "Delete" (DeleteRawModel level) params = div [ class "cache-model-params" ] [ labeledInput "Block size" rcm.blockSize (wrapUpdate updateBlockSize) , labeledInput "Set count" rcm.setCount (wrapUpdate updateSetCount) , labeledInput "Set size" rcm.setSize (wrapUpdate updateSetSize) ] in div [ class "cache-model" ] [ h3 [] [ text <| "L" ++ String.fromInt (level + 1) ++ " Cache" ] , buttonWrapper [ deleteButton ] , params ] viewRawCacheModelHierarchy : RawCacheModelHierarchy -> Html Msg viewRawCacheModelHierarchy rcmh = let models = div [ class "cache-model-levels" ] <|List.indexedMap viewRawCacheModel rcmh translationResult = Result.andThen validateCacheModelHierarchy <| translateRawCacheModelHierarchy rcmh isValid = case translationResult of Ok _ -> True Err _ -> False errorHtml = case translationResult of Ok _ -> viewError True "" Err e -> viewError False e newButton = button "Add level" CreateRawModel useButton = case translationResult of Ok cmh -> optionalButton True "Use hierarchy" (UseHierarchy <| Just cmh) Err _ -> optionalButton False "Use hierarchy" (UseHierarchy Nothing) in div [ class "cache-model-hierarchy" ] [ h2 [] [ text "Cache hierarchy" ] , errorHtml , buttonWrapper [ newButton, useButton ] , models ] viewError : Bool -> String -> Html Msg viewError hide e = span [ classList [ ("hidden", hide) ] ] [ text e ] viewBase : Model -> Html Msg viewBase m = div [] [ viewRawCacheModelHierarchy m.rawHierarchy ]