Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from jonsterling/master
Browse files Browse the repository at this point in the history
Initial implementation and demo
  • Loading branch information
jdegoes committed Sep 4, 2015
2 parents db7dbf8 + 4c88f29 commit 9a4a741
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.*
!/.gitignore
!/.jscsrc
!/.jshintrc
!/.travis.yml
/bower_components/
/node_modules/
/output/
/tmp/
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
language: node_js
sudo: false
node_js:
- 0.12.7
env:
- PATH=$HOME/purescript:$PATH
install:
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
- chmod a+x $HOME/purescript
- npm install
- npm install -g bower
- bower install
script:
- npm run build
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# purescript-browserfeatures
A data type for browser features and detectors to test for the features.

To build, run

```
gulp
```

Then open `test/index.html` and observe the console to see this in action.
24 changes: 24 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "purescript-browserfeatures",
"repository": {
"type": "git",
"url": "https://github.com/slamdata/purescript-browserfeatures"
},
"homepage": "https://github.com/slamdata/purescript-browserfeatures",
"authors": [
"Jon Sterling <[email protected]>"
],
"description": "A data type for browser features and detectors to test for the features",
"keywords": [
"purescript",
"platform"
],
"license": "Apache-2.0",
"dependencies": {
"purescript-base": "^0.1.0",
"purescript-exceptions": "v0.3.0",
"purescript-prelude": "<= 0.1.1",
"purescript-dom": "^0.1.2",
"purescript-maps": "^0.5.0"
}
}
11 changes: 11 additions & 0 deletions docs/DOM/BrowserFeatures/Detectors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Module DOM.BrowserFeatures.Detectors

#### `detectBrowserFeatures`

``` purescript
detectBrowserFeatures :: forall e. Eff (dom :: DOM | e) BrowserFeatures
```

Detect browser features by testing them using the DOM.


9 changes: 9 additions & 0 deletions docs/Data/BrowserFeatures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Module Data.BrowserFeatures

#### `BrowserFeatures`

``` purescript
type BrowserFeatures = { inputTypeSupported :: InputType -> Boolean }
```


43 changes: 43 additions & 0 deletions docs/Data/BrowserFeatures/InputType.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Module Data.BrowserFeatures.InputType

#### `InputType`

``` purescript
data InputType
= Color
| Date
| DateTime
| DateTimeLocal
| Time
| Month
| Week
| Email
| Url
| Number
| Search
| Range
```

##### Instances
``` purescript
instance showInputType :: Show InputType
instance eqInputType :: Eq InputType
instance ordInputType :: Ord InputType
```

#### `allInputTypes`

``` purescript
allInputTypes :: Array InputType
```

#### `renderInputType`

``` purescript
renderInputType :: InputType -> String
```

Render an `InputType` into the corresponding value of the `type` attribute
on an `input` element.


55 changes: 55 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict"

var gulp = require("gulp"),
purescript = require("gulp-purescript"),
webpack = require("webpack-stream");

var sources = [
"src/**/*.purs",
"bower_components/purescript-*/src/**/*.purs",
"test/*.purs"
];
var foreigns = [
"src/**/*.js",
"bower_components/purescript-*/src/**/*.js",
"test/*/*.js"
];

gulp.task("make", function() {
return purescript.psc({ src: sources, ffi: foreigns });
});

gulp.task("docs", function() {
return purescript.pscDocs({
src: sources,
docgen: {
"Data.BrowserFeatures": "docs/Data/BrowserFeatures.md",
"Data.BrowserFeatures.InputType": "docs/Data/BrowserFeatures/InputType.md",
"DOM.BrowserFeatures.Detectors": "docs/DOM/BrowserFeatures/Detectors.md"
}
});
});

gulp.task("bundle", ["make"], function() {
return purescript.pscBundle({
src: "output/**/*.js",
output: "tmp/test.js",
main: "Test.Main"
});
});

gulp.task("bundle-test", ["bundle"], function() {
return gulp.src("tmp/test.js")
.pipe(webpack({
resolve: { moduleDirectories: ["node_modules"] },
output: { filename: "test.js" }
}))
.pipe(gulp.dest("tmp"));
});

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

gulp.task("default", ["bundle-test", "docs", "dotpsci"]);
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "purescript-browserfeatures",
"description": "A PureScript interface to browserfeatures.js.",
"repository": {
"type": "git",
"url": "https://github.com/slamdata/purescript-browserfeatures.git"
},
"author": "Jon Sterling <[email protected]>",
"license": "Apache-2.0",
"scripts": {
"build": "npm install && gulp bundle-test"
},
"devDependencies": {
"gulp": "^3.9.0",
"gulp-purescript": "^0.5.0",
"purescript": "^0.7.1",
"webpack-stream": "^2.1.0"
},
"dependencies": {
}
}
15 changes: 15 additions & 0 deletions src/DOM/BrowserFeatures/Detectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// module DOM.BrowserFeatures.Detectors

exports._detectInputTypeSupport = function(type) {
return function() {
var el = document.createElement("input");

try {
el.setAttribute("type", type);
} catch (exn) {
return false;
}

return el.type === type;
};
};
38 changes: 38 additions & 0 deletions src/DOM/BrowserFeatures/Detectors.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module DOM.BrowserFeatures.Detectors
( detectBrowserFeatures
) where

import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Exception

import qualified Data.Array as Arr
import qualified Data.List as L
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Foldable (foldr)
import Data.Traversable (traverse)
import Data.Tuple

import DOM
import Data.BrowserFeatures
import qualified Data.BrowserFeatures.InputType as IT

foreign import _detectInputTypeSupport :: forall e. String -> Eff (dom :: DOM | e) Boolean

detectInputTypeSupport :: forall e. IT.InputType -> Eff (dom :: DOM | e) Boolean
detectInputTypeSupport = _detectInputTypeSupport <<< IT.renderInputType

detectInputTypeSupportMap :: forall e. Eff (dom :: DOM | e) (M.Map IT.InputType Boolean)
detectInputTypeSupportMap = M.fromList <$> traverse (\t -> Tuple t <$> detectInputTypeSupport t) inputTypes
where
inputTypes :: L.List IT.InputType
inputTypes = foldr L.Cons L.Nil IT.allInputTypes

-- | Detect browser features by testing them using the DOM.
detectBrowserFeatures :: forall e. Eff (dom :: DOM | e) BrowserFeatures
detectBrowserFeatures = do
inputTypeSupportMap <- detectInputTypeSupportMap
pure { inputTypeSupported : fromMaybe false <<< flip M.lookup inputTypeSupportMap
}

9 changes: 9 additions & 0 deletions src/Data/BrowserFeatures.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Data.BrowserFeatures
( BrowserFeatures(..)
) where

import Data.BrowserFeatures.InputType

type BrowserFeatures =
{ inputTypeSupported :: InputType -> Boolean
}
75 changes: 75 additions & 0 deletions src/Data/BrowserFeatures/InputType.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module Data.BrowserFeatures.InputType
( InputType(..)
, renderInputType
, allInputTypes
) where

import Prelude
import qualified Data.Array as Arr

data InputType
= Color
| Date
| DateTime
| DateTimeLocal
| Time
| Month
| Week
| Email
| Url
| Number
| Search
| Range

allInputTypes :: Array InputType
allInputTypes =
[ Color
, Date
, DateTime
, DateTimeLocal
, Time
, Month
, Week
, Email
, Url
, Number
, Search
, Range
]

instance showInputType :: Show InputType where
show Color = "Color"
show Date = "Date"
show DateTime = "DateTime"
show DateTimeLocal = "DateTimeLocal"
show Time = "Time"
show Month = "Month"
show Week = "Week"
show Email = "Email"
show Url = "Url"
show Number = "Number"
show Search = "Search"
show Range = "Range"

-- | Render an `InputType` into the corresponding value of the `type` attribute
-- | on an `input` element.
renderInputType :: InputType -> String
renderInputType Color = "color"
renderInputType Date = "date"
renderInputType DateTime = "datetime"
renderInputType DateTimeLocal = "datetime-local"
renderInputType Time = "time"
renderInputType Month = "month"
renderInputType Week = "week"
renderInputType Email = "email"
renderInputType Url = "url"
renderInputType Number = "number"
renderInputType Search = "search"
renderInputType Range = "range"

instance eqInputType :: Eq InputType where
eq x y = renderInputType x == renderInputType y

instance ordInputType :: Ord InputType where
compare x y = compare (renderInputType x) (renderInputType y)

16 changes: 16 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Test.Main where

import Prelude
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Console (CONSOLE(), log)

import Data.Traversable (traverse, for)
import qualified Data.BrowserFeatures.InputType as IT
import DOM
import DOM.BrowserFeatures.Detectors

main :: Eff (dom :: DOM, console :: CONSOLE) Unit
main = do
features <- detectBrowserFeatures
void $ for IT.allInputTypes \ty ->
log $ show ty ++ if features.inputTypeSupported ty then " supported" else " not supported"
6 changes: 6 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<title>Platform Test</title>
<script src="../tmp/test.js" type="text/javascript"></script>
</head>
</html>

0 comments on commit 9a4a741

Please sign in to comment.