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

Commit

Permalink
control (#2)
Browse files Browse the repository at this point in the history
* control, events

* removed console.log
  • Loading branch information
cryogenian authored Jun 6, 2017
1 parent ebf0d2a commit 1b755a2
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "purescript-leaflet",
"name": "purescript-leafletjs",
"ignore": [
"**/.*",
"node_modules",
Expand Down
3 changes: 3 additions & 0 deletions src/Leaflet/Core.purs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

module Leaflet.Core
( module Converter
, module DOM
Expand All @@ -7,6 +8,7 @@ module Leaflet.Core
, module Plugin
, module Types
, module Units
, module Control
) where

import Leaflet.Core.Converter as Converter
Expand All @@ -17,3 +19,4 @@ import Leaflet.Core.Map as Map
import Leaflet.Core.Plugin as Plugin
import Leaflet.Core.Types as Types
import Leaflet.Core.Units as Units
import Leaflet.Core.Control as Control
19 changes: 19 additions & 0 deletions src/Leaflet/Core/Control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var L = require("leaflet");

exports.layers_ = function(baseLayers, overlays, options) {
return function() {
return L.control.layers(baseLayers, overlays, options);
};
};

exports.addTo_ = function(map, control) {
return function() {
return control.addTo(map);
};
};

exports.remove_ = function(control) {
return function() {
return control.remove();
};
};
54 changes: 54 additions & 0 deletions src/Leaflet/Core/Control.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Leaflet.Core.Control
( layers
, addTo
, remove
) where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (class MonadEff, liftEff)

import Data.StrMap as SM
import Data.Function.Uncurried (Fn2, runFn2, Fn3, runFn3)

import DOM (DOM)

import Leaflet.Core.Types as T

foreign import layers_
e r. Fn3 (SM.StrMap T.Layer) (SM.StrMap T.LayerGroup) r (Eff (dom DOM|e) T.Control)

foreign import addTo_
e. Fn2 T.Leaflet T.Control (Eff (dom DOM|e) T.Control)

foreign import remove_
e. T.Control Eff (dom DOM|e) Unit

layers
m e r1 r2
. MonadEff (domDOM|e) m
Union r1 r2 (T.LayerControlConf ())
SM.StrMap T.Layer
SM.StrMap T.LayerGroup
Record r1
m T.Control
layers bs os r =
liftEff $ runFn3 layers_ bs os r

addTo
m e
. MonadEff (domDOM|e) m
T.Leaflet
T.Control
m T.Control
addTo leaf control =
liftEff $ runFn2 addTo_ leaf control

remove
m e
. MonadEff (domDOM|e) m
T.Control
m Unit
remove control =
liftEff $ remove_ control
20 changes: 20 additions & 0 deletions src/Leaflet/Core/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ exports.eventZoom_ = function(Nothing, Just, e) {
}
};
};

exports.eventContainerPoint_ = function(Nothing, Just, Tuple, e) {
return function() {
if (e.containerPoint === undefined) {
return Nothing;
} else {
return Just(Tuple(e.containerPoint.x)(e.containerPoint.y));
}
};
};

exports.eventLatLng_ = function(Nothing, Just, e) {
return function() {
if (e.latlng === undefined) {
return Nothing;
} else {
return Just(e.latlng);
}
};
};
27 changes: 26 additions & 1 deletion src/Leaflet/Core/Event.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Leaflet.Core.Event
( eventCenter
, eventZoom
, eventContainerPoint
, eventLatLng
) where

import Prelude
Expand All @@ -15,7 +17,7 @@ import Data.Tuple (Tuple(..))
import DOM (DOM)

import Leaflet.Util (type (×))
import Leaflet.Core.Types (Point, Zoom, Event)
import Leaflet.Core.Types (Point, Zoom, Event, LatLng)


foreign import eventZoom_
Expand All @@ -24,6 +26,12 @@ foreign import eventZoom_
foreign import eventCenter_
e a. Fn4 (Maybe a) (a Maybe a) (a a a × a) Event (Eff (dom DOM|e) (Maybe Point))

foreign import eventContainerPoint_
e a. Fn4 (Maybe a) (a Maybe a) (a a a × a) Event (Eff (dom DOM|e) (Maybe Point))

foreign import eventLatLng_
e a. Fn3 (Maybe a) (a Maybe a) Event (Eff (dom DOM|e) (Maybe LatLng))

eventCenter
m e
. MonadEff (domDOM|e) m
Expand All @@ -39,3 +47,20 @@ eventZoom
m (Maybe Zoom)
eventZoom e =
liftEff $ runFn3 eventZoom_ Nothing Just e

eventContainerPoint
m e
. MonadEff (domDOM|e) m
Event
m (Maybe Point)
eventContainerPoint e =
liftEff $ runFn4 eventContainerPoint_ Nothing Just Tuple e


eventLatLng
m e
. MonadEff (domDOM|e) m
Event
m (Maybe LatLng)
eventLatLng e =
liftEff $ runFn3 eventLatLng_ Nothing Just e
23 changes: 22 additions & 1 deletion src/Leaflet/Core/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ exports.popup_ = function(converter, conf) {
};
};

exports.setURI_ = function(uri, layer) {
return function() {
return layer.setUrl(uri);
};
};

exports.setLatLng_ = function(latlng, popup) {
return function() {
return popup.setLatLng(latlng);
Expand Down Expand Up @@ -166,12 +172,21 @@ exports.rectangle_ = function(latlngs, converter, conf) {

exports.on_ = function(e, cb, l) {
return function() {
l.on(e, function() {
l.on(e, function(ev) {
cb(ev)();
});
};
};

exports.once_ = function(e, cb, l) {
return function() {
l.once(e, function() {
cb(e)();
});
};
};


exports.addLayer_ = function(layer, leaflet) {
return function() {
layer.addTo(leaflet);
Expand All @@ -185,3 +200,9 @@ exports.removeLayer_ = function(layer, leaflet) {
return leaflet;
};
};

exports.layerGroup_ = function(layers) {
return function() {
return L.layerGroup(layers);
};
};
41 changes: 41 additions & 0 deletions src/Leaflet/Core/Layer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Leaflet.Core.Layer
, marker
, icon
, popup
, setURI
, setContent
, setLatLng
, openOn
, bindPopup
Expand All @@ -14,9 +16,11 @@ module Leaflet.Core.Layer
, polygon
, rectangle
, on
, once
, setIcon
, addLayer
, removeLayer
, layerGroup
) where

import Prelude
Expand Down Expand Up @@ -51,6 +55,9 @@ foreign import icon_
foreign import popup_
e r. Fn2 ConvertDict r (Eff (dom DOM|e) T.Popup)

foreign import setURI_
e. Fn2 String T.TileLayer (Eff (dom DOM|e) T.TileLayer)

foreign import setLatLng_
e. Fn2 (Array T.Degrees) T.Popup (Eff (dom DOM|e) T.Popup)

Expand Down Expand Up @@ -84,6 +91,9 @@ foreign import rectangle_
foreign import on_
e. Fn3 String (T.Event Eff (dom DOM|e) Unit) T.Evented (Eff (dom DOM|e) Unit)

foreign import once_
e. Fn3 String (T.Event Eff (dom DOM|e) Unit) T.Evented (Eff (dom DOM|e) Unit)

foreign import setIcon_
e. Fn2 T.Icon T.Marker (Eff (dom DOM|e) T.Marker)

Expand All @@ -93,6 +103,18 @@ foreign import addLayer_
foreign import removeLayer_
e. Fn2 T.Layer T.Leaflet (Eff (dom DOM|e) T.Leaflet)

foreign import layerGroup_
e. Array T.Layer Eff (dom DOM|e) T.LayerGroup


layerGroup
e m f
. Foldable f
MonadEff (domDOM|e) m
f T.Layer
m T.LayerGroup
layerGroup =
liftEff ∘ layerGroup_ ∘ A.fromFoldable

layer
e m
Expand Down Expand Up @@ -134,6 +156,15 @@ popup
popup r =
liftEff $ runFn2 popup_ converter r

setURI
e m
. MonadEff (domDOM|e) m
URIRef
T.TileLayer
m T.TileLayer
setURI uri tl =
liftEff $ runFn2 setURI_ (converter.printURI uri) tl

setLatLng
e m
. MonadEff (domDOM|e) m
Expand Down Expand Up @@ -228,6 +259,16 @@ on
on e fn l =
liftEff $ runFn3 on_ e fn l

once
e m
. MonadEff (domDOM|e) m
String
(T.Event Eff (domDOM|e) Unit)
T.Evented
m Unit
once e fn l =
liftEff $ runFn3 once_ e fn l

setIcon
m e
. MonadEff (domDOM|e) m
Expand Down
6 changes: 6 additions & 0 deletions src/Leaflet/Core/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ exports.getZoom_ = function(leaflet) {
return leaflet.getZoom();
};
};

exports.invalidateSize_ = function(b, leaflet) {
return function() {
return leaflet.invalidateSize(b);
};
};
13 changes: 13 additions & 0 deletions src/Leaflet/Core/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Leaflet.Core.Map
, getMaxZoom
, getZoom
, latLngToContainerPoint
, invalidateSize
) where

import Prelude
Expand Down Expand Up @@ -70,6 +71,9 @@ foreign import getMaxZoom_
foreign import getZoom_
e. T.Leaflet Eff (dom DOM|e) T.Zoom

foreign import invalidateSize_
e. Fn2 Boolean T.Leaflet (Eff (dom DOM|e) T.Leaflet)


leaflet
n e m
Expand Down Expand Up @@ -177,3 +181,12 @@ getZoom
T.Leaflet
m T.Zoom
getZoom = liftEff ∘ getZoom_

invalidateSize
e m
. MonadEff (domDOM|e) m
Boolean
T.Leaflet
m T.Leaflet
invalidateSize b l =
liftEff $ runFn2 invalidateSize_ b l
21 changes: 21 additions & 0 deletions src/Leaflet/Core/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ data Evented
data Event
data Zoom
data Degrees
data LayerGroup
data Control

-- layer converters
tileToLayer TileLayer Layer
Expand All @@ -52,6 +54,9 @@ circleToLayer = unsafeCoerce
rectangleToLayer Rectangle Layer
rectangleToLayer = unsafeCoerce

groupToLayer LayerGroup Layer
groupToLayer = unsafeCoerce

mapToEvented Leaflet Evented
mapToEvented = unsafeCoerce

Expand Down Expand Up @@ -168,3 +173,19 @@ type PolylineConf r =
type CircleConf =
PathConf
( radiusNumber )

--------------------------------------------------------------------------------
-- Control options
--------------------------------------------------------------------------------

type ControlConf r =
( positionString
| r)

type LayerControlConf r =
ControlConf
( collapsedBoolean
, autoZIndexBoolean
, hideSingleBaseBoolean
, sortLayersBoolean
| r )
Loading

0 comments on commit 1b755a2

Please sign in to comment.