on develop help sign in
Merge branch 'develop' of github.com:ryantrinkle/reflex-dom into develop

Conflicts: src/Reflex/Dom/Widget/Basic.hs

commit64c4303445021000e3d822d066f29399f655ff88 authorRyan Trinkle <ryan.trinkle@gmail.com> dateTue 24 Mar 2015, 14:44 (12 years ago) parentb10f2adf8f8af4
2 files changed+68−13
src/Reflex/Dom/Widget/Basic.hs+16−8 src/Reflex/Dom/Widget/Input.hs+52−5
src/Reflex/Dom/Widget/Basic.hs+16−8
@@ -347,14 +347,17 @@ deleteBetweenExclusive s e = do
347347-- | s and e must both be children of the same node and s must precede e
348348deleteBetweenInclusive :: (IsNode start, IsNode end) => start -> end -> IO ()
349349deleteBetweenInclusive s e = do
350− Just currentParent <- nodeGetParentNode e -- May be different than it was at initial construction, e.g., because the parent may have dumped us in from a DocumentFragment
351− let go = do
352− Just x <- nodeGetPreviousSibling e -- This can't be Nothing because we should hit 's' first
353− _ <- nodeRemoveChild currentParent $ Just x
354− when (unNode (toNode s) /= unNode (toNode x)) go
355− go
356− _ <- nodeRemoveChild currentParent $ Just e
357− return ()
350+ mCurrentParent <- nodeGetParentNode e -- May be different than it was at initial construction, e.g., because the parent may have dumped us in from a DocumentFragment
351+ case mCurrentParent of
352+ Nothing -> return () --TODO: Is this the right behavior?
353+ Just currentParent -> do
354+ let go = do
355+ Just x <- nodeGetPreviousSibling e -- This can't be Nothing because we should hit 's' first
356+ _ <- nodeRemoveChild currentParent $ Just x
357+ when (unNode (toNode s) /= unNode (toNode x)) go
358+ go
359+ _ <- nodeRemoveChild currentParent $ Just e
360+ return ()
358361
359362--------------------------------------------------------------------------------
360363-- Adapters
@@ -467,6 +470,11 @@ linkClass s c = do
467470link :: MonadWidget t m => String -> m (Link t)
468471link s = linkClass s ""
469472
473+button :: MonadWidget t m => String -> m (Event t ())
474+button s = do
475+ (e, _) <- el' "button" $ text s
476+ return $ _el_clicked e
477+
470478newtype Workflow t m a = Workflow { unWorkflow :: m (a, Event t (Workflow t m a)) }
471479
472480workflowView :: forall t m a. MonadWidget t m => Workflow t m a -> m (Event t a)
src/Reflex/Dom/Widget/Input.hs+52−5
@@ -1,4 +1,4 @@
1−{-# LANGUAGE ConstraintKinds, TypeFamilies, FlexibleContexts, DataKinds, GADTs, ScopedTypeVariables, FlexibleInstances, RecursiveDo #-}
1+{-# LANGUAGE ConstraintKinds, TypeFamilies, FlexibleContexts, DataKinds, GADTs, ScopedTypeVariables, FlexibleInstances, RecursiveDo, TemplateHaskell #-}
22module Reflex.Dom.Widget.Input where
33
44import Prelude
@@ -88,6 +88,10 @@ input' inputType eSetValue dAttrs = do
8888 return $ TextInput dValue eKeypress eKeydown eKeyup dFocus dMyElement
8989-}
9090
91+class HasValue a where
92+ type Value a :: *
93+ value :: a -> Value a
94+
9195data TextInput t
9296 = TextInput { _textInput_value :: Dynamic t String
9397 , _textInput_keypress :: Event t Int
@@ -97,20 +101,41 @@ data TextInput t
97101 , _textInput_element :: HTMLInputElement
98102 }
99103
104+instance HasValue (TextInput t) where
105+ type Value (TextInput t) = Dynamic t String
106+ value = _textInput_value
107+
100108textInput :: MonadWidget t m => m (TextInput t)
101109textInput = input' "text" "" never (constDyn $ Map.empty)
102110
103111textInputGetEnter :: Reflex t => TextInput t -> Event t ()
104112textInputGetEnter i = fmapMaybe (\n -> if n == keycodeEnter then Just () else Nothing) $ _textInput_keypress i
105113
114+data TextAreaConfig t
115+ = TextAreaConfig { _textAreaConfig_initialValue :: String
116+ , _textAreaConfig_setValue :: Event t String
117+ , _textAreaConfig_attributes :: Dynamic t (Map String String)
118+ }
119+
120+instance Reflex t => Default (TextAreaConfig t) where
121+ def = TextAreaConfig { _textAreaConfig_initialValue = ""
122+ , _textAreaConfig_setValue = never
123+ , _textAreaConfig_attributes = constDyn mempty
124+ }
125+
106126data TextArea t
107127 = TextArea { _textArea_value :: Dynamic t String
108128 , _textArea_element :: HTMLTextAreaElement
109129 , _textArea_hasFocus :: Dynamic t Bool
130+ , _textArea_keypress :: Event t Int
110131 }
111132
112−textArea :: MonadWidget t m => String -> Event t String -> Dynamic t (Map String String) -> m (TextArea t)
113−textArea initial eSet attrs = do
133+instance HasValue (TextArea t) where
134+ type Value (TextArea t) = Dynamic t String
135+ value = _textArea_value
136+
137+textArea :: MonadWidget t m => TextAreaConfig t -> m (TextArea t)
138+textArea (TextAreaConfig initial eSet attrs) = do
114139 e <- liftM castToHTMLTextAreaElement $ buildEmptyElement "textarea" attrs
115140 liftIO $ htmlTextAreaElementSetValue e initial
116141 postGui <- askPostGui
@@ -124,8 +149,9 @@ textArea initial eSet attrs = do
124149 performEvent_ $ fmap (liftIO . htmlTextAreaElementSetValue e) eSet
125150 f <- holdDyn False eChangeFocus
126151 ev <- wrapDomEvent e elementOninput $ liftIO $ htmlTextAreaElementGetValue e
127− v <- holdDyn "" ev
128− return $ TextArea v e f
152+ v <- holdDyn "" $ leftmost [eSet, ev]
153+ eKeypress <- wrapDomEvent e elementOnkeypress $ liftIO . uiEventGetKeyCode =<< event
154+ return $ TextArea v e f eKeypress
129155
130156{-
131157type family Controller sm t a where
@@ -293,3 +319,24 @@ searchInputResultsList' results builder = do
293319 resultsList <- elDynAttr "ul" attrs $ builder results
294320 liftM switch $ hold never $ fmap (leftmost . Map.elems) (updated resultsList)
295321
322+liftM concat $ mapM makeLenses
323+ [ ''TextAreaConfig
324+ , ''TextArea
325+ , ''TextInput
326+ ]
327+
328+class HasAttributes a where
329+ type Attrs a :: *
330+ attributes :: Lens' a (Attrs a)
331+
332+instance HasAttributes (TextAreaConfig t) where
333+ type Attrs (TextAreaConfig t) = Dynamic t (Map String String)
334+ attributes = textAreaConfig_attributes
335+
336+class HasSetValue a where
337+ type SetValue a :: *
338+ setValue :: Lens' a (SetValue a)
339+
340+instance HasSetValue (TextAreaConfig t) where
341+ type SetValue (TextAreaConfig t) = Event t String
342+ setValue = textAreaConfig_setValue