Minimal utility library for working with Node
,
NodeList
and Event
objects in modern browsers.
Utilizes a functional style without extending any of the built-in DOM
objects.
$ component install mkitt/poma
@returns node
Wrapper around addEventListener
. Handles either Node
or
NodeList
elements.
poma.on(elements, 'tap', tapped)
@returns node
Wrapper around removeEventListener
. Handles either Node
or
NodeList
elements.
poma.off(elements, 'tap', tapped)
@returns node
Adds the event listener(s) and removes them after the first call.
Handles either Node
or NodeList
elements.
poma.once(elements, 'tap', tapped)
@returns Event
Create a custom event
with an optional data object.
poma.event('custom:event', {bling: 1})
@returns node
Dispatches a custom event from the node, with an optional data object.
The second option can either be an eventType
string or an
Event
object. If it's an Event
object, the data
parameter is ignored. Handles either Node
or
NodeList
elements.
// create and dispatch an event..
poma.trigger(document, 'custom:event', {bling: 1})
// dispatch an already created event..
var event = poma.event('custom:event', {bling: 1})
poma.trigger(element, event)
Normalizes the browser name for Element.matches
as a cached
variable.
if (poma.matches.bind(node)(selector)) {
nodes.push(node)
}
Returns the first parent element that matches
the
selector
starting at node
as a Node
object. The selector
can either be a class
, id
or a tag
.
var parent = poma.parentSelector(child, '.box')
Returns all parent elements which match
the selector
starting at node
as a native Array
object. The selector
can either be a class
, id
or a tag
.
var parents = poma.parentSelectorAll(child, 'div')
@returns node
Removes the node
and all of it's children from the DOM.
var removed = poma.remove(parent)
@returns node
Inserts the node
in the DOM after the target
element.
var target = document.getElementById('target')
var element = poma.insertAfter(document.createElement('div'), target)
@returns poma.fn
or Error
Allows for creating a reference to another object's method within the
poma
object. If a function
of the same name already
exists, the reference is not created and an error is logged.
var obj = {noop: function(){}, remove: function(){}}
// obj.noop can be accessed via poma.noop
poma.plugin(obj, 'noop')
poma.noop()
// reference not created, the original poma.remove function is preserved
poma.plugin(obj, 'remove')
Scope the poma
object to the $
, for easier reference after the
poma
library has loaded.
if (!window.$)
window.$ = window.poma
$.on(elements, 'tap', tapped)