Commit db3d9aa7 authored by Kim Nguyễn's avatar Kim Nguyễn
Browse files

Add more Js Events.

parent 6b8e4354
......@@ -165,7 +165,7 @@ let js_to_cduce =
| "boolean" -> Value.vbool (Js.to_bool (Obj.magic v))
| "number" -> let f : float = Obj.magic v in
let i = int_of_float f in
if f == (float_of_int i) then Value.ocaml2cduce_int i
if f = (float_of_int i) then Value.ocaml2cduce_int i
else Value.float f
| "object" -> if deep then
if Js.(instanceof v array_empty) then
......@@ -216,12 +216,14 @@ let register_event id event handler =
match Js.Opt.to_option elem with
None -> Value.failwith' ("No element with id : " ^ mlid)
| Some elem ->
Js.Unsafe.set
elem
event
(Js.wrap_callback
(fun e -> ignore (Value.apply (Value.apply handler id) (js_to_cduce true e )))
)
let () = ignore (
Js.Unsafe.meth_call
elem "addEventListener"
[| Js.Unsafe.inject event;
Js.Unsafe.inject (Js.wrap_callback
(fun e -> ignore (Value.apply (Value.apply handler id) (js_to_cduce true e )))
)
|]) in Value.nil
let get s =
let s = Value.cduce2ocaml_string s in
......@@ -270,12 +272,7 @@ let define_prims () =
register_fun3 ~ns:Ns.cduce_unsafe_ns "js_register_event"
string_latin1 string_latin1 htype nil
(fun id event handler ->
register_event
id
event
handler;
Value.nil)
register_event
end;
begin
register_fun ~ns:Ns.cduce_unsafe_ns "js_cast"
......
......@@ -7,7 +7,7 @@ type Undefined = cduce_:js_undefined
type Function = cduce_:js_function
type Value = Object | Null | Undefined | Function
type Number = Float | Int
type Record = { js:this = Object
.. }
......@@ -24,3 +24,84 @@ let cast (Object -> Record;
let get : (Latin1 -> Value) = cduce_:js_get
(* DOM Events
http://www.w3.org/TR/DOM-Level-3-Events/#interface-Event
*)
type Event = Record &
{
type = String
target = Object|Null
currentTarget = Object|Null
bubbles = Bool
cancelable = Bool
eventPhase = (0 | 1 | 2 | 3)
NONE = 0
CAPTURING_PHASE = 1
AT_TARGET = 2
BUBBLING_PHASE = 3
defaultPrevented = Bool
timeStamp = Number
..
}
type UIEvent = Event &
{
view = Object | Null
detail = Int
..
}
type FocusEvent = UIEvent &
{
relatedTarget = Object | Null
.. }
type MouseEvent = UIEvent &
{
screenX = Int
screenY = Int
clientX = Int
clientY = Int
ctrlKey = Bool
shiftKey = Bool
metaKey = Bool
button = Int
buttons = Int
relatedTarget = Object | Null
..
}
type MouseEventName = "mousemove" | "mouseover" | "mouseenter" | "mouseout"
| "mouseleave" | "mousedown" | "mouseup" | "click" | "dblclick"
type WheelEvent = MouseEvent & {
deltaX = Number
deltaY = Number
deltaZ = Number
deltaMode = (0 | 1 | 2)
..
}
type KeyboardEvent = UIEvent & {
key = String
code = String
location = (0|1|2|3)
ctrlKey = Bool
shiftKey = Bool
metaKey = Bool
repeat = Bool
isComposing = Bool
..
}
type KeyboardEventName = "keydown" | "keyup"
| "keypress" (*Legacy *)
type OtherEventName = Latin1 \ (KeyboardEventName | MouseEventName)
let register_event : ((Latin1 -> OtherEventName -> (Latin1 -> Event -> []) -> []) &
(Latin1 -> KeyboardEventName -> (Latin1 -> KeyboardEvent -> []) -> []) &
(Latin1 -> MouseEventName -> (Latin1 -> MouseEvent -> []) -> [])) =
cduce_:js_register_event
(*
namespace js = Js.js
let h (_ : Latin1) (ev : {..}) : [] =
( match ev with
Js.MouseEvent -> print "Test ok !\n"
Js.MouseEvent -> print "Test ok !\n"
| _ -> print "Test not ok !\n");
print (string_of ev);
print "\n";
print "Handler appelé\n"
;;
let [] = Js.register_event "tutu" ("onclick" : Latin1) h
let [] = Js.register_event "tutu" ("click" : Latin1) h
;;
*)
let w = Js.get "Array"
let cw = Js.cast w
let [] = print (string_of w);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment