summaryrefslogtreecommitdiff
path: root/elm3/VNEdit/Seiyuu.elm
blob: 3473bb7b4175bd7992266af19e1fc42f98de70e5 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module VNEdit.Seiyuu exposing (Model, Msg, init, update, view)

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


type alias Model =
  { chars       : List Gen.VNEditChars
  , seiyuu      : List Gen.VNEditSeiyuu
  , search      : A.Model Gen.ApiStaffResult
  , duplicates  : Bool
  }


init : List Gen.VNEditSeiyuu -> List Gen.VNEditChars -> Model
init s c =
  { chars      = c
  , seiyuu     = s
  , search     = A.init
  , duplicates = False
  }


type Msg
  = Del Int
  | SetNote Int String
  | SetChar Int String
  | Search (A.Msg Gen.ApiStaffResult)


searchConfig : A.Config Msg Gen.ApiStaffResult
searchConfig = { wrap = Search, id = "add-seiyuu", source = A.staffSource }


validate : Model -> Model
validate model = { model | duplicates = hasDuplicates <| List.map (\e -> (e.aid,e.cid )) model.seiyuu }


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Del i       -> (validate { model | seiyuu = delidx i model.seiyuu }, Cmd.none)
    SetNote i s -> (validate { model | seiyuu = modidx i (\e -> { e | note = s }) model.seiyuu }, Cmd.none)
    SetChar i s -> (validate { model | seiyuu = modidx i (\e -> { e | cid = Maybe.withDefault e.cid (String.toInt s) }) model.seiyuu }, 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
            char = List.head model.chars |> Maybe.map .id |> Maybe.withDefault 0
            nrow = { aid = r.aid, cid = char, id = r.id, name = r.name, note = "" }
            nmod = { model | search = A.clear nm, seiyuu = model.seiyuu ++ [nrow] }
          in (validate nmod, c)



view : Model -> Maybe Int -> Html Msg
view model id =
  let
    entry n e = editListRow ""
      [ editListField 1 "col-form-label single-line"
        [ a [href <| "/s" ++ String.fromInt e.id, target "_blank" ] [ text e.name ] ]
      , editListField 1 ""
        [ inputSelect
            [onInput (SetChar n)]
            (String.fromInt e.cid)
            (List.map (\c -> (String.fromInt c.id, c.name)) model.chars)
        ]
      , editListField 2 "" [ inputText "" e.note (SetNote n) [placeholder "Note", maxlength 250] ]
      , editListField 0 "" [ removeButton (Del n) ]
      ]

    nochars =
      case id of
        Nothing -> [ text "Cast can be added when the visual novel entry has characters linked to it." ]
        Just n ->
          [ text "Cast can be added after "
          , a [ href <| "/c/new?vid=" ++ (String.fromInt n), target "_blank" ] [ text "creating" ]
          , text " the appropriate character entries, or after linking "
          , a [ href "/c/all" ] [ text "existing characters" ]
          , text " to this visual novel entry."
          ]

  in if List.isEmpty model.chars
  then card "cast" "Cast" [ div [class "card__subheading"] nochars ] []
  else card "cast" "Cast" [] <|
    editList (List.indexedMap entry model.seiyuu)
    ++ formGroups (
      (if model.duplicates
        then [ [ div [ class "invalid-feedback" ]
          [ text "The cast list contains duplicates. Make sure that each person is only listed at most once for the same character" ] ] ]
        else []
      ) ++
      [ label [for "add-seiyuu"] [text "Add cast"]
        :: A.view searchConfig model.search [placeholder "Cast name", style "max-width" "400px"]
      ]
    )