Skip to content

Instantly share code, notes, and snippets.

@MangelMaxime
Created July 12, 2016 22:34
Show Gist options
  • Save MangelMaxime/7c2bfd735d62d5d74fe5aa73c3837a43 to your computer and use it in GitHub Desktop.
Save MangelMaxime/7c2bfd735d62d5d74fe5aa73c3837a43 to your computer and use it in GitHub Desktop.
Sample about using a Result to handle Function oriented in fable
namespace Weirdbird.Jdr.Browser
module Program =
open Fable.Core
open Fable.Import.Browser
Fable.Import.Node.require.Invoke("core-js") |> ignore
type AppModel = {
ContextMenu: HTMLElement
}
type Result<'TResult, 'TError> =
| Ok of 'TResult
| Error of 'TError
let clickInsideElement (e: PointerEvent, className) =
let mutable el = unbox<Element> (unbox e.srcElement || unbox e.target)
let rec matchClassName (el: Element) className =
if (unbox el = null) then
Error "Not inside an element with the given className"
else
if (unbox el.classList <> null && el.classList.contains className) then
Ok el
else
matchClassName (unbox el.parentNode) className
if (el.classList.contains className) then
Ok el
else
matchClassName el.parentElement className
let onContextMenu (e: PointerEvent) =
console.log "oncontextmenu"
match clickInsideElement (e, "w-container") with
| Ok s -> console.log "inside"
| Error error -> console.log error
|> ignore
null
let model = {
ContextMenu = document.querySelector """aside[data-context-menu="main"]""" :?> HTMLElement
}
document.addEventListener_contextmenu (fun e -> onContextMenu(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment