Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
Move events to appropriate places
Browse files Browse the repository at this point in the history
  • Loading branch information
garyb committed Jun 11, 2016
1 parent 3b66568 commit 2a93d41
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 768 deletions.
21 changes: 21 additions & 0 deletions src/DOM/CSS/Event/Types.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module DOM.CSS.Event.Types
( TransitionEvent
, transitionEventToEvent
, readTransitionEvent
) where

import Data.Foreign (Foreign, F, unsafeReadTagged)
import Data.Foreign.Class (class IsForeign)
import DOM.Event.Types (Event)
import Unsafe.Coerce as U

foreign import data TransitionEvent :: *

transitionEventToEvent :: TransitionEvent -> Event
transitionEventToEvent = U.unsafeCoerce

readTransitionEvent :: Foreign -> F TransitionEvent
readTransitionEvent = unsafeReadTagged "TransitionEvent"

instance isForeignTransitionEvent :: IsForeign TransitionEvent where
read = readTransitionEvent
30 changes: 15 additions & 15 deletions src/DOM/Event/Event.purs
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
module DOM.Event.Event
( bubbles
, cancelable
, currentTarget
, defaultPrevented
, eventPhase
, eventPhaseIndex
, target
, timeStamp
, type_
, preventDefault
, stopImmediatePropagation
, stopPropagation
( module DOM.Event.Event
, module T
) where

import Prelude
Expand All @@ -20,6 +10,7 @@ import Data.Maybe (fromJust)
import DOM (DOM)
import DOM.Event.EventPhase (EventPhase)
import DOM.Event.Types (Event, EventType)
import DOM.Event.Types (Event) as T
import DOM.Node.Types (Node)

-- | The event type.
Expand All @@ -41,11 +32,17 @@ foreign import eventPhaseIndex :: Event -> Int

-- | Prevents the event from bubbling up to futher event listeners. Other event
-- | listeners on the current target will still fire.
foreign import stopPropagation :: forall eff. Event -> Eff (dom :: DOM | eff) Unit
foreign import stopPropagation
:: forall eff
. Event
-> Eff (dom :: DOM | eff) Unit

-- | Prevents all other listeners for the event from being called. This includes
-- | event listeners added to the current target after the current listener.
foreign import stopImmediatePropagation :: forall eff. Event -> Eff (dom :: DOM | eff) Unit
foreign import stopImmediatePropagation
:: forall eff
. Event
-> Eff (dom :: DOM | eff) Unit

-- | Indicates whether the event will bubble up through the DOM or not.
foreign import bubbles :: Event -> Boolean
Expand All @@ -54,7 +51,10 @@ foreign import bubbles :: Event -> Boolean
foreign import cancelable :: Event -> Boolean

-- | Cancels the event if it can be cancelled.
foreign import preventDefault :: forall eff. Event -> Eff (dom :: DOM | eff) Unit
foreign import preventDefault
:: forall eff
. Event
-> Eff (dom :: DOM | eff) Unit

-- | Indicates whether `preventDefault` was called on the event.
foreign import defaultPrevented :: Event -> Boolean
Expand Down
28 changes: 15 additions & 13 deletions src/DOM/Event/EventPhase.purs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module DOM.Event.EventPhase (EventPhase(..)) where
module DOM.Event.EventPhase where

import Prelude
import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), defaultSucc, defaultPred)
Expand All @@ -11,9 +11,7 @@ data EventPhase
| Bubbling

derive instance eqEventPhase :: Eq EventPhase

instance ordEventPhase :: Ord EventPhase where
compare x y = compare (fromEnumEventPhase x) (fromEnumEventPhase y)
derive instance ordEventPhase :: Ord EventPhase

instance boundedEventPhase :: Bounded EventPhase where
bottom = None
Expand All @@ -29,14 +27,18 @@ instance boundedEnumEventPhase :: BoundedEnum EventPhase where
fromEnum = fromEnumEventPhase

toEnumEventPhase :: Int -> Maybe EventPhase
toEnumEventPhase 0 = Just None
toEnumEventPhase 1 = Just Capturing
toEnumEventPhase 2 = Just AtTarget
toEnumEventPhase 3 = Just Bubbling
toEnumEventPhase _ = Nothing
toEnumEventPhase =
case _ of
0 -> Just None
1 -> Just Capturing
2 -> Just AtTarget
3 -> Just Bubbling
_ -> Nothing

fromEnumEventPhase :: EventPhase -> Int
fromEnumEventPhase None = 0
fromEnumEventPhase Capturing = 1
fromEnumEventPhase AtTarget = 2
fromEnumEventPhase Bubbling = 3
fromEnumEventPhase =
case _ of
None -> 0
Capturing -> 1
AtTarget -> 2
Bubbling -> 3
27 changes: 23 additions & 4 deletions src/DOM/Event/EventTarget.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,34 @@ import DOM.Event.Types (EventTarget, Event, EventType)
foreign import data EventListener :: # ! -> *

-- | Creates an EventListener from a normal PureScript Eff function.
foreign import eventListener :: forall eff a. (Event -> Eff eff a) -> EventListener eff
foreign import eventListener
:: forall eff a
. (Event -> Eff eff a)
-> EventListener eff

-- | Adds a listener to an event target. The boolean argument indicates whether
-- | the listener should be added for the "capture" phase.
foreign import addEventListener :: forall eff. EventType -> EventListener (dom :: DOM | eff) -> Boolean -> EventTarget -> Eff (dom :: DOM | eff) Unit
foreign import addEventListener
:: forall eff
. EventType
-> EventListener (dom :: DOM | eff)
-> Boolean
-> EventTarget
-> Eff (dom :: DOM | eff) Unit

-- | Removes a listener to an event target. The boolean argument indicates
-- | whether the listener should be removed for the "capture" phase.
foreign import removeEventListener :: forall eff. EventType -> EventListener (dom :: DOM | eff) -> Boolean -> EventTarget -> Eff (dom :: DOM | eff) Unit
foreign import removeEventListener
:: forall eff
. EventType
-> EventListener (dom :: DOM | eff)
-> Boolean
-> EventTarget
-> Eff (dom :: DOM | eff) Unit

-- | Dispatches an event from an event target.
foreign import dispatchEvent :: forall eff. Event -> EventTarget -> Eff (dom :: DOM, err :: EXCEPTION | eff) Boolean
foreign import dispatchEvent
:: forall eff
. Event
-> EventTarget
-> Eff (dom :: DOM, err :: EXCEPTION | eff) Boolean
4 changes: 1 addition & 3 deletions src/DOM/Event/Types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
exports._readEventTarget = function (left) {
return function (right) {
return function (foreign) {
return foreign instanceof EventTarget
? left("Value is not an EventTarget")
: right(foreign);
return foreign instanceof EventTarget ? left("Value is not an EventTarget") : right(foreign);
};
};
};
Loading

0 comments on commit 2a93d41

Please sign in to comment.