/
parallax.opa
328 lines (297 loc) · 10.3 KB
/
parallax.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
/*
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 configurable parallax widget.
*
* @author Jessica Castejon, 2011
* @category WIDGET
* @destination PUBLIC
* @stability EXPERIMENTAL
* @version 0.1
*/
package stdlib.widgets.parallax
import stdlib.widgets.core
/*
* {1 About this module}
*
* Parallax widget
* Inspired by https://github.com/500 and
* http://webdev.stephband.info/parallax_demos.html
*
*/
type WParallax.dim = {px_x: int; px_y: int}
type WParallax.percent = {x: float; y: float}
type WParallax.offset = {px: WParallax.dim}/{percent: WParallax.percent}
type WParallax.src_type = {img: string} / {text: string}
/**
*An element of the parallax
*/
type WParallax.src = {
content: WParallax.src_type
id: string
depth: int
offset: WParallax.offset
content_style : WStyler.styler
}
type WParallax.display = {vertical} / {horizontal} / {both}
/**
*The configuration of a widget
*/
type WParallax.config = {
display: WParallax.display
width: int
height: int
global_style: WStyler.styler
}
WParallax = {{
/**
*Default configuration of a widget
*/
default_config : WParallax.config = {
display = {both}
width = 500
height = 200
global_style = WStyler.empty
}
/**
*Main display function of the parallax
*
*@param config The widget configuration
*@param sources The elements configuration list
*@param id The widget ID
*@return The HTML corresponding to the widget
*/
html(config: WParallax.config, sources: list(WParallax.src), id: string): xhtml =
list_sources = sort_sources(sources)
parallax = <div id="{id}_parallax" class=opa_parallax style="positon:absolute overflow:hidden" onready={_ -> configure(config,sources,id)}>
{get_content(config,list_sources)}
</div>
WStyler.add(config.global_style,parallax)
/*
*Function that calculates a dimension(width of height) of a container inside
*the parallax, depending on its depth
*
*@param depth Depth of the element
*@param dimension Dimension (width or height) of the parallax
*@return The calculated dimension
*/
@private
get_dimension(depth: int, dimension: int): int=
d = if depth>0
then float_of_int(depth)
else float_of_int((-1)*depth)
dim = float_of_int(dimension)
percent = dim / 20.
adjustment = d * percent
int_of_float(dim + adjustment)
/*
*Sorts the elements depending on their depth
*
*@param sources The elements configuration list
*@return The sorted list
*/
@private
sort_sources(sources: list(WParallax.src)): list(WParallax.src) =
rec division(list) = (match list with
|[] -> ([],[])
|[x] -> ([x],[])
|[x|[y|t]] -> (l1,l2)=division(t)
([x|l1],[y|l2])
)
rec fusion(l1,l2) = (match (l1,l2) with
|(x,[]) -> x
|([],x) -> x
|([x|t1],[y|t2]) -> if (x.depth)<(y.depth)
then [y|[x|fusion(t1,t2)]]
else [x|[y|fusion(t1,t2)]]
)
match sources with
|[] -> []
|[x] -> [x]
|l -> (l1,l2)=division(l)
fusion(sort_sources(l1),sort_sources(l2))
/*
*Function that gives the dimensions of a container inside the parallax,
*depending on its depth and the config (horizontal, vertical or both)
*
*@param config The widget configuration
*@param source The element configuration
*@return The calculated dimensions
*/
@private
get_container_dimensions(config: WParallax.config, source: WParallax.src): WParallax.dim =
(w,h) = match config.display with
|{horizontal} -> (get_dimension(source.depth,config.width), config.height)
|{vertical} -> (config.width, get_dimension(source.depth,config.height))
| _ -> (get_dimension(source.depth,config.width),get_dimension(source.depth,config.height))
{px_x=w;px_y=h}
/*
*Function that calculates the margins of an element of the parallax
*
*@param config The widget configuration
*@param source The element configuration
*@return The margins of the element
*/
@private
get_margin(config: WParallax.config, source: WParallax.src): WParallax.dim=
match source.offset with
|~{px} -> px
|~{percent} -> offset_x = (percent.x / 2.) + 50.
offset_y = (percent.y / 2.) + 50.
dimensions = get_container_dimensions(config, source)
w = float_of_int(dimensions.px_x)
h = float_of_int(dimensions.px_y)
margin_left = int_of_float((w * offset_x) / 100.)
margin_top = int_of_float((h * offset_y) / 100.)
{px_x=margin_left;px_y=margin_top}
/*
*Give the adjustment for an element
*
*@param pos The position of the mouse on the page
*@param off The offset of the parallax on the page
*@param dim The dimension of the parallax
*@return The adjustment (in percent)
*/
@private
get_adjustment(pos: int, off: int, dim: int): float =
p = float_of_int(pos)
o = float_of_int(off)
d = float_of_int(dim)
offset = p - o
half = d / 2.
(offset - half) / half
/*
*Add the style to an element of the parallax
*
*@param config The widget configuration
*@param source The element configuration
*/
@private
configure_content(config: WParallax.config, source: WParallax.src): void =
margin = get_margin(config, source)
top = margin.px_y
left = margin.px_x
padding={t={none};
l={none};
b={none};
r={none}}
margin={t={some={px=top}};
l={some={px=left}};
b={none};
r={none}}
style = [{position={absolute}},{padding=padding},{margin=margin}]
dom_src = Dom.select_id("{source.id}_content")
Dom.set_style(dom_src,style)
/*
*Add the style to the container of an element of the parallax
*
*@param config The widget configuration
*@param source The element configuration
*/
@private
configure_container(config: WParallax.config, source: WParallax.src): void =
dimensions = get_container_dimensions(config, source)
w = dimensions.px_x
h = dimensions.px_y
margin_top = (config.height - h)/2
margin_left = (config.width - w)/2
margin={t={some={px=margin_top}};
l={some={px=margin_left}};
b={none};
r={none}}
style = [{position={absolute}},{width={px=w}},{height={px=h}},{margin=margin}]
dom_container = Dom.select_id(source.id)
Dom.set_style(dom_container,style)
/*
*Add the style to the parallax and the HTML of the elements
*
*@param config The widget configuration
*@param sources The elements configuration list
*@param id The widget ID
*/
@private
configure(config: WParallax.config, sources: list(WParallax.src), id: string): void =
list_sources = sort_sources(sources)
dom_parallax = Dom.select_id("{id}_parallax")
do Dom.set_style(dom_parallax,[{position={relative}},{overflow={hidden}},{width={px=config.width}},{height={px=config.height}}])
_ = Dom.bind(dom_parallax,{mousemove},handle(config,list_sources,_,id))
void
/*
*Function that generates the HTML correspondig to a elements list
*
*@param config The widget configuration
*@param sources The elements configuration list
*@return The HTML corresponding to the elements list
*/
@private
get_content(config: WParallax.config, sources: list(WParallax.src)): xhtml=
add_src(source)=
src = match source.content with
|~{img} -> <img id="{source.id}_content" src={img} alt="" onready={_->configure_content(config,source)}/>
|~{text} -> <div id="{source.id}_content" onready={_->configure_content(config,source)}>{text}</div>
src = WStyler.add(source.content_style,src)
<div id={source.id} onready={_->configure_container(config,source)}>
{src}
</div>
list_src = List.map(add_src,sources)
<>{list_src}</>
/*
*Function that applies the changes to the elements of the parallax
*
*@param config The widget configuration
*@param sources The elements configuration list
*@param event The event information
*/
@private
handle(config: WParallax.config, sources: list(WParallax.src), event: Dom.event, id: string): void =
dimensions = event.mouse_position_on_page
offset = Dom.get_offset(Dom.select_id("{id}_parallax"))
percent_x = get_adjustment(dimensions.x_px,offset.x_px,config.width)
percent_y = get_adjustment(dimensions.y_px,offset.y_px,config.height)
adjust(source) = (
(w,h) = match config.display with
|{horizontal} -> (get_dimension(source.depth,config.width), config.height)
|{vertical} -> (config.width, get_dimension(source.depth,config.height))
| _ -> (get_dimension(source.depth,config.width),get_dimension(source.depth,config.height))
margin_top = float_of_int(config.height - h) / 2.
margin_left = float_of_int(config.width - w) / 2.
adj_x = int_of_float(percent_x * margin_left)
adj_y = int_of_float(percent_y * margin_top)
src = Dom.select_inside(Dom.select_id("{id}_parallax"),Dom.select_id("{source.id}"))
style = match config.display with
|{horizontal} -> if source.depth>0
then [{left={px=adj_x}}]
else (
if source.depth<0
then [{left={px=-adj_x}}]
else []
)
|{vertical} -> if source.depth>0
then [{top={px=adj_y}}]
else (
if source.depth<0
then [{top={px=-adj_y}}]
else []
)
|_ -> if source.depth>0
then [{left={px=adj_x}},{top={px=adj_y}}]
else (
if source.depth<0
then [{left={px=-adj_x}},{top={px=-adj_y}}]
else []
)
Dom.set_style(src,style)
)
List.iter(adjust,sources)
}}