Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm no data except isTrusted in JSON events

Tags:

elm

I wrote a simple program based on time example, to test what data are in events. It decodes JSON to value then encodes it back to JSON, then show it in SVG text element. And the only thing I get is {"isTrusted":true}.

Why that happens? How do I get another data of event? I'm using Firefox 49 and online compiler:

import Html exposing (Html)
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Svg.Events exposing(on)
import Json.Decode as Json
import Json.Encode exposing (encode)

main =
  Html.program
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }

-- MODEL
type alias Model = String

init : (Model, Cmd Msg)
init =
  ("No event", Cmd.none)


-- UPDATE
type Msg
  = Event String

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Event event ->
      (event, Cmd.none)


subscriptions model = Sub.none


stringifyEvent: Json.Encode.Value -> Msg
stringifyEvent x = 
  Event (encode 2 x)

-- VIEW

view : Model -> Svg Msg
view model =
    svg [ viewBox "0 0 300 300", width "300px", on "mousedown" (Json.map stringifyEvent Json.value) ] [ 
       text_ [x "0", y "30"] [text model]
    ]

When I try in console

svgElement.addEventListener('click', function(e) {console.log(e)})

It works with all the attributes.

like image 343
Bunyk Avatar asked Sep 06 '25 07:09

Bunyk


1 Answers

I do not know a way to achieve your goal. But I can give you an answer why it does the way you described.

If you look at the source code, you'll find that Elm runtime uses JSON.stringify() for converting Value to String.

And guess what... svgElement.addEventListener('click', function(e) {console.log(JSON.stringify(e))}) will give you {"isTrusted":true} when you click...

like image 166
Tosh Avatar answered Sep 11 '25 02:09

Tosh