Skip to content

Commit ea331b1

Browse files
author
Chris Hurlburt
committed
onLastEntered callback added
1 parent 316abc0 commit ea331b1

File tree

9 files changed

+55
-24
lines changed

9 files changed

+55
-24
lines changed

.flowconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
.*/examples/.*
44

55
[libs]
6-
flow
6+
flow
7+
8+
[options]
9+
unsafe.enable_getters_and_setters=true

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ We accept contributions via Pull Requests on [Github](https://github.com/chrishu
2929
Launch visual tests and watch the components at the same time
3030

3131
``` bash
32-
$ npm run dev
32+
$ npm run example
3333
```
3434

3535

examples/src/components/pages/Home.vue

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,10 @@
1111
</template>
1212

1313
<script>
14+
import { $scrollview } from '../../../../src'
15+
1416
export default {
15-
data() {
16-
return {
17-
test: 'testing'
18-
}
19-
},
20-
watch: {
21-
test(val) {
22-
console.log(val)
23-
}
24-
},
25-
mounted() {
26-
console.log(this)
27-
}
17+
2818
}
2919
</script>
3020

examples/src/components/pages/lazyload/LazyLoad.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
</template>
2424

2525
<script>
26+
import { $scrollview } from '../../../../../src'
27+
2628
import ExampleStart from '.././../ExampleStart'
2729
import LazyImage from './LazyImage'
2830
2931
export default {
3032
components: {
3133
LazyImage,
3234
ExampleStart
33-
}
35+
},
3436
}
3537
</script>
3638

flow/state.flow.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
declare type State = { // eslint-disable-line no-undef
2+
declare type State = {| // eslint-disable-line no-undef
33
callbacks: Array<?(State) => void>,
44
throttle: number,
55
scrollviews: {
@@ -13,5 +13,7 @@ declare type State = { // eslint-disable-line no-undef
1313
previousScrollLocation: number,
1414
scrollDirection: ScrollDirection,
1515
scrollListener?: () => void,
16-
recacheEl?: HTMLSpanElement // eslint-disable-line
17-
}
16+
recacheEl?: HTMLSpanElement, // eslint-disable-line
17+
lastComponent: { key?: ComponentKey, position?: ComponentPosition },
18+
onLastEntered: Function
19+
|}

src/core/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const _private = (state: State): ScrollviewPrivateAPI => ({
2020
_track: (scrollview) => {
2121
if (keysAreUnique(state, scrollview.$children)) {
2222
state.scrollviews[scrollview._uid] = scrollview
23-
const { tracking, locations } = initializeScrollview(scrollview)
23+
const { tracking, locations } = initializeScrollview(state, scrollview)
2424
state.locations = state.locations.concat(locations)
2525
state.tracking[scrollview._uid] = tracking
2626
checkInViewport(state)

src/core/init.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import {
1313
/**
1414
* Initializes a ScrollView $vm into tracking.
1515
*
16+
* @param {Object} state - ScrollView tracking state.
1617
* @param {Object} ScrollView - The ScrollView $vm
1718
* @returns {Object} Component locations and ScrollViews being tracked.
1819
*/
19-
export const initializeScrollview = ({ _uid, $children }: ScrollviewComponent) => {
20+
export const initializeScrollview = (state: State, { _uid, $children }: ScrollviewComponent) => {
2021
return $children.reduce((data, { $el, $vnode: { key: child }}) => {
2122
const position = getElPosition($el)
2223
data.locations.push({
@@ -25,10 +26,26 @@ export const initializeScrollview = ({ _uid, $children }: ScrollviewComponent) =
2526
component: child
2627
})
2728
data.tracking[child] = false
29+
setLastComponent(state, { key: child, position })
2830
return data
2931
}, { locations: [], tracking: {}})
3032
}
3133

34+
/**
35+
* Stores data for the last component on the page.
36+
*
37+
* @param {Object} State - ScrollView tracking state.
38+
* @param {Object} ComponentData - An object containing the key and position of a component.
39+
* @returns {Object} null
40+
*/
41+
export const setLastComponent = (state: State, { key, position }: { key: ComponentKey, position: ComponentPosition }) => {
42+
if (!state.lastComponent.key || !state.lastComponent.position) {
43+
state.lastComponent = { key, position }
44+
} else if (state.lastComponent.position.top < position.top) {
45+
state.lastComponent = { key, position }
46+
}
47+
}
48+
3249
/**
3350
* Attaches the scroll listener.
3451
*

src/core/scroll.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ export const checkInViewport = (state: State) => {
2626
const { offset } = state.scrollviews[scrollview]
2727
const withinTopBounds = (position.bottom - offset) - window.pageYOffset > 0
2828
const withinBottomBounds = (position.top + offset) - window.pageYOffset <= window.innerHeight
29-
state.tracking[scrollview][component] = (withinTopBounds && withinBottomBounds)
29+
const visible = (withinTopBounds && withinBottomBounds)
30+
state.tracking[scrollview][component] = visible
31+
if (visible && component === state.lastComponent.key && state.lastComponent.key) {
32+
state.onLastEntered(component)
33+
state.onLastEntered = () => {}
34+
}
3035
})
3136
}
3237

src/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@ function plugin (Vue: Function, options: Object = {}) {
1414
scrollviews: {},
1515
locations: [],
1616
tracking: {},
17-
bottom: 0,
1817
scrollDirection: 'DOWN',
1918
previousScrollLocation: 0,
20-
documentHeight: 0
19+
documentHeight: 0,
20+
lastComponent: {},
21+
onLastEntered: () => {}
2122
}
2223

2324
$scrollview = {
2425
state: initialState,
26+
set onLastEntered (fn) {
27+
if (typeof fn !== 'function') {
28+
console.error('[vue-scrollview]: onLastEntered expects a function.')
29+
} else {
30+
initialState.onLastEntered = fn
31+
this._onLastEntered = fn
32+
}
33+
},
34+
get onLastEntered () {
35+
return this._onLastEntered
36+
},
2537
..._private(initialState),
2638
..._public(initialState)
2739
}

0 commit comments

Comments
 (0)