mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-02-15 20:33:26 +00:00
Merge pull request #466 from eikek/wildcard-number-search
Allow to search with wildcard in a number custom field
This commit is contained in:
commit
436022ceee
@ -6,6 +6,7 @@ module Comp.CustomFieldInput exposing
|
||||
, init
|
||||
, initWith
|
||||
, update
|
||||
, updateSearch
|
||||
, view
|
||||
)
|
||||
|
||||
@ -137,7 +138,7 @@ initWith value =
|
||||
Data.CustomFieldType.Numeric ->
|
||||
let
|
||||
( fm, _ ) =
|
||||
updateFloatModel value.value string2Float identity
|
||||
updateFloatModel False value.value string2Float identity
|
||||
in
|
||||
NumberField fm
|
||||
|
||||
@ -145,6 +146,7 @@ initWith value =
|
||||
let
|
||||
( fm, _ ) =
|
||||
updateFloatModel
|
||||
False
|
||||
value.value
|
||||
Data.Money.fromString
|
||||
Data.Money.normalizeInput
|
||||
@ -170,6 +172,10 @@ initWith value =
|
||||
)
|
||||
|
||||
|
||||
|
||||
--- Update
|
||||
|
||||
|
||||
type FieldResult
|
||||
= NoResult
|
||||
| RemoveField
|
||||
@ -183,40 +189,18 @@ type alias UpdateResult =
|
||||
}
|
||||
|
||||
|
||||
updateFloatModel :
|
||||
String
|
||||
-> (String -> Result String Float)
|
||||
-> (String -> String)
|
||||
-> ( FloatModel, FieldResult )
|
||||
updateFloatModel msg parse normalize =
|
||||
case parse msg of
|
||||
Ok n ->
|
||||
( { input = normalize msg
|
||||
, result = Ok n
|
||||
}
|
||||
, Value (normalize msg)
|
||||
)
|
||||
|
||||
Err err ->
|
||||
( { input = msg
|
||||
, result = Err err
|
||||
}
|
||||
, NoResult
|
||||
)
|
||||
|
||||
|
||||
string2Float : String -> Result String Float
|
||||
string2Float str =
|
||||
case String.toFloat str of
|
||||
Just n ->
|
||||
Ok n
|
||||
|
||||
Nothing ->
|
||||
Err ("Not a number: " ++ str)
|
||||
|
||||
|
||||
update : Msg -> Model -> UpdateResult
|
||||
update msg model =
|
||||
update =
|
||||
update1 False
|
||||
|
||||
|
||||
updateSearch : Msg -> Model -> UpdateResult
|
||||
updateSearch =
|
||||
update1 True
|
||||
|
||||
|
||||
update1 : Bool -> Msg -> Model -> UpdateResult
|
||||
update1 forSearch msg model =
|
||||
case ( msg, model.fieldModel ) of
|
||||
( SetText str, TextField _ ) ->
|
||||
let
|
||||
@ -231,7 +215,7 @@ update msg model =
|
||||
( NumberMsg str, NumberField _ ) ->
|
||||
let
|
||||
( fm, res ) =
|
||||
updateFloatModel str string2Float identity
|
||||
updateFloatModel forSearch str string2Float identity
|
||||
|
||||
model_ =
|
||||
{ model | fieldModel = NumberField fm }
|
||||
@ -242,6 +226,7 @@ update msg model =
|
||||
let
|
||||
( fm, res ) =
|
||||
updateFloatModel
|
||||
forSearch
|
||||
str
|
||||
Data.Money.fromString
|
||||
Data.Money.normalizeInput
|
||||
@ -297,6 +282,45 @@ update msg model =
|
||||
UpdateResult model Cmd.none NoResult
|
||||
|
||||
|
||||
updateFloatModel :
|
||||
Bool
|
||||
-> String
|
||||
-> (String -> Result String Float)
|
||||
-> (String -> String)
|
||||
-> ( FloatModel, FieldResult )
|
||||
updateFloatModel forSearch msg parse normalize =
|
||||
let
|
||||
hasWildCards =
|
||||
String.startsWith "*" msg || String.endsWith "*" msg
|
||||
in
|
||||
if forSearch && hasWildCards then
|
||||
( { input = normalize msg
|
||||
, result = Ok 0
|
||||
}
|
||||
, Value (normalize msg)
|
||||
)
|
||||
|
||||
else
|
||||
case parse msg of
|
||||
Ok n ->
|
||||
( { input = normalize msg
|
||||
, result = Ok n
|
||||
}
|
||||
, Value (normalize msg)
|
||||
)
|
||||
|
||||
Err err ->
|
||||
( { input = msg
|
||||
, result = Err err
|
||||
}
|
||||
, NoResult
|
||||
)
|
||||
|
||||
|
||||
|
||||
--- View
|
||||
|
||||
|
||||
mkLabel : Model -> String
|
||||
mkLabel model =
|
||||
Maybe.withDefault model.field.name model.field.label
|
||||
@ -408,3 +432,17 @@ makeInput icon model =
|
||||
, removeButton ""
|
||||
, i [ class (iconOr <| Icons.customFieldType Data.CustomFieldType.Date) ] []
|
||||
]
|
||||
|
||||
|
||||
|
||||
--- Helper
|
||||
|
||||
|
||||
string2Float : String -> Result String Float
|
||||
string2Float str =
|
||||
case String.toFloat str of
|
||||
Just n ->
|
||||
Ok n
|
||||
|
||||
Nothing ->
|
||||
Err ("Not a number: " ++ str)
|
||||
|
@ -11,6 +11,7 @@ module Comp.CustomFieldMultiInput exposing
|
||||
, reset
|
||||
, setValues
|
||||
, update
|
||||
, updateSearch
|
||||
, view
|
||||
)
|
||||
|
||||
@ -148,7 +149,17 @@ mkItem f =
|
||||
|
||||
|
||||
update : Msg -> Model -> UpdateResult
|
||||
update msg model =
|
||||
update =
|
||||
update1 False
|
||||
|
||||
|
||||
updateSearch : Msg -> Model -> UpdateResult
|
||||
updateSearch =
|
||||
update1 True
|
||||
|
||||
|
||||
update1 : Bool -> Msg -> Model -> UpdateResult
|
||||
update1 forSearch msg model =
|
||||
case msg of
|
||||
CreateNewField ->
|
||||
UpdateResult model Cmd.none FieldCreateNew
|
||||
@ -241,7 +252,11 @@ update msg model =
|
||||
Just { field, inputModel } ->
|
||||
let
|
||||
res =
|
||||
Comp.CustomFieldInput.update lm inputModel
|
||||
if forSearch then
|
||||
Comp.CustomFieldInput.updateSearch lm inputModel
|
||||
|
||||
else
|
||||
Comp.CustomFieldInput.update lm inputModel
|
||||
|
||||
model_ =
|
||||
{ model
|
||||
|
@ -710,7 +710,7 @@ updateDrop ddm flags settings msg model =
|
||||
CustomFieldMsg lm ->
|
||||
let
|
||||
res =
|
||||
Comp.CustomFieldMultiInput.update lm model.customFieldModel
|
||||
Comp.CustomFieldMultiInput.updateSearch lm model.customFieldModel
|
||||
in
|
||||
{ model =
|
||||
{ model
|
||||
|
Loading…
Reference in New Issue
Block a user