-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-stretch.js
More file actions
204 lines (188 loc) · 5.66 KB
/
angular-stretch.js
File metadata and controls
204 lines (188 loc) · 5.66 KB
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
angular.module('angular-stretch', [])
.directive('ngStretch', ['$window', '$timeout', function($window, $timeout){
return {
restrict: 'EA',
link: function(scope, element, attr){
var windowElement = angular.element($window);
var bottomElement, topElement;
var minHeight, minAction;
var maxHeight, maxAction;
var origCss = {
top: element[0].style.top,
height: element[0].style.height
};
var enabled;
var stretchListener;
attr.$observe('ngStretch', setBottom);
attr.$observe('ngStretchTop', setTop);
attr.$observe('ngStretchMin', setMin);
attr.$observe('ngStretchMinAction', setMinAction);
attr.$observe('ngStretchMax', setMax);
attr.$observe('ngStretchMaxAction', setMaxAction);
attr.$observe('ngStretchEnabled', setEnabled);
setEnabled(attr['ngStretchEnabled']);
element.on('$destroy', function(){
setEnabled(false);
});
function resetCSS(){
for(var prop in origCss){
element.css(prop, origCss[prop]);
}
}
function setMin(stretchMin){
minHeight = parseFloat(stretchMin);
checkSize();
}
function setMinAction(stretchMinAction){
if(angular.isDefined(stretchMinAction)){
stretchMinAction = scope.$eval(stretchMinAction);
}
minAction = stretchMinAction;
}
function setMax(stretchMax){
maxHeight = parseFloat(stretchMax);
checkSize();
}
function setMaxAction(stretchMaxAction){
if(angular.isDefined(stretchMaxAction)){
stretchMaxAction = scope.$eval(stretchMaxAction);
}
maxAction = stretchMaxAction;
}
function setEnabled(stretchEnabled){
if(angular.isDefined(stretchEnabled)){
if(stretchEnabled !== true && stretchEnabled !== false){
stretchEnabled = scope.$eval(stretchEnabled);
}
}
else{
stretchEnabled = true;
}
if(enabled != stretchEnabled){
enabled = stretchEnabled;
if(!enabled){
windowElement.off('resize', checkSize);
windowElement.off('scroll', checkSize);
if(angular.isFunction(stretchListener)){
stretchListener();
stretchListener = undefined;
}
resetCSS();
element.triggerHandler('stretch', {height: element[0].clientHeight, old_height: element[0].clientHeight});
}
else{
windowElement.on('resize', checkSize);
windowElement.on('scroll', checkSize);
if(angular.isUndefined(stretchListener)){
stretchListener = scope.$on('stretchShouldApply', checkSize);
}
element.ready(checkSize);
$timeout(checkSize);
}
}
}
function getElement(query){
return document.querySelector(query);
}
function setBottom(query){
if(query){
var getBottom = getElement(query);
if(getBottom){
bottomElement = angular.element(getBottom);
}
}
else{
bottomElement = undefined;
}
}
function setTop(query){
if(query){
var getTop = getElement(query);
if(getTop){
topElement = angular.element(getTop);
}
}
else{
topElement = undefined;
}
}
function isFixed(check){
var style = $window.getComputedStyle( check.length ? check[0] : check );
return (style && style.position == 'fixed');
}
function checkSize(){
if(!enabled){
return;
}
$timeout(function(){
if(!enabled){
return;
}
var elemBounds = element[0].getBoundingClientRect();
if(topElement){
var topElemBounds = topElement[0].getBoundingClientRect();
if(topElemBounds.bottom < 0 && isFixed(element)){
element.css('top', '0px');
elemBounds = element[0].getBoundingClientRect();
}
else{
var topDiff = topElemBounds.bottom - elemBounds.top;
if(topDiff){
element.css('top', (element[0].offsetTop + topDiff)+'px');
elemBounds = element[0].getBoundingClientRect();
}
}
}
var windowHeight = windowElement[0].innerHeight;
var heightDiff = windowHeight - elemBounds.bottom;
if(bottomElement){
var bottomElemBounds = bottomElement[0].getBoundingClientRect();
if(bottomElemBounds.top < windowHeight){
heightDiff = bottomElemBounds.top - elemBounds.bottom;
}
}
// calculate newHeight
var newHeight = element[0].clientHeight + heightDiff;
// check min-stretch bounds
if(angular.isDefined(minHeight) && !isNaN(minHeight) && newHeight <= minHeight){
newHeight = minHeight;
if(element[0].clientHeight != minHeight){
element.addClass('stretch-min');
if(angular.isDefined(minAction) && angular.isFunction(minAction)){
minAction(true, minHeight);
}
}
}
else if(element.hasClass('stretch-min')){
element.removeClass('stretch-min');
if(angular.isDefined(minAction) && angular.isFunction(minAction)){
minAction(false, minHeight);
}
}
// check max-stretch bounds
if(angular.isDefined(maxHeight) && !isNaN(maxHeight) && newHeight >= maxHeight){
newHeight = maxHeight;
if(element[0].clientHeight != maxHeight){
element.addClass('stretch-max');
if(angular.isDefined(maxAction) && angular.isFunction(maxAction)){
maxAction(true, maxHeight);
}
}
}
else if(element.hasClass('stretch-max')){
element.removeClass('stretch-max');
if(angular.isDefined(maxAction) && angular.isFunction(maxAction)){
maxAction(false, maxHeight);
}
}
// update element's height if different from newHeight
if(element[0].clientHeight != newHeight){
var oldHeight = element[0].clientHeight;
element.css('height', newHeight+'px');
element.triggerHandler('stretch', {height: newHeight, old_height: oldHeight});
}
});
}
}
};
}]);