Skip to content

Commit 183689e

Browse files
#waypoint('prev/next'[, contextSelector]). imakewebthings#69
1 parent 9c3ce32 commit 183689e

File tree

3 files changed

+103
-34
lines changed

3 files changed

+103
-34
lines changed

test/spec.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ describe 'jQuery Waypoints', ->
316316

317317
runs ->
318318
expect(hit).toBeFalsy()
319+
320+
describe '#waypoint("prev")', ->
321+
it 'returns jQuery object containing previous waypoint', ->
322+
$e = $ '#same1'
323+
$f = $ '#near1'
324+
$e.add($f).waypoint()
325+
# console.log($f.waypoint('prev'))
326+
# console.log($e)
327+
expect($f.waypoint('prev')[0]).toEqual $e[0]
328+
329+
describe '#waypoint("next")', ->
330+
it 'returns jQuery object containing next waypoint', ->
331+
$e = $ '#same1'
332+
$f = $ '#near1'
333+
$e.add($f).waypoint()
334+
expect($e.waypoint('next')[0]).toEqual $f[0]
319335

320336
describe 'jQuery#waypoints()', ->
321337
it 'starts as an empty array for each axis', ->

waypoints.coffee

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ methods =
219219
enable: -> methods._invoke this, 'enable'
220220
destroy: -> methods._invoke this, 'destroy'
221221

222+
prev: (axis, selector) ->
223+
methods._traverse.call this, axis, selector, (stack, index, waypoints) ->
224+
stack.push waypoints[index-1] if index > 0
225+
226+
next: (axis, selector) ->
227+
methods._traverse.call this, axis, selector, (stack, index, waypoints) ->
228+
stack.push waypoints[index+1] if index < waypoints.length-1
229+
230+
_traverse: (axis = 'vertical', selector = window, push) ->
231+
waypoints = jQMethods.aggregate selector
232+
stack = []
233+
@each ->
234+
index = $.inArray this, waypoints[axis]
235+
push stack, index, waypoints[axis]
236+
@pushStack stack
237+
222238
_invoke: ($elements, method) ->
223239
$elements.each ->
224240
waypoints = Waypoint.getWaypointsByElement this
@@ -253,28 +269,21 @@ jQMethods =
253269
viewportHeight: ->
254270
window.innerHeight ? $w.height()
255271

256-
aggregate: ->
272+
aggregate: (contextSelector) ->
273+
collection = allWaypoints
274+
if contextSelector
275+
collection = contexts[$(contextSelector).data contextKey].waypoints
257276
waypoints =
258277
horizontal: []
259278
vertical: []
260279
$.each waypoints, (axis, arr) ->
261-
$.each allWaypoints[axis], (key, waypoint) ->
280+
$.each collection[axis], (key, waypoint) ->
262281
arr.push waypoint
263282
arr.sort (a, b) -> a.offset - b.offset
264283
waypoints[axis] = $.map arr, (waypoint) -> waypoint.element
265284
waypoints[axis] = $.unique waypoints[axis]
266285
waypoints
267286

268-
_filter: (selector, axis, test) ->
269-
context = contexts[$(selector).data contextKey]
270-
console.log $(selector).
271-
return [] unless context
272-
waypoints = []
273-
$.each context.waypoints[axis], (i, waypoint) ->
274-
waypoints.push waypoint if test context, waypoint
275-
waypoints.sort (a, b) -> a.offset - b.offset
276-
$.map waypoints, (waypoint) -> waypoint.element
277-
278287
above: (contextSelector = window) ->
279288
jQMethods._filter contextSelector, 'vertical', (context, waypoint) ->
280289
waypoint.offset < context.oldScroll.y
@@ -291,6 +300,15 @@ jQMethods =
291300
jQMethods._filter contextSelector, 'horizontal', (context, waypoint) ->
292301
waypoint.offset >= context.oldScroll.x
293302

303+
_filter: (selector, axis, test) ->
304+
context = contexts[$(selector).data contextKey]
305+
return [] unless context
306+
waypoints = []
307+
$.each context.waypoints[axis], (i, waypoint) ->
308+
waypoints.push waypoint if test context, waypoint
309+
waypoints.sort (a, b) -> a.offset - b.offset
310+
$.map waypoints, (waypoint) -> waypoint.element
311+
294312

295313
$[wps] = (method) ->
296314
if jQMethods[method] then jQMethods[method]() else jQMethods.aggregate()

waypoints.js

Lines changed: 57 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)