module NetworkMap.Interfaces.View exposing (..) import Array exposing (Array) import Dict exposing (Dict) import Element exposing (Element, column, fillPortion, text, width) import Element.Font as Font import Element.Input as Input import NetworkMap.Interfaces.Msg exposing (Msg(..)) import NetworkMap.Interfaces.Model exposing (Function, Inputs, Interface, Model, Module) import Palette exposing (fillWidth) import Screen.Model as Screen view : Screen.Model -> Model -> Element Msg view screen model = let modulesEl = (model.interface |> Maybe.map (interfaceForms model.id model.inputs)) |> Maybe.withDefault Element.none in column [ fillWidth ] [modulesEl, Input.button [ width <| Element.px 80 ] <| { onPress = Just <| GetInterface model.id , label = Element.el [ Font.underline ] (Element.text "Get Interfaces") }] interfaceForms : String -> Inputs -> Interface -> Element Msg interfaceForms id inputs interface = let modules = interface.modules in column [ fillWidth ] (modules |> List.map (moduleForms id inputs)) moduleForms : String -> Inputs -> Module -> Element Msg moduleForms id inputs mod = let name = mod.name functions = mod.functions in column [ fillWidth ] ([text name] ++ (functions |> List.map (functionForms id inputs name))) functionForms : String -> Inputs -> String -> Function -> Element Msg functionForms id inputs moduleId function = let name = function.name inputsElements = function.inputs |> Array.indexedMap (\i -> \inp -> genInput moduleId name i inp inputs) btn = Input.button [ Font.underline ] { onPress = Just <| CallFunction id moduleId name, label = text "Call Function" } outputs = function.outputs in column [ fillWidth ] ([text name] ++ (Array.toList inputsElements)) genInput : String -> String -> Int -> String -> Inputs -> Element Msg genInput moduleId functionId idx fieldType inputs = Input.text [ width (fillPortion 5) ] { onChange = UpdateInput moduleId functionId idx , text = (inputs |> getInputText moduleId functionId idx) , placeholder = Just (Input.placeholder [] (text fieldType)) , label = Input.labelHidden fieldType } getInputText : String -> String -> Int -> Inputs -> String getInputText moduleId functionId idx inputs = case (Dict.get moduleId inputs) of Just f -> f |> getInputFromFunction functionId idx Nothing -> "" getInputFromFunction : String -> Int -> Dict String (Array String) -> String getInputFromFunction functionId idx dic = case (Dict.get functionId dic) of Just arr -> Maybe.withDefault "" (Array.get idx arr) Nothing -> ""