This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from jonsterling/master
Initial implementation and demo
- Loading branch information
Showing
15 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
## Module Data.BrowserFeatures | ||
|
||
#### `BrowserFeatures` | ||
|
||
``` purescript | ||
type BrowserFeatures = { inputTypeSupported :: InputType -> Boolean } | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
| 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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
| Url | ||
| Number | ||
| Search | ||
| Range | ||
|
||
allInputTypes :: Array InputType | ||
allInputTypes = | ||
[ Color | ||
, Date | ||
, DateTime | ||
, DateTimeLocal | ||
, Time | ||
, Month | ||
, Week | ||
, 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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |