diff options
author | Yorhel <git@yorhel.nl> | 2020-02-23 15:07:31 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2020-02-23 15:17:30 +0100 |
commit | 63c2f525b8e600c3577ce46587137a61635bdf27 (patch) | |
tree | 0b9fb5574c8fca52e84b60d6c9bf377b038d2db2 /elm/Lib | |
parent | c798f672bab6175c0a1db10a57e7cfa60ea48896 (diff) |
v2rw/RelEdit: Instant autocompletion for the engine field
So much more convenient. \o/
Diffstat (limited to 'elm/Lib')
-rw-r--r-- | elm/Lib/Api.elm | 1 | ||||
-rw-r--r-- | elm/Lib/Autocomplete.elm | 58 |
2 files changed, 27 insertions, 32 deletions
diff --git a/elm/Lib/Api.elm b/elm/Lib/Api.elm index ce0bd1d1..1863cbf9 100644 --- a/elm/Lib/Api.elm +++ b/elm/Lib/Api.elm @@ -45,7 +45,6 @@ showResponse res = Releases _ -> unexp BoardResult _ -> unexp TagResult _ -> unexp - EngineResult _ -> unexp expectResponse : (Response -> msg) -> Http.Expect msg diff --git a/elm/Lib/Autocomplete.elm b/elm/Lib/Autocomplete.elm index f3edabd6..733a1175 100644 --- a/elm/Lib/Autocomplete.elm +++ b/elm/Lib/Autocomplete.elm @@ -1,11 +1,11 @@ module Lib.Autocomplete exposing ( Config + , SearchSource(..) , SourceConfig , Model , Msg , boardSource , tagSource - , engineSource , init , clear , update @@ -28,7 +28,6 @@ import Gen.Types exposing (boardTypes) import Gen.Api as GApi import Gen.Boards as GB import Gen.Tags as GT -import Gen.Engines as GE type alias Config m a = @@ -41,11 +40,16 @@ type alias Config m a = } +type SearchSource m a + -- API endpoint to query for completion results + Function to decode results from the API + = Endpoint (String -> (GApi.Response -> m) -> Cmd m) (GApi.Response -> Maybe (List a)) + -- Pure function for instant completion results + | Func (String -> List a) + + type alias SourceConfig m a = - -- API endpoint to query for completion results. - { endpoint : String -> (GApi.Response -> m) -> Cmd m - -- How to decode results from the API - , decode : GApi.Response -> Maybe (List a) + -- Where to get completion results from + { source : SearchSource m a -- How to display the decoded results , view : a -> List (Html m) -- Unique ID of an item (must not be an empty string). @@ -55,11 +59,10 @@ type alias SourceConfig m a = } - boardSource : SourceConfig m GApi.ApiBoardResult boardSource = - { endpoint = \s -> GB.send { search = s } - , decode = \x -> case x of + { source = Endpoint (\s -> GB.send { search = s }) + <| \x -> case x of GApi.BoardResult e -> Just e _ -> Nothing , view = (\i -> @@ -74,8 +77,8 @@ boardSource = tagSource : SourceConfig m GApi.ApiTagResult tagSource = - { endpoint = \s -> GT.send { search = s } - , decode = \x -> case x of + { source = Endpoint (\s -> GT.send { search = s }) + <| \x -> case x of GApi.TagResult e -> Just e _ -> Nothing , view = \i -> @@ -92,17 +95,6 @@ tagSource = } -engineSource : SourceConfig m GApi.ApiEngineResult -engineSource = - { endpoint = \s -> GE.send { search = s } - , decode = \x -> case x of - GApi.EngineResult e -> Just e - _ -> Nothing - , view = \i -> [ text i.engine, b [ class "grayedout" ] [ text <| " (" ++ String.fromInt i.count ++ ")" ] ] - , key = \i -> i.engine - } - - type alias Model a = { visible : Bool , value : String @@ -191,21 +183,25 @@ update cfg msg model = Input s -> if String.trim s == "" then mod { model | value = s, default = "", loading = False, results = [] } - else ( { model | value = s, default = "", loading = True, wait = model.wait + 1 } - , Task.perform (always <| cfg.wrap <| Search <| model.wait + 1) (Process.sleep 500) - , Nothing ) + else case cfg.source.source of + Endpoint _ _ -> + ( { model | value = s, default = "", loading = True, wait = model.wait + 1 } + , Task.perform (always <| cfg.wrap <| Search <| model.wait + 1) (Process.sleep 500) + , Nothing ) + Func f -> mod { model | value = s, default = "", results = f s } Search i -> if model.value == "" || model.wait /= i then mod model - else ( model - , cfg.source.endpoint model.value (cfg.wrap << Results model.value) - , Nothing ) + else case cfg.source.source of + Endpoint e _ -> (model, e model.value (cfg.wrap << Results model.value), Nothing) + Func _ -> mod model Results s r -> mod <| - if s == model.value - then { model | loading = False, results = cfg.source.decode r |> Maybe.withDefault [] } - else model -- Discard stale results + if s /= model.value then model -- Discard stale results + else case cfg.source.source of + Endpoint _ d -> { model | loading = False, results = d r |> Maybe.withDefault [] } + Func _ -> model view : Config m a -> Model a -> List (Attribute m) -> Html m |