Skip to content

Instantly share code, notes, and snippets.

@kuon
Last active July 2, 2017 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuon/95b1bb43b7ec7f06c874e8f9975f7944 to your computer and use it in GitHub Desktop.
Save kuon/95b1bb43b7ec7f06c874e8f9975f7944 to your computer and use it in GitHub Desktop.
predicate
type Predicate
= And (List Predicate)
| Or (List Predicate)
| Not Predicate
| Condition String Op String
type Op
= Contains
| Equals
| GreaterThan
| LessThan
| EqualsOrGreaterThan
| EqualsOrLessThan
type Msg
= SetType String
empty : Predicate
empty =
And []
editor : Predicate -> Html Msg
editor model =
div [] [ predicate model ]
update : Msg -> Predicate -> ( Predicate, Cmd Msg )
update msg predicate =
case msg of
SetType newType ->
predicate => Cmd.none
predicate : Predicate -> Html Msg
predicate model =
let
subs =
case model of
And els ->
List.map predicate els
Or els ->
List.map predicate els
_ ->
[]
in
div []
[ select [ onInput SetType ]
[ typeOption (Not empty) model "Inverse"
, typeOption (And []) model "Toutes les"
, typeOption (Or []) model "Au moins une"
, typeOption (Condition "" Contains "") model "Contient"
]
, div [] subs
]
typeOption val currentValue lbl =
let
valueName =
typeToString val
currentValueName =
typeToString currentValue
sel =
valueName == currentValueName
in
option [ value valueName, selected sel ] [ text lbl ]
typeToString predicate =
case predicate of
And _ ->
"and"
Or _ ->
"or"
Not _ ->
"not"
Condition _ Contains _ ->
"contains"
Condition _ Equals _ ->
"equals"
Condition _ GreaterThan _ ->
"gt"
Condition _ LessThan _ ->
"lt"
Condition _ EqualsOrGreaterThan _ ->
"gte"
Condition _ EqualsOrLessThan _ ->
"lte"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment