/
sidepanel.opa
908 lines (820 loc) · 29.4 KB
/
sidepanel.opa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A togglable side-panel
*
* @author Guillem Rieu, 2010
* @category WIDGET
* @destination PUBLIC
* @stability EXPERIMENTAL
*/
/**
* {1 About this module}
*
* This module provides togglable side-panels. Basically, it displays any kind
* of XHTML in panels attached to a side of the parent element and can be
* opened or closed on demand (whether by the user or the application).
*
* {1 Where should I start?}
*
* A side-panel is a XHTML chunk made of one main part, the actual content, and
* an optional handle aimed at letting the user open or close the panel using
* the mouse.
*
* Therefore, in order to create a panel, you need at least the XHTML part
* which will be its content. Additionnaly, you need to decide which events
* will open or close it. The easier (and default behaviour) way to proceed is
* to provide the user with a “handle”: a bar with the same length as the panel
* which enables the user to switch between opened and closed states.
*
* The most basic default config [WSidepanel.default_config_noresize] can be
* used to obtain a left side-panel with no resizing of the main content. If
* such a resizing is needed, you can either write your own dedicated function,
* or use the one provided (it's called [WSidepanel.change_content_size]). In
* the latter case, you may as well use directly default configs named
* [WSidepanel.default_*_config], replacing the '*' with either 'left', 'right',
* 'top' or 'bottom' depending on which side you want the panel to be. It takes
* the HTML ID of your main content as sole argument.
*
* The function [WSidepanel.html] can then be used to create a side-panel with
* the config.
*
* {1 What if I need more?}
*
*/
// FIXME: automated resizing and margin setting when opening top panel in
// [change_content_size] doesn't work properly
import stdlib.widgets.core
/**
* {1 Types defined in this module}
*/
/**
* Place of a panel relatively to its parent element
*/
type WSidepanel.place =
{ left } / { right } / { top } / { bottom }
/**
* Different possible panel positioning relatively to the rest of the page
*/
type WSidepanel.position =
/** Scrolling the side-panel along with the page */
{ absolute }
/** Scrolling the page doesn't affect the side-panel */
/ { fixed }
/**
* Type of a main content element (e.g. a DIV side-by-side with a panel.
* Information in such a record is used to resize the said content when a
* change of a side-panel size occurs.
*/
type WSidepanel.content = {
/** Main ID */
id : string
/** Functions yielding sizes given the content ID */
size_opt : string -> option(Css.size)
margin_size_opt : string -> option(Css.unary)
parent_size_opt : string -> option(Css.size)
}
/**
* The different display states a panel can be in.
*/
type WSidepanel.display =
/** The panel is closed */
{ closed }
/** The panel is opened with the current size */
/ { opened }
/** The panel is opened with a given (and possibly new) size */
/ { opened_with_size: Css.size_or_normal }
/** The panel covers the whole page */
/ { fullpage }
/** Let the user handle the display of the panel with the given ID */
/ { custom: string -> option(Css.size_or_normal) }
/**
* Possible panel handle types
*/
type WSidepanel.handle =
/** No handle */
{ none }
/** Static XHTML handle */
/ { content: xhtml size: Css.size }
/**
* The type of a panel configuration.
*/
type WSidepanel.config = {
/**
* Take an ID and the state of a panel ('true' if opened, 'false' if closed)
* and return the corresponding handle
*/
get_handle: WSidepanel.config, string, WSidepanel.display ->
WSidepanel.handle
/** Placement of the panel */
place: WSidepanel.place
/** Positioning of the panel */
position: WSidepanel.position
/** Size of the panel, given its ID */
get_size: string -> Css.size_or_normal
/** z-index of the panel, given its ID */
get_zindex: string -> int
/** Closing / opening animation duration in ms */
opening_delay : int
/** Callback event when the panel is changed to a new state */
on_change_state :
WSidepanel.place, Css.size_or_normal, Css.size_or_normal -> void
}
/**
* {1 Private types}
*
* The following types are used internally by the module. They shouldn't be
* used anywhere else.
*/
/**
* A more vague type than [WSidepanel.place] giving the orientation of a panel
* (left and right panels are oriented horizontally, whether top and bottom
* ones are oriented vertically.
*
* @private
*/
type WSidepanel.private.orientation =
{ horizontal } / { vertical }
/**
* A type describing the different styles needed for a side-panel positioning
*
* @private
*/
type WSidepanel.private.place_css = {
container : WStyler.styler
content : WStyler.styler
handle : WStyler.styler
}
WSidepanel =
get_opened_class(id : string) : string = "{id}_opened"
get_handle_id(id : string) : string = "{id}_handle"
get_handle_char_id(id : string) : string = "{id}_handle_char"
get_handle_background_id(id : string) : string = "{id}_handle_background"
get_content_id(id: string) : string = "{id}_content"
{{
/**
* {1 Configuration}
*/
default_config_noresize: WSidepanel.config = {
get_handle = open_close_handles(<>→</>, <>←</>)
place = {left}
position = {absolute}
get_size = _ -> {size={percent=21.}}
get_zindex = _ -> 20
opening_delay = 200
on_change_state = (_, _, _ -> void)
}
/**
* A default configuration of a side panel.
* It's on the left side and takes 25% of the page. It can be opened or
* closed by the user using handles.
*/
default_config(content_element_opt: option(WSidepanel.content)): WSidepanel.config =
Option.switch(content_element -> {default_config_noresize with
on_change_state =
change_content_size(content_element, _, _, _)
}, default_config_noresize, content_element_opt)
default_left_config = default_config
default_right_config(content_element: option(WSidepanel.content))
: WSidepanel.config =
{default_config(content_element) with
get_handle = open_close_handles((<>←</>), (<>→</>))
place = {right}
}
default_top_config(content_element: option(WSidepanel.content))
: WSidepanel.config =
{default_config(content_element) with
get_handle = open_close_handles((<>↓</>), (<>↑</>))
place = {top}
}
default_bottom_config(content_element: option(WSidepanel.content))
: WSidepanel.config =
{default_config(content_element) with
get_handle = open_close_handles((<>↑</>), (<>↓</>))
place = {bottom}
}
/**
* {1 High-level interface}
*/
/**
* The main function used to create a side-panel widget.
*
* @param config The configuration of the side-panel
* @param id The HTML identifier surrounding the side-panel
* @param init_content The HTML content to display in the side-panel
* @param start_opened Whether or not to display the side-panel by default
* @return The HTML representing the side-panel
*/
html(config: WSidepanel.config, id: string, init_content: xhtml,
start_opened: bool)
: xhtml =
pos_css = css_of_place(config.place)
/* Build the XHTML structure of the panel */
<div id={id}
onready={_ ->
if start_opened then
do_open(config, id, false)
else
_ = set_handle_size(config, id, {closed})
void
}
style="z-index: {some(config.get_zindex(id))};
position: {css_of_position(config.position)};">
{<div id={get_content_id(id)}
style="display: none; position: relative;">
{init_content}
</div>
|> WStyler.add(pos_css.content, _)}
{<div id={get_handle_id(id)}></div>
|> WStyler.add(pos_css.handle, _)}
</div>
|> WStyler.add(pos_css.container, _)
/**
* {2 Imperative interface}
*/
/**
* Open the side-panel
* @param config Configuration of the side-panel
* @param id HTML id of the side-panel
* @param animate Whether or not to animate the side-panel when opening
*/
@client do_open(config: WSidepanel.config, id: string, animate: bool)
: void =
set_state(config, id, {opened}, animate)
/**
* Close the side-panel
* @param config Configuration of the side-panel
* @param id HTML id of the side-panel
* @param animate Whether or not to animate the side-panel when closing
*/
@client do_close(config: WSidepanel.config, id: string, animate: bool)
: void =
set_state(config, id, {closed}, animate)
/**
* Switch the panel state (open it if it’s closed, close it if it’s opened)
* @param config Configuration of the side-panel
* @param id HTML id of the side-panel
* @param animate Whether or not to animate the side-panel change
*/
@client toggle(config: WSidepanel.config, id: string, animate: bool): void =
if Dom.has_class(#{id}, get_opened_class(id)) then
do_close(config, id, animate)
else
do_open(config, id, animate)
/**
* {1 Lower-level interface}
*/
/**
* A helper function building an empty [get_handle]. It can be used to
* disable handles of the side-panel.
*/
no_handles(_: WSidepanel.config, _: string, _: WSidepanel.display)
: WSidepanel.handle =
{none}
/**
* Auxiliary function to change the handle colors
*/
change_handle_colors(id: string, bg_color: color, fg_color: color)
: Dom.event -> void = _ ->
Dom.transform([#{get_handle_background_id(id)} -> css <-
css {background: {Css_build.background_color(bg_color)};},
#{get_handle_char_id(id)} -> css <- [Css_build.color(fg_color)]])
/**
* A helper function building a default [get_handle] given two XHTML chunks
* (corresponding to opening and closing values
*/
open_close_handles(close_elt: xhtml, open_elt: xhtml)
: (WSidepanel.config, string, WSidepanel.display -> WSidepanel.handle) =
/* Auxiliary function to build the handle given a character to display */
handle_aux(id: string, char_elt: xhtml, place: WSidepanel.place,
event_callback: (Dom.event -> void)): xhtml =
css_border_attr = match place with
| {left} -> "border-right"
| {right} -> "border-left"
| {top} -> "border-bottom"
| {bottom} -> "border-top"
<a title = "Click to open the side-panel"
onmouseover =
{change_handle_colors(id, Color.darkgray, Color.lightgrey)}
onmouseout =
{change_handle_colors(id, Color.lightgrey, Color.darkgray)}
onclick = {event_callback}>
<div id={get_handle_background_id(id)}
style="position: absolute; width: 100%; height: 100%;
{css_border_attr}: 1px solid #696969;
top: 0; right: 0; background: #D2D2D2;">
<span id={get_handle_char_id(id)}
style="font-size: 20px; font-weight: bold; color: #878787;
position: absolute; text-align: center; top: 50%;
margin-top: -12px; width: 100%;">
{char_elt}
</span>
</div>
</a>
/* Handle building functions */
(config, id, display ->
if is_opened(id, display) then
{content=handle_aux(id, open_elt, config.place,
(_ -> do_close(config, id, true)))
size={px=20}}
else
{content=handle_aux(id, close_elt, config.place,
(_ -> do_open(config, id, true)))
size={px=20}})
/**
* {2 Lower-level imperative interface}
*/
/**
* Modify the current side-panel state (opened or closed)
*/
@client set_state(config: WSidepanel.config, id: string,
display: WSidepanel.display, animate: bool)
: void =
orientation = orientation_of_place(config.place)
delay = if animate then config.opening_delay else 0
is_opened = Dom.has_class(#{id}, get_opened_class(id))
config_size = config.get_size(id)
fullsize = {size = {percent = 100.}}
/* Auxiliary function opening the panel to the given size */
open_aux(open_size: Css.size_or_normal): Css.size_or_normal =
size_delta = set_size(orientation, id, open_size)
_ = Dom.transition(#{get_content_id(id)}, Dom.Effect.with_duration({millisec = delay}, Dom.Effect.show()))
do Dom.add_class(#{id}, get_opened_class(id))
size_delta
/* Auxiliary function closing the panel to the given size */
close_aux(): Css.size_or_normal =
_ = Dom.transition(#{get_content_id(id)}, Dom.Effect.with_duration({millisec = delay}, Dom.Effect.hide()))
size_delta = set_size(orientation, id, {size={em=0.}})
do Dom.remove_class(#{id}, get_opened_class(id))
size_delta
/* Determine action to take given the wanted and current state */
size_delta = match (display : WSidepanel.display, is_opened) with
| ({closed}, {true}) ->
close_aux()
| ({opened}, {true}) -> /* Only reset the size to config one */
set_size(orientation, id, config_size)
| ({opened}, {false}) ->
open_aux(config_size)
| ({opened_with_size=size}, {true}) -> /* Only change the size */
set_size(orientation, id, size)
| ({opened_with_size=size}, {false}) ->
open_aux(size)
| ({fullpage}, {true}) ->
set_size(orientation, id, fullsize)
| ({fullpage}, {false}) ->
open_aux(fullsize)
| ({~custom}, {true}) ->
Option.lazy_switch(set_size(orientation, id, _), close_aux, custom(id))
| ({~custom}, {false}) ->
Option.switch(open_aux, {normal}, custom(id))
| _ -> @fail
/* Update the handle */
handle_size_delta = set_handle_size(config, id, display)
/* Trigger callback function */
config.on_change_state(config.place, size_delta, handle_size_delta)
/**
* Update the handle content to match the new state of the panel
*/
@client set_handle_size(config: WSidepanel.config, id: string,
display: WSidepanel.display): Css.size_or_normal =
orientation = orientation_of_place(config.place)
/* Save the size of the panel before any resizement */
init_size = get_oriented_size(orientation, #{id})
/* Resize the handle */
handle_id = get_handle_id(id)
(min_size_attr, size_attr, css_build_margin) =
css_of_handle(config.place)
(handle_xhtml, handle_size) = config.get_handle(config, id, display)
|> values_of_handle(_)
// TODO: simplify and factorize funactions
do Dom.transform([#{handle_id} <- handle_xhtml,
#{handle_id} -> css <- [{not_typed=(size_attr,
string_of_size(handle_size))}],
#{get_content_id(id)} -> css <- [css_build_margin(handle_size)],
#{id} -> css <- [{not_typed=(min_size_attr,
string_of_size(handle_size))}]])
/* Retrieve the new size of the panel */
new_size = get_oriented_size(orientation, #{id})
{size={px=(new_size - init_size)}}
/**
* Change the size of an element, and return the actual change in size
*/
@client set_size(orientation: WSidepanel.private.orientation, id: string,
size: Css.size_or_normal): Css.size_or_normal =
match size with {normal} -> {normal} | {~size} ->
init_size = get_oriented_size(orientation, #{id})
elt_css = switch_orientation(
(-> Css_build.width(size)),
(-> Css_build.height(size)), orientation)
do Dom.transform([#{id} -> css <- [elt_css]])
new_size = get_oriented_size(orientation, #{id})
size_delta_px = new_size - init_size
match size with
| {percent=_} ->
size_delta_percent =
percent_of_px(orientation, Dom.select_parent_one(#{id}),
size_delta_px)
{size={percent=size_delta_percent}}
| _ -> {size={px=size_delta_px}}
/**
* Update the size of the main content according to side-panel info. Only
* pixel and percentage sizes are handled for now.
*/
@client change_content_size(content_element: WSidepanel.content,
place: WSidepanel.place, panel_size_delta: Css.size_or_normal,
handle_size_delta: Css.size_or_normal)
: void =
orientation = orientation_of_place(place)
/* Gather content and content's parent sizes in pixels */
parent = Dom.select_parent_one(#{content_element.id})
init_content_px = Option.lazy_switch(
px_of_size(orientation, parent, _),
(-> get_oriented_size(orientation,
#{content_element.id})),
content_element.size_opt(content_element.id))
size_delta =
add_size(some((orientation, parent)),
option_of_size(panel_size_delta),
option_of_size(handle_size_delta))
|> size_of_option(_)
use_percent = match size_delta with {size={percent=_}} -> true | _ -> false
delta_px = match size_delta with
| {normal} -> 0
| {~size} -> px_of_size(orientation, parent, size)
/* Resize the content */
new_content_px = init_content_px - delta_px
new_content_size = if use_percent then
{percent=percent_of_px(orientation, parent,
new_content_px)}
else
{px=new_content_px}
new_content_css = [switch_orientation(
(-> Css_build.width(new_content_size)),
(-> Css_build.height(new_content_size)), orientation)]
/* Hide the panel if the new size is null */
new_content_css =
if new_content_px <= 1 then Css_build.display_none +> new_content_css
else Css_build.display_block +> new_content_css
/* Auxiliary function retrieving current margin of the panel */
init_margin() = Option.lazy_default(->
null_size = if use_percent then {percent=0.} else {px=0}
null_margin = Css_build.margin_all(null_size)
init_margin_str_opt =
Dom.get_property_unsafe(#{content_element.id}, "style")
|> try_parse_style
|> Option.default([], _)
|> List.assoc("margin", _)
Option.switch((init_margin_str ->
try_parse_margin(use_percent, init_margin_str) ?
null_margin),
null_margin, init_margin_str_opt),
content_element.margin_size_opt(content_element.id))
/* Shift the content if necessary */
new_content_css = match (place, size_delta) with
| ({left}, {~size}) ->
add_margin(some((orientation, parent)),
Css_build.margin_left(size),
init_margin()) +> new_content_css
| ({top}, {~size}) ->
add_margin(some((orientation, parent)),
Css_build.margin_top(size),
init_margin()) +> new_content_css
| _ -> new_content_css
/* Actually apply CSS changes */
Dom.transform([#{content_element.id} -> css <- new_content_css])
/**
* {1 Private functions aimed at internal use}
*
* Do not use them outside of the module.
*
* {2 Panel styles}
*/
@private
left_place_style: WSidepanel.private.place_css = {
container = WStyler.make_style(css {
top: 0px;
left: 0px;
height: 100%;
})
content = WStyler.make_style(css {
top: 0px;
left: 0px;
width: 100%;
height: 100%;
padding: 0;
})
handle = WStyler.make_style(css {
position: absolute;
right: 0;
top: 0;
width: 20px;
height: 100%;
})
}
@private
right_place_style: WSidepanel.private.place_css = {
container = WStyler.make_style(css {
top: 0px;
right: 0px;
height: 100%;
})
content = WStyler.make_style(css {
top: 0px;
left: 0px;
width: 100%;
height: 100%;
padding: 0;
})
handle = WStyler.make_style(css {
position: absolute;
left: 0;
top: 0;
width: 20px;
height: 100%;
})
}
@private
top_place_style: WSidepanel.private.place_css = {
container = WStyler.make_style(css {
top: 0px;
left: 0px;
width: 100%;
})
content = WStyler.make_style(css {
top: 0px;
left: 0px;
width: 100%;
height: 100%;
padding: 0;
})
handle = WStyler.make_style(css {
position: absolute;
bottom: 0px;
width: 100%;
height: 20px;
})
}
@private
bottom_place_style: WSidepanel.private.place_css = {
container = WStyler.make_style(css {
bottom: 0px;
left: 0px;
width: 100%;
})
content = WStyler.make_style(css {
top: 0px;
left: 0px;
width: 100%;
height: 100%;
padding: 0;
})
handle = WStyler.make_style(css {
position: absolute;
top: 0px;
width: 100%;
height: 20px;
})
}
/**
* {2 Panel state related functions}
*/
@private
@client is_opened(id: string, display: WSidepanel.display): bool =
match display with
| {closed} -> false
| {opened} | {opened_with_size=_} | {fullpage} -> true
| {~custom} -> Option.is_some(custom(id))
/**
* {2 Conversion functions}
*/
@private
@client string_of_size(size: Css.size): string = match size with
| { ~percent } -> "{percent}%"
| { ~px } -> "{px}px"
| _ -> error("string_of_size pattern matching: This kind of CSS size is not implemented.")
@private
css_of_place(pos: WSidepanel.place): WSidepanel.private.place_css =
match pos with
| {left} -> left_place_style
| {right} -> right_place_style
| {top} -> top_place_style
| {bottom} -> bottom_place_style
/**
* Get CSS elements corresponding to the panel place
*/
@private
@client css_of_handle(place: WSidepanel.place)
: (string, string, (Css.size -> Css.unary)) =
match place with
| {left} -> ("min-width", "width", Css_build.margin_right)
| {right} -> ("min-width", "width", Css_build.margin_left)
| {top} -> ("min-height", "heigh", Css_build.margin_bottom)
| {bottom} -> ("min-height", "heigh", Css_build.margin_top)
/**
* Get a handle value from an option, or a default empty one
*/
@private
@client values_of_handle(handle: WSidepanel.handle): (xhtml, Css.size) =
match handle with
| ~{content size} -> (content, size)
| {none} -> (<></>, {px=0})
@private
@client switch_orientation(horizontal_action: -> 'a,
vertical_action: -> 'a, orientation: WSidepanel.private.orientation)
: 'a =
match orientation with
| {horizontal} -> horizontal_action()
| {vertical} -> vertical_action()
@private
@client orientation_of_place(place: WSidepanel.place)
: WSidepanel.private.orientation =
match place with
| {left} | {right} -> {horizontal}
| {top} | {bottom} -> {vertical}
/**
* Retrieve the width or height of an element given its orientation
*/
@private
@client get_oriented_size(orientation: WSidepanel.private.orientation,
element: dom)
: int =
match orientation with
| {horizontal} -> Dom.get_width(element)
| {vertical} -> Dom.get_height(element)
/**
* Convert a pixel value to a percent relative to an element
*/
@private
@client percent_of_px(orientation: WSidepanel.private.orientation, element: dom,
px: int): float =
element_px = get_oriented_size(orientation, element)
Float.of_int(px) / Float.of_int(element_px) * 100.
/**
* Convert a percent (relative to an element) to a pixel value
*/
@private
@client px_of_percent(orientation: WSidepanel.private.orientation, element: dom,
percent: float): int =
element_px = get_oriented_size(orientation, element)
Int.of_float(percent * Float.of_int(element_px) / 100.)
/**
* Try to convert the given [size] to pixels
*/
@private
@client px_of_size(orientation: WSidepanel.private.orientation, element: dom,
size: Css.size): int =
match size with
| {~px} -> px
| {~percent} -> px_of_percent(orientation, element, percent)
| _ -> error("px_of_percent: Css.size unit not supported")
@private
@client option_of_size(css_size: Css.size_or_normal): option(Css.size) =
match css_size with
| {normal} -> none
| {~size} -> some(size)
@private
@client size_of_option(opt_size: option(Css.size)): Css.size_or_normal =
match opt_size with
| {none} -> {normal}
| {~some} -> {size=some}
/**
* Boring conversion of [Css.position] to a more restrictive type
*/
@private
css_of_position(sidepanel_pos: WSidepanel.position): Css.position =
match sidepanel_pos with
| {absolute} -> {absolute}
| {fixed} -> {fixed}
/**
* {2 CSS operations}
*/
/**
* Get the null size corresponding to the given size unit.
*/
@private @client
null_size: Css.size -> Css.size = map_css_size((_: float -> 0.))
/**
* Make an operation on two [Css.size]
*/
@private
@client op_size(op_int: int, int -> int, op_float: float, float -> float,
ref_element_opt: option((WSidepanel.private.orientation, dom)),
size1_opt: option(Css.size), size2_opt: option(Css.size))
: option(Css.size) =
error() = error("op_size: incompatible CSS size operation")
match (size1_opt, size2_opt) with
| ({none}, {none}) -> none
| ({none}, {some=css_size}) -> op_size(op_int, op_float, ref_element_opt,
some(null_size(css_size)), size2_opt)
| ({some=css_size}, {none}) -> op_size(op_int, op_float, ref_element_opt,
size1_opt, some(null_size(css_size)))
| ({some=size1}, {some=size2}) -> some(
match (size1, size2) with
| ({cm=s1} , {cm=s2}) -> {cm=op_float(s1, s2)}
| ({em=s1} , {em=s2}) -> {em=op_float(s1, s2)}
| ({ex=s1} , {ex=s2}) -> {ex=op_float(s1, s2)}
| ({inch=s1} , {inch=s2}) -> {inch=op_float(s1, s2)}
| ({mm=s1} , {mm=s2}) -> {mm=op_float(s1, s2)}
| ({percent=s1} , {percent=s2}) -> {percent=op_float(s1, s2)}
| ({pc=s1} , {pc=s2}) -> {pc=op_float(s1, s2)}
| ({pt=s1} , {pt=s2}) -> {pt=op_float(s1, s2)}
| ({px=s1} , {px=s2}) -> {px=op_int(s1, s2)}
| ({percent=s1} , {px=s2}) ->
Option.lazy_switch(((orientation, element) ->
{percent=op_float(s1, percent_of_px(orientation, element, s2))}),
error, ref_element_opt)
| ({px=s1}, {percent=s2}) ->
Option.lazy_switch(((orientation, element) ->
{px=op_int(s1, px_of_percent(orientation, element, s2))}),
error, ref_element_opt)
| _ -> error())
@private add_size = op_size(`+`, Float.`+`, _, _, _)
@private sub_size = op_size(`-`, Float.`-`, _, _, _)
@private
@client add_block_size(ref_elt, b1: Css.block_size, b2: Css.block_size)
: Css.block_size =
add_size_aux = add_size(ref_elt, _, _)
{
t = add_size_aux(b1.t, b2.t)
r = add_size_aux(b1.r, b2.r)
l = add_size_aux(b1.l, b2.l)
b = add_size_aux(b1.b, b2.b)
}
@private
@client add_margin(ref_elt, margin1: Css.unary, margin2: Css.unary)
: Css.unary =
match (margin1, margin2) with
| ({margin=m1}, {margin=m2}) -> {margin=add_block_size(ref_elt, m1, m2)}
| _ -> error("add_margin: arguments are not margins")
/**
* {2 CSS parsing}
*/
/**
* {3 Parsing rules}
*/
@private
@client size_rule = parser value=Rule.float
unit=("%"|"px"|"cm"|"em"|"ex"|"inch"|"mm"|"pc"|"pt") ->
match Text.to_string(unit) with
| "%" -> {percent=value}
| "px" -> {px=Int.of_float(value)}
| "cm" -> {cm=value}
| "em" -> {em=value}
| "ex" -> {ex=value}
| "inch" -> {inch=value}
| "mm" -> {mm=value}
| "pc" -> {pc=value}
| "pt" -> {pt=value}
| _ -> @fail
@private
@client attr_rule =
parser attr=([a-zA-Z\-]+)":" Rule.ws value=(([a-zA-Z0-9%# \-]|".")+) ->
(Text.to_string(attr), Text.to_string(value))
@private
@client style_rule =
parser css_list={Rule.parse_list(attr_rule, parser ";" Rule.ws)} ";"? ->
css_list
/**
* {3 Parsing functions}
*/
/**
* Parse the content of a 'style' attribute and return a corresponding list
* of pairs (css_attribute, value)
*/
@private
@client try_parse_style(style_str: string): option(list((string, string))) =
Parser.try_parse(style_rule, style_str)
@private
@client try_parse_size(size_str: string): option(Css.size) =
Parser.try_parse(size_rule, size_str)
@private
@client try_parse_margin(use_percent: bool, margin_str: string)
: option(Css.unary) =
margin_rule = Rule.parse_list(size_rule, Rule.strict_ws)
margin_list_opt = Parser.try_parse(margin_rule, margin_str)
null_size = if use_percent then {percent= 0.} else {px = 0}
Option.map(margin_list ->
List.foldi(idx, size, margin_acc ->
size_opt = some(size)
block = (match margin_acc with {~margin} -> margin
| _ -> error("try_parse_margin: Css.unary should be margin"))
(match idx with
| 0 -> Css_build.margin_all(size)
| 1 -> {margin={block with l=size_opt r=size_opt}}
| 2 -> {margin={block with b=size_opt}}
| 3 -> {margin={block with l=size_opt}}
| _ -> @fail),
margin_list, Css_build.margin_all(null_size)),
margin_list_opt)
}}