module Dingo.Internal.Widget ( getWidgetState , setWidgetState , widgetSelector -- Re-export , Widget(..) ) where import Control.Monad.Trans.Class (lift) import Data.Aeson (ToJSON(..)) import qualified Data.Text.Lazy as TL import Dingo.Selector import Dingo.Internal.Base (Command(..), widgetIdToLazyText) import Dingo.Internal.Callback (CallbackM, addCommand) import qualified Dingo.Internal.Session as S import Dingo.Internal.WidgetTypes -- | Widget selector. widgetSelector :: (Widget w s) => w -> SimpleSelector widgetSelector w = anyElement `withId` TL.concat ["i", widgetIdToLazyText $ getWidgetId w] -- | Update the value of a widget. This function pushes -- the new value to the browser. setWidgetState :: Widget w s => w -> s -> CallbackM () setWidgetState c s = do -- Update the session's idea of what the widget state is. lift $ S.setWidgetStateM c s -- Serialize to client. addCommand $ SetWidgetValue (getWidgetId c) (toJSON s) -- | Get the current value of widget. getWidgetState :: Widget w s => w -> CallbackM (Maybe s) getWidgetState c = lift $ S.getWidgetStateM c