about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-08-31 22:52:28 +0200
committerbptato <nincsnevem662@gmail.com>2023-08-31 22:52:28 +0200
commit3a7d189d88747d119ab475c96e067b86c4cd2957 (patch)
tree044978abafa0f2eb288bdaf6c0e840b530fadd2f /src/html
parent40aef0b310bb84628b759bdb3f17ff7229afc634 (diff)
downloadchawan-3a7d189d88747d119ab475c96e067b86c4cd2957.tar.gz
buffer: basic click event support
Mostly a proof of concept. Just bubble it unconditionally for now
and never prevent default.

Also, fix fromJSFunction by Dup'ing its closure JSValue.
Diffstat (limited to 'src/html')
-rw-r--r--src/html/event.nim22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/html/event.nim b/src/html/event.nim
index 5f841fd1..a6ab2e36 100644
--- a/src/html/event.nim
+++ b/src/html/event.nim
@@ -15,7 +15,7 @@ type
     AT_TARGET = 2u16
     BUBBLING_PHASE = 3u16
 
-  EventFlag = enum
+  EventFlag* = enum
     FLAG_STOP_PROPAGATION
     FLAG_STOP_IMMEDIATE_PROPAGATION
     FLAG_CANCELED
@@ -26,14 +26,14 @@ type
 
   Event* = ref object of RootObj
     ctype {.jsget: "type".}: string
-    target {.jsget.}: EventTarget
+    target* {.jsget.}: EventTarget
     currentTarget {.jsget.}: EventTarget
     eventPhase {.jsget.}: uint16
     bubbles {.jsget.}: bool
     cancelable {.jsget.}: bool
     #TODO DOMHighResTimeStamp?
     timeStamp {.jsget.}: float64
-    flags: set[EventFlag]
+    flags*: set[EventFlag]
     isTrusted {.jsufget.}: bool
 
   CustomEvent* = ref object of Event
@@ -47,9 +47,9 @@ type
 
   EventListenerCallback = proc (event: Event): Err[JSError]
 
-  EventListener = ref object
-    ctype: string
-    callback: EventListenerCallback
+  EventListener* = ref object
+    ctype*: string
+    callback*: EventListenerCallback
     capture: bool
     passive: Opt[bool]
     once: bool
@@ -93,6 +93,13 @@ proc newEvent(ctx: JSContext, ctype: string, eventInitDict = JS_UNDEFINED):
   event.ctype = ctype
   return ok(event)
 
+proc newEvent*(ctx: JSContext, ctype: string, target, currentTarget: EventTarget): Event =
+  return Event(
+    ctype: ctype,
+    target: target,
+    currentTarget: currentTarget
+  )
+
 proc initialize(this: Event, ctype: string, bubbles, cancelable: bool) =
   this.flags.incl(FLAG_INITIALIZED)
   this.isTrusted = false
@@ -231,10 +238,11 @@ proc flattenMore(ctx: JSContext, options: JSValue):
       passive = opt(x.get)
   return (capture, once, passive)
 
-proc addEventListener(ctx: JSContext, eventTarget: EventTarget,
+proc addEventListener(ctx: JSContext, eventTarget: EventTarget, ctype: string,
     callback: EventListenerCallback, options = JS_UNDEFINED) {.jsfunc.} =
   let (capture, once, passive) = flattenMore(ctx, options)
   let listener = EventListener(
+    ctype: ctype,
     capture: capture,
     passive: passive,
     once: once,