-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscrollglue.js
164 lines (143 loc) · 5.13 KB
/
scrollglue.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
/* angularjs Scroll Glue
* version 2.1.0
* https://github.com/Luegg/angularjs-scroll-glue
* An AngularJs directive that automatically scrolls to the bottom of an element on changes in it's scope.
*/
// Allow module to be loaded via require when using common js. e.g. npm
if(typeof module === "object" && module.exports){
module.exports = 'luegg.directives';
}
(function(angular, undefined){
'use strict';
function createActivationState($parse, attr, scope){
function unboundState(initValue){
var activated = initValue;
return {
getValue: function(){
return activated;
},
setValue: function(value){
activated = value;
}
};
}
function oneWayBindingState(getter, scope){
return {
getValue: function(){
return getter(scope);
},
setValue: function(){}
};
}
function twoWayBindingState(getter, setter, scope){
return {
getValue: function(){
return getter(scope);
},
setValue: function(value){
if(value !== getter(scope)){
scope.$apply(function(){
setter(scope, value);
});
}
}
};
}
if(attr !== ""){
var getter = $parse(attr);
if(getter.assign !== undefined){
return twoWayBindingState(getter, getter.assign, scope);
} else {
return oneWayBindingState(getter, scope);
}
} else {
return unboundState(true);
}
}
function createDirective(module, attrName, direction){
module.directive(attrName, ['$parse', '$window', '$timeout', function($parse, $window, $timeout){
return {
priority: 1,
restrict: 'A',
link: function(scope, $el, attrs){
var el = $el[0],
activationState = createActivationState($parse, attrs[attrName], scope);
function scrollIfGlued() {
if(activationState.getValue() && !direction.isAttached(el)){
// Ensures scroll after angular template digest
$timeout(function() {
direction.scroll(el);
});
}
}
function onScroll() {
activationState.setValue(direction.isAttached(el));
}
$timeout(scrollIfGlued, 0, false);
if (!$el[0].hasAttribute('force-glue')) {
$el.on('scroll', onScroll);
}
var hasAnchor = false;
angular.forEach($el.children(), function(child) {
if (child.hasAttribute('scroll-glue-anchor')) {
hasAnchor = true;
scope.$watch(function() { return child.offsetHeight }, function() {
scrollIfGlued();
});
}
});
if (!hasAnchor) {
scope.$watch(scrollIfGlued);
$window.addEventListener('resize', scrollIfGlued, false);
}
// Remove listeners on directive destroy
$el.on('$destroy', function() {
$el.unbind('scroll', onScroll);
});
scope.$on('$destroy', function() {
$window.removeEventListener('resize', scrollIfGlued, false);
});
}
};
}]);
}
var bottom = {
isAttached: function(el){
// + 1 catches off by one errors in chrome
return el.scrollTop + el.clientHeight + 1 >= el.scrollHeight;
},
scroll: function(el){
el.scrollTop = el.scrollHeight;
}
};
var top = {
isAttached: function(el){
return el.scrollTop <= 1;
},
scroll: function(el){
el.scrollTop = 0;
}
};
var right = {
isAttached: function(el){
return el.scrollLeft + el.clientWidth + 1 >= el.scrollWidth;
},
scroll: function(el){
el.scrollLeft = el.scrollWidth;
}
};
var left = {
isAttached: function(el){
return el.scrollLeft <= 1;
},
scroll: function(el){
el.scrollLeft = 0;
}
};
var module = angular.module('luegg.directives', []);
createDirective(module, 'scrollGlue', bottom);
createDirective(module, 'scrollGlueTop', top);
createDirective(module, 'scrollGlueBottom', bottom);
createDirective(module, 'scrollGlueLeft', left);
createDirective(module, 'scrollGlueRight', right);
}(angular));