summary refs log tree commit diff stats
path: root/lib/js
diff options
context:
space:
mode:
authorYuriy Glukhov <yuriy.glukhov@gmail.com>2015-11-25 19:27:13 +0200
committerYuriy Glukhov <yuriy.glukhov@gmail.com>2015-11-25 19:27:13 +0200
commitc0754beb229ada7f5fd1e60065b68ed4fe56e62d (patch)
tree21a16db455b7718500054342b1418ef3cf0b02ea /lib/js
parenteaed36092c5caa5854dcdb4bb205a3c1050e323c (diff)
downloadNim-c0754beb229ada7f5fd1e60065b68ed4fe56e62d.tar.gz
Dom refactoring
Diffstat (limited to 'lib/js')
-rw-r--r--lib/js/dom.nim306
1 files changed, 173 insertions, 133 deletions
diff --git a/lib/js/dom.nim b/lib/js/dom.nim
index b063fa838..11df959d7 100644
--- a/lib/js/dom.nim
+++ b/lib/js/dom.nim
@@ -14,36 +14,35 @@ when not defined(js) and not defined(Nimdoc):
   {.error: "This module only works on the JavaScript platform".}
 
 type
-  TEventHandlers* {.importc.} = object of RootObj
-    onabort*: proc (event: ref TEvent) {.nimcall.}
-    onblur*: proc (event: ref TEvent) {.nimcall.}
-    onchange*: proc (event: ref TEvent) {.nimcall.}
-    onclick*: proc (event: ref TEvent) {.nimcall.}
-    ondblclick*: proc (event: ref TEvent) {.nimcall.}
-    onerror*: proc (event: ref TEvent) {.nimcall.}
-    onfocus*: proc (event: ref TEvent) {.nimcall.}
-    onkeydown*: proc (event: ref TEvent) {.nimcall.}
-    onkeypress*: proc (event: ref TEvent) {.nimcall.}
-    onkeyup*: proc (event: ref TEvent) {.nimcall.}
-    onload*: proc (event: ref TEvent) {.nimcall.}
-    onmousedown*: proc (event: ref TEvent) {.nimcall.}
-    onmousemove*: proc (event: ref TEvent) {.nimcall.}
-    onmouseout*: proc (event: ref TEvent) {.nimcall.}
-    onmouseover*: proc (event: ref TEvent) {.nimcall.}
-    onmouseup*: proc (event: ref TEvent) {.nimcall.}
-    onreset*: proc (event: ref TEvent) {.nimcall.}
-    onselect*: proc (event: ref TEvent) {.nimcall.}
-    onsubmit*: proc (event: ref TEvent) {.nimcall.}
-    onunload*: proc (event: ref TEvent) {.nimcall.}
-
-    addEventListener*: proc(ev: cstring, cb: proc(ev: ref TEvent), useCapture: bool = false) {.nimcall.}
+  EventTarget* = ref EventTargetObj
+  EventTargetObj {.importc.} = object of RootObj
+    onabort*: proc (event: Event) {.nimcall.}
+    onblur*: proc (event: Event) {.nimcall.}
+    onchange*: proc (event: Event) {.nimcall.}
+    onclick*: proc (event: Event) {.nimcall.}
+    ondblclick*: proc (event: Event) {.nimcall.}
+    onerror*: proc (event: Event) {.nimcall.}
+    onfocus*: proc (event: Event) {.nimcall.}
+    onkeydown*: proc (event: Event) {.nimcall.}
+    onkeypress*: proc (event: Event) {.nimcall.}
+    onkeyup*: proc (event: Event) {.nimcall.}
+    onload*: proc (event: Event) {.nimcall.}
+    onmousedown*: proc (event: Event) {.nimcall.}
+    onmousemove*: proc (event: Event) {.nimcall.}
+    onmouseout*: proc (event: Event) {.nimcall.}
+    onmouseover*: proc (event: Event) {.nimcall.}
+    onmouseup*: proc (event: Event) {.nimcall.}
+    onreset*: proc (event: Event) {.nimcall.}
+    onselect*: proc (event: Event) {.nimcall.}
+    onsubmit*: proc (event: Event) {.nimcall.}
+    onunload*: proc (event: Event) {.nimcall.}
 
   Window* = ref WindowObj
-  WindowObj {.importc.} = object of TEventHandlers
+  WindowObj {.importc.} = object of EventTargetObj
     document*: Document
-    event*: ref TEvent
-    history*: ref THistory
-    location*: ref TLocation
+    event*: Event
+    history*: History
+    location*: Location
     closed*: bool
     defaultStatus*: cstring
     innerHeight*, innerWidth*: int
@@ -57,50 +56,15 @@ type
     statusbar*: ref TStatusBar
     status*: cstring
     toolbar*: ref TToolBar
-
-    alert*: proc (msg: cstring) {.nimcall.}
-    back*: proc () {.nimcall.}
-    blur*: proc () {.nimcall.}
-    captureEvents*: proc (eventMask: int) {.nimcall.}
-    clearInterval*: proc (interval: ref TInterval) {.nimcall.}
-    clearTimeout*: proc (timeout: ref TTimeOut) {.nimcall.}
-    close*: proc () {.nimcall.}
-    confirm*: proc (msg: cstring): bool {.nimcall.}
-    disableExternalCapture*: proc () {.nimcall.}
-    enableExternalCapture*: proc () {.nimcall.}
-    find*: proc (text: cstring, caseSensitive = false,
-                 backwards = false) {.nimcall.}
-    focus*: proc () {.nimcall.}
-    forward*: proc () {.nimcall.}
-    handleEvent*: proc (e: ref TEvent) {.nimcall.}
-    home*: proc () {.nimcall.}
-    moveBy*: proc (x, y: int) {.nimcall.}
-    moveTo*: proc (x, y: int) {.nimcall.}
-    open*: proc (uri, windowname: cstring,
-                 properties: cstring = nil): Window {.nimcall.}
-    print*: proc () {.nimcall.}
-    prompt*: proc (text, default: cstring): cstring {.nimcall.}
-    releaseEvents*: proc (eventMask: int) {.nimcall.}
-    resizeBy*: proc (x, y: int) {.nimcall.}
-    resizeTo*: proc (x, y: int) {.nimcall.}
-    routeEvent*: proc (event: ref TEvent) {.nimcall.}
-    scrollBy*: proc (x, y: int) {.nimcall.}
-    scrollTo*: proc (x, y: int) {.nimcall.}
-    setInterval*: proc (code: cstring, pause: int): ref TInterval {.nimcall.}
-    setTimeout*: proc (code: cstring, pause: int): ref TTimeOut {.nimcall.}
-    stop*: proc () {.nimcall.}
     frames*: seq[TFrame]
 
   Frame* = ref FrameObj
   FrameObj {.importc.} = object of WindowObj
 
-  ClassList* {.importc.} = object of RootObj
-    add*: proc (class: cstring) {.nimcall.}
-    remove*: proc (class: cstring) {.nimcall.}
-    contains*: proc (class: cstring):bool {.nimcall.}
-    toggle*: proc (class: cstring) {.nimcall.}
+  ClassList* = ref ClassListObj
+  ClassListObj {.importc.} = object of RootObj
 
-  TNodeType* = enum
+  NodeType* = enum
     ElementNode = 1,
     AttributeNode,
     TextNode,
@@ -115,7 +79,7 @@ type
     NotationNode
 
   Node* = ref NodeObj
-  NodeObj {.importc.} = object of TEventHandlers
+  NodeObj {.importc.} = object of EventTargetObj
     attributes*: seq[Node]
     childNodes*: seq[Node]
     children*: seq[Node]
@@ -124,29 +88,12 @@ type
     lastChild*: Node
     nextSibling*: Node
     nodeName*: cstring
-    nodeType*: TNodeType
+    nodeType*: NodeType
     nodeValue*: cstring
     parentNode*: Node
     previousSibling*: Node
-    appendChild*: proc (child: Node) {.nimcall.}
-    appendData*: proc (data: cstring) {.nimcall.}
-    cloneNode*: proc (copyContent: bool): Node {.nimcall.}
-    deleteData*: proc (start, len: int) {.nimcall.}
-    getAttribute*: proc (attr: cstring): cstring {.nimcall.}
-    getAttributeNode*: proc (attr: cstring): Node {.nimcall.}
-    hasChildNodes*: proc (): bool {.nimcall.}
     innerHTML*: cstring
-    insertBefore*: proc (newNode, before: Node) {.nimcall.}
-    insertData*: proc (position: int, data: cstring) {.nimcall.}
-    removeAttribute*: proc (attr: cstring) {.nimcall.}
-    removeAttributeNode*: proc (attr: Node) {.nimcall.}
-    removeChild*: proc (child: Node) {.nimcall.}
-    replaceChild*: proc (newNode, oldNode: Node) {.nimcall.}
-    replaceData*: proc (start, len: int, text: cstring) {.nimcall.}
-    scrollIntoView*: proc () {.nimcall.}
-    setAttribute*: proc (name, value: cstring) {.nimcall.}
-    setAttributeNode*: proc (attr: Node) {.nimcall.}
-    style*: ref TStyle
+    style*: Style
 
   Document* = ref DocumentObj
   DocumentObj {.importc.} = object of NodeObj
@@ -164,31 +111,16 @@ type
     title*: cstring
     URL*: cstring
     vlinkColor*: cstring
-    captureEvents*: proc (eventMask: int) {.nimcall.}
-    createAttribute*: proc (identifier: cstring): Node {.nimcall.}
-    createElement*: proc (identifier: cstring): Element {.nimcall.}
-    createTextNode*: proc (identifier: cstring): Node {.nimcall.}
-    getElementById*: proc (id: cstring): Element {.nimcall.}
-    getElementsByName*: proc (name: cstring): seq[Element] {.nimcall.}
-    getElementsByTagName*: proc (name: cstring): seq[Element] {.nimcall.}
-    getElementsByClassName*: proc (name: cstring): seq[Element] {.nimcall.}
-    getSelection*: proc (): cstring {.nimcall.}
-    handleEvent*: proc (event: ref TEvent) {.nimcall.}
-    open*: proc () {.nimcall.}
-    releaseEvents*: proc (eventMask: int) {.nimcall.}
-    routeEvent*: proc (event: ref TEvent) {.nimcall.}
-    write*: proc (text: cstring) {.nimcall.}
-    writeln*: proc (text: cstring) {.nimcall.}
     anchors*: seq[AnchorElement]
     forms*: seq[FormElement]
     images*: seq[ImageElement]
-    applets*: seq[ref TApplet]
+    applets*: seq[Element]
     embeds*: seq[EmbedElement]
     links*: seq[LinkElement]
 
   Element* = ref ElementObj
   ElementObj {.importc.} = object of NodeObj
-    classList*: ref Classlist
+    classList*: Classlist
     checked*: bool
     defaultChecked*: bool
     defaultValue*: cstring
@@ -196,14 +128,7 @@ type
     form*: FormElement
     name*: cstring
     readOnly*: bool
-    blur*: proc () {.nimcall.}
-    click*: proc () {.nimcall.}
-    focus*: proc () {.nimcall.}
-    handleEvent*: proc (event: ref TEvent) {.nimcall.}
-    select*: proc () {.nimcall.}
     options*: seq[OptionElement]
-    getElementsByTagName*: proc (name: cstring): seq[Element] {.nimcall.}
-    getElementsByClassName*: proc (name: cstring): seq[Element] {.nimcall.}
 
   LinkElement* = ref LinkObj
   LinkObj {.importc.} = object of ElementObj
@@ -220,16 +145,12 @@ type
     width*: int
     `type`*: cstring
     vspace*: int
-    play*: proc () {.nimcall.}
-    stop*: proc () {.nimcall.}
 
   AnchorElement* = ref AnchorObj
   AnchorObj {.importc.} = object of ElementObj
     text*: cstring
     x*, y*: int
 
-  TApplet* {.importc.} = object of RootObj
-
   OptionElement* = ref OptionObj
   OptionObj {.importc.} = object of ElementObj
     defaultSelected*: bool
@@ -244,8 +165,6 @@ type
     encoding*: cstring
     `method`*: cstring
     target*: cstring
-    reset*: proc () {.nimcall.}
-    submit*: proc () {.nimcall.}
     elements*: seq[Element]
 
   ImageElement* = ref ImageObj
@@ -259,8 +178,8 @@ type
     vspace*: int
     width*: int
 
-
-  TStyle* {.importc.} = object of RootObj
+  Style = ref StyleObj
+  StyleObj {.importc.} = object of RootObj
     background*: cstring
     backgroundAttachment*: cstring
     backgroundColor*: cstring
@@ -350,11 +269,9 @@ type
     width*: cstring
     wordSpacing*: cstring
     zIndex*: int
-    getAttribute*: proc (attr: cstring, caseSensitive=false): cstring {.nimcall.}
-    removeAttribute*: proc (attr: cstring, caseSensitive=false) {.nimcall.}
-    setAttribute*: proc (attr, value: cstring, caseSensitive=false) {.nimcall.}
 
-  TEvent* {.importc.} = object of RootObj
+  Event* = ref EventObj
+  EventObj {.importc.} = object of RootObj
     target*: Node
     altKey*, ctrlKey*, shiftKey*: bool
     button*: int
@@ -393,7 +310,8 @@ type
     SUBMIT*: int
     UNLOAD*: int
 
-  TLocation* {.importc.} = object of RootObj
+  Location* = ref LocationObj
+  LocationObj {.importc.} = object of RootObj
     hash*: cstring
     host*: cstring
     hostname*: cstring
@@ -402,16 +320,13 @@ type
     port*: cstring
     protocol*: cstring
     search*: cstring
-    reload*: proc () {.nimcall.}
-    replace*: proc (s: cstring) {.nimcall.}
 
-  THistory* {.importc.} = object of RootObj
+  History* = ref HistoryObj
+  HistoryObj {.importc.} = object of RootObj
     length*: int
-    back*: proc () {.nimcall.}
-    forward*: proc () {.nimcall.}
-    go*: proc (pagesToJump: int) {.nimcall.}
 
-  TNavigator* {.importc.} = object of RootObj
+  Navigator* = ref NavigatorObj
+  NavigatorObj {.importc.} = object of RootObj
     appCodeName*: cstring
     appName*: cstring
     appVersion*: cstring
@@ -419,7 +334,6 @@ type
     language*: cstring
     platform*: cstring
     userAgent*: cstring
-    javaEnabled*: proc (): bool {.nimcall.}
     mimeTypes*: seq[ref TMimeType]
 
   TPlugin* {.importc.} = object of RootObj
@@ -441,7 +355,8 @@ type
   TToolBar* = TLocationBar
   TStatusBar* = TLocationBar
 
-  TScreen* {.importc.} = object of RootObj
+  Screen = ref ScreenObj
+  ScreenObj {.importc.} = object of RootObj
     availHeight*: int
     availWidth*: int
     colorDepth*: int
@@ -452,11 +367,127 @@ type
   TTimeOut* {.importc.} = object of RootObj
   TInterval* {.importc.} = object of RootObj
 
+{.push importcpp.}
+
+# EventTarget "methods"
+proc addEventListener*(et: EventTarget, ev: cstring, cb: proc(ev: Event), useCapture: bool = false)
+
+# Window "methods"
+proc alert*(w: Window, msg: cstring)
+proc back*(w: Window)
+proc blur*(w: Window)
+proc captureEvents*(w: Window, eventMask: int) {.deprecated.}
+proc clearInterval*(w: Window, interval: ref TInterval)
+proc clearTimeout*(w: Window, timeout: ref TTimeOut)
+proc close*(w: Window)
+proc confirm*(w: Window, msg: cstring): bool
+proc disableExternalCapture*(w: Window)
+proc enableExternalCapture*(w: Window)
+proc find*(w: Window, text: cstring, caseSensitive = false,
+           backwards = false)
+proc focus*(w: Window)
+proc forward*(w: Window)
+proc handleEvent*(w: Window, e: Event)
+proc home*(w: Window)
+proc moveBy*(w: Window, x, y: int)
+proc moveTo*(w: Window, x, y: int)
+proc open*(w: Window, uri, windowname: cstring,
+           properties: cstring = nil): Window
+proc print*(w: Window)
+proc prompt*(w: Window, text, default: cstring): cstring
+proc releaseEvents*(w: Window, eventMask: int) {.deprecated.}
+proc resizeBy*(w: Window, x, y: int)
+proc resizeTo*(w: Window, x, y: int)
+proc routeEvent*(w: Window, event: Event)
+proc scrollBy*(w: Window, x, y: int)
+proc scrollTo*(w: Window, x, y: int)
+proc setInterval*(w: Window, code: cstring, pause: int): ref TInterval
+proc setTimeout*(w: Window, code: cstring, pause: int): ref TTimeOut
+proc stop*(w: Window)
+
+# Node "methods"
+proc appendChild*(n, child: Node)
+proc appendData*(n: Node, data: cstring)
+proc cloneNode*(n: Node, copyContent: bool): Node
+proc deleteData*(n: Node, start, len: int)
+proc getAttribute*(n: Node, attr: cstring): cstring
+proc getAttributeNode*(n: Node, attr: cstring): Node
+proc hasChildNodes*(n: Node): bool
+proc insertBefore*(n, newNode, before: Node)
+proc insertData*(n: Node, position: int, data: cstring)
+proc removeAttribute*(n: Node, attr: cstring)
+proc removeAttributeNode*(n, attr: Node)
+proc removeChild*(n, child: Node)
+proc replaceChild*(n, newNode, oldNode: Node)
+proc replaceData*(n: Node, start, len: int, text: cstring)
+proc scrollIntoView*(n: Node)
+proc setAttribute*(n: Node, name, value: cstring)
+proc setAttributeNode*(n: Node, attr: Node)
+
+# Document "methods"
+proc captureEvents*(d: Document, eventMask: int) {.deprecated.}
+proc createAttribute*(d: Document, identifier: cstring): Node
+proc createElement*(d: Document, identifier: cstring): Element
+proc createTextNode*(d: Document, identifier: cstring): Node
+proc getElementById*(d: Document, id: cstring): Element
+proc getElementsByName*(d: Document, name: cstring): seq[Element]
+proc getElementsByTagName*(d: Document, name: cstring): seq[Element]
+proc getElementsByClassName*(d: Document, name: cstring): seq[Element]
+proc getSelection*(d: Document): cstring
+proc handleEvent*(d: Document, event: Event)
+proc open*(d: Document)
+proc releaseEvents*(d: Document, eventMask: int) {.deprecated.}
+proc routeEvent*(d: Document, event: Event)
+proc write*(d: Document, text: cstring)
+proc writeln*(d: Document, text: cstring)
+
+# Element "methods"
+proc blur*(e: Element)
+proc click*(e: Element)
+proc focus*(e: Element)
+proc handleEvent*(e: Element, event: Event)
+proc select*(e: Element)
+proc getElementsByTagName*(e: Element, name: cstring): seq[Element]
+proc getElementsByClassName*(e: Element, name: cstring): seq[Element]
+
+# FormElement "methods"
+proc reset*(f: FormElement)
+proc submit*(f: FormElement)
+
+# EmbedElement "methods"
+proc play*(e: EmbedElement)
+proc stop*(e: EmbedElement)
+
+# Location "methods"
+proc reload*(loc: Location)
+proc replace*(loc: Location, s: cstring)
+
+# History "methods"
+proc back*(h: History)
+proc forward*(h: History)
+proc go*(h: History, pagesToJump: int)
+
+# Navigator "methods"
+proc javaEnabled*(h: Navigator): bool
+
+# ClassList "methods"
+proc add*(c: ClassList, class: cstring)
+proc remove*(c: ClassList, class: cstring)
+proc contains*(c: ClassList, class: cstring):bool
+proc toggle*(c: ClassList, class: cstring)
+
+# Style "methods"
+proc getAttribute*(s: Style, attr: cstring, caseSensitive=false): cstring
+proc removeAttribute*(s: Style, attr: cstring, caseSensitive=false)
+proc setAttribute*(s: Style, attr, value: cstring, caseSensitive=false)
+
+{.pop.}
+
 var
   window* {.importc, nodecl.}: Window
   document* {.importc, nodecl.}: Document
-  navigator* {.importc, nodecl.}: ref TNavigator
-  screen* {.importc, nodecl.}: ref TScreen
+  navigator* {.importc, nodecl.}: Navigator
+  screen* {.importc, nodecl.}: Screen
 
 proc decodeURI*(uri: cstring): cstring {.importc, nodecl.}
 proc encodeURI*(uri: cstring): cstring {.importc, nodecl.}
@@ -474,6 +505,7 @@ proc parseInt*(s: cstring, radix: int):int {.importc, nodecl.}
 
 
 type
+  TEventHandlers* {.deprecated.} = EventTargetObj
   TWindow* {.deprecated.} = WindowObj
   TFrame* {.deprecated.} = FrameObj
   TNode* {.deprecated.} = NodeObj
@@ -485,3 +517,11 @@ type
   TOption* {.deprecated.} = OptionObj
   TForm* {.deprecated.} = FormObj
   TImage* {.deprecated.} = ImageObj
+  TNodeType* {.deprecated.} = NodeType
+  TEvent* {.deprecated.} = EventObj
+  TLocation* {.deprecated.} = LocationObj
+  THistory* {.deprecated.} = HistoryObj
+  TNavigator* {.deprecated.} = NavigatorObj
+  TStyle* {.deprecated.} = StyleObj
+  TScreen* {.deprecated.} = ScreenObj
+  TApplet* {.importc, deprecated.} = object of RootObj