module Dingo.Internal.Base
( WidgetId
, CallbackId
, Command(..)
, callbackIdParser
, succCallbackId
, succWidgetId
, widgetIdParser
, widgetIdToLazyText
, zeroCallbackId
, zeroWidgetId
) where
import Data.Aeson (Value)
import Data.Attoparsec (Parser)
import Data.Attoparsec.Char8 (decimal)
import Data.Hashable (Hashable(..))
import Data.Text (Text)
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder.Int as TLBI
import Dingo.Internal.EventTypes (Event(..))
import Dingo.Selector
import Text.Blaze (Html, ToValue(..))
import Text.Julius (JavascriptUrl, ToJavascript(..))
newtype WidgetId = WidgetId Int
deriving (Show, Eq, Ord)
succWidgetId :: WidgetId -> WidgetId
succWidgetId (WidgetId i) = WidgetId (i+1)
zeroWidgetId :: WidgetId
zeroWidgetId = WidgetId 0
widgetIdParser :: Parser WidgetId
widgetIdParser = fmap WidgetId decimal
widgetIdToLazyText :: WidgetId -> TL.Text
widgetIdToLazyText (WidgetId i) = TL.pack $ show i
instance Hashable WidgetId where
hash (WidgetId i) = hash i
newtype CallbackId = CallbackId Int
deriving (Show, Eq, Ord)
succCallbackId :: CallbackId -> CallbackId
succCallbackId (CallbackId i) = CallbackId (i+1)
zeroCallbackId :: CallbackId
zeroCallbackId = CallbackId 0
callbackIdParser :: Parser CallbackId
callbackIdParser = fmap CallbackId decimal
data Command = CallbackBySelector Event Selector CallbackId
| SetWidgetValue WidgetId Value
| SetTitle Text
| AppendToWidgetChildren WidgetId Html
| ReplaceWidgetChildren WidgetId Html
| HeadMerge Html
| AddEncoderDecoderFunctions WidgetId (JavascriptUrl Text) (JavascriptUrl Text)
| TriggerEvent WidgetId Event
| RawJavascript (JavascriptUrl Text)
instance ToValue WidgetId where
toValue (WidgetId widgetId) = toValue $ 'i' : show widgetId
instance ToJavascript WidgetId where
toJavascript (WidgetId i) = TLBI.decimal i
instance ToJavascript CallbackId where
toJavascript (CallbackId i) = TLBI.decimal i