Skip to content

Commit c233659

Browse files
vouillonvasilisp
authored andcommitted
Ot_tip: change positioning (#164)
It is centered if this fits on screen. Otherwise, its right or left side (whichever fits best) is at the center of the origin.
1 parent ff92194 commit c233659

File tree

2 files changed

+47
-46
lines changed

2 files changed

+47
-46
lines changed

src/widgets/ot_tip.eliom

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ open%client Eliom_content.Html.F
2525
let%client display
2626
?(container_a = [ a_class ["ot-tip-container"] ])
2727
?(filter_a = [ a_class ["ot-tip-filter"] ])
28-
?(side : [ `Center | `Left | `Right ] option)
2928
~(origin : Dom_html.element Js.t)
30-
?(onopen = fun _ _ _ () -> ())
31-
?(onclose = fun _ _ _ () -> ())
29+
?(onopen = fun _ _ -> ())
30+
?(onclose = fun _ _ -> ())
3231
~(content : (unit -> unit) ->
3332
[< Html_types.div_content_fun > `Div] Eliom_content.Html.elt list) () =
3433
let close = ref @@ fun () -> () in
@@ -50,40 +49,47 @@ let%client display
5049
- int_of_float bb##.right in
5150
let print_px x = Js.string (Printf.sprintf "%gpx" x) in
5251
m##.style##.minWidth := print_px w ;
53-
if top < bottom
54-
then ( let top = print_px bb##.bottom in
55-
m##.style##.top := top
56-
; m##.classList##add (Js.string "ot-tip-top") )
57-
else (let bottom =
58-
print_px
59-
(float_of_int Dom_html.document##.documentElement##.clientHeight
60-
-. bb##.top) in
61-
m##.style##.bottom := bottom
62-
; m##.classList##add (Js.string "ot-tip-bottom") ) ;
63-
let side =
64-
if side = Some `Center || (side = None && right = left)
65-
then (Lwt.async (fun () ->
66-
let%lwt () = Ot_nodeready.nodeready m in
67-
let _ = Dom_html.window##getComputedStyle m in (* Force layout *)
68-
m##.style##.left :=
69-
print_px (((bb##.right +. bb##.left) /. 2.)
70-
-. (float_of_int m##.offsetWidth) /. 2.)
71-
; Lwt.return_unit)
72-
; m##.classList##add (Js.string "ot-tip-center")
73-
; `Center )
74-
else if side = Some `Left || (side = None && right < left)
75-
then (let right =
76-
print_px
77-
(float_of_int Dom_html.document##.documentElement##.clientWidth
78-
-. bb##.right) in
79-
m##.style##.right := right
80-
; m##.classList##add (Js.string "ot-tip-left")
81-
; `Left)
82-
else (* if side = Some `Right || (side = None && right > left) *)
83-
( m##.style##.left := print_px bb##.left
84-
; m##.classList##add (Js.string "ot-tip-right")
85-
; `Right )
86-
in
52+
if top < bottom then begin
53+
let top = print_px bb##.bottom in
54+
m##.style##.top := top;
55+
m##.classList##add (Js.string "ot-tip-top")
56+
end else begin
57+
let bottom =
58+
print_px
59+
(float_of_int Dom_html.document##.documentElement##.clientHeight
60+
-. bb##.top)
61+
in
62+
m##.style##.bottom := bottom;
63+
m##.classList##add (Js.string "ot-tip-bottom")
64+
end;
65+
if right < left then begin
66+
let right =
67+
float_of_int Dom_html.document##.documentElement##.clientWidth
68+
-. (bb##.right +. bb##.left) /. 2.
69+
in
70+
m##.style##.right := print_px right;
71+
Lwt.async @@ fun () ->
72+
let%lwt () = Ot_nodeready.nodeready m in
73+
let off = float (m##.offsetWidth / 2) in
74+
if off <= right -. 1. then begin
75+
m##.style##.right := print_px (right -. off);
76+
Manip.Class.add container "ot-tip-center"
77+
end else
78+
Manip.Class.add container "ot-tip-left";
79+
Lwt.return_unit
80+
end else begin
81+
let left = (bb##.right +. bb##.left) /. 2. in
82+
m##.style##.left := print_px left;
83+
Lwt.async @@ fun () ->
84+
let%lwt () = Ot_nodeready.nodeready m in
85+
let off = float (m##.offsetWidth / 2) in
86+
if off <= left -. 1. then begin
87+
m##.style##.left := print_px (left -. off);
88+
Manip.Class.add container "ot-tip-center"
89+
end else
90+
Manip.Class.add container "ot-tip-right";
91+
Lwt.return_unit
92+
end;
8793
let filter =
8894
D.div ~a:(a_onclick (fun _ -> !close ()) :: filter_a) [ container ]
8995
in
@@ -96,8 +102,8 @@ let%client display
96102
close := (fun () ->
97103
Dom.removeEventListener scroll_handler;
98104
Manip.removeSelf filter;
99-
onclose filter container side () ;
105+
onclose filter container
100106
);
101107
Manip.appendToBody filter ;
102-
onopen filter container side () ;
108+
onopen filter container;
103109
(filter, !close)

src/widgets/ot_tip.eliomi

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
[menu_a]: menu attributes default is [ [ a_class ["ot-drp-menu"] ] ]
3838
and will be overriden if you provide this argument.
3939
40-
[side]: is the side you want the tip to be display. It references the [origin]
41-
element. If no value is provided, side is choosen automatically according to
42-
the space available on each side of the [origin] element.
43-
4440
[origin] is the element from which the tip is supposed to pop out.
4541
4642
[onopen filter menu side] is called after the filter is append to body.
@@ -56,14 +52,13 @@ val display
5652
: ?container_a:[< Html_types.div_attrib > `Class] Eliom_content.Html.attrib list
5753
-> ?filter_a:[< Html_types.div_attrib > `Class `OnClick]
5854
Eliom_content.Html.attrib list
59-
-> ?side:[ `Left | `Right | `Center ]
6055
-> origin : Dom_html.element Js.t
6156
-> ?onopen:([> Html_types.div ] Eliom_content.Html.elt
6257
-> [> Html_types.div ] Eliom_content.Html.elt
63-
-> [> `Left | `Right | `Center ] -> unit -> unit)
58+
-> unit)
6459
-> ?onclose:([> Html_types.div ] Eliom_content.Html.elt
6560
-> [> Html_types.div ] Eliom_content.Html.elt
66-
-> [> `Left | `Right | `Center ] -> unit -> unit)
61+
-> unit)
6762
-> content: ((unit -> unit)
6863
-> [< Html_types.div_content_fun > `Div]
6964
Eliom_content.Html.elt list)

0 commit comments

Comments
 (0)