-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
338 lines (277 loc) · 6.78 KB
/
index.js
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
// utils
const compose = f => g => x => f (g (x));
const tap = f => x => {f(x); return x;};
const props = keys => obj =>
keys.reduce ((acc, key) => acc[key], obj);
const pipe = fns => x =>
fns.reduce ((x, f) => f (x), x);
const when = predicate => f => x =>
predicate (x) ? f (x) : x;
const on = name => cb => element =>
element.addEventListener (name, cb);
const from = element => target =>
element.querySelectorAll (target)[0];
// aliases
const K = x => y => x;
const T = x => f => f (x);
const $ = T;
const log = console.log;
const debug = compose (tap (log));
const prop = key => props ([key]);
const select = from (document);
const gt = x => y => y > x;
const eq = x => y => x === y;
const nth = x => xs => xs[x];
const head = xs => xs[0];
const last = xs => xs[xs.length - 1];
const is_zero = eq (0);
const is_undef = eq (undefined);
const length = x => x.length;
const sub = x => y => y - x;
const add = x => y => y + x;
const inc = add (1);
const dec = sub (1);
const map = f => xs => xs.map (f);
const sum = xs => xs.reduce ((x, y) => x + y, 0);
const is_empty = xs => eq (0) (length (xs));
const crop = xs => xs.slice (1, -1);
const length_gt = n => compose (gt (n)) (length);
const floor = x => Math.floor (x);
const cut_at = i => xs => [
...xs.slice (0, i),
...xs.slice (i + 1, length (xs))
];
// main
const $html = select ("html");
const get_gallery = () =>
select (".gallery");
const get_img_count = pipe ([
get_gallery,
props (["children", "length"]),
]);
const get_client_width = () =>
prop ("clientWidth") ($html);
const rand = min => max =>
floor (Math.random() * (max - min + 1) + min);
const up_to = rand (1);
const range = start => end =>
start >= end
? [end]
: [start, ...range (inc (start)) (end)];
const dec_percent = percentage => x =>
floor (x - (x * (percentage / 100)));
const duplicate = n =>
when (gt (0)) (x => map (K (x)) (range (1) (n)));
const split_by = n => num => {
if (n > num) {
return split_by (num) (n);
}
const rest = num % n;
const base = floor (num / n);
const initial_value =
gt (num) (inc (base) * n)
? duplicate (dec (n)) (base)
: duplicate (dec (n)) (inc (base));
return (
eq (0) (rest)
? duplicate (n) (base)
: [num].reduce (
(acc, x) => acc.concat ([x - sum (acc)]),
initial_value
)
);
};
const inc_dec = acc => ([a, b]) => {
const temp_acc = is_empty (acc) ? [[a, b]] : acc;
const elem = gt (1) (b) ? [inc (a), dec (b)] : undefined;
return (
elem === undefined
? temp_acc
: inc_dec ([...temp_acc, elem]) (elem)
);
};
const shuffle_ends = ys => {
const shuffle = pipe ([
xs => inc_dec ([]) ([head (xs), last (xs)]),
map (([a, b]) => [a, ...crop (ys), b]),
]);
return when (length_gt (1)) (shuffle) (ys);
};
const median = xs =>
floor (length (xs) / 2);
const backward = index => xs =>
eq (index) (median (xs))
? []
: (
gt (0) (index)
? shuffle_ends (xs.slice (index, index * -1))
.map (
ys => [
...xs.slice (0, index),
...ys,
...xs.slice (index * -1)
]
)
: shuffle_ends(xs)
).concat(backward (inc (index)) (xs));
const split_by_all = n => num => {
return (
gt (1) (n)
? backward (0) (split_by (n) (num))
: num
);
};
const random_nth = xs =>
xs[rand (0) (length (xs) - 1)];
const shuffle_arr = xs => {
const index = rand (0) (length (xs) - 1);
return (
is_empty (xs)
? []
: [nth (index) (xs), ...shuffle_arr (cut_at (index) (xs))]
);
};
const make_row = height_limit => width => n => {
const height = up_to (height_limit);
const combo = (
gt (1) (n)
? shuffle_arr (random_nth (split_by_all (n) (width)))
: [width]
);
return map (x => [x, height]) (combo);
};
const item_per_row = max => rest => {
const limit = rest < max ? rest : max;
const number = up_to (limit);
return (
is_zero (rest)
? []
: [number, ...item_per_row (max) (rest - number)]
);
};
const grid = pics_count => height_limit => grid_size => {
const pic_per_row = item_per_row (grid_size) (pics_count);
return (
map (make_row (height_limit) (grid_size)) (pic_per_row)
);
};
const inject_style = css =>
select ("style").innerHTML += css;
const counter = start => {
let temp = dec (start);
return () => {
temp = inc (temp);
return temp;
};
};
const template_span = index => col => row => factor => `
.gallery__item:nth-child(${index}) {
grid-area: span ${row} / span ${col};
height: ${factor * row}px
}
`;
const is_divisable_by = x => y =>
y % x === 0;
const get_optimal_grid_size = width => {
const gt = x => width > x;
const optimal_grid_size =
gt (4000)
? 12
: (
gt (3000)
? 10
: (
gt (2000)
? 8
: (
gt (1200)
? 6
: (
gt (1000)
? 4
: (
gt (600)
? 3
: 2
)
)
)
)
);
return optimal_grid_size;
};
const get_grid_factor = width => {
const grid_size = get_optimal_grid_size (width);
const divisors =
range (1) (grid_size)
.map (num => (x => is_divisable_by (num) (x) ? num : undefined))
.map (T (width))
.filter (x => (x !== undefined && x <= grid_size));
const divisor = last (divisors);
return width / divisor;
};
const update_grid = spec => {
const {
element_count,
grid_size,
factor,
height,
} = spec;
const index = counter (1);
const height_per_row =
height > 0 ? height : rand (1) (2);
const virtual_grid =
grid (element_count) (height_per_row) (grid_size);
const css = (
virtual_grid
.flat ()
.reduce (
(css, [col, row]) =>
css += template_span (index ()) (col) (row) (factor),
""
)
);
inject_style (css);
select (".gallery").style["grid-template-columns"] =
`repeat(auto-fit, ${factor}px)`
select (".gallery").style["grid-template-rows"] =
`${factor}px`;
};
const set_basic_styling = () =>
inject_style (`
.gallery {
display: grid;
}
.gallery__item {
overflow: hidden;
}
.gallery__item img {
max-width: 100%;
width: 100%;
height: 100%;
object-fit: cover;
}
`);
const update_grid_layout = (size, height) => {
const get_factor = (
size > 0
? () => size * 100
: () => get_grid_factor (get_client_width ())
);
const get_grid_size = () =>
floor (get_client_width () / get_factor ());
const update = () => {
set_basic_styling ();
update_grid ({
"element_count": get_img_count (),
"grid_size": get_grid_size (),
"factor": get_factor (),
"height": height,
});
};
update ();
$ (window) (on ("resize") (update));
};
export {
update_grid_layout as uhu
};