@@ -5,12 +5,12 @@ port module Components.SearchDialog exposing
5
5
, SearchResult
6
6
, hide
7
7
, init
8
- , makeFirstSearchResultActive
9
8
, onChangeSearchString
10
9
, onHide
11
10
, onShow
12
11
, searchResult
13
12
, searchResultHref
13
+ , searchStringWasJustUpdated
14
14
, show
15
15
, update
16
16
, view
@@ -51,6 +51,7 @@ type Model parentMsg
51
51
{ idPrefix : String
52
52
, visibility : GradualVisibility
53
53
, config : Config parentMsg
54
+ , searchStringWasJustUpdated : Bool
54
55
, activeSearchResultIndex : Maybe SearchResultIndex
55
56
}
56
57
@@ -85,6 +86,7 @@ init idPrefix properties =
85
86
{ idPrefix = idPrefix
86
87
, visibility = Invisible
87
88
, config = configFromProperties properties
89
+ , searchStringWasJustUpdated = False
88
90
, activeSearchResultIndex = Nothing
89
91
}
90
92
@@ -95,6 +97,7 @@ innerModel :
95
97
{ idPrefix : String
96
98
, visibility : GradualVisibility
97
99
, config : Config parentMsg
100
+ , searchStringWasJustUpdated : Bool
98
101
, activeSearchResultIndex : Maybe SearchResultIndex
99
102
}
100
103
innerModel model =
@@ -122,7 +125,9 @@ type Msg
122
125
| Show
123
126
| StartHiding
124
127
| CompleteHiding
125
- | MakeSearchResultActive SearchResultIndex
128
+ | SearchStringWasJustUpdated
129
+ | SearchStringWasNotJustUpdated
130
+ | MakeSearchResultActive Bool SearchResultIndex
126
131
| MakeSearchResultInactive SearchResultIndex
127
132
| MakePreviousOrNextSearchResultActive Int Bool
128
133
| LoadUrl String
@@ -139,15 +144,21 @@ hide =
139
144
StartHiding
140
145
141
146
142
- makeFirstSearchResultActive : Msg
143
- makeFirstSearchResultActive =
144
- MakeSearchResultActive 0
147
+ searchStringWasJustUpdated : Msg
148
+ searchStringWasJustUpdated =
149
+ SearchStringWasJustUpdated
145
150
146
151
147
152
update : (Model parentMsg -> parentModel ) -> (Msg -> parentMsg ) -> Msg -> Model parentMsg -> ( parentModel , Cmd parentMsg )
148
153
update updateParentModel toParentMsg msg model =
149
154
let
150
- model_ : { idPrefix : String , visibility : GradualVisibility , config : Config parentMsg, activeSearchResultIndex : Maybe SearchResultIndex }
155
+ model_ :
156
+ { idPrefix : String
157
+ , visibility : GradualVisibility
158
+ , config : Config parentMsg
159
+ , searchStringWasJustUpdated : Bool
160
+ , activeSearchResultIndex : Maybe SearchResultIndex
161
+ }
151
162
model_ =
152
163
innerModel model
153
164
@@ -175,11 +186,30 @@ update updateParentModel toParentMsg msg model =
175
186
|> Maybe . withDefault Cmd . none
176
187
)
177
188
178
- MakeSearchResultActive searchResultIndex ->
179
- ( { model_ | activeSearchResultIndex = Just searchResultIndex }
180
- , scrollSearchResultIntoView <| searchResultId searchResultIndex model_. idPrefix
189
+ SearchStringWasJustUpdated ->
190
+ ( { model_
191
+ | searchStringWasJustUpdated = True
192
+ , activeSearchResultIndex = Just 0
193
+ }
194
+ , Process . sleep 200
195
+ |> Task . perform ( always <| SearchStringWasNotJustUpdated )
196
+ |> Cmd . map toParentMsg
181
197
)
182
198
199
+ SearchStringWasNotJustUpdated ->
200
+ ( { model_ | searchStringWasJustUpdated = False }, Cmd . none )
201
+
202
+ MakeSearchResultActive becauseOfMouseEnter searchResultIndex ->
203
+ if becauseOfMouseEnter && model_. searchStringWasJustUpdated then
204
+ -- Prevent a search result being made active just because the mouse pointer was already over it.
205
+ -- We want to keep the first result (if any) active as the search string was just updated.
206
+ ( model_, Cmd . none )
207
+
208
+ else
209
+ ( { model_ | activeSearchResultIndex = Just searchResultIndex }
210
+ , scrollSearchResultIntoView <| searchResultId searchResultIndex model_. idPrefix
211
+ )
212
+
183
213
MakeSearchResultInactive searchResultIndex ->
184
214
( { model_
185
215
| activeSearchResultIndex =
@@ -263,13 +293,15 @@ loadUrl :
263
293
{ idPrefix : String
264
294
, visibility : GradualVisibility
265
295
, config : Config parentMsg
296
+ , searchStringWasJustUpdated : Bool
266
297
, activeSearchResultIndex : Maybe SearchResultIndex
267
298
}
268
299
-> String
269
300
->
270
301
( { idPrefix : String
271
302
, visibility : GradualVisibility
272
303
, config : Config parentMsg
304
+ , searchStringWasJustUpdated : Bool
273
305
, activeSearchResultIndex : Maybe SearchResultIndex
274
306
}
275
307
, Cmd parentMsg
@@ -374,6 +406,7 @@ view toParentMsg model searchString messageAboveSearchResults searchResults =
374
406
{ idPrefix : String
375
407
, visibility : GradualVisibility
376
408
, config : Config parentMsg
409
+ , searchStringWasJustUpdated : Bool
377
410
, activeSearchResultIndex : Maybe SearchResultIndex
378
411
}
379
412
model_ =
@@ -467,13 +500,13 @@ view toParentMsg model searchString messageAboveSearchResults searchResults =
467
500
468
501
else if event == Extras . HtmlEvents . home then
469
502
Just
470
- ( toParentMsg <| MakeSearchResultActive 0
503
+ ( toParentMsg <| MakeSearchResultActive False 0
471
504
, True
472
505
)
473
506
474
507
else if event == Extras . HtmlEvents . end then
475
508
Just
476
- ( toParentMsg <| MakeSearchResultActive ( List . length searchResults - 1 )
509
+ ( toParentMsg <| MakeSearchResultActive False ( List . length searchResults - 1 )
477
510
, True
478
511
)
479
512
@@ -511,7 +544,7 @@ view toParentMsg model searchString messageAboveSearchResults searchResults =
511
544
, Accessibility . Role . option
512
545
, Accessibility . Key . tabbable False
513
546
, Html . Attributes . id <| searchResultId index model_. idPrefix
514
- , Html . Events . onMouseEnter ( toParentMsg <| MakeSearchResultActive index)
547
+ , Html . Events . onMouseEnter ( toParentMsg <| MakeSearchResultActive True index)
515
548
, Html . Events . onMouseLeave ( toParentMsg <| MakeSearchResultInactive index)
516
549
]
517
550
[ Html . a
0 commit comments