diff options
-rw-r--r-- | src/display/client.nim | 10 | ||||
-rw-r--r-- | src/html/dom.nim | 6 | ||||
-rw-r--r-- | src/html/env.nim | 6 | ||||
-rw-r--r-- | src/html/event.nim | 15 | ||||
-rw-r--r-- | src/js/javascript.nim | 5 | ||||
-rw-r--r-- | src/xhr/xmlhttprequest.nim | 46 |
6 files changed, 80 insertions, 8 deletions
diff --git a/src/display/client.nim b/src/display/client.nim index 2f99a7d5..6b18e092 100644 --- a/src/display/client.nim +++ b/src/display/client.nim @@ -22,6 +22,7 @@ import data/charset import display/pager import display/term import html/dom +import html/event import html/htmlparser import io/headers import io/lineedit @@ -44,7 +45,8 @@ import types/cookie import types/dispatcher import types/url import utils/opt -import xhr/formdata as formdata_impl +import xhr/formdata +import xhr/xmlhttprequest type Client* = ref ClientObj @@ -378,7 +380,7 @@ proc inputLoop(client: Client) = assert event.fd == sigwinch client.attrs = getWindowAttributes(client.console.tty) client.pager.windowChange(client.attrs) - if Event.Timer in event.events: + if selectors.Event.Timer in event.events: assert client.timeouts.runTimeoutFd(event.fd) client.runJSJobs() client.console.container.requestLines().then(proc() = @@ -409,7 +411,7 @@ proc headlessLoop(client: Client) = client.handleRead(event.fd) if Error in event.events: client.handleError(event.fd) - if Event.Timer in event.events: + if selectors.Event.Timer in event.events: assert client.timeouts.runTimeoutFd(event.fd) client.runJSJobs() client.loader.unregistered.setLen(0) @@ -583,11 +585,13 @@ proc newClient*(config: Config, dispatcher: Dispatcher): Client = ctx.addDOMExceptionModule() ctx.addCookieModule() ctx.addURLModule() + ctx.addEventModule() ctx.addDOMModule() ctx.addHTMLModule() ctx.addIntlModule() ctx.addBlobModule() ctx.addFormDataModule() + ctx.addXMLHttpRequestModule() ctx.addHeadersModule() ctx.addRequestModule() ctx.addResponseModule() diff --git a/src/html/dom.nim b/src/html/dom.nim index 40e627eb..4abd683f 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -12,6 +12,7 @@ import css/sheet import css/values import data/charset import encoding/decoderstream +import html/event import html/tags import img/bitmap import img/painter @@ -127,8 +128,6 @@ type EnvironmentSettings* = object scripting*: bool - EventTarget* = ref object of RootObj - Collection = ref CollectionObj CollectionObj = object of RootObj islive: bool @@ -402,7 +401,6 @@ jsDestructor(HTMLTextAreaElement) jsDestructor(HTMLLabelElement) jsDestructor(HTMLCanvasElement) jsDestructor(HTMLImageElement) -jsDestructor(EventTarget) jsDestructor(Node) jsDestructor(NodeList) jsDestructor(HTMLCollection) @@ -3043,7 +3041,7 @@ proc registerElements(ctx: JSContext, nodeCID: JSClassID) = register(HTMLImageElement, TAG_IMG) proc addDOMModule*(ctx: JSContext) = - let eventTargetCID = ctx.registerType(EventTarget) + let eventTargetCID = ctx.getClass("EventTarget") let nodeCID = ctx.registerType(Node, parent = eventTargetCID) ctx.registerType(NodeList) ctx.registerType(HTMLCollection) diff --git a/src/html/env.nim b/src/html/env.nim index 174f0a12..06289c4e 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -2,6 +2,7 @@ import selectors import streams import html/dom +import html/event import html/htmlparser import io/headers import io/loader @@ -15,7 +16,8 @@ import js/javascript import js/timeout import types/blob import types/url -import xhr/formdata as formdata_impl +import xhr/formdata +import xhr/xmlhttprequest # NavigatorID proc appCodeName(navigator: Navigator): string {.jsfget.} = "Mozilla" @@ -118,12 +120,14 @@ proc addScripting*(window: Window, selector: Selector[int]) = ctx.addDOMExceptionModule() ctx.addconsoleModule() ctx.addNavigatorModule() + ctx.addEventModule() ctx.addDOMModule() ctx.addURLModule() ctx.addHTMLModule() ctx.addIntlModule() ctx.addBlobModule() ctx.addFormDataModule() + ctx.addXMLHttpRequestModule() ctx.addHeadersModule() ctx.addRequestModule() ctx.addResponseModule() diff --git a/src/html/event.nim b/src/html/event.nim new file mode 100644 index 00000000..d4b6b691 --- /dev/null +++ b/src/html/event.nim @@ -0,0 +1,15 @@ +import js/javascript + +type + Event* = ref object + + EventTarget* = ref object of RootObj + + EventHandler* = proc(event: Event): JSValue + +jsDestructor(Event) +jsDestructor(EventTarget) + +proc addEventModule*(ctx: JSContext) = + ctx.registerType(Event) + ctx.registerType(EventTarget) diff --git a/src/js/javascript.nim b/src/js/javascript.nim index 02d22903..ac8069b7 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -1724,6 +1724,8 @@ template jsget*() {.pragma.} template jsget*(name: string) {.pragma.} template jsset*() {.pragma.} template jsset*(name: string) {.pragma.} +template jsgetset*() {.pragma.} +template jsgetset*(name: string) {.pragma.} proc js_illegal_ctor*(ctx: JSContext, this: JSValue, argc: cint, argv: ptr JSValue): JSValue {.cdecl.} = return JS_ThrowTypeError(ctx, "Illegal constructor") @@ -1787,6 +1789,9 @@ proc findPragmas(t: NimNode): JSObjectPragmas = case pragmaName of "jsget": result.jsget.add(op) of "jsset": result.jsset.add(op) + of "jsgetset": + result.jsget.add(op) + result.jsset.add(op) of "jsinclude": result.jsinclude.add(op) proc nim_finalize_for_js*[T](obj: ptr T) = diff --git a/src/xhr/xmlhttprequest.nim b/src/xhr/xmlhttprequest.nim new file mode 100644 index 00000000..ef0e6601 --- /dev/null +++ b/src/xhr/xmlhttprequest.nim @@ -0,0 +1,46 @@ +import html/event +import js/javascript + +type + XMLHttpRequestResponseType = enum + TYPE_UNKNOWN = "" + TYPE_ARRAYBUFFER = "arraybuffer" + TYPE_BLOB = "blob" + TYPE_DOCUMENT = "document" + TYPE_JSON = "json" + TYPE_TEXT = "text" + + XMLHttpRequestEventTarget = ref object of EventTarget + onloadstart {.jsgetset.}: EventHandler + onprogress {.jsgetset.}: EventHandler + onabort {.jsgetset.}: EventHandler + onerror {.jsgetset.}: EventHandler + onload {.jsgetset.}: EventHandler + ontimeout {.jsgetset.}: EventHandler + onloadend {.jsgetset.}: EventHandler + + XMLHttpRequestUpload = ref object of XMLHttpRequestEventTarget + + XMLHttpRequest = ref object of XMLHttpRequestEventTarget + onreadystatechange {.jsgetset.}: EventHandler + readyState {.jsget.}: uint16 + upload {.jsget.}: XMLHttpRequestUpload + +jsDestructor(XMLHttpRequestEventTarget) +jsDestructor(XMLHttpRequestUpload) +jsDestructor(XMLHttpRequest) + +func newXMLHttpRequest(): XMLHttpRequest {.jsctor.} = + let upload = XMLHttpRequestUpload() + return XMLHttpRequest( + upload: upload + ) + +proc open(this: XMLHttpRequest, httpMethod, url: string) {.jsfunc.} = + discard #TODO implement + +proc addXMLHttpRequestModule*(ctx: JSContext) = + let eventTargetCID = ctx.getClass("EventTarget") + let xhretCID = ctx.registerType(XMLHttpRequestEventTarget, eventTargetCID) + ctx.registerType(XMLHttpRequestUpload, xhretCID) + ctx.registerType(XMLHttpRequest, xhretCID) |