-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathhelpers.js
174 lines (149 loc) · 5.5 KB
/
helpers.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
angular.module('duScroll.scrollHelpers', ['duScroll.requestAnimation'])
.run(function($window, $q, cancelAnimation, requestAnimation, duScrollEasing, duScrollDuration, duScrollOffset, duScrollCancelOnEvents) {
'use strict';
var proto = {};
var isDocument = function(el) {
return (typeof HTMLDocument !== 'undefined' && el instanceof HTMLDocument) || (el.nodeType && el.nodeType === el.DOCUMENT_NODE);
};
var isElement = function(el) {
return (typeof HTMLElement !== 'undefined' && el instanceof HTMLElement) || (el.nodeType && el.nodeType === el.ELEMENT_NODE);
};
var unwrap = function(el) {
return isElement(el) || isDocument(el) ? el : el[0];
};
proto.duScrollTo = function(left, top, duration, easing) {
var aliasFn;
if(angular.isElement(left)) {
aliasFn = this.duScrollToElement;
} else if(angular.isDefined(duration)) {
aliasFn = this.duScrollToAnimated;
}
if(aliasFn) {
return aliasFn.apply(this, arguments);
}
var el = unwrap(this);
if(isDocument(el)) {
return $window.scrollTo(left, top);
}
el.scrollLeft = left;
el.scrollTop = top;
};
var scrollAnimation, deferred;
proto.duScrollToAnimated = function(left, top, duration, easing) {
if(duration && !easing) {
easing = duScrollEasing;
}
var startLeft = this.duScrollLeft(),
startTop = this.duScrollTop(),
deltaLeft = Math.round(left - startLeft),
deltaTop = Math.round(top - startTop);
var startTime = null, progress = 0;
var el = this;
var cancelScrollAnimation = function($event){
if(!$event || (progress && ($event.which > 0 || $event.type === 'wheel' || $event.type === 'mousedown' || $event.type === 'mousewheel'))){
if(duScrollCancelOnEvents) {
el.unbind(duScrollCancelOnEvents, cancelScrollAnimation);
}
cancelAnimation(scrollAnimation);
deferred.reject();
scrollAnimation = null;
}
};
if(scrollAnimation) {
cancelScrollAnimation();
}
deferred = $q.defer();
var elem = unwrap(el);
var isScrollBottom = false;
var scrollY;
if(isDocument(elem)){
scrollY = $window.scrollY || elem.documentElement.scrollTop || elem.body.scrollTop;
isScrollBottom = elem.documentElement.scrollHeight - scrollY === elem.documentElement.clientHeight;
}
else if(isElement(elem)){
isScrollBottom = elem.scrollHeight - elem.scrollTop === elem.clientHeight;
}
if(duration === 0 || (!deltaLeft && (!deltaTop || (deltaTop > 0 && isScrollBottom) ))){
if(duration === 0) {
el.duScrollTo(left, top);
}
deferred.resolve();
return deferred.promise;
}
var animationStep = function(timestamp) {
if (startTime === null) {
startTime = timestamp;
}
progress = timestamp - startTime;
var percent = (progress >= duration ? 1 : easing(progress/duration));
el.scrollTo(
startLeft + Math.ceil(deltaLeft * percent),
startTop + Math.ceil(deltaTop * percent)
);
if(percent < 1) {
scrollAnimation = requestAnimation(animationStep);
} else {
if(duScrollCancelOnEvents) {
el.unbind(duScrollCancelOnEvents, cancelScrollAnimation);
}
scrollAnimation = null;
deferred.resolve();
}
};
//Fix random mobile safari bug when scrolling to top by hitting status bar
el.duScrollTo(startLeft, startTop);
if(duScrollCancelOnEvents) {
el.bind(duScrollCancelOnEvents, cancelScrollAnimation);
}
scrollAnimation = requestAnimation(animationStep);
return deferred.promise;
};
proto.duScrollToElement = function(target, offset, duration, easing) {
var el = unwrap(this);
if(!angular.isNumber(offset) || isNaN(offset)) {
offset = duScrollOffset;
}
var top = this.duScrollTop() + unwrap(target).getBoundingClientRect().top - offset;
if(isElement(el)) {
top -= el.getBoundingClientRect().top;
}
return this.duScrollTo(0, top, duration, easing);
};
proto.duScrollLeft = function(value, duration, easing) {
if(angular.isNumber(value)) {
return this.duScrollTo(value, this.duScrollTop(), duration, easing);
}
var el = unwrap(this);
if(isDocument(el)) {
return $window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft;
}
return el.scrollLeft;
};
proto.duScrollTop = function(value, duration, easing) {
if(angular.isNumber(value)) {
return this.duScrollTo(this.duScrollLeft(), value, duration, easing);
}
var el = unwrap(this);
if(isDocument(el)) {
return $window.scrollY || document.documentElement.scrollTop || document.body.scrollTop;
}
return el.scrollTop;
};
proto.duScrollToElementAnimated = function(target, offset, duration, easing) {
return this.duScrollToElement(target, offset, duration || duScrollDuration, easing);
};
proto.duScrollTopAnimated = function(top, duration, easing) {
return this.duScrollTop(top, duration || duScrollDuration, easing);
};
proto.duScrollLeftAnimated = function(left, duration, easing) {
return this.duScrollLeft(left, duration || duScrollDuration, easing);
};
angular.forEach(proto, function(fn, key) {
angular.element.prototype[key] = fn;
//Remove prefix if not already claimed by jQuery / ui.utils
var unprefixed = key.replace(/^duScroll/, 'scroll');
if(angular.isUndefined(angular.element.prototype[unprefixed])) {
angular.element.prototype[unprefixed] = fn;
}
});
});