summaryrefslogtreecommitdiff
path: root/elm/Lib
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-10-16 10:31:24 +0200
committerYorhel <git@yorhel.nl>2019-10-16 15:29:38 +0200
commit678f511619708ba893cb2414eead90cdae685708 (patch)
tree2c79c111805f38454e07d96645f3fdc31fe75860 /elm/Lib
parent1fb8a234cf5a455af6d78c893320b21de8347bc4 (diff)
v2rw: Convert staff adding/editing form
This is the first major editing form to be converted, so I'm expecting a little breakage. A good chunk of this code has been copied from v3. In terms of the UI there has been a small change: aliases that are still referenced do not have the 'remove' link and instead have a flag that shows that they are still referenced. This ought to be a bit friendlier than throwing an error message after the user has submitted the form. Some other things I'd like to improve in this form: - BBCode preview - Pasting in external links and letting the form figure out the Pixiv ID, etc. - Or perhaps even: Integrate AniDB/Wikidata search/autocompletion.
Diffstat (limited to 'elm/Lib')
-rw-r--r--elm/Lib/Html.elm44
-rw-r--r--elm/Lib/Util.elm32
2 files changed, 67 insertions, 9 deletions
diff --git a/elm/Lib/Html.elm b/elm/Lib/Html.elm
index d2588799..fe67da32 100644
--- a/elm/Lib/Html.elm
+++ b/elm/Lib/Html.elm
@@ -82,6 +82,8 @@ inputTextArea : String -> String -> (String -> m) -> List (Attribute m) -> Html
inputTextArea nam val onch attrs = textarea (
[ tabindex 10
, onInput onch
+ , rows 4
+ , cols 50
]
++ attrs
++ (if nam == "" then [] else [ id nam, name nam ])
@@ -99,12 +101,33 @@ inputCheck nam val onch = input (
) []
+inputRadio : String -> Bool -> (Bool -> m) -> Html m
+inputRadio nam val onch = input (
+ [ type_ "radio"
+ , tabindex 10
+ , onCheck onch
+ , checked val
+ ]
+ ++ (if nam == "" then [] else [ name nam ])
+ ) []
+
+
+-- Same as an inputText, but formats/parses an integer as Q###
+inputWikidata : String -> Int -> (Int -> m) -> Html m
+inputWikidata nam val onch =
+ inputText nam
+ (if val == 0 then "" else "Q" ++ String.fromInt val)
+ (\v -> onch <| if v == "" then 0 else Maybe.withDefault val <| String.toInt <| if String.startsWith "Q" v then String.dropLeft 1 v else v)
+ [ pattern "^Q?[1-9][0-9]{0,8}$" ]
+
+
-- Generate a form field (table row) with a label. The `label` string can be:
--
--- "none" -> To generate a full-width field (colspan=2)
--- "" -> Empty label
--- "Some string" -> Text label
--- "input::String" -> Label that refers to the named input
+-- "none" -> To generate a full-width field (colspan=2)
+-- "" -> Empty label
+-- "Some string" -> Text label
+-- "Some string#eng" -> Text label with (English please!) message
+-- "input::String" -> Label that refers to the named input (also supports #eng)
--
-- (Yeah, stringly typed arguments; I wish Elm had typeclasses)
formField : String -> List (Html m) -> Html m
@@ -113,10 +136,13 @@ formField lbl cont =
[ if lbl == "none"
then text ""
else
- td [ class "label" ]
- [ case String.split "::" lbl of
- [name, txt] -> label [ for name ] [ text txt ]
- txt -> text <| String.concat txt
- ]
+ let
+ (nlbl, eng) = if String.endsWith "#eng" lbl then (String.dropRight 4 lbl, True) else (lbl, False)
+ genlbl str = text str :: if eng then [ br [] [], b [ class "standout" ] [ text "English please!" ] ] else []
+ in
+ td [ class "label" ] <|
+ case String.split "::" nlbl of
+ [name, txt] -> [ label [ for name ] (genlbl txt) ]
+ txt -> genlbl (String.concat txt)
, td (class "field" :: if lbl == "none" then [ colspan 2 ] else []) cont
]
diff --git a/elm/Lib/Util.elm b/elm/Lib/Util.elm
new file mode 100644
index 00000000..186cf365
--- /dev/null
+++ b/elm/Lib/Util.elm
@@ -0,0 +1,32 @@
+module Lib.Util exposing (..)
+
+import Dict
+
+-- Delete an element from a List
+delidx : Int -> List a -> List a
+delidx n l = List.take n l ++ List.drop (n+1) l
+
+
+-- Modify an element in a List
+modidx : Int -> (a -> a) -> List a -> List a
+modidx n f = List.indexedMap (\i e -> if i == n then f e else e)
+
+
+isJust : Maybe a -> Bool
+isJust m = case m of
+ Just _ -> True
+ _ -> False
+
+
+-- Returns true if the list contains duplicates
+hasDuplicates : List comparable -> Bool
+hasDuplicates l =
+ let
+ step e acc =
+ case acc of
+ Nothing -> Nothing
+ Just m -> if Dict.member e m then Nothing else Just (Dict.insert e True m)
+ in
+ case List.foldr step (Just Dict.empty) l of
+ Nothing -> True
+ Just _ -> False