Skip to content

Instantly share code, notes, and snippets.

@threepointone
Last active August 29, 2015 13:56
Show Gist options
  • Save threepointone/9066975 to your computer and use it in GitHub Desktop.
Save threepointone/9066975 to your computer and use it in GitHub Desktop.
var _ = require('underscore');
var domready = require('domready'),
bean = require('bean'), // events
bonzo = require('bonzo'), // DOM wrapper/manipulation
qwery = require('qwery'), // css selectors
morpheus = require('morpheus'),
slice = Array.prototype.slice;
// Attach events API to the prototype of DOM wrapper:
var aug = {};
['on', 'off', 'one', 'add', 'fire', 'clone'].forEach(function(k) {
aug[k] = function() {
for (var i = 0; i < this.length; i += 1) {
bean[k].apply(this, [this[i]].concat(slice.call(arguments)));
}
return this;
};
});
// uncomment these if you want all the aliases
aug.hover = function(enter, leave) {
for (var i = 0; i < this.length; i += 1) {
bean.on.call(this, this[i], 'mouseenter', enter);
bean.on.call(this, this[i], 'mouseleave', leave);
}
return this;
};
aug.animate = function(options) {
return morpheus(this, options);
};
var aliases = {
'fire': ['emit', 'trigger'],
'on': ['addListener', 'listen', 'bind'],
'add': ['delegate'],
'off': ['unbind', 'unlisten', 'removeListener', 'undelegate'],
'clone': ['cloneEvents']
};
Object.keys(aliases).forEach(function(key) {
aliases[key].forEach(function(alias) {
aug[alias] = aug[key];
});
});
var shorts = [
'blur', 'change', 'click', 'dblclick', 'error', 'focus',
'focusin', 'focusout', 'keydown', 'keypress', 'keyup', 'load',
'mousedown', 'mouseenter', 'mouseleave', 'mouseout',
'mouseover', 'mouseup', 'mousemove', 'resize', 'scroll',
'select', 'submit', 'unload'
];
shorts.forEach(function(s) {
aug[s] = function() {
for (var i = 0; i < this.length; i += 1) {
bean.on.apply(this, [this[i], s].concat(slice.call(arguments)));
}
return this;
};
});
bonzo.aug(aug);
var $ = function(selector) {
return bonzo(qwery(selector));
};
bonzo.setQueryEngine(qwery);
$.ready = domready;
module.exports = $;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment