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

Commit 7b0eafb

Browse files
committed
Initial work on a more complete library
1 parent 8582dec commit 7b0eafb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2009
-148
lines changed

bower.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@
1818
"bower.json",
1919
"gulpfile.js",
2020
"package.json"
21-
]
21+
],
22+
"dependencies": {
23+
"purescript-unsafe-coerce": "^0.1.0",
24+
"purescript-enums": "^0.7.0",
25+
"purescript-foreign": "^0.7.0",
26+
"purescript-exceptions": "~0.3.0",
27+
"purescript-nullable": "~0.2.1"
28+
}
2229
}

docs/DOM.md

Lines changed: 0 additions & 35 deletions
This file was deleted.

docs/DOM/File.md

Lines changed: 0 additions & 37 deletions
This file was deleted.

docs/DOM/XHR.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

gulpfile.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ gulp.task("make", function() {
2929
return purescript.psc({ src: sources, ffi: foreigns });
3030
});
3131

32-
gulp.task("docs", ["clean-docs"], function () {
33-
return purescript.pscDocs({
34-
src: sources,
35-
docgen: {
36-
"DOM": "docs/DOM.md",
37-
"DOM.File": "docs/DOM/File.md",
38-
"DOM.XHR": "docs/DOM/XHR.md"
39-
}
40-
});
41-
});
32+
// gulp.task("docs", ["clean-docs"], function () {
33+
// return purescript.pscDocs({
34+
// src: sources,
35+
// docgen: {
36+
// "DOM": "docs/DOM.md",
37+
// "DOM.File": "docs/DOM/File.md",
38+
// "DOM.XHR": "docs/DOM/XHR.md"
39+
// }
40+
// });
41+
// });
4242

4343
gulp.task("dotpsci", function () {
4444
return purescript.psci({ src: sources, ffi: foreigns })
4545
.pipe(gulp.dest("."));
4646
});
4747

48-
gulp.task("default", ["make", "docs", "dotpsci"]);
48+
gulp.task("default", ["make", "dotpsci"]);
49+
// gulp.task("default", ["make", "docs", "dotpsci"]);

src/DOM.purs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
module DOM where
22

3-
-- | Effect type for DOM maniupulation
3+
-- | Effect type for when the DOM is being manipulated or mutable values are
4+
-- | being read from the DOM.
45
foreign import data DOM :: !
5-
6-
-- | General type for DOM documents.
7-
foreign import data Document :: *
8-
9-
-- | General type for DOM nodes.
10-
foreign import data Node :: *
11-
12-
-- | General type for DOM node lists.
13-
foreign import data NodeList :: *

src/DOM/Event/Event.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* global exports */
2+
"use strict";
3+
4+
// module DOM.Event.Event
5+
6+
exports.bubbles = function (e) {
7+
return e.bubbles;
8+
};
9+
10+
exports.cancelable = function (e) {
11+
return e.cancelable;
12+
};
13+
14+
exports.currentTarget = function (e) {
15+
return e.currentTarget;
16+
};
17+
18+
exports.defaultPrevented = function (e) {
19+
return e.defaultPrevented;
20+
};
21+
22+
exports.eventPhaseIndex = function (e) {
23+
return e.eventPhase;
24+
};
25+
26+
exports.target = function (e) {
27+
return e.target;
28+
};
29+
30+
exports.timeStamp = function (e) {
31+
return e.timeStamp;
32+
};
33+
34+
exports.type_ = function (e) {
35+
return e.type;
36+
};
37+
38+
exports.preventDefault = function (e) {
39+
return function () {
40+
return e.preventDefault();
41+
};
42+
};
43+
44+
exports.stopImmediatePropagation = function (e) {
45+
return function () {
46+
return e.stopImmediatePropagation();
47+
};
48+
};
49+
50+
exports.stopPropagation = function (e) {
51+
return function () {
52+
return e.stopPropagation();
53+
};
54+
};

src/DOM/Event/Event.purs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module DOM.Event.Event
2+
( bubbles
3+
, cancelable
4+
, currentTarget
5+
, defaultPrevented
6+
, eventPhase
7+
, eventPhaseIndex
8+
, target
9+
, timeStamp
10+
, type_
11+
, preventDefault
12+
, stopImmediatePropagation
13+
, stopPropagation
14+
) where
15+
16+
import Prelude
17+
18+
import Control.Monad.Eff (Eff())
19+
20+
import Data.Enum (toEnum)
21+
import qualified Data.Maybe.Unsafe as U
22+
23+
import DOM
24+
import DOM.Event.EventPhase
25+
import DOM.Event.Types
26+
import DOM.Node.Types
27+
28+
-- | The event type.
29+
foreign import type_ :: Event -> EventType
30+
31+
-- | The element that was the source of the event.
32+
foreign import target :: Event -> Node
33+
34+
-- | The element that the event listener was added to.
35+
foreign import currentTarget :: Event -> Node
36+
37+
-- | Indicates which phase of the event flow that is currently being processed
38+
-- | for the event.
39+
eventPhase :: Event -> EventPhase
40+
eventPhase = U.fromJust <<< toEnum <<< eventPhaseIndex
41+
42+
-- | The integer value for the current event phase.
43+
foreign import eventPhaseIndex :: Event -> Int
44+
45+
-- | Prevents the event from bubbling up to futher event listeners. Other event
46+
-- | listeners on the current target will still fire.
47+
foreign import stopPropagation :: Event -> Eff (dom :: DOM) Unit
48+
49+
-- | Prevents all other listeners for the event from being called. This includes
50+
-- | event listeners added to the current target after the current listener.
51+
foreign import stopImmediatePropagation :: Event -> Eff (dom :: DOM) Unit
52+
53+
-- | Indicates whether the event will bubble up through the DOM or not.
54+
foreign import bubbles :: Event -> Boolean
55+
56+
-- | Indicates whether the event can be cancelled.
57+
foreign import cancelable :: Event -> Boolean
58+
59+
-- | Cancels the event if it can be cancelled.
60+
foreign import preventDefault :: Event -> Eff (dom :: DOM) Unit
61+
62+
-- | Indicates whether `preventDefault` was called on the event.
63+
foreign import defaultPrevented :: Event -> Boolean
64+
65+
-- | The time in milliseconds between 01/01/1970 and when the event was
66+
-- | dispatched.
67+
foreign import timeStamp :: Event -> Number

src/DOM/Event/EventPhase.purs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module DOM.Event.EventPhase (EventPhase(..)) where
2+
3+
import Prelude
4+
5+
import Data.Enum (Enum, Cardinality(..), defaultSucc, defaultPred, toEnum, fromEnum)
6+
import Data.Maybe (Maybe(..))
7+
8+
data EventPhase
9+
= None
10+
| Capturing
11+
| AtTarget
12+
| Bubbling
13+
14+
instance eqEventPhase :: Eq EventPhase where
15+
eq None None = true
16+
eq Capturing Capturing = true
17+
eq AtTarget AtTarget = true
18+
eq Bubbling Bubbling = true
19+
eq _ _ = false
20+
21+
instance ordEventPhase :: Ord EventPhase where
22+
compare x y = compare (fromEnumEventPhase x) (fromEnumEventPhase y)
23+
24+
instance boundedEventPhase :: Bounded EventPhase where
25+
bottom = None
26+
top = Bubbling
27+
28+
instance boundedOrdEventPhase :: BoundedOrd EventPhase
29+
30+
instance enumEventPhase :: Enum EventPhase where
31+
cardinality = Cardinality 4
32+
succ = defaultSucc toEnumEventPhase fromEnumEventPhase
33+
pred = defaultPred toEnumEventPhase fromEnumEventPhase
34+
toEnum = toEnumEventPhase
35+
fromEnum = fromEnumEventPhase
36+
37+
toEnumEventPhase :: Int -> Maybe EventPhase
38+
toEnumEventPhase 0 = Just None
39+
toEnumEventPhase 1 = Just Capturing
40+
toEnumEventPhase 2 = Just AtTarget
41+
toEnumEventPhase 3 = Just Bubbling
42+
toEnumEventPhase _ = Nothing
43+
44+
fromEnumEventPhase :: EventPhase -> Int
45+
fromEnumEventPhase None = 0
46+
fromEnumEventPhase Capturing = 1
47+
fromEnumEventPhase AtTarget = 2
48+
fromEnumEventPhase Bubbling = 3

src/DOM/Event/EventTarget.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* global exports */
2+
"use strict";
3+
4+
// module DOM.Event.EventTarget
5+
6+
exports.eventListener = function (fn) {
7+
return function (event) {
8+
return fn(event)();
9+
};
10+
};
11+
12+
exports.addEventListener = function (type) {
13+
return function (listener) {
14+
return function (useCapture) {
15+
return function (target) {
16+
return function () {
17+
target.addEventListener(type, listener, useCapture);
18+
return {};
19+
};
20+
};
21+
};
22+
};
23+
};
24+
25+
exports.removeEventListener = function (type) {
26+
return function (listener) {
27+
return function (useCapture) {
28+
return function (target) {
29+
return function () {
30+
target.removeEventListener(type, listener, useCapture);
31+
return {};
32+
};
33+
};
34+
};
35+
};
36+
};
37+
38+
exports.dispatchEvent = function (event) {
39+
return function (target) {
40+
return function () {
41+
return target.dispatchEvent(event);
42+
};
43+
};
44+
};

0 commit comments

Comments
 (0)