forked from thenikso/angular-inview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-inview.js
387 lines (353 loc) · 12.3 KB
/
angular-inview.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
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
// # Angular-Inview
// - Author: [Nicola Peduzzi](https://github.com/thenikso)
// - Repository: https://github.com/thenikso/angular-inview
// - Install with: `npm install angular-inview`
// - Version: **3.0.0**
(function() {
'use strict';
// An [angular.js](https://angularjs.org) directive to evaluate an expression if
// a DOM element is or not in the current visible browser viewport.
// Use it in your AngularJS app by including the javascript and requireing it:
//
// `angular.module('myApp', ['angular-inview'])`
var moduleName = 'angular-inview';
angular.module(moduleName, [])
// ## in-view directive
//
// ### Usage
// ```html
// <any in-view="{expression}" [in-view-options="{object}"]></any>
// ```
.directive('inView', ['$parse', inViewDirective])
// ## in-view-container directive
.directive('inViewContainer', inViewContainerDirective);
// ## Implementation
function inViewDirective ($parse) {
return {
// Evaluate the expression passet to the attribute `in-view` when the DOM
// element is visible in the viewport.
restrict: 'A',
require: '?^^inViewContainer',
link: function inViewDirectiveLink (scope, element, attrs, container) {
// in-view-options attribute can be specified with an object expression
// containing:
// - `offset`: An array of values to offset the element position.
// Offsets are expressed as arrays of 4 numbers [top, right, bottom, left].
// Like CSS, you can also specify only 2 numbers [top/bottom, left/right].
// Instead of numbers, some array elements can be a string with a percentage.
// Positive numbers are offsets outside the element rectangle and
// negative numbers are offsets to the inside.
// - `viewportOffset`: Like the element offset but appied to the viewport.
// - `generateDirection`: Indicate if the `direction` information should
// be included in `$inviewInfo` (default false).
// - `generateParts`: Indicate if the `parts` information should
// be included in `$inviewInfo` (default false).
// - `throttle`: Specify a number of milliseconds by which to limit the
// number of incoming events.
var options = {};
if (attrs.inViewOptions) {
options = scope.$eval(attrs.inViewOptions);
}
if (options.offset) {
options.offset = normalizeOffset(options.offset);
}
if (options.viewportOffset) {
options.viewportOffset = normalizeOffset(options.viewportOffset);
}
// Build reactive chain from an initial event
var viewportEventSignal = signalSingle({ type: 'initial' })
// Merged with the window events
.merge(signalFromEvent(window, 'checkInView click ready wheel mousewheel DomMouseScroll MozMousePixelScroll resize scroll touchmove mouseup keydown'))
// Merge with container's events signal
if (container) {
viewportEventSignal = viewportEventSignal.merge(container.eventsSignal);
}
// Throttle if option specified
if (options.throttle) {
viewportEventSignal = viewportEventSignal.throttle(options.throttle);
}
// Map to viewport intersection and in-view informations
var inviewInfoSignal = viewportEventSignal
// Inview information structure contains:
// - `inView`: a boolean value indicating if the element is
// visible in the viewport;
// - `changed`: a boolean value indicating if the inview status
// changed after the last event;
// - `event`: the event that initiated the in-view check;
.map(function(event) {
var viewportRect;
if (container) {
viewportRect = container.getViewportRect();
// TODO merge with actual window!
} else {
viewportRect = getViewportRect();
}
viewportRect = offsetRect(viewportRect, options.viewportOffset);
var elementRect = offsetRect(element[0].getBoundingClientRect(), options.offset);
var isVisible = !!(element[0].offsetWidth || element[0].offsetHeight || element[0].getClientRects().length);
var info = {
inView: isVisible && intersectRect(elementRect, viewportRect),
event: event,
element: element,
elementRect: elementRect,
viewportRect: viewportRect
};
// Add inview parts
if (options.generateParts && info.inView) {
info.parts = {};
info.parts.top = elementRect.top >= viewportRect.top;
info.parts.left = elementRect.left >= viewportRect.left;
info.parts.bottom = elementRect.bottom <= viewportRect.bottom;
info.parts.right = elementRect.right <= viewportRect.right;
}
return info;
})
// Add the changed information to the inview structure.
.scan({}, function (lastInfo, newInfo) {
// Add inview direction info
if (options.generateDirection && newInfo.inView && lastInfo.elementRect) {
newInfo.direction = {
horizontal: newInfo.elementRect.left - lastInfo.elementRect.left,
vertical: newInfo.elementRect.top - lastInfo.elementRect.top
};
}
// Calculate changed flag
newInfo.changed =
newInfo.inView !== lastInfo.inView ||
!angular.equals(newInfo.parts, lastInfo.parts) ||
!angular.equals(newInfo.direction, lastInfo.direction);
return newInfo;
})
// Filters only informations that should be forwarded to the callback
.filter(function (info) {
// Don't forward if no relevant infomation changed
if (!info.changed) {
return false;
}
// Don't forward if not initially in-view
if (info.event.type === 'initial' && !info.inView) {
return false;
}
return true;
});
// Execute in-view callback
var inViewExpression = $parse(attrs.inView);
var dispose = inviewInfoSignal.subscribe(function (info) {
scope.$applyAsync(function () {
inViewExpression(scope, {
'$inview': info.inView,
'$inviewInfo': info
});
});
});
// Dispose of reactive chain
scope.$on('$destroy', dispose);
}
}
}
function inViewContainerDirective () {
return {
restrict: 'A',
controller: ['$element', function ($element) {
this.element = $element;
this.eventsSignal = signalFromEvent($element, 'scroll');
this.getViewportRect = function () {
return $element[0].getBoundingClientRect();
};
}]
}
}
// ## Utilities
function getViewportRect () {
var result = {
top: 0,
left: 0,
width: window.innerWidth,
right: window.innerWidth,
height: window.innerHeight,
bottom: window.innerHeight
};
if (result.height) {
return result;
}
var mode = document.compatMode;
if (mode === 'CSS1Compat') {
result.width = result.right = document.documentElement.clientWidth;
result.height = result.bottom = document.documentElement.clientHeight;
} else {
result.width = result.right = document.body.clientWidth;
result.height = result.bottom = document.body.clientHeight;
}
return result;
}
function intersectRect (r1, r2) {
return !(r2.left > r1.right ||
r2.right < r1.left ||
r2.top > r1.bottom ||
r2.bottom < r1.top);
}
function normalizeOffset (offset) {
if (!angular.isArray(offset)) {
return [offset, offset, offset, offset];
}
if (offset.length == 2) {
return offset.concat(offset);
}
else if (offset.length == 3) {
return offset.concat([offset[1]]);
}
return offset;
}
function offsetRect (rect, offset) {
if (!offset) {
return rect;
}
var offsetObject = {
top: isPercent(offset[0]) ? (parseFloat(offset[0]) * rect.height / 100) : offset[0],
right: isPercent(offset[1]) ? (parseFloat(offset[1]) * rect.width / 100) : offset[1],
bottom: isPercent(offset[2]) ? (parseFloat(offset[2]) * rect.height / 100) : offset[2],
left: isPercent(offset[3]) ? (parseFloat(offset[3]) * rect.width / 100) : offset[3]
};
// Note: ClientRect object does not allow its properties to be written to therefore a new object has to be created.
return {
top: rect.top - offsetObject.top,
left: rect.left - offsetObject.left,
bottom: rect.bottom + offsetObject.bottom,
right: rect.right + offsetObject.right,
height: rect.height + offsetObject.top + offsetObject.bottom,
width: rect.width + offsetObject.left + offsetObject.right
};
}
function isPercent (n) {
return angular.isString(n) && n.indexOf('%') > 0;
}
// ## QuickSignal FRP
// A quick and dirty implementation of Rx to have a streamlined code in the
// directives.
// ### QuickSignal
//
// - `didSubscribeFunc`: a function receiving a `subscriber` as described below
//
// Usage:
// var mySignal = new QuickSignal(function(subscriber) { ... })
function QuickSignal (didSubscribeFunc) {
this.didSubscribeFunc = didSubscribeFunc;
}
// Subscribe to a signal and consume the steam of data.
//
// Returns a function that can be called to stop the signal stream of data and
// perform cleanup.
//
// A `subscriber` is a function that will be called when a new value arrives.
// a `subscriber.$dispose` property can be set to a function to be called uppon
// disposal. When setting the `$dispose` function, the previously set function
// should be chained.
QuickSignal.prototype.subscribe = function (subscriber) {
this.didSubscribeFunc(subscriber);
var dispose = function () {
if (subscriber.$dispose) {
subscriber.$dispose();
subscriber.$dispose = null;
}
}
return dispose;
}
QuickSignal.prototype.map = function (f) {
var s = this;
return new QuickSignal(function (subscriber) {
subscriber.$dispose = s.subscribe(function (nextValue) {
subscriber(f(nextValue));
});
});
};
QuickSignal.prototype.filter = function (f) {
var s = this;
return new QuickSignal(function (subscriber) {
subscriber.$dispose = s.subscribe(function (nextValue) {
if (f(nextValue)) {
subscriber(nextValue);
}
});
});
};
QuickSignal.prototype.scan = function (initial, scanFunc) {
var s = this;
return new QuickSignal(function (subscriber) {
var last = initial;
subscriber.$dispose = s.subscribe(function (nextValue) {
last = scanFunc(last, nextValue);
subscriber(last);
});
});
}
QuickSignal.prototype.merge = function (signal) {
return signalMerge(this, signal);
};
QuickSignal.prototype.throttle = function (threshhold) {
var s = this, last, deferTimer;
return new QuickSignal(function (subscriber) {
var chainDisposable = s.subscribe(function () {
var now = +new Date,
args = arguments;
if (last && now < last + threshhold) {
clearTimeout(deferTimer);
deferTimer = setTimeout(function () {
last = now;
subscriber.apply(null, args);
}, threshhold);
} else {
last = now;
subscriber.apply(null, args);
}
});
subscriber.$dispose = function () {
clearTimeout(deferTimer);
if (chainDisposable) chainDisposable();
};
});
};
function signalMerge () {
var signals = arguments;
return new QuickSignal(function (subscriber) {
var disposables = [];
for (var i = signals.length - 1; i >= 0; i--) {
disposables.push(signals[i].subscribe(function () {
subscriber.apply(null, arguments);
}));
}
subscriber.$dispose = function () {
for (var i = disposables.length - 1; i >= 0; i--) {
if (disposables[i]) disposables[i]();
}
}
});
}
// Returns a signal from DOM events of a target.
function signalFromEvent (target, event) {
return new QuickSignal(function (subscriber) {
var handler = function (e) {
subscriber(e);
};
var el = angular.element(target);
event.split(' ').map(function (e) {
el[0].addEventListener(e, handler, true);
});
subscriber.$dispose = function () {
event.split(' ').map(function (e) {
el[0].removeEventListener(e, handler, true);
});
};
});
}
function signalSingle (value) {
return new QuickSignal(function (subscriber) {
setTimeout(function() { subscriber(value); });
});
}
// Module loaders exports
if (typeof define === 'function' && define.amd) {
define(['angular'], moduleName);
} else if (typeof module !== 'undefined' && module && module.exports) {
module.exports = moduleName;
}
})();