forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed_scroll.js
114 lines (96 loc) · 3.21 KB
/
fixed_scroll.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
import $ from 'jquery';
import _ from 'lodash';
import uiModules from 'ui/modules';
const SCROLLER_HEIGHT = 20;
uiModules
.get('kibana')
.directive('fixedScroll', function ($timeout) {
return {
restrict: 'A',
link: function ($scope, $el) {
let $window = $(window);
let $scroller = $('<div class="fixed-scroll-scroller">').height(SCROLLER_HEIGHT);
/**
* Remove the listeners bound in listen()
* @type {function}
*/
let unlisten = _.noop;
/**
* Listen for scroll events on the $scroller and the $el, sets unlisten()
*
* unlisten must be called before calling or listen() will throw an Error
*
* Since the browser emits "scroll" events after setting scrollLeft
* the listeners also prevent tug-of-war
*
* @throws {Error} If unlisten was not called first
* @return {undefined}
*/
function listen() {
if (unlisten !== _.noop) {
throw new Error('fixedScroll listeners were not cleaned up properly before re-listening!');
}
let blockTo;
function bind($from, $to) {
function handler() {
if (blockTo === $to) return (blockTo = null);
$to.scrollLeft((blockTo = $from).scrollLeft());
}
$from.on('scroll', handler);
return function () {
$from.off('scroll', handler);
};
}
unlisten = _.flow(
bind($el, $scroller),
bind($scroller, $el),
function () { unlisten = _.noop; }
);
}
/**
* Revert DOM changes and event listeners
* @return {undefined}
*/
function cleanUp() {
unlisten();
$scroller.detach();
$el.css('padding-bottom', 0);
}
/**
* Modify the DOM and attach event listeners based on need.
* Is called many times to re-setup, must be idempotent
* @return {undefined}
*/
function setup() {
cleanUp();
const containerWidth = $el.width();
const contentWidth = $el.prop('scrollWidth');
const containerHorizOverflow = contentWidth - containerWidth;
const elTop = $el.offset().top - $window.scrollTop();
const elBottom = elTop + $el.height();
const windowVertOverflow = elBottom - $window.height();
const requireScroller = containerHorizOverflow > 0 && windowVertOverflow > 0;
if (!requireScroller) return;
// push the content away from the scroller
$el.css('padding-bottom', SCROLLER_HEIGHT);
// fill the scroller with a dummy element that mimics the content
$scroller
.width(containerWidth)
.html($('<div>').css({ width: contentWidth, height: SCROLLER_HEIGHT }))
.insertAfter($el);
// listen for scroll events
listen();
}
// reset when the width or scrollWidth of the $el changes
$scope.$watchMulti([
function () { return $el.prop('scrollWidth'); },
function () { return $el.width(); }
], setup);
// cleanup when the scope is destroyed
$scope.$on('$destroy', function () {
cleanUp();
$scroller = $window = null;
});
}
};
});