Created
May 22, 2017 19:22
-
-
Save illarionvk/44d6409f0a1d0aa422462b42ff62fb3f to your computer and use it in GitHub Desktop.
Find Waypoint instances by DOM element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/* | |
* Find Waypoint instances by DOM element | |
*/ | |
var compact = require('lodash/fp/compact'); | |
var filter = require('lodash/fp/filter'); | |
var flatten = require('lodash/fp/flatten'); | |
var forEach = require('lodash/fp/forEach'); | |
var values = require('lodash/fp/values'); | |
var $ = window.jQuery; | |
module.exports = { | |
find: function(elements) { | |
var Waypoint, context, instances, items; | |
Waypoint = window.Waypoint; | |
items = $(elements); | |
if (items.length === 0) { | |
return []; | |
} | |
context = Waypoint.Context.findByElement(window); | |
if (!(context instanceof Waypoint.Context)) { | |
return []; | |
} | |
instances = flatten([values(context.waypoints.horizontal), values(context.waypoints.vertical)]); | |
return compact(flatten(items.map(function(index, element) { | |
return filter(function(instance) { | |
return instance.element === element; | |
})(instances); | |
}).get())); | |
}, | |
destroy: function(instances) { | |
if (instances == null) { | |
instances = []; | |
} | |
if (instances.length === 0) { | |
return; | |
} | |
forEach(function(instance) { | |
instance.destroy(); | |
})(instances); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment