module GHCJS.HPlay.Cell(Cell(..),boxCell,bcell,(.=),get,mkscell,scell, gcell, calc) where
import Transient.Internals
import GHCJS.HPlay.View
import Data.Typeable
import Unsafe.Coerce
import qualified Data.Map as M hiding ((!))
import Control.Monad.IO.Class
import Control.Monad
import Data.List
import Control.Exception
#ifdef ghcjs_HOST_OS
import Data.JSString hiding (empty)
#else
type JSString = String
#endif
data Cell a = Cell { mk :: Maybe a -> Widget a
, setter :: a -> IO ()
, getter :: IO (Maybe a)}
bcell :: (Show a, Read a, Typeable a) =>TransIO (Cell a)
bcell= genNewId >>= return . boxCell
boxCell :: (Show a, Read a, Typeable a) => ElemID -> Cell a
boxCell id = Cell{ mk= \mv -> getParam (Just id) "text" mv
, setter= \x -> do
me <- elemById id
case me of
Just e -> setProp e "value" (toJSString $ show1 x)
Nothing -> return ()
, getter= getID id}
getID id = withElem id $ \e -> do
ms <- getValue e
case ms of
Nothing -> return Nothing
Just s -> return $ read1 s
where
read1 s=
if typeOf(typeIO getID) /= typestring
then case readsPrec 0 s of
[(v,_)] -> v `seq` Just v
_ -> Nothing
else Just $ unsafeCoerce s
typeIO :: (ElemID -> IO (Maybe a)) -> a
typeIO = undefined
typestring= typeOf (undefined :: String)
show1 x= if typeOf x== typestring
then unsafeCoerce x
else show x
instance Attributable (Cell a) where
(Cell mk setter getter) ! atr = Cell (\ma -> mk ma ! atr) setter getter
(.=) :: MonadIO m => Cell a -> a -> m ()
(.=) cell x = liftIO $ (setter cell ) x
get cell = Transient $ liftIO (getter cell)
infixr 0 .=
gcell :: JSString -> Widget Double
gcell n= Widget $ do
Vars vars <- getSData <|> return(Vars M.empty )
case M.lookup n vars of
Just exp -> do inc n exp; exp
Nothing -> error $ "cell not found: " ++ show n
where
inc n exp= do
Tries tries maxtries<- getSData <|> do
Exprs exprs <- getSData
return . Tries 0 $ 3 * (M.size $ exprs)
if tries <= maxtries
then setData $ Tries (tries+1) maxtries
else back Loop
data Loop= Loop deriving (Show,Typeable)
instance Exception Loop
type Expr a = TransIO a
data Tries= Tries Int Int deriving Typeable
newtype Exprs= Exprs (M.Map JSString (Expr Double))
newtype Vars= Vars (M.Map JSString (Expr Double))
newtype Modified= Modified (M.Map JSString (Expr Double)) deriving Typeable
mkscell :: JSString -> Maybe Double -> Expr Double -> Widget Double
mkscell name val expr= mk (scell name expr) val
scell :: JSString -> Expr Double -> Cell Double
scell id expr= Cell{ mk= \mv -> Widget $ do
Exprs exprs <- getSData <|> return (Exprs M.empty)
setData . Exprs $ M.insert id expr exprs
r <- norender $ getParam (Just id) "text" mv `fire` OnKeyUp
Modified mod <- getSData <|> return(Modified M.empty)
setData . Modified $ M.insert id (return r) mod
return r
, setter= \x -> withElem id $ \e -> setProp e "value" (toJSString $ show1 x)
, getter= getID id}
calc :: Widget ()
calc= Widget $ do
st <- getCont
return() `onBack` (\(e::Loop) -> do removeVar st e; forward Loop )
Modified nvs <- getSData <|> error "no modified"
when (not $ M.null nvs) $ do
values <- calc1
mapM_ (\(n,v) -> boxCell n .= v) values
where
run' st x= runTransState st x >> return ()
calc1 :: TransIO [(JSString,Double)]
calc1= do
setData $ Tries 0
Exprs cells <- getSData <|> error "no exprs"
Modified nvs <- getSData <|> error "mo modified2"
setData . Vars $ M.union nvs cells
solve
solve :: TransIO [(JSString,Double)]
solve = do
Vars vars <- getSData <|> error "no vars"
mapM (solve1 vars) $ M.toList vars
where
solve1 vars (k,f)= do
x <- f
setData . Vars $ M.insert k (return x) vars
return (k,x) :: TransIO (JSString,Double)
removeVar st = \(e:: Loop) -> do
Modified nvs <- getSData <|> error "no modified 3"
Exprs exprs <- getSData <|> error " no Exprs2"
case M.keys exprs \\ M.keys nvs of
[] -> error "non solvable circularity in cell dependencies"
(name:_) -> do
mv <- liftIO $ getID name
case mv of
Nothing -> return ()
Just v -> do
setData . Modified $ M.insert name ( return v) nvs
return ()
norender calc
return ()