summaryrefslogtreecommitdiff
path: root/elm/Tagmod.elm
blob: 500eb4ba162e15b8395a461d5831e4e9b764ab37 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
module Tagmod exposing (main)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Html.Lazy
import Browser
import Browser.Navigation exposing (reload)
import Browser.Dom exposing (focus)
import Task
import Lib.Html exposing (..)
import Lib.Api as Api
import Lib.Ffi as Ffi
import Lib.Autocomplete as A
import Gen.Api as GApi
import Gen.Tagmod as GT


main : Program GT.Recv Model Msg
main = Browser.element
  { init   = \e -> (init e, Cmd.none)
  , view   = view
  , update = update
  , subscriptions = always Sub.none
  }

type alias Tag = GT.RecvTags

type Sel
  = NoSel
  | Vote Int
  | Spoil (Maybe Int)
  | Note
  | NoteSet

type alias Model =
  { state    : Api.State
  , title    : String
  , id       : Int
  , mod      : Bool
  , tags     : List Tag
  , saved    : List Tag
  , changed  : Bool
  , selId    : Int
  , selType  : Sel
  , negCount : Int
  , negShow  : Bool
  , add      : A.Model GApi.ApiTagResult
  , addMsg   : String
  }


init : GT.Recv -> Model
init f =
  { state    = Api.Normal
  , title    = f.title
  , id       = f.id
  , mod      = f.mod
  , tags     = f.tags
  , saved    = f.tags
  , changed  = False
  , selId    = 0
  , selType  = NoSel
  , negCount = List.length <| List.filter (\t -> t.rating <= 0) f.tags
  , negShow  = False
  , add      = A.init ""
  , addMsg   = ""
  }

searchConfig : A.Config Msg GApi.ApiTagResult
searchConfig = { wrap = TagSearch, id = "tagadd", source = A.tagSource }


type Msg
  = Noop
  | SetSel Int Sel
  | SetVote Int Int
  | SetOver Int Bool
  | SetSpoil Int (Maybe Int)
  | SetNote Int String
  | NegShow Bool
  | TagSearch (A.Msg GApi.ApiTagResult)
  | Submit
  | Submitted GApi.Response


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  let
    changed m = { m | changed = m.saved /= m.tags }
    modtag id f = changed { model | addMsg = "", tags = List.map (\t -> if t.id == id then f t else t) model.tags }
  in
  case msg of
    Noop -> (model, Cmd.none)
    SetSel id v ->
      ( if model.selType == NoteSet && not (id == model.selId && v == NoSel) then model else { model | selId = id, selType = v }
      , if v == NoteSet then Task.attempt (always Noop) (focus "tag_note") else Cmd.none)

    SetVote  id v -> (modtag id (\t -> { t | vote = v }), Cmd.none)
    SetOver  id b -> (modtag id (\t -> { t | overrule = b }), Cmd.none)
    SetSpoil id s -> (modtag id (\t -> { t | spoil = s }), Cmd.none)
    SetNote  id s -> (modtag id (\t -> { t | notes = s }), Cmd.none)
    NegShow  b    -> ({ model | negShow = b }, Cmd.none)

    TagSearch m ->
      let (nm, c, res) = A.update searchConfig m model.add
      in case res of
        Nothing -> ({ model | add = nm }, c)
        Just t ->
          let (nl, ms) =
                if t.state == 1                                    then ([], "Can't add deleted tags")
                else if not t.applicable                           then ([], "Tag is not applicable")
                else if List.any (\it -> it.id == t.id) model.tags then ([], "Tag is already in the list")
                else ([{ id = t.id, vote = 2, spoil = Nothing, overrule = False, notes = "", cat = "new", name = t.name, rating = 0, count = 0, spoiler = 0, overruled = False, othnotes = "", applicable = t.applicable }], "")
          in (changed { model | add = if ms == "" then A.clear nm "" else nm, tags = model.tags ++ nl, addMsg = ms }, c)

    Submit ->
      ( { model | state = Api.Loading, addMsg = "" }
      , GT.send { id = model.id, tags = List.map (\t -> { id = t.id, vote = t.vote, spoil = t.spoil, overrule = t.overrule, notes = t.notes }) model.tags } Submitted)
    Submitted GApi.Success -> (model, reload)
    Submitted r -> ({ model | state = Api.Error r }, Cmd.none)



viewTag : Tag -> Sel -> Int -> Bool -> Html Msg
viewTag t sel vid mod =
  let
    -- Similar to VNWeb::Tags::Lib::tagscore_
    tagscore s =
      div [ class "tagscore", classList [("negative", s < 0)] ]
      [ span [] [ text <| Ffi.fmtFloat s 1 ]
      , div [ style "width" <| String.fromFloat (abs (s/3*30)) ++ "px" ] []
      ]

    vote  = case sel of Vote v  -> v
                        _       -> t.vote
    spoil = case sel of Spoil s -> s
                        _       -> t.spoil
  in
    tr [] <|
    [ td [ class "tc_tagname" ]
      [ a [ href <| "/g"++String.fromInt t.id, style "text-decoration" (if t.applicable then "none" else "line-through") ] [ text t.name ]
      , if t.applicable then text "" else b [ class "grayedout" ] [ text " (not applicable)" ]
      ]
    , td [ class "tc_myvote buts"  ]
      [ a [ href "#", onMouseOver (SetSel t.id (Vote -3)), onMouseOut (SetSel 0 NoSel), onClickD (SetVote t.id -3), classList [("ld", vote <  0)], title "Downvote"    ] []
      , a [ href "#", onMouseOver (SetSel t.id (Vote  0)), onMouseOut (SetSel 0 NoSel), onClickD (SetVote t.id  0), classList [("l0", vote == 0)], title "Remove vote" ] []
      , a [ href "#", onMouseOver (SetSel t.id (Vote  1)), onMouseOut (SetSel 0 NoSel), onClickD (SetVote t.id  1), classList [("l1", vote >= 1)], title "+1"          ] []
      , a [ href "#", onMouseOver (SetSel t.id (Vote  2)), onMouseOut (SetSel 0 NoSel), onClickD (SetVote t.id  2), classList [("l2", vote >= 2)], title "+2"          ] []
      , a [ href "#", onMouseOver (SetSel t.id (Vote  3)), onMouseOut (SetSel 0 NoSel), onClickD (SetVote t.id  3), classList [("l3", vote == 3)], title "+3"          ] []
      ]
    , td [ class "tc_myover" ] [ if mod && t.vote /= 0 then inputCheck "" t.overrule (SetOver t.id) else text "" ]
    , td [ class "tc_myspoil buts" ] <|
      if t.vote <= 0 then [] else
      [ a [ href "#", onMouseOver (SetSel t.id (Spoil Nothing)),  onMouseOut (SetSel 0 NoSel), onClickD (SetSpoil t.id Nothing),  classList [("sn", spoil == Nothing)], title "Unknown"       ] []
      , a [ href "#", onMouseOver (SetSel t.id (Spoil (Just 0))), onMouseOut (SetSel 0 NoSel), onClickD (SetSpoil t.id (Just 0)), classList [("s0", spoil == Just 0 )], title "Not a spoiler" ] []
      , a [ href "#", onMouseOver (SetSel t.id (Spoil (Just 1))), onMouseOut (SetSel 0 NoSel), onClickD (SetSpoil t.id (Just 1)), classList [("s1", spoil == Just 1 )], title "Minor spoiler" ] []
      , a [ href "#", onMouseOver (SetSel t.id (Spoil (Just 2))), onMouseOut (SetSel 0 NoSel), onClickD (SetSpoil t.id (Just 2)), classList [("s2", spoil == Just 2 )], title "Major spoiler" ] []
      ]
    , td [ class "tc_mynote" ] <|
      if t.vote == 0 then [] else
      [ span
        [ onMouseOver (SetSel t.id Note)
        , onMouseOut (SetSel 0 NoSel)
        , onClickD (SetSel t.id NoteSet)
        , title <| if t.notes == "" then "set note" else t.notes
        , style "opacity" <| if t.notes == "" then "0.5" else "1.0"
        ] [ text "💬" ]
      ]
    ] ++
    case sel of
      Vote 0         -> [ td [ colspan 3 ] [ text "Remove vote" ] ]
      Vote 1         -> [ td [ colspan 3 ] [ text "Vote +1" ] ]
      Vote 2         -> [ td [ colspan 3 ] [ text "Vote +2" ] ]
      Vote 3         -> [ td [ colspan 3 ] [ text "Vote +3" ] ]
      Vote _         -> [ td [ colspan 3 ] [ text "Downvote (-3)" ] ]
      Spoil Nothing  -> [ td [ colspan 3 ] [ text "Spoiler status not known" ] ]
      Spoil (Just 0) -> [ td [ colspan 3 ] [ text "This is not spoiler" ] ]
      Spoil (Just 1) -> [ td [ colspan 3 ] [ text "This is a minor spoiler" ] ]
      Spoil (Just 2) -> [ td [ colspan 3 ] [ text "This is a major spoiler" ] ]
      Note           -> [ td [ colspan 3 ] [ if t.notes == "" then text "Set note" else div [ class "noteview" ] [ text t.notes ] ] ]
      NoteSet ->
        [ td [ colspan 3, class "compact" ]
          [ Html.form [ onSubmit (SetSel t.id NoSel) ]
            [ inputText "tag_note" t.notes (SetNote t.id) (onBlur (SetSel t.id NoSel) :: style "width" "400px" :: style "position" "absolute" :: placeholder "Set note..." :: GT.valTagsNotes) ]
          ]
        ]
      _ ->
        if t.count == 0 then [ td [ colspan 3 ] [] ]
        else
        [ td [ class "tc_allvote" ]
          [ tagscore t.rating
          , i [ classList [("grayedout", t.overruled)] ] [ text <| " (" ++ String.fromInt t.count ++ ")" ]
          , if not t.overruled then text ""
            else b [ class "standout", style "font-weight" "bold", title "Tag overruled. All votes other than that of the moderator who overruled it will be ignored." ] [ text "!" ]
          ]
        , td [ class "tc_allspoil"] [ text <| Ffi.fmtFloat t.spoiler 2 ]
        , td [ class "tc_allwho"  ]
          [ span [ style "opacity" <| if t.othnotes == "" then "0" else "1", style "cursor" "default", title t.othnotes ] [ text "💬 " ]
          , a [ href <| "/g/links?v="++String.fromInt vid++"&t="++String.fromInt t.id ] [ text "Who?" ]
          ]
        ]

viewHead : Bool -> Int -> Bool -> Html Msg
viewHead mod negCount negShow =
  thead []
  [ tr []
    [ td [ style "font-weight" "normal", style "text-align" "right" ] <|
      if negCount == 0 then []
      else [ linkRadio negShow NegShow [ text "Show downvoted tags " ], i [] [ text <| " (" ++ String.fromInt negCount ++ ")" ] ]
    , td [ colspan 4, class "tc_you" ] [ text "You" ]
    , td [ colspan 3, class "tc_others" ] [ text "Others" ]
    ]
  , tr []
    [ td [ class "tc_tagname" ] [ text "Tag" ]
    , td [ class "tc_myvote"  ] [ text "Rating" ]
    , td [ class "tc_myover"  ] [ text (if mod then "O" else "") ]
    , td [ class "tc_myspoil" ] [ text "Spoiler" ]
    , td [ class "tc_mynote"  ] []
    , td [ class "tc_allvote" ] [ text "Rating" ]
    , td [ class "tc_allspoil"] [ text "Spoiler" ]
    , td [ class "tc_allwho"  ] []
    ]
  ]

viewFoot : Api.State -> Bool -> A.Model GApi.ApiTagResult -> String -> Html Msg
viewFoot state changed add addMsg =
  tfoot [] [ tr [] [ td [ colspan 8 ]
  [ div [ style "display" "flex", style "justify-content" "space-between" ]
    [ A.view searchConfig add [placeholder "Add tags..."]
    , if addMsg /= ""
      then b [ class "standout" ] [ text addMsg ]
      else if changed
      then b [ class "standout" ] [ text "You have unsaved changes" ]
      else text ""
    , submitButton "Save changes" state True
    ]
  , text "Check the ", a [ href "/g" ] [ text "tag list" ], text " to browse all available tags."
  , br [] []
  , text "Can't find what you're looking for? ", a [ href "/g/new" ] [ text "Request a new tag" ]
  ] ] ]


-- The table has a lot of interactivity, the use of Html.Lazy is absolutely necessary for good responsiveness.
view : Model -> Html Msg
view model =
  form_ Submit (model.state == Api.Loading)
    [ div [ class "mainbox" ]
      [ h1 [] [ text <| "Edit tags for " ++ model.title ]
      , p []
        [ text "This is where you can add tags to the visual novel and vote on the existing tags."
        , br [] []
        , text "Don't forget to also select the appropriate spoiler option for each tag."
        , br [] []
        , text "For more information, check out the ", a [ href "/d10" ] [ text "guidelines." ]
        ]
      , table [ class "tgl stripe" ]
        [ Html.Lazy.lazy3 viewHead model.mod model.negCount model.negShow
        , Html.Lazy.lazy4 viewFoot model.state model.changed model.add model.addMsg
        , tbody []
          <| List.concatMap (\(id,nam) ->
            let lst = List.filter (\t -> t.cat == id && (t.cat == "new" || t.rating > 0 || t.vote > 0 || model.negShow)) model.tags
            in
              if List.length lst == 0
              then []
              else tr [class "tagmod_cat"] [ td [] [text nam], td [ class "tc_you", colspan 4 ] [], td [ class "tc_others", colspan 3 ] [] ]
                   :: List.map (\t -> Html.Lazy.lazy4 viewTag t (if t.id == model.selId then model.selType else NoSel) model.id model.mod) lst)
          [ ("cont", "Content")
          , ("ero",  "Sexual content")
          , ("tech", "Technical")
          , ("new",  "Newly added tags")
          ]
        ]
      ]
    ]