forked from tdreyno/morlock.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
125 lines (105 loc) · 3.2 KB
/
api.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
var ResizeController = require('./controllers/resize-controller');
var BreakpointController = require('./controllers/breakpoint-controller');
var ScrollController = require('./controllers/scroll-controller');
var ElementVisibleController = require('./controllers/element-visible-controller');
var ScrollPositionController = require('./controllers/scroll-position-controller');
var StickyElementController = require('./controllers/sticky-element-controller');
var Util = require('./core/util');
var Events = require('./core/events');
var Buffer = require('./core/buffer');
var Stream = require('./core/stream');
var getResizeTracker = Util.memoize(function(options) {
return new ResizeController(options);
});
var getScrollTracker = Util.memoize(function(options) {
return new ScrollController(options);
});
var getPositionTracker = Util.memoize(function(pos) {
return morlock.observePosition(pos);
});
var sharedBreakpointDefs = [];
var sharedBreakpointsVals = [];
function getBreakpointTracker(def) {
var found = false;
for (var i = 0; i < sharedBreakpointDefs.length; i++) {
if (Util.equals(sharedBreakpointDefs[i], def)) {
found = true;
break;
}
}
if (found) {
return sharedBreakpointsVals[i];
} else {
var controller = new BreakpointController(def);
sharedBreakpointDefs.push(def);
sharedBreakpointsVals.push(controller);
return controller;
}
}
module.exports = {
onResize: function onResize(cb) {
var st = getResizeTracker({ debounceMs: 0 });
return st.on('resize', cb);
},
onResizeEnd: function onResizeEnd(cb, options) {
var st = getResizeTracker(options);
return st.on('resizeEnd', cb);
},
onScroll: function onScroll(cb) {
var st = getScrollTracker();
return st.on('scroll', cb);
},
onScrollEnd: function onScrollEnd(cb) {
var st = getScrollTracker();
return st.on('scrollEnd', cb);
},
observeElement: function observeElement(elem, options) {
return new ElementVisibleController(elem, options);
},
observePosition: function observePosition(positionY) {
return new ScrollPositionController(positionY);
},
stickyElement: function stickyElement(elem, container, options) {
return new StickyElementController(elem, container, options);
},
breakpoint: {
enter: function(def, cb) {
var controller = getBreakpointTracker({
breakpoints: {
singleton: def
}
});
controller.on('breakpoint:singleton', function(data) {
if (data[1] === 'enter') {
cb(data);
}
});
},
exit: function(def, cb) {
var controller = getBreakpointTracker({
breakpoints: {
singleton: def
}
});
controller.on('breakpoint:singleton', function(data) {
if (data[1] === 'exit') {
cb(data);
}
});
}
},
position: {
before: function(pos, cb) {
var observer = getPositionTracker(pos);
return observer.on('before', cb);
},
after: function(pos, cb) {
var observer = getPositionTracker(pos);
return observer.on('after', cb);
}
}
};
module.exports.Stream = Stream;
module.exports.Events = Events;
module.exports.Buffer = Buffer;
module.exports.Util = Util;