Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

custom scroll view: emit events when child elements go offscreen #70

Merged
merged 1 commit into from
Aug 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/components/scrollview/customscrollview.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require('jquery-mousewheel') $

KDCustomHTMLView = require './../../core/customhtmlview'
KDScrollTrack = require './scrolltrack'
KDCustomScrollViewWrapper = require './customscrollviewinner'
Expand All @@ -16,10 +14,15 @@ module.exports = class KDCustomScrollView extends KDCustomHTMLView

{mouseWheelSpeed, lazyLoadThreshold} = @getOptions()

@listenWindowResize() if options.offscreenIndicatorClassName?

@wrapper = new KDCustomScrollViewWrapper {
tagName : 'main'
lazyLoadThreshold
mouseWheelSpeed
scroll:
if options.offscreenIndicatorClassName?
then @bound 'updateOffscreenIndicators'
}

@verticalTrack = new KDScrollTrack delegate : @wrapper
Expand All @@ -33,6 +36,30 @@ module.exports = class KDCustomScrollView extends KDCustomHTMLView
@on 'mouseenter', @bound 'showTracks'
@on 'mouseleave', @bound 'hideTracks'

_windowDidResize: ->
@updateOffscreenIndicators()

updateOffscreenIndicators: ->
wrapperEl = @wrapper.getElement()
{ offscreenIndicatorClassName } = @getOptions()
{ above, below } = [].slice.call(
wrapperEl.getElementsByClassName(offscreenIndicatorClassName))
.reduce (memo, child) ->
[_, y] = KD.utils.relativeOffset child, wrapperEl
if y < wrapperEl.scrollTop
memo.above.push child
else if y > wrapperEl.scrollTop + wrapperEl.offsetHeight
memo.below.push child
return memo
, { above: [], below: [] }
if above.length > 0
@emit 'OffscreenItemsAbove', above
else
@emit 'NoOffscreenItemsAbove'
if below.length > 0
@emit 'OffscreenItemsBelow', below
else
@emit 'NoOffscreenItemsBelow'

viewAppended: ->

Expand Down
11 changes: 11 additions & 0 deletions src/core/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,14 @@ module.exports =

clearTimeout timeout
timeout = setTimeout later, wait

relativeOffset: (child, parent) ->
x = 0; y = 0
node = child
while node
x += node.offsetLeft
y += node.offsetTop
break if node is parent
node = node.parentNode
throw new Error "Not a descendant!" unless node?
[x, y]