module Decisions.BulkDecisionsView exposing (view) import Decisions.Model exposing (..) import Decisions.Messages exposing (..) import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onClick, onInput, onMouseEnter, onWithOptions, on) import Json.Decode as Json import Set import Dict view : Model -> Html Msg view model = if model.hasBulkDecisions then let bulkDecisionToggle = div [ class "button table__button-bulk-decisions" , onClick ToggleShowBulkDecisions ] [ text "bulk decisions" ] in span [ class "table__bulk-decisions-container" ] (if model.showBulkDecisions then [ bulkDecisionToggle , div [ class "table__bulk-decisions-menu" ] [ span [] [ textarea [ rows 10 , cols 15 , class "form__input form__input--textarea-bulk-decisions" , placeholder "Eg.\n10\n11\n12" , onInput SetBulkDecisionsText , value model.bulkDecisionsText ] [ text model.bulkDecisionsText ] , bulkDecisionDropdownSelect model ] ] ] else [ bulkDecisionToggle ] ) else text "" bulkDecisionDropdownSelect model = let bulkDecisionsOptions = model.bulkDecisionsOptions optionList = (List.map viewOption bulkDecisionsOptions) viewOption { parent, isSelectable, childHeader, children } = if List.isEmpty children then li [ class "table-cell__menu-item" , onClick (SelectBulkDecision parent Nothing) ] [ text parent ] else (li (if isSelectable then [ class "table-cell__menu-item table-cell__menu-item--with-children" , onMouseEnter (ViewBulkDecisionDropdownChildren parent) , onClick (SelectBulkDecision parent Nothing) ] else [ class "table-cell__menu-item table-cell__menu-item--disabled-with-children" , onMouseEnter (ViewBulkDecisionDropdownChildren parent) ] ) [ text parent , (case model.focussedBulkDecisionOption of Nothing -> text "" Just option -> if option == parent then let childHeaderLi = case childHeader of Just headerString -> li [ class "table-cell__menu-item table-cell__menu-item--header" ] [ text headerString ] Nothing -> text "" in ul [ class "table-cell__menu table-cell__menu--sub" ] (childHeaderLi :: List.map (viewSubChoice parent) children) else text "" ) ] ) viewSubChoice parent child = li [ class "table-cell__menu-item" , onClickNoPropagate (SelectBulkDecision parent (Just child)) ] [ text child ] rowIds = model.tableState.rows |> List.map getSerialNumber |> Set.fromList getSerialNumber row = Dict.get "0" row.data.stringDictData |> Maybe.map .value |> Maybe.withDefault "" invalidBulkDecisionIds = model.bulkDecisionsText |> String.split "\n" |> List.map String.trim |> List.filter (String.isEmpty >> not) |> List.filter (\i -> not (Set.member i rowIds)) |> String.join ", " userPrompt = div [ class "bulk-decisions__prompt" ] (if String.isEmpty invalidBulkDecisionIds then [ p [ class "hint" ] [ text "Select rows or enter ids in the text input, one per new line." ] , p [ class "hint" ] [ text "Then choose a decision to apply it to all of them." ] ] else [ p [ class "hint" ] [ text "The following lines are not valid ids in the decisions table and will not be updated: " ] , p [ class "hint" ] [ text invalidBulkDecisionIds ] ] ) in span [] [ span [ class "bulk-decisions-dropdown--control" ] [ a [ class "table-cell__control" , onClickNoPropagate ToggleBulkDecisionsDropdown ] [ text "Choose decision", span [ class "caret" ] [] ] , (if model.showBulkDecisionsDropdown then ul [ class "bulk-decisions__decision-menu" ] optionList else text "" ) ] , userPrompt ] onClickNoPropagate : msg -> Attribute msg onClickNoPropagate msg = onWithOptions "click" { stopPropagation = True, preventDefault = False } (Json.succeed msg) onChange : (String -> msg) -> Attribute msg onChange handler = on "change" <| Json.map handler <| Json.at [ "target", "value" ] Json.string