Skip to content

Commit c224764

Browse files
committed
Move all functions into methods property to support extends feature #64
1 parent e347e66 commit c224764

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

src/components/InfiniteLoading.vue

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,6 @@
2424
waveDots: 'loading-wave-dots',
2525
};
2626
27-
/**
28-
* get the first scroll parent of an element
29-
* @param {DOM} elm the element which find scorll parent
30-
* @return {DOM} the first scroll parent
31-
*/
32-
function getScrollParent(elm) {
33-
if (elm.tagName === 'BODY') {
34-
return window;
35-
} else if (['scroll', 'auto'].indexOf(getComputedStyle(elm).overflowY) > -1) {
36-
return elm;
37-
} else if (elm.hasAttribute('infinite-wrapper') || elm.hasAttribute('data-infinite-wrapper')) {
38-
return elm;
39-
}
40-
return getScrollParent(elm.parentNode);
41-
}
42-
43-
/**
44-
* get current distance from bottom
45-
* @param {DOM} scrollElm scroll element
46-
* @param {DOM} infiniteElm infinite element
47-
* @param {String} dir calculate direction
48-
* @return {Number} distance
49-
*/
50-
function getCurrentDistance(scrollElm, infiniteElm, dir) {
51-
let distance;
52-
53-
if (dir === 'top') {
54-
distance = isNaN(scrollElm.scrollTop) ? scrollElm.pageYOffset : scrollElm.scrollTop;
55-
} else {
56-
const infiniteElmOffsetTopFromBottom = infiniteElm.getBoundingClientRect().top;
57-
const scrollElmOffsetTopFromBottom = scrollElm === window ?
58-
window.innerHeight :
59-
scrollElm.getBoundingClientRect().bottom;
60-
61-
distance = infiniteElmOffsetTopFromBottom - scrollElmOffsetTopFromBottom;
62-
}
63-
64-
return distance;
65-
}
66-
6727
export default {
6828
data() {
6929
return {
@@ -112,7 +72,7 @@
11272
},
11373
},
11474
mounted() {
115-
this.scrollParent = getScrollParent(this.$el);
75+
this.scrollParent = this.getScrollParent();
11676
11777
this.scrollHandler = function scrollHandlerOriginal(ev) {
11878
if (!this.isLoading) {
@@ -170,8 +130,12 @@
170130
this.scrollParent.addEventListener('scroll', this.scrollHandler);
171131
},
172132
methods: {
133+
/**
134+
* attempt trigger load
135+
*/
173136
attemptLoad() {
174-
const currentDistance = getCurrentDistance(this.scrollParent, this.$el, this.direction);
137+
const currentDistance = this.getCurrentDistance();
138+
175139
if (!this.isComplete && currentDistance <= this.distance) {
176140
this.isLoading = true;
177141
@@ -184,6 +148,46 @@
184148
this.isLoading = false;
185149
}
186150
},
151+
/**
152+
* get current distance from the specified direction
153+
* @return {Number} distance
154+
*/
155+
getCurrentDistance() {
156+
let distance;
157+
158+
if (this.direction === 'top') {
159+
distance = isNaN(this.scrollParent.scrollTop) ?
160+
this.scrollParent.pageYOffset :
161+
this.scrollParent.scrollTop;
162+
} else {
163+
const infiniteElmOffsetTopFromBottom = this.$el.getBoundingClientRect().top;
164+
const scrollElmOffsetTopFromBottom = this.scrollParent === window ?
165+
window.innerHeight :
166+
this.scrollParent.getBoundingClientRect().bottom;
167+
168+
distance = infiniteElmOffsetTopFromBottom - scrollElmOffsetTopFromBottom;
169+
}
170+
171+
return distance;
172+
},
173+
/**
174+
* get the first scroll parent of an element
175+
* @param {DOM} elm cache element for recursive search
176+
* @return {DOM} the first scroll parent
177+
*/
178+
getScrollParent(elm = this.$el) {
179+
let result;
180+
181+
if (elm.tagName === 'BODY') {
182+
result = window;
183+
} else if (['scroll', 'auto'].indexOf(getComputedStyle(elm).overflowY) > -1) {
184+
result = elm;
185+
} else if (elm.hasAttribute('infinite-wrapper') || elm.hasAttribute('data-infinite-wrapper')) {
186+
result = elm;
187+
}
188+
189+
return result || this.getScrollParent(elm.parentNode);
190+
},
187191
},
188192
destroyed() {
189193
if (!this.isComplete) {

0 commit comments

Comments
 (0)