3131 POSSIBILITY OF SUCH DAMAGE.
3232 ----------------------------------------------------------------------------*)
3333
34+ (* * js_of_ocaml bindings for the D3.js library. *)
35+
3436type ('a, 'b) t
37+ (* * The type of a D3 operation that assumes that its parent has data of type
38+ ['a] bound to it, and which itself can act as a context with data of type
39+ ['b] bound to it. *)
40+
3541type ('a, 'b) fn = Dom .node Js .t -> 'a -> int -> 'b
42+ (* * A type alias for the sorts of functions that can be used to construct
43+ {!attr}, {!classed}, {!style}, and {!property}, selections below. *)
44+
45+ val select : string -> ('a , 'a ) t
46+ (* * [select selector] selects the first descendant element that matches the
47+ specified selector string, for each element in the selection. If the
48+ current element has associated data, this data is inherited by the returned
49+ subselection, and automatically bound to the newly selected elements. If
50+ multiple elements match the selector, only the first matching element in
51+ document traversal order will be selected.
52+
53+ {{:https://github.com/mbostock/d3/wiki/Selections#select}D3.js docs} *)
3654
37- val select : string -> ('a , 'a ) t
3855val selectAll : string -> ('a , 'a ) t
56+ (* * [selectAll selector] selects descendant elements that match the specified
57+ selector string, for each element in the selection. The subselection does
58+ not inherit data from the current selection; however, if the data value is
59+ specified as a function, this function will be called with the data d of the
60+ ancestor node and the group index i to determine the data bindings for the
61+ subselection.
62+
63+ {{:https://github.com/mbostock/d3/wiki/Selections#selectAll}D3.js docs} *)
64+
65+ val attr : string -> ('a , string ) fn -> ('a , 'a ) t
66+ (* * [attr name f] sets the [name] attribute to the specified value on all
67+ selected elements. The value is determined by calling [f] for each element,
68+ passing it the current DOM element, the datum attached to the DOM element,
69+ and the element's index in the selection.
70+
71+ {{:https://github.com/mbostock/d3/wiki/Selections#attr}D3.js docs} *)
72+
73+ val classed : string -> ('a , bool ) fn -> ('a , 'a ) t
74+ (* * [classed class f] sets whether or not [class] is associated with the
75+ selected elements. This is determined by calling [f] for each element,
76+ passing it the current DOM element, the datum attached to the DOM element,
77+ and the element's index in the selection.
78+
79+ This operator is a convenience routine for setting the ["class"] attribute;
80+ it understands that the ["class"] attribute is a set of tokens separated by
81+ spaces. If you want to set several classes at once use a space-separated
82+ list of class names as the first argument.
83+
84+ {{:https://github.com/mbostock/d3/wiki/Selections#classes}D3.js docs} *)
85+
86+ val style : string -> ('a , string ) fn -> ('a , 'a ) t
87+ (* * [style name f] sets the [name] CSS style property to the specified value on
88+ all selected elements. The value is determined by calling [f] for each element,
89+ passing it the current DOM element, the datum attached to the DOM element,
90+ and the element's index in the selection.
91+
92+ {{:https://github.com/mbostock/d3/wiki/Selections#style}D3.js docs} *)
3993
40- val attr : string -> ('a , string ) fn -> ('a , 'a ) t
41- val classed : string -> ('a , bool ) fn -> ('a , 'a ) t
42- val style : string -> ('a , string ) fn -> ('a , 'a ) t
4394val property : string -> ('a , 'b Js .t ) fn -> ('a , 'a ) t
95+ (* * [property name f] sets the [name] property to the specified value on all
96+ selected elements. The value is determined by calling [f] for each element,
97+ passing it the current DOM element, the datum attached to the DOM element,
98+ and the element's index in the selection.
99+
100+ {{:https://github.com/mbostock/d3/wiki/Selections#property}D3.js docs} *)
44101
45102val text : ('a , string ) fn -> ('a , 'a ) t
103+ (* * [text f] sets the text content to the specified value on all selected
104+ elements. The value is determined by calling [f] for each element, passing
105+ it the current DOM element, the datum attached to the DOM element, and the
106+ element's index in the selection.
107+
108+ The [text] operator is based on the
109+ {{:http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent}textContent}
110+ property; setting the text content will replace any existing child elements.
111+
112+ {{:https://github.com/mbostock/d3/wiki/Selections#text}D3.js docs} *)
113+
46114val html : ('a , string ) fn -> ('a , 'a ) t
115+ (* * [html f] sets the inner HTML content to the specified value on all selected
116+ elements. The value is determined by calling [f] for each element, passing
117+ it the current DOM element, the datum attached to the DOM element, and the
118+ element's index in the selection.
119+
120+ The [html] operator is based on the
121+ {{:http://dev.w3.org/html5/spec-LC/apis-in-html-documents.html#innerhtml}innerHTML}
122+ property; setting the inner HTML content will replace any existing child
123+ elements. Also, you may prefer to use the {!append} operator to create HTML
124+ content in a data-driven way; this operator is intended for when you want a
125+ little bit of HTML, say for rich formatting.
126+
127+ {{:https://github.com/mbostock/d3/wiki/Selections#html}D3.js docs} *)
47128
48129val append : string -> ('a , 'a ) t
49- val static : string -> ('a , 'a ) t
130+ (* * [append name] appends a new [name] element as the last child of each
131+ element in the current selection, returning a new selection containing the
132+ appended elements. Each new element inherits the data of the current
133+ elements in the same manner as {!select}.
134+
135+ {{:https://github.com/mbostock/d3/wiki/Selections#append}D3.js docs} *)
136+
50137val remove : ('a , 'a ) t
138+ (* * [remove] removes the elements in the current selection from the current
139+ document. Returns the current selection (the same elements that were
140+ removed) which are now "off-screen", detached from the DOM.
141+
142+ {{:https://github.com/mbostock/d3/wiki/Selections#remove}D3.js docs} *)
143+
144+ val static : string -> ('a , 'a ) t
145+ (* * [static name] appends a new [name] element as the last child of each
146+ element in the current selection, if [name] child element does not already
147+ exist. If an [name] element already does exist, then it will return a
148+ selection containing just that element.
149+
150+ Note that you can use multiple [static] operators to create elements with
151+ the same name.
152+
153+ {[nest (select "body")
154+ [ static "div"
155+ |. text (fun _ _ _ -> "first div")
156+ ; static "div"
157+ |. text (fun _ _ _ -> "second div") ] ]}
158+
159+ This is not part of the D3.js API. *)
160+
161+ (* * {2 Data Binding} *)
51162
52- val datum : ('a -> int -> 'b list ) -> ('a , 'b ) t
53163val data : ('a -> int -> 'b list ) -> ('a , 'b ) t
164+ (* * {{:https://github.com/mbostock/d3/wiki/Selections#data}D3.js docs} *)
165+
166+ val datum : ('a -> int -> 'b list ) -> ('a , 'b ) t
167+ (* * {{:https://github.com/mbostock/d3/wiki/Selections#datum}D3.js docs} *)
168+
169+ val enter : ('a , 'a ) t
170+ (* * [enter] returns the enter selection: placeholder nodes for each data
171+ element for which no corresponding existing DOM element was found in the
172+ current selection. This operation can only be composed below an update
173+ selection, which is produced by the {!data} operator.
174+
175+ {{:https://github.com/mbostock/d3/wiki/Selections#enter}D3.js docs} *)
54176
55- val enter : ('a , 'a ) t
56177val update : ('a , 'a ) t
57- val exit : ('a , 'a ) t
178+ (* * [update] is a NOOP operation. It leaves the parent selection unchanged and
179+ has no side-effects.
180+
181+ This is not part of the D3.js API. *)
182+
183+ val exit : ('a , 'a ) t
184+ (* * [exit] returns the exit selection: existing DOM elements in the current
185+ selection for which no new data element was found. This operation can only
186+ be composed below an update selection, which is produced by the {!data}
187+ operator.
188+
189+ {{:https://github.com/mbostock/d3/wiki/Selections#exit}D3.js docs} *)
190+
191+ (* * {2 Selection manipulation} *)
58192
59193val filter : ('a , bool ) fn -> ('a , 'a ) t
194+ (* * [filter f] returns a new selection that contains only the elements for
195+ which [f] evaluates to [true].
196+
197+ {{:https://github.com/mbostock/d3/wiki/Selections#filter}D3.js docs} *)
198+
60199val sort : ('a -> 'a -> int ) -> ('a , 'a ) t
200+ (* * [sort f] sorts the elements in the current selection according to the
201+ comparator function, and then re-inserts the document elements to match.
202+
203+ {{:https://github.com/mbostock/d3/wiki/Selections#sort}D3.js docs} *)
204+
205+ (* * {2 Composition operators} *)
61206
62207val (|. ) : ('a, 'b) t -> ('b, 'c) t -> ('a, 'c) t
63208val (< .> ) : ('a, 'b) t -> ('b, 'c) t -> ('a, 'c) t
209+ (* * [x |. y] is the equivalent of method chaining operators [x] and [y] in
210+ JavaScript. [<.>] is an alias for [|.]. *)
211+
64212val (|- ) : ('a, 'b) t -> ('b, 'c) t -> ('a, 'b) t
65213
66214val chain : ('a , 'a ) t list -> ('a , 'a ) t
67215val seq : ('a , 'a ) t list -> ('a , 'a ) t
216+
68217val nest : ('a , 'b ) t -> ('b , 'b ) t list -> ('a , 'b ) t
218+ (* * [nest x ys] is equivalent to [x |- seq ys]. *)
219+
220+ (* * {2 Casting} *)
69221
70222val str : (string -> ('a , string ) fn -> ('a , 'a ) t ) -> string -> string -> ('a , 'a ) t
71223val int : (string -> ('a , string ) fn -> ('a , 'a ) t ) -> string -> int -> ('a , 'a ) t
72224val flt : (string -> ('a , string ) fn -> ('a , 'a ) t ) -> string -> float -> ('a , 'a ) t
225+ (* * [str attr x y] will set the [x] attribute to the constant string value [y].
226+ The [int] and [flt] functions do the same thing except with constant values
227+ of different types. These convenience functions should be used with the
228+ {!attr}, and {!style} functions when dealing with constant values. That way
229+ you can write code like this:
230+
231+ {[selectAll "rect"
232+ |. int attr "height" height
233+ |. int attr "width" height
234+ |. str style "fill" "blue"]}
235+
236+ rather than this:
73237
238+ {[selectAll "rect"
239+ |. attr "height" (fun _ _ _ -> string_of_int height)
240+ |. attr "width" (fun _ _ _ -> string_of_int width)
241+ |. style "fill" (fun _ _ _ -> "blue")]} *)
242+
243+
244+ (* * Event handlers *)
74245module E : sig
75246 type ('event, 'a) handler = 'event Js .t -> 'a -> int -> unit
247+ (* * The type of an event handler. The handler expects an event of type
248+ ['event] that it can interact with via the js_of_ocaml API, as well as
249+ data of type ['a] to be associated with the DOM node that produced the
250+ event. *)
251+
252+ (* * {2 Mouse events} *)
76253
77254 val click : (Dom_html .mouseEvent , 'a ) handler -> ('a , 'a ) t
78255 val dblclick : (Dom_html .mouseEvent , 'a ) handler -> ('a , 'a ) t
@@ -82,10 +259,14 @@ module E : sig
82259 val mousemove : (Dom_html .mouseEvent , 'a ) handler -> ('a , 'a ) t
83260 val mouseout : (Dom_html .mouseEvent , 'a ) handler -> ('a , 'a ) t
84261
262+ (* * {2 Keyboard events} *)
263+
85264 val keypress : (Dom_html .keyboardEvent , 'a ) handler -> ('a , 'a ) t
86265 val keydown : (Dom_html .keyboardEvent , 'a ) handler -> ('a , 'a ) t
87266 val keyup : (Dom_html .keyboardEvent , 'a ) handler -> ('a , 'a ) t
88267
268+ (* * {2 Drag-and-drop events} *)
269+
89270 val dragstart : (Dom_html .dragEvent , 'a ) handler -> ('a , 'a ) t
90271 val dragend : (Dom_html .dragEvent , 'a ) handler -> ('a , 'a ) t
91272 val dragenter : (Dom_html .dragEvent , 'a ) handler -> ('a , 'a ) t
@@ -94,6 +275,8 @@ module E : sig
94275 val drag : (Dom_html .dragEvent , 'a ) handler -> ('a , 'a ) t
95276 val drop : (Dom_html .dragEvent , 'a ) handler -> ('a , 'a ) t
96277
278+ (* * {2 Miscellaneous events} *)
279+
97280 val input : (Dom_html .event , 'a ) handler -> ('a , 'a ) t
98281 val timeupdate : (Dom_html .event , 'a ) handler -> ('a , 'a ) t
99282 val change : (Dom_html .event , 'a ) handler -> ('a , 'a ) t
@@ -105,7 +288,24 @@ module E : sig
105288 val select : (Dom_html .event , 'a ) handler -> ('a , 'a ) t
106289 val mousewheel : (Dom_html .event , 'a ) handler -> ('a , 'a ) t
107290
291+ (* * {2 Generic events} *)
292+
108293 val handle : string -> (Dom_html .event , 'a ) handler -> ('a , 'a ) t
109294end
110295
111296val run : string -> 'a -> ('a , _ ) t -> unit
297+ (* * [run selector datum op] selects the first element that matches [selector],
298+ binds [datum] to it, and runs [op] on that element. This is the only way to
299+ run a D3.t operation. It can be used in a variety of contexts, however its
300+ indended use is in an event loop of an application, along these lines:
301+
302+ {[
303+ let main () =
304+ let model = ref Model.init in
305+ D3.run "body" !model view;
306+ Stream.iter events (fun e ->
307+ model := handle e model;
308+ D3.run "body" !model view)
309+ ;;
310+
311+ main ()]}*)
0 commit comments