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

Commit 9174791

Browse files
authored
Merge pull request #129 from purescript-web/readyState
Add document ReadyState
2 parents 52bfe8f + 92c7b6a commit 9174791

File tree

8 files changed

+195
-146
lines changed

8 files changed

+195
-146
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
55
"build": "eslint src && pulp build -- --censor-lib --strict",
6-
"test": "PHANTOM_TEST_PATH=$(pwd) pulp test --runtime phantomjs"
6+
"test": "pulp test --runtime phantomjs"
77
},
88
"devDependencies": {
99
"eslint": "^3.19.0",

src/DOM/HTML/Document.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ exports._body = function (doc) {
55
return doc.body;
66
};
77
};
8+
9+
exports._readyState = function (doc) {
10+
return function () {
11+
return doc.readyState;
12+
};
13+
};

src/DOM/HTML/Document.purs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
module DOM.HTML.Document
22
( body
3+
, readyState
4+
, module Exports
35
) where
46

57
import Prelude
8+
69
import Control.Monad.Eff (Eff)
7-
import Data.Maybe (Maybe)
8-
import Data.Nullable (Nullable, toMaybe)
910
import DOM (DOM)
11+
import DOM.HTML.Document.ReadyState (ReadyState(..)) as Exports
12+
import DOM.HTML.Document.ReadyState (ReadyState, parseReadyState)
1013
import DOM.HTML.Types (HTMLElement, HTMLDocument)
14+
import Data.Maybe (Maybe, fromJust)
15+
import Data.Nullable (Nullable, toMaybe)
16+
import Partial.Unsafe (unsafePartial)
1117

1218
foreign import _body :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) (Nullable HTMLElement)
1319

1420
body :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) (Maybe HTMLElement)
1521
body = map toMaybe <<< _body
22+
23+
foreign import _readyState :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) String
24+
25+
readyState :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) ReadyState
26+
readyState = map (unsafePartial fromJust <<< parseReadyState) <<< _readyState

src/DOM/HTML/Document/ReadyState.purs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module DOM.HTML.Document.ReadyState where
2+
3+
import Prelude
4+
import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), defaultSucc, defaultPred)
5+
import Data.Maybe (Maybe(..))
6+
7+
data ReadyState
8+
= Loading
9+
| Interactive
10+
| Complete
11+
12+
derive instance eqReadyState :: Eq ReadyState
13+
derive instance ordReadyState :: Ord ReadyState
14+
15+
instance showReadyState :: Show ReadyState where
16+
show = case _ of
17+
Loading -> "Loading"
18+
Interactive -> "Interactive"
19+
Complete -> "Complete"
20+
21+
printReadyState :: ReadyState -> String
22+
printReadyState = case _ of
23+
Loading -> "loading"
24+
Interactive -> "interactive"
25+
Complete -> "complete"
26+
27+
parseReadyState :: String -> Maybe ReadyState
28+
parseReadyState = case _ of
29+
"loading" -> Just Loading
30+
"interactive" -> Just Interactive
31+
"complete" -> Just Complete
32+
_ -> Nothing
33+
34+
instance boundedReadyState :: Bounded ReadyState where
35+
bottom = Loading
36+
top = Complete
37+
38+
instance enumReadyState :: Enum ReadyState where
39+
succ = defaultSucc toEnumReadyState fromEnumReadyState
40+
pred = defaultPred toEnumReadyState fromEnumReadyState
41+
42+
instance boundedEnumReadyState :: BoundedEnum ReadyState where
43+
cardinality = Cardinality 3
44+
toEnum = toEnumReadyState
45+
fromEnum = fromEnumReadyState
46+
47+
toEnumReadyState :: Int -> Maybe ReadyState
48+
toEnumReadyState =
49+
case _ of
50+
0 -> Just Loading
51+
1 -> Just Interactive
52+
2 -> Just Complete
53+
_ -> Nothing
54+
55+
fromEnumReadyState :: ReadyState -> Int
56+
fromEnumReadyState =
57+
case _ of
58+
Loading -> 0
59+
Interactive -> 1
60+
Complete -> 2

test/DOM/HTML/Document.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Test.DOM.HTML.Document where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff.Class (liftEff)
6+
import DOM (DOM)
7+
import DOM.HTML (window)
8+
import DOM.HTML.Document (ReadyState(..), readyState)
9+
import DOM.HTML.Window (document)
10+
import Test.Unit (TestSuite, describe, it)
11+
import Test.Unit.Assert (shouldEqual)
12+
13+
domHtmlDocumentTests :: forall eff. TestSuite (dom :: DOM | eff)
14+
domHtmlDocumentTests = do
15+
describe "readyState" do
16+
it "should return a sensible readyState" do
17+
rs <- liftEff $ readyState =<< document =<< window
18+
rs `shouldEqual` Complete

test/DOM/HTML/Window.purs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,39 @@
11
module Test.DOM.HTML.Window where
22

3-
import Prelude (Unit, bind, (<<<), discard)
3+
import Prelude
4+
5+
import Control.Monad.Eff.Class (liftEff)
46
import DOM (DOM)
57
import DOM.HTML (window)
68
import DOM.HTML.Types (WINDOW)
7-
import DOM.HTML.Window
8-
import Control.Monad.Free (Free)
9-
import Control.Monad.Aff (Aff)
10-
import Control.Monad.Aff.Console (CONSOLE)
11-
import Control.Monad.Eff (Eff)
12-
import Control.Monad.Eff.Class (liftEff) as EffClass
13-
import Test.Unit (TestF, describe, it)
14-
import Test.Unit.Assert (shouldEqual)
9+
import DOM.HTML.Window as Window
1510
import Data.Maybe (isJust)
16-
import Data.Traversable (class Traversable, sequence)
17-
18-
19-
liftEff :: forall eff a. Eff eff a -> Aff eff a
20-
liftEff = EffClass.liftEff
21-
22-
liftSeq :: forall eff m a. Traversable m => m (Eff eff a) -> Aff eff (m a)
23-
liftSeq = liftEff <<< sequence
11+
import Test.Unit (TestSuite, describe, it)
12+
import Test.Unit.Assert (shouldEqual)
2413

25-
domHtmlWindowTests
26-
:: forall eff. Free (TestF (dom :: DOM, console :: CONSOLE, window :: WINDOW | eff)) Unit
14+
domHtmlWindowTests :: forall eff. TestSuite (dom :: DOM, window :: WINDOW | eff)
2715
domHtmlWindowTests = do
2816
describe "innerHeight" do
2917
it "should return the default inner height" do
30-
windowHeight <- liftEff do
31-
window' <- window
32-
innerHeight window'
18+
windowHeight <- liftEff $ Window.innerHeight =<< window
3319
windowHeight `shouldEqual` 300
3420

3521
describe "innerWidth" do
3622
it "should return the default inner width" do
37-
windowWidth <- liftEff do
38-
window' <- window
39-
innerWidth window'
23+
windowWidth <- liftEff $ Window.innerWidth =<< window
4024
windowWidth `shouldEqual` 400
4125

4226
describe "screenX" do
4327
it "should get the X coordinate of the window" do
44-
x <- liftEff do
45-
window' <- window
46-
screenX window'
28+
x <- liftEff $ Window.screenX =<< window
4729
x `shouldEqual` 0
4830

4931
describe "screenY" do
5032
it "should get the Y coordinate of the window" do
51-
y <- liftEff do
52-
window' <- window
53-
screenY window'
33+
y <- liftEff $ Window.screenY =<< window
5434
y `shouldEqual` 0
5535

5636
describe "open" do
5737
it "should open a new window" do
58-
newWindow' <- liftEff do
59-
window' <- window
60-
open "about:blank" "foobar" "" window'
38+
newWindow' <- liftEff $ Window.open "about:blank" "foobar" "" =<< window
6139
isJust newWindow' `shouldEqual` true

0 commit comments

Comments
 (0)