Skip to content

Commit

Permalink
haste-lib: getContentWindow for postMessage API
Browse files Browse the repository at this point in the history
  • Loading branch information
valderman committed Jan 24, 2017
1 parent 8704564 commit d01c998
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion libraries/haste-lib/src/Haste/Events.hs
Expand Up @@ -18,7 +18,8 @@ module Haste.Events (
TouchEvent (..), TouchData (..), Touch (..),

-- * Messaging events and @postMessage@ API.
MessageEvent (..), MessageData (..), Window, postMessage, fromAny
MessageEvent (..), MessageData (..), Window,
postMessage, getContentWindow, fromAny
) where
import Haste.Events.Core as Core
import Haste.Events.BasicEvents as BasicEvents
Expand Down
11 changes: 10 additions & 1 deletion libraries/haste-lib/src/Haste/Events/MessageEvents.hs
Expand Up @@ -2,12 +2,13 @@
-- | Events relating to mouse keyboard input.
module Haste.Events.MessageEvents
( MessageEvent (..) , MessageData (..), Window
, postMessage, fromAny
, postMessage, getContentWindow, fromAny
) where
import Control.Monad.IO.Class
import Haste.JSString (JSString)
import Haste.Prim.Foreign
import Haste.Events.Core
import Haste.DOM.Core (Elem)

newtype Window = Window JSAny
deriving (ToAny, FromAny, Eq)
Expand Down Expand Up @@ -37,3 +38,11 @@ postMessage wnd msg = liftIO $ postMessage' wnd (toAny msg)

postMessage' :: Window -> JSAny -> IO ()
postMessage' = ffi "(function(w,m){w.postMessage(m,'*');})"

-- | Get the window object for an iframe. Must be called after the iframe is
-- attached to the DOM.
getContentWindow :: MonadIO m => Elem -> m (Maybe Window)
getContentWindow e = liftIO $ getContentWindow' e

getContentWindow' :: Elem -> IO (Maybe Window)
getContentWindow' = ffi "(function(e)){return e.contentWindow;}"

0 comments on commit d01c998

Please sign in to comment.