Skip to content

Commit

Permalink
add some froc-dom API
Browse files Browse the repository at this point in the history
  • Loading branch information
duckpilot committed Jun 24, 2010
1 parent 64480e6 commit abfb490
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
6 changes: 3 additions & 3 deletions examples/froc-dom/clicks/clicks.ml
Expand Up @@ -8,15 +8,15 @@ let onload () =
let clicks = F.count (Fd.clicks (D.document#getElementById "click")) in
let ticks = F.count (Fd.ticks 1000.) in

Fd.attach_innerHTML
Fd.attach_innerHTML_b
(D.document#getElementById "clicks")
(clicks >>= fun c -> F.return (string_of_int c));

Fd.attach_innerHTML
Fd.attach_innerHTML_b
(D.document#getElementById "seconds")
(ticks >>= fun s -> F.return (string_of_int s));

Fd.attach_innerHTML
Fd.attach_innerHTML_b
(D.document#getElementById "difference")
(F.bind2
clicks ticks
Expand Down
6 changes: 3 additions & 3 deletions examples/froc-dom/quickhull/quickhull.ml
Expand Up @@ -205,9 +205,9 @@ let onload () =

let clicks =
F.merge [
F.map (fun () -> `Stationary) (Fd.clicks (get "stationary"));
F.map (fun () -> `Bouncing) (Fd.clicks (get "bouncing"));
F.map (fun () -> `Remove) (Fd.clicks (get "remove"));
F.map (fun _ -> `Stationary) (Fd.clicks (get "stationary"));
F.map (fun _ -> `Bouncing) (Fd.clicks (get "bouncing"));
F.map (fun _ -> `Remove) (Fd.clicks (get "remove"));
] in

let points : (float * float * Fda.color) L.t =
Expand Down
41 changes: 30 additions & 11 deletions src/froc-dom/froc_dom.ml
Expand Up @@ -90,10 +90,6 @@ let mouse_e () =

let mouse_b () = hold (0, 0) (mouse_e ())

let attach_innerHTML elem b =
let e = changes b in
notify_e e (fun s -> elem#_set_innerHTML s)

let input_value_e input =
let e, s = make_event () in
let f _ = send s input#_get_value in
Expand All @@ -103,13 +99,20 @@ let input_value_e input =

let input_value_b input = hold input#_get_value (input_value_e input)

let attach_input_value_e i e = notify_e e (fun v -> i#_set_value v)
let attach_innerHTML_e el e = notify_e e (fun s -> el#_set_innerHTML s)
let attach_innerHTML_b el b = notify_b b (fun s -> el#_set_innerHTML s)

let attach_input_value_b i b = attach_input_value_e i (changes b)
let attach_input_value_e i e = notify_e e (fun v -> i#_set_value v)
let attach_input_value_b i b = notify_b b (fun v -> i#_set_value v)

let attach_backgroundColor_e el e = notify_e e (fun v -> el#_get_style#_set_backgroundColor v)
let attach_backgroundColor_b el b = notify_b b (fun v -> el#_get_style#_set_backgroundColor v)

let attach_backgroundColor_b el b = attach_backgroundColor_e el (changes b)
let attach_display_e el e = notify_e e (fun v -> el#_get_style#_set_display v)
let attach_display_b el b = notify_b b (fun v -> el#_get_style#_set_display v)

let attach_fontSize_e el e = notify_e e (fun v -> el#_get_style#_set_fontSize v)
let attach_fontSize_b el b = notify_b b (fun v -> el#_get_style#_set_fontSize v)

let node_of_result = function
| Value v -> (v :> Dom.node)
Expand Down Expand Up @@ -141,9 +144,25 @@ let replaceNode n nb =
old := c in
notify_result_b nb update

let clicks (elem : #Dom.element) =
let event name (elem : #Dom.element) =
let e, s = make_event () in
let f ev = ev#preventDefault; send s () in
elem#addEventListener "click" f false;
cleanup (fun () -> elem#removeEventListener "click" f false);
let f = send s in
elem#addEventListener name f false;
cleanup (fun () -> elem#removeEventListener name f false);
e

let mouseEvent name (elem : #Dom.element) =
let e, s = make_event () in
let f = send s in
elem#addEventListener_mouseEvent_ name f false;
cleanup (fun () -> elem#removeEventListener_mouseEvent_ name f false);
e

let keyEvent name (elem : #Dom.element) =
let e, s = make_event () in
let f = send s in
elem#addEventListener_keyEvent_ name f false;
cleanup (fun () -> elem#removeEventListener_keyEvent_ name f false);
e

let clicks (elem : #Dom.element) = event "click" elem
18 changes: 15 additions & 3 deletions src/froc-dom/froc_dom.mli
Expand Up @@ -29,17 +29,29 @@ val delay_bb : 'a Froc.behavior -> float Froc.behavior -> 'a Froc.behavior
val mouse_e : unit -> (int * int) Froc.event
val mouse_b : unit -> (int * int) Froc.behavior

val attach_innerHTML : #Dom.element -> string Froc.behavior -> unit

val input_value_e : #Dom.input -> string Froc.event
val input_value_b : #Dom.input -> string Froc.behavior

val attach_innerHTML_e : #Dom.element -> string Froc.event -> unit
val attach_innerHTML_b : #Dom.element -> string Froc.behavior -> unit

val attach_input_value_e : #Dom.input -> string Froc.event -> unit
val attach_input_value_b : #Dom.input -> string Froc.behavior -> unit

val attach_backgroundColor_e : #Dom.element -> string Froc.event -> unit
val attach_backgroundColor_b : #Dom.element -> string Froc.behavior -> unit

val attach_display_e : #Dom.element -> string Froc.event -> unit
val attach_display_b : #Dom.element -> string Froc.behavior -> unit

val attach_fontSize_e : #Dom.element -> string Froc.event -> unit
val attach_fontSize_b : #Dom.element -> string Froc.behavior -> unit

val appendChild : #Dom.node -> #Dom.node Froc.behavior -> unit
val replaceNode : #Dom.node -> #Dom.node Froc.behavior -> unit

val clicks : #Dom.element -> unit Froc.event
val clicks : #Dom.element -> Dom.event Froc.event

val event : string -> #Dom.element -> Dom.event Froc.event
val mouseEvent : string -> #Dom.element -> Dom.mouseEvent Froc.event
val keyEvent : string -> #Dom.element -> Dom.keyEvent Froc.event

0 comments on commit abfb490

Please sign in to comment.