From 3a7d189d88747d119ab475c96e067b86c4cd2957 Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 31 Aug 2023 22:52:28 +0200 Subject: 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. --- src/html/event.nim | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/html') 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, -- cgit 1.4.1-2-gfad0