diff options
author | Yorhel <git@yorhel.nl> | 2019-10-19 15:12:43 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-11-10 12:44:55 +0100 |
commit | 2eff1dc7ad645cf51252d5a64275bcfc783fc325 (patch) | |
tree | cd993e754e98583508cd94f2a4869d1b64587c83 /elm | |
parent | 2d0a71b5083192a06de9e47a4424546a6bc4b639 (diff) |
ulist: Add data model for new lists + Perl/Elm for label management
Very much experimental. I'm not sure this is even the right model.
Diffstat (limited to 'elm')
-rw-r--r-- | elm/Lib/Html.elm | 6 | ||||
-rw-r--r-- | elm/ULists/ManageLabels.elm | 116 | ||||
-rw-r--r-- | elm/ULists/ManageLabels.js | 6 |
3 files changed, 128 insertions, 0 deletions
diff --git a/elm/Lib/Html.elm b/elm/Lib/Html.elm index 3aae21c7..edf0a0c5 100644 --- a/elm/Lib/Html.elm +++ b/elm/Lib/Html.elm @@ -17,6 +17,12 @@ onClickN action = custom "click" (JD.succeed { message = action, stopPropagation br_ : Int -> Html m br_ n = if n == 1 then br [] [] else span [] <| List.repeat n <| br [] [] + +inputButton : String -> m -> List (Attribute m) -> Html m +inputButton val onch attrs = + input ([ type_ "button", class "submit", tabindex 10, value val, onClick onch] ++ attrs) [] + + -- Submit button with loading indicator and error message display submitButton : String -> Api.State -> Bool -> Bool -> Html m submitButton val state valid load = div [] diff --git a/elm/ULists/ManageLabels.elm b/elm/ULists/ManageLabels.elm new file mode 100644 index 00000000..f8f8bb6d --- /dev/null +++ b/elm/ULists/ManageLabels.elm @@ -0,0 +1,116 @@ +module ULists.ManageLabels exposing (main) + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) +import Browser +import Browser.Navigation exposing (reload) +import Json.Encode as JE +import Lib.Html exposing (..) +import Lib.Util exposing (..) +import Lib.Api as Api +import Gen.Api as GApi +import Gen.ManageLabels as GML + + +main : Program GML.Send Model Msg +main = Browser.element + { init = \e -> (init e, Cmd.none) + , view = view + , update = update + , subscriptions = always Sub.none + } + +type alias Model = + { uid : Int + , state : Api.State + , labels : List GML.SendLabels + , editing : Maybe Int + } + +init : GML.Send -> Model +init d = + { uid = d.uid + , state = Api.Normal + , labels = d.labels + , editing = Nothing + } + +type Msg + = Noop + | Private Int Bool + | Label Int String + | Delete Int (Maybe Int) + | Add + | Submit + | Submitted GApi.Response + + +update : Msg -> Model -> (Model, Cmd Msg) +update msg model = + case msg of + Noop -> (model, Cmd.none) + Private n b -> ({ model | labels = modidx n (\l -> { l | private = b }) model.labels }, Cmd.none) + Label n s -> ({ model | labels = modidx n (\l -> { l | label = s }) model.labels }, Cmd.none) + Delete n o -> ({ model | labels = List.filter (\l -> l.count > 0 || l.delete == Nothing) <| modidx n (\l -> { l | delete = o }) model.labels }, Cmd.none) + Add -> ({ model | labels = model.labels ++ [{ id = -1, label = "New label", private = List.all (\il -> il.private) model.labels, count = 0, delete = Nothing }] }, Cmd.none) + + Submit -> ({ model | state = Api.Loading }, Api.post "/u/ulist/labels.json" (GML.encode { uid = model.uid, labels = model.labels }) Submitted) + + Submitted GApi.Success -> (model, reload) + Submitted r -> ({ model | state = Api.Error r }, Cmd.none) + + +view : Model -> Html Msg +view model = + let + item n l = + let strid = "form_pr" ++ String.fromInt n + in tr [ class "compact" ] + [ td [] [ text <| if l.count == 0 then "" else String.fromInt l.count ] + , td [ class "stealth" ] + [ if l.id > 0 && l.id < 1000 then text l.label + else inputText "" l.label (Label n) GML.valLabelsLabel + ] + , td [ class "linkradio" ] [ inputCheck strid l.private (Private n), label [ for strid ] [ text "private" ] ] + , td [ class "stealth" ] + [ if l.id == 7 then b [ class "grayedout" ] [ text "applied when you vote" ] + else if l.id > 0 && l.id < 1000 then b [ class "grayedout" ] [ text "built-in" ] + else if l.delete == Nothing then a [ onClick (Delete n (Just 1)) ] [ text "remove" ] + else inputSelect "" l.delete (Delete n) [] + [ (Nothing, "Keep label") + , (Just 1, "Delete label but keep VNs in my list") + , (Just 2, "Delete label and VNs with only this label") + , (Just 3, "Delete label and all VNs with this label") + ] + ] + ] + in + Html.form [ onSubmit Submit, class "labeledit hidden" ] + [ div [ ] + [ b [] [ text "How to use labels" ] + , ul [] + [ li [] [ text "You can assign multiple labels to a visual novel" ] + , li [] [ text "You can create custom labels or just use the built-in labels" ] + , li [] [ text "Private labels will not be visible to other users" ] + , li [] [ text "Your vote and notes will be public when at least one non-private label has been assigned to the visual novel" ] + ] + ] + , table [ class "stripe" ] <| + [ thead [] [ tr [] + [ td [] [ text "VNs" ] + , td [] [ text "Label" ] + , td [] [ text "Private" ] + , td [] [ ] + ] ] + , tfoot [] [ tr [] + [ td [] [] + , td [ colspan 3 ] + [ a [ onClick Add ] [ text "New label" ] + --, inputButton "Save changes" Noop [] + , submitButton "Save changes" model.state True False + ] + ] ] + , tbody [] <| List.indexedMap item model.labels + ] + ] diff --git a/elm/ULists/ManageLabels.js b/elm/ULists/ManageLabels.js new file mode 100644 index 00000000..85312d21 --- /dev/null +++ b/elm/ULists/ManageLabels.js @@ -0,0 +1,6 @@ +document.querySelectorAll('#labeledit').forEach(function(b) { + b.onclick = function() { + document.querySelectorAll('.labeledit').forEach(function(e) { e.classList.toggle('hidden') }) + }; + return false; +}) |