Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add this to Route.href to prevent click but not Cmd or middle clicks #3

Open
wking-io opened this issue Apr 18, 2018 · 1 comment
Open

Comments

@wking-io
Copy link
Owner

https://github.com/elm-lang/html/issues/110

@wking-io
Copy link
Owner Author

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        NewLocation location ->
            let
                newRoute =
                    parseLocation location
            in
                { model | page = newRoute } ! []

        NewPage page ->
            model ! [ Navigation.newUrl (Routing.pageToUrl page) ]

href : ( Route -> Cmd Msg) -> Route -> List (Attribute Msg)
href msg route =
    [ href (routeToString route)
    , onPreventDefaultClick (msg route)
    ]


onPreventDefaultClick : msg -> Attribute msg
onPreventDefaultClick message =
    onWithOptions "click"
        { defaultOptions | preventDefault = True }
        (preventDefault2
            |> Json.Decode.andThen (maybePreventDefault message)
        )


preventDefault2 : Decoder Bool
preventDefault2 =
    Json.Decode.map2
        (invertedOr)
        (Json.Decode.field "ctrlKey" Json.Decode.bool)
        (Json.Decode.field "metaKey" Json.Decode.bool)


maybePreventDefault : msg -> Bool -> Decoder msg
maybePreventDefault msg preventDefault =
    case preventDefault of
        True ->
            Json.Decode.succeed msg

        False ->
            Json.Decode.fail "Normal link"


invertedOr : Bool -> Bool -> Bool
invertedOr x y =
    not (x || y)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant