@@ -7,6 +7,7 @@ import Prelude
7import CSS (border, borderRadius, color, em, float, floatLeft, fontWeight, lineHeight, marginBottom, marginLeft, paddingBottom, paddingLeft, paddingRight, paddingTop, pct, px, rgb, solid, weight, width)
7import CSS (border, borderRadius, color, em, float, floatLeft, fontWeight, lineHeight, marginBottom, marginLeft, paddingBottom, paddingLeft, paddingRight, paddingTop, pct, px, rgb, solid, weight, width)
8import Data.Maybe (Maybe(..), maybe)
8import Data.Maybe (Maybe(..), maybe)
9import Data.Newtype (wrap)
9import Data.Newtype (wrap)
10+import Docs.Search.URIHash as URIHash
10import Effect (Effect)
11import Effect (Effect)
11import Effect.Aff (Aff)
12import Effect.Aff (Aff)
12import Halogen as H
13import Halogen as H
@@ -38,21 +39,34 @@ data Action
38 | InitKeyboardListener
39 | InitKeyboardListener
39 | HandleKey H.SubscriptionId KeyboardEvent
40 | HandleKey H.SubscriptionId KeyboardEvent
41data SearchFieldMessage
45data SearchFieldMessage
42 = InputUpdated String
46 = InputUpdated String
43 | InputCleared
47 | InputCleared
45 | LostFocus
49 | LostFocus
47−component :: forall q i. H.Component HH.HTML q i SearchFieldMessage Aff
51+component :: forall i. H.Component HH.HTML Query i SearchFieldMessage Aff
48component =
52component =
49 H.mkComponent
53 H.mkComponent
50 { initialState
54 { initialState
52 , eval: H.mkEval $ H.defaultEval { handleAction = handleAction
56 , eval: H.mkEval $ H.defaultEval { handleAction = handleAction
57+ , handleQuery = handleQuery
53 , initialize = Just InitKeyboardListener }
58 , initialize = Just InitKeyboardListener }
64+ -> H.HalogenM State Action () SearchFieldMessage Aff (Maybe a)
65+handleQuery (ReadURIHash next) = do
67+ H.raise (InputUpdated state.input)
56initialState :: forall i. i -> State
70initialState :: forall i. i -> State
57initialState _ = { input: "", focused: false }
71initialState _ = { input: "", focused: false }
@@ -60,6 +74,10 @@ handleAction :: Action -> H.HalogenM State Action () SearchFieldMessage Aff Unit
60handleAction = case _ of
74handleAction = case _ of
62 InitKeyboardListener -> do
76 InitKeyboardListener -> do
78+ input <- H.liftEffect URIHash.getInput
79+ H.modify_ (_ { input = input })
63 document <- H.liftEffect $ Web.document =<< Web.window
81 document <- H.liftEffect $ Web.document =<< Web.window
64 H.subscribe' \sid ->
82 H.subscribe' \sid ->
65 ES.eventListenerEventSource
83 ES.eventListenerEventSource
@@ -82,9 +100,7 @@ handleAction = case _ of
83 H.liftEffect do
101 H.liftEffect do
84 withSearchField (HTMLInputElement.toHTMLElement >>> Web.blur)
102 withSearchField (HTMLInputElement.toHTMLElement >>> Web.blur)
85− else do
103+ else clearInput
86− H.modify_ (_ { input = "" })
87− H.raise $ InputCleared
89 InputAction input -> do
105 InputAction input -> do
90 H.modify_ $ (_ { input = input })
106 H.modify_ $ (_ { input = input })
@@ -93,6 +109,7 @@ handleAction = case _ of
93 state <- H.get
109 state <- H.get
94 H.liftEffect do
110 H.liftEffect do
95 withSearchField (HTMLInputElement.toHTMLElement >>> Web.blur)
111 withSearchField (HTMLInputElement.toHTMLElement >>> Web.blur)
112+ H.liftEffect (URIHash.setInput state.input)
96 H.raise $ InputUpdated state.input
113 H.raise $ InputUpdated state.input
98 FocusChanged status -> do
115 FocusChanged status -> do
@@ -102,6 +119,12 @@ handleAction = case _ of
102 then Focused
119 then Focused
103 else LostFocus
120 else LostFocus
122+clearInput :: H.HalogenM State Action () SearchFieldMessage Aff Unit
124+ H.modify_ (_ { input = "" })
125+ H.liftEffect URIHash.removeHash
126+ H.raise InputCleared
105withSearchField :: (HTML.HTMLInputElement -> Effect Unit) -> Effect Unit
128withSearchField :: (HTML.HTMLInputElement -> Effect Unit) -> Effect Unit
106withSearchField cont = do
129withSearchField cont = do
107 doc <- Document.toParentNode <$>
130 doc <- Document.toParentNode <$>