This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathillume.js
90 lines (72 loc) · 2.1 KB
/
illume.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
import { query } from "@standard-library/q-prime";
import { Kefir as K } from "kefir";
const offsetAbove = (y, element) => y > offsetTop(element);
const bottomAbove = (y, element) => {
return y < offsetTop(element) + element.offsetHeight;
};
const offsetTop = element => {
if (!element.getClientRects().length) {
return 0;
}
const rect = element.getBoundingClientRect();
const win = element.ownerDocument.defaultView;
return rect.top + win.pageYOffset;
};
const uniq = function(arr, select) {
var len = arr.length;
var ret = [];
var v;
select = select ? (select instanceof Array ? select : [select]) : false;
for (var i = 0; i < len; i++) {
v = arr[i];
if (select && !~indexOf(select, v)) {
ret.push(v);
} else if (!~indexOf(ret, v)) {
ret.push(v);
}
}
return ret;
};
const DEFAULT_Y = () => {
const scroll = K.fromEvents(window, "scroll");
const resize = K.fromEvents(window, "resize");
const redraw = K.merge([scroll, resize]);
return redraw.map(() => window.scrollY);
};
const DEFAULT_HEIGHT = () => {
const scroll = K.fromEvents(window, "scroll");
const resize = K.fromEvents(window, "resize");
const redraw = K.merge([scroll, resize]);
return redraw.map(() => window.innerHeight);
};
function illume(
attribute,
{ scrollY = DEFAULT_Y(), windowHeight = DEFAULT_HEIGHT() } = {}
) {
const getName = a => a.dataset[attribute];
const areas = query(`[data-${attribute}]`);
const names = areas.map(getName);
const visibleY = K.combine([scrollY, windowHeight], (y, h) => y + h);
const viewedAreas = visibleY.map(y => {
return areas.filter(a => offsetAbove(y, a));
});
const lastViewedArea = viewedAreas.map(as => as[as.length - 1]);
const activeArea = lastViewedArea
.map(element => {
if (element && bottomAbove(window.scrollY, element)) {
return element;
}
})
.toProperty()
.skipDuplicates();
const active = activeArea.map(getName);
const inactive = active
.map(a => names.filter(n => n !== a))
.map(uniq)
.flatten();
return {
active,
inactive
};
}
export default illume;