diff options
| author | bptato <nincsnevem662@gmail.com> | 2023-07-04 16:55:56 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2023-07-04 16:58:19 +0200 |
| commit | 01027f1749c80ce6def8546cb4ee43a5db064c1e (patch) | |
| tree | a02d516b4361a4238a9f54cf02258ea658e15a29 /src | |
| parent | c355c176863a930d2107254198749f420f272c06 (diff) | |
| download | chawan-01027f1749c80ce6def8546cb4ee43a5db064c1e.tar.gz | |
Event: add some properties, js: add defineConsts
Diffstat (limited to 'src')
| -rw-r--r-- | src/html/event.nim | 19 | ||||
| -rw-r--r-- | src/js/javascript.nim | 19 |
2 files changed, 35 insertions, 3 deletions
diff --git a/src/html/event.nim b/src/html/event.nim index 67e21b71..d862b40c 100644 --- a/src/html/event.nim +++ b/src/html/event.nim @@ -1,16 +1,31 @@ import js/javascript type + EventPhase = enum + NONE = 0u16 + CAPTURING_PHASE = 1u16 + AT_TARGET = 2u16 + BUBBLING_PHASE = 3u16 + Event* = ref object + ctype {.jsget: "type".}: string + target {.jsget.}: EventTarget + currentTarget {.jsget.}: EventTarget + eventPhase {.jsget.}: uint16 + bubbles {.jsget.}: bool + cancelable {.jsget.}: bool EventTarget* = ref object of RootObj - #EventHandler* = proc(event: Event): JSValue #TODO ? EventHandler* = JSValue jsDestructor(Event) jsDestructor(EventTarget) +func srcElement(this: Event): EventTarget {.jsfget.} = + return this.target + proc addEventModule*(ctx: JSContext) = - ctx.registerType(Event) + let eventCID = ctx.registerType(Event) + ctx.defineConsts(eventCID, EventPhase, uint16) ctx.registerType(EventTarget) diff --git a/src/js/javascript.nim b/src/js/javascript.nim index 1f213f64..3b1661d2 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -192,7 +192,8 @@ func findClass*(ctx: JSContext, class: string): Option[JSClassID] = return some(opaque.creg[class]) return none(JSClassID) -func newJSCFunction*(ctx: JSContext, name: string, fun: JSCFunction, argc: int = 0, proto = JS_CFUNC_generic, magic = 0): JSValue = +func newJSCFunction*(ctx: JSContext, name: string, fun: JSCFunction, + argc: int = 0, proto = JS_CFUNC_generic, magic = 0): JSValue = return JS_NewCFunction2(ctx, fun, cstring(name), cint(argc), proto, cint(magic)) proc free*(ctx: var JSContext) = @@ -717,6 +718,7 @@ proc toJS(ctx: JSContext, promise: EmptyPromise): JSValue proc toJSRefObj(ctx: JSContext, obj: ref object): JSValue proc toJS*(ctx: JSContext, obj: ref object): JSValue proc toJS*(ctx: JSContext, err: JSError): JSValue +proc toJS*(ctx: JSContext, f: JSCFunction): JSValue #TODO varargs proc fromJSFunction1[T, U](ctx: JSContext, val: JSValue): @@ -1039,6 +1041,21 @@ proc toJS*(ctx: JSContext, err: JSError): JSValue = let ctor = ctx.getOpaque().err_ctors[err.e] return JS_CallConstructor(ctx, ctor, 1, addr msg) +proc toJS*(ctx: JSContext, f: JSCFunction): JSValue = + return ctx.newJSCFunction("", f) + +proc defineConsts*[T](ctx: JSContext, classid: JSClassID, + consts: static openarray[(string, T)]) = + let proto = ctx.getOpaque().ctors[classid] + for (k, v) in consts: + ctx.defineProperty(proto, k, v) + +proc defineConsts*(ctx: JSContext, classid: JSClassID, + consts: typedesc[enum], astype: typedesc) = + let proto = ctx.getOpaque().ctors[classid] + for e in consts: + ctx.defineProperty(proto, $e, astype(e)) + type JSFuncGenerator = object t: BoundFunctionType |