From af8b5c077da700236667ccb3cdcd736774dd9e3b Mon Sep 17 00:00:00 2001 From: Jakub Hampl Date: Tue, 21 Aug 2018 15:51:05 +0100 Subject: v0.19 compatibility (#1) --- src/LngLat.elm | 39 +++++++++++++++++++++++++++++++++++---- src/Mapbox/Cmd/Option.elm | 11 +++++------ src/Mapbox/Cmd/Template.elm | 20 ++++++++++---------- src/Mapbox/Element.elm | 15 +++++++-------- src/Mapbox/Expression.elm | 10 +++++----- src/Mapbox/Helpers.elm | 2 +- src/Mapbox/Layer.elm | 8 ++++---- src/Mapbox/Source.elm | 18 +++++++++--------- src/Mapbox/Style.elm | 6 +++--- src/js/main.js | 30 +++++++++++++++++++----------- 10 files changed, 98 insertions(+), 61 deletions(-) (limited to 'src') diff --git a/src/LngLat.elm b/src/LngLat.elm index 537b125..a97d22b 100644 --- a/src/LngLat.elm +++ b/src/LngLat.elm @@ -1,13 +1,13 @@ -module LngLat exposing (LngLat, encodeAsPair, encodeAsObject, decodeFromPair, decodeFromObject, map) +module LngLat exposing (LngLat, decodeFromObject, decodeFromPair, encodeAsObject, encodeAsPair, map, toString) {-| Encodes geographic position. -@docs LngLat, encodeAsPair, encodeAsObject, decodeFromPair, decodeFromObject, map +@docs LngLat, encodeAsPair, encodeAsObject, decodeFromPair, decodeFromObject, map, toString -} -import Json.Encode as Encode exposing (Value) import Json.Decode as Decode exposing (Decoder) +import Json.Encode as Encode exposing (Value) {-| A LngLat represents a geographic position. @@ -22,7 +22,7 @@ type alias LngLat = -} encodeAsPair : LngLat -> Value encodeAsPair { lng, lat } = - Encode.list [ Encode.float lng, Encode.float lat ] + Encode.list Encode.float [ lng, lat ] {-| Most implementations seem to encode these as a 2 member array. @@ -59,3 +59,34 @@ decodeFromObject = map : (Float -> Float) -> LngLat -> LngLat map f { lng, lat } = { lng = f lng, lat = f lat } + + +toDMS : Float -> String -> String -> String +toDMS angle pos neg = + let + absAngle = + abs angle + + degrees = + truncate absAngle + + minutes = + (absAngle - toFloat degrees) * 60 + + seconds = + (minutes - toFloat (truncate minutes)) * 60 |> round + + prefix = + if angle > 0 then + pos + else + neg + in + String.fromInt degrees ++ "° " ++ String.fromInt (truncate minutes) ++ "' " ++ String.fromInt seconds ++ "\"" ++ prefix + + +{-| Returns a text representation suitable for humans. +-} +toString : LngLat -> String +toString { lng, lat } = + toDMS lat "N" "S" ++ " " ++ toDMS lng "E" "W" diff --git a/src/Mapbox/Cmd/Option.elm b/src/Mapbox/Cmd/Option.elm index 2e3321d..4c6374f 100644 --- a/src/Mapbox/Cmd/Option.elm +++ b/src/Mapbox/Cmd/Option.elm @@ -1,4 +1,4 @@ -module Mapbox.Cmd.Option exposing (duration, easing, offset, animate, curve, minZoom, speed, screenSpeed, maxDuration, center, zoom, bearing, pitch, around, padding, Padding, linear, maxZoom) +module Mapbox.Cmd.Option exposing (Padding, animate, around, bearing, center, curve, duration, easing, linear, maxDuration, maxZoom, minZoom, offset, padding, pitch, screenSpeed, speed, zoom) {-| @@ -25,12 +25,11 @@ Options common to `jumpTo`, `easeTo`, and `flyTo`, controlling the desired locat -} -import Mapbox.Helpers exposing (encodePair) import Json.Encode as Encode exposing (Value) -import Mapbox.Cmd.Internal as Internal exposing (Option(..), Supported) -import Mapbox.Helpers exposing (encodePair) import LngLat exposing (LngLat) +import Mapbox.Cmd.Internal as Internal exposing (Option(..), Supported) import Mapbox.Expression exposing (DataExpression, Expression) +import Mapbox.Helpers exposing (encodePair) {-| The animation's duration, measured in milliseconds. @@ -176,7 +175,7 @@ maxDuration = -} layers : List String -> Option { a | layers : Supported } layers = - List.map Encode.string >> Encode.list >> Option "layers" + Encode.list Encode.string >> Option "layers" {-| A filter to limit query results. @@ -197,4 +196,4 @@ intersectsPoint = -} intersectsBox : ( LngLat, LngLat ) -> Option { a | query : Supported } intersectsBox = - encodePair (LngLat.encodeAsPair) >> Option "query" + encodePair LngLat.encodeAsPair >> Option "query" diff --git a/src/Mapbox/Cmd/Template.elm b/src/Mapbox/Cmd/Template.elm index 4d412d5..3dbf66d 100644 --- a/src/Mapbox/Cmd/Template.elm +++ b/src/Mapbox/Cmd/Template.elm @@ -1,4 +1,4 @@ -module Mapbox.Cmd.Template exposing (Id, Outgoing, Option, Supported, panBy, panTo, zoomTo, zoomIn, zoomOut, rotateTo, jumpTo, easeTo, flyTo, stop, fitBounds, resize) +module Mapbox.Cmd.Template exposing (Id, Option, Outgoing, Supported, easeTo, fitBounds, flyTo, jumpTo, panBy, panTo, resize, rotateTo, stop, zoomIn, zoomOut, zoomTo) {-| This module has a bunch of essentially imperative commands for your map. @@ -27,9 +27,9 @@ You can of course customize the module you copy into your codebase to support th import Json.Decode as Decode import Json.Encode as Encode exposing (Value) +import LngLat exposing (LngLat) import Mapbox.Cmd.Internal as Internal exposing (Option(..), Supported) import Mapbox.Helpers exposing (encodePair) -import LngLat exposing (LngLat) {-| The type of a port that you need to provide for this module to work. @@ -367,30 +367,30 @@ queryRenderedFeatures prt id reqId options = -} queryResults : ((Value -> msg) -> Sub msg) -> (Int -> ( LngLat, LngLat ) -> response) -> (Int -> List Value -> response) -> (String -> response) -> (response -> msg) -> Sub msg -queryResults prt getBounds queryRenderedFeatures error tagger = - prt (decodeResponse getBounds queryRenderedFeatures error >> tagger) +queryResults prt getBounds_ queryRenderedFeatures_ error_ tagger = + prt (decodeResponse getBounds_ queryRenderedFeatures_ error_ >> tagger) -responseDecoder getBounds queryRenderedFeatures = +responseDecoder getBounds_ queryRenderedFeatures_ = Decode.field "type" Decode.string |> Decode.andThen (\s -> case s of "getBounds" -> - Decode.map2 getBounds (Decode.field "id" Decode.int) (Decode.field "bounds" (decodePair LngLat.decodeFromPair)) + Decode.map2 getBounds_ (Decode.field "id" Decode.int) (Decode.field "bounds" (decodePair LngLat.decodeFromPair)) "queryRenderedFeatures" -> - Decode.map2 queryRenderedFeatures (Decode.field "id" Decode.int) (Decode.field "features" (Decode.list Decode.value)) + Decode.map2 queryRenderedFeatures_ (Decode.field "id" Decode.int) (Decode.field "features" (Decode.list Decode.value)) _ -> Decode.fail <| "Unrecognized response type: " ++ s ) -decodeResponse getBounds queryRenderedFeatures error value = - case Decode.decodeValue (responseDecoder getBounds queryRenderedFeatures) value of +decodeResponse getBounds_ queryRenderedFeatures_ error_ value = + case Decode.decodeValue (responseDecoder getBounds_ queryRenderedFeatures_) value of Ok res -> res Err e -> - error e + error_ (Decode.errorToString e) diff --git a/src/Mapbox/Element.elm b/src/Mapbox/Element.elm index 14b6873..b9cd74c 100644 --- a/src/Mapbox/Element.elm +++ b/src/Mapbox/Element.elm @@ -20,7 +20,7 @@ module Mapbox.Element exposing (EventData, MapboxAttr, TouchEvent, css, eventFea import Html exposing (Attribute, Html, node) import Html.Attributes exposing (attribute, property) -import Html.Events exposing (Options) +import Html.Events import Json.Decode as Decode exposing (Decoder, Value) import Json.Encode as Encode import LngLat exposing (LngLat) @@ -127,8 +127,7 @@ This needs more design before release. -} featureState : List ( Value, List ( String, Value ) ) -> MapboxAttr msg featureState = - List.map (\( feature, state ) -> Encode.list [ feature, Encode.object state ]) - >> Encode.list + Encode.list (\( feature, state ) -> Encode.list identity [ feature, Encode.object state ]) >> property "featureState" >> MapboxAttr @@ -150,7 +149,7 @@ a lot of data. Here you can specify which layers you want to search for intersec -} eventFeaturesLayers : List String -> MapboxAttr msg eventFeaturesLayers = - List.map Encode.string >> Encode.list >> property "eventFeaturesLayers" >> MapboxAttr + Encode.list Encode.string >> property "eventFeaturesLayers" >> MapboxAttr {-| This allows you to use other events not provided by this libary. @@ -158,9 +157,9 @@ eventFeaturesLayers = See for all supported events. -} -onWithOptions : String -> Options -> Decoder msg -> MapboxAttr msg -onWithOptions type_ opts decoder = - Html.Events.onWithOptions type_ opts decoder |> MapboxAttr +on : String -> Decoder msg -> MapboxAttr msg +on type_ decoder = + Html.Events.on type_ decoder |> MapboxAttr {-| `point` is the coordinates in pixels in screen space. @@ -199,7 +198,7 @@ type alias TouchEvent = decodePoint = - Decode.map2 (,) (Decode.field "x" Decode.int) (Decode.field "y" Decode.int) + Decode.map2 (\a b -> ( a, b )) (Decode.field "x" Decode.int) (Decode.field "y" Decode.int) decodeEventData = diff --git a/src/Mapbox/Expression.elm b/src/Mapbox/Expression.elm index d800188..0206a0c 100644 --- a/src/Mapbox/Expression.elm +++ b/src/Mapbox/Expression.elm @@ -236,7 +236,7 @@ The expressions in this section can be used to add conditional logic to your sty Strings can be compared with a collator for locale specific comparisons: -@docs isEqualWithCollator, notEqualWithCollator,lessThanWithCollator, lessThanOrEqualWithCollator, greaterThanWithCollator, greaterThanOrEqualWithCollator +@docs isEqualWithCollator, notEqualWithCollator, lessThanWithCollator, lessThanOrEqualWithCollator, greaterThanWithCollator, greaterThanOrEqualWithCollator Logical operators: @@ -657,7 +657,7 @@ rasterResamplingNearest = {-| -} rgba : Float -> Float -> Float -> Float -> Expression exprType Color rgba r g b a = - Expression (Json.Encode.string ("rgba" ++ Basics.toString ( r, g, b, a ))) + Expression (Json.Encode.string ("rgba(" ++ String.fromFloat r ++ ", " ++ String.fromFloat g ++ ", " ++ String.fromFloat b ++ ", " ++ String.fromFloat a ++ ")")) {-| -} @@ -710,7 +710,7 @@ strings = list : List (Expression exprType a) -> Expression exprType (Array a) list = - List.map encode >> Json.Encode.list >> Expression >> call1 "literal" + Json.Encode.list encode >> Expression >> call1 "literal" {-| Returns a `Collator` for use in locale-dependent comparison operations. The first argument specifies if the comparison should be case sensitive. The second specifies if it is diacritic sensitive. The final locale argument specifies the IETF language tag of the locale to use. @@ -718,7 +718,7 @@ list = collator : Expression e1 Bool -> Expression e2 Bool -> Expression e3 String -> Expression e4 Collator collator (Expression caseSensitive) (Expression diacriticSensitive) (Expression locale) = Expression - (Json.Encode.list + (Json.Encode.list identity (Json.Encode.string "collator" :: [ Json.Encode.object [ ( "case-sensitive", caseSensitive ) @@ -881,7 +881,7 @@ calln n expressions = call name args = - Expression (Json.Encode.list (Json.Encode.string name :: args)) + Expression (Json.Encode.list identity (Json.Encode.string name :: args)) {-| Retrieves a property value from the current feature's state. Returns null if the requested property is not present on the feature's state. A feature's state is not part of the GeoJSON or vector tile data, and must be set programmatically on each feature. Note that `featureState` can only be used with paint properties that support data-driven styling. diff --git a/src/Mapbox/Helpers.elm b/src/Mapbox/Helpers.elm index ed66d37..0f164ac 100644 --- a/src/Mapbox/Helpers.elm +++ b/src/Mapbox/Helpers.elm @@ -15,4 +15,4 @@ encodeAnchor v = encodePair encoder ( a, b ) = - Encode.list [ encoder a, encoder b ] + Encode.list encoder [ a, b ] diff --git a/src/Mapbox/Layer.elm b/src/Mapbox/Layer.elm index ce0fa67..3d75373 100644 --- a/src/Mapbox/Layer.elm +++ b/src/Mapbox/Layer.elm @@ -299,16 +299,16 @@ encodeAttrs attrs = let { top, layout, paint } = List.foldl - (\attr ({ top, layout, paint } as lists) -> + (\attr lists -> case attr of Top key val -> - { lists | top = ( key, val ) :: top } + { lists | top = ( key, val ) :: lists.top } Paint key val -> - { lists | paint = ( key, val ) :: paint } + { lists | paint = ( key, val ) :: lists.paint } Layout key val -> - { lists | layout = ( key, val ) :: layout } + { lists | layout = ( key, val ) :: lists.layout } ) { top = [], layout = [], paint = [] } attrs diff --git a/src/Mapbox/Source.elm b/src/Mapbox/Source.elm index 5cc571d..8cccfd8 100644 --- a/src/Mapbox/Source.elm +++ b/src/Mapbox/Source.elm @@ -120,7 +120,7 @@ getId (Source k _) = -} bounds : LngLat -> LngLat -> SourceOption any bounds sw ne = - SourceOption "bounds" (Json.Encode.list [ Json.Encode.float sw.lng, Json.Encode.float sw.lat, Json.Encode.float sw.lng, Json.Encode.float sw.lat ]) + SourceOption "bounds" (Json.Encode.list Json.Encode.float [ sw.lng, sw.lat, sw.lng, sw.lat ]) {-| Minimum zoom level for which tiles are available, as in the TileJSON spec. @@ -222,7 +222,7 @@ This takes an array of one or more tile source URLs, as in the TileJSON spec. -} vector : Id -> List Url -> List (SourceOption VectorSource) -> Source vector id urls options = - ( "tiles", Json.Encode.list (List.map Json.Encode.string urls) ) + ( "tiles", Json.Encode.list Json.Encode.string urls ) :: ( "type", Json.Encode.string "vector" ) :: List.map (\(SourceOption k v) -> ( k, v )) options |> Json.Encode.object @@ -240,7 +240,7 @@ rasterFromUrl id url = -} raster : Id -> List Url -> List (SourceOption RasterSource) -> Source raster id urls options = - ( "tiles", Json.Encode.list (List.map Json.Encode.string urls) ) + ( "tiles", Json.Encode.list Json.Encode.string urls ) :: ( "type", Json.Encode.string "raster" ) :: List.map (\(SourceOption k v) -> ( k, v )) options |> Json.Encode.object @@ -306,11 +306,11 @@ type alias Coords = encodeCoordinates : Coords -> Value encodeCoordinates { topLeft, topRight, bottomRight, bottomLeft } = - Json.Encode.list - [ LngLat.encodeAsPair topLeft - , LngLat.encodeAsPair topRight - , LngLat.encodeAsPair bottomRight - , LngLat.encodeAsPair bottomLeft + Json.Encode.list LngLat.encodeAsPair + [ topLeft + , topRight + , bottomRight + , bottomLeft ] @@ -331,7 +331,7 @@ image id url coordinates = video : Id -> List Url -> Coords -> Source video id urls coordinates = [ ( "type", Json.Encode.string "video" ) - , ( "urls", Json.Encode.list (List.map Json.Encode.string urls) ) + , ( "urls", Json.Encode.list Json.Encode.string urls ) , ( "coordinates", encodeCoordinates coordinates ) ] |> Json.Encode.object diff --git a/src/Mapbox/Style.elm b/src/Mapbox/Style.elm index 500cf47..97eac31 100644 --- a/src/Mapbox/Style.elm +++ b/src/Mapbox/Style.elm @@ -2,7 +2,7 @@ module Mapbox.Style exposing (Light, MiscAttr, Style(..), StyleDef, Transition, {-| A Mapbox style is a document that defines the visual appearance of a map: what data to draw, the order to draw it in, and how to style the data when drawing it. A style document is a JSON object with specific root level and nested properties. This specification defines and describes these properties. -@docs Style, encode , StyleDef +@docs Style, encode, StyleDef ### Light @@ -31,7 +31,7 @@ You can also use one of these predefined styles. import Array exposing (Array) import Json.Encode as Encode exposing (Value) import LngLat exposing (LngLat) -import Mapbox.Expression exposing (Anchor(Viewport), CameraExpression, Color, Expression, float, floats, rgba) +import Mapbox.Expression exposing (Anchor(..), CameraExpression, Color, Expression, float, floats, rgba) import Mapbox.Helpers exposing (encodeAnchor) import Mapbox.Layer exposing (Layer) import Mapbox.Source exposing (Source) @@ -95,7 +95,7 @@ encode style = , ( "transition", encodeTransition styleDef.transition ) , ( "light", encodeLight styleDef.light ) , ( "sources", Encode.object <| List.map (\source -> ( Mapbox.Source.getId source, Mapbox.Source.encode source )) styleDef.sources ) - , ( "layers", Encode.list (List.map Mapbox.Layer.encode styleDef.layers) ) + , ( "layers", Encode.list Mapbox.Layer.encode styleDef.layers ) ] ++ List.map (\(MiscAttr key value) -> ( key, value )) styleDef.misc |> Encode.object diff --git a/src/js/main.js b/src/js/main.js index eff72ed..e56533c 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -1,18 +1,13 @@ import mapboxgl from "mapbox-gl"; -function wrapElmApplication(elmApp, settings = {}) { + +export function registerCustomElement(settings) { const options = Object.assign( { - outgoingPort: "elmMapboxOutgoing", - incomingPort: "elmMapboxIncoming", - easingFunctions: { - linear: t => t - }, onMount() {} }, settings ); - if (options.token) { mapboxgl.accessToken = options.token; } @@ -279,8 +274,21 @@ function wrapElmApplication(elmApp, settings = {}) { } } ); +} - if (elmApp.ports && elmApp.ports.elmMapboxOutgoing) { +export function registerPorts(elmApp, settings = {}) { + const options = Object.assign( + { + outgoingPort: "elmMapboxOutgoing", + incomingPort: "elmMapboxIncoming", + easingFunctions: { + linear: t => t + } + }, + settings + ); + + if (elmApp.ports && elmApp.ports[options.outgoingPort]) { function processOptions(opts) { if (opts.easing) { return Object.assign({}, opts, { @@ -353,11 +361,11 @@ function wrapElmApplication(elmApp, settings = {}) { }); } }); + } else { + throw new Error(`Expected Elm App to expose ${elmApp.ports[options.outgoingPort]} port.`); } return elmApp; } -wrapElmApplication.supported = mapboxgl.supported; - -export default wrapElmApplication; +export const supported = mapboxgl.supported; -- cgit v1.2.3