summaryrefslogtreecommitdiff
path: root/elm3/CharEdit/Traits.elm
blob: 66d220a5729a4200c47f3d3d3ef203a2dfdd4b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
module CharEdit.Traits exposing (Model, Msg, init, update, view)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Lib.Html exposing (..)
import Lib.Autocomplete as A
import Lib.Gen exposing (CharEditTraits)
import Lib.Util exposing (..)
import Lib.Api exposing (Trait)


type alias Model =
  { traits     : List CharEditTraits
  , search     : A.Model Trait
  , duplicates : Bool
  }


init : List CharEditTraits -> Model
init l =
  { traits     = l
  , search     = A.init
  , duplicates = False
  }


type Msg
  = Del Int
  | SetSpoil Int String
  | Search (A.Msg Trait)


searchConfig : A.Config Msg Trait
searchConfig = { wrap = Search, id = "add-trait", source = A.traitSource }


validate : Model -> Model
validate model = { model | duplicates = hasDuplicates <| List.map .tid model.traits }


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Del i        -> (validate { model | traits = delidx i model.traits }, Cmd.none)
    SetSpoil i s -> (validate { model | traits = modidx i (\e -> { e | spoil = Maybe.withDefault e.spoil (String.toInt s) }) model.traits }
                   , Cmd.none )

    Search m ->
      let (nm, c, res) = A.update searchConfig m model.search
      in case res of
        Nothing -> ({ model | search = nm }, c)
        Just r  ->
          let nrow = { tid = r.id, name = r.name, group = Maybe.withDefault "" r.group, spoil = 0 }
          in (validate { model | search = A.clear nm, traits = model.traits ++ [nrow] }, c)



view : Model -> Html Msg
view model =
  let
    entry n e = editListRow ""
      [ editListField 2 "col-form-label single-line"
        [ span [ class "muted" ] [ text <| e.group ++ " / " ]
        , a [href <| "/i" ++ String.fromInt e.tid, title e.name, target "_blank" ] [ text e.name ] ]
      , editListField 1 ""
        [ inputSelect [onInput (SetSpoil n)] (String.fromInt e.spoil) spoilLevels ]
      , editListField 0 "" [ removeButton (Del n) ]
      ]

  in card "traits" "Traits" []
    <| editList (List.indexedMap entry model.traits)
    ++ formGroups (
      (if model.duplicates
        then [ [ div [ class "invalid-feedback" ]
          [ text "There are duplicate traits." ] ] ]
        else []
      ) ++
      [ label [for "add-trait"] [text "Add trait"]
        :: A.view searchConfig model.search [placeholder "Trait", style "max-width" "400px"]
      ]
    )