Skip to content

Instantly share code, notes, and snippets.

@DC3
Last active March 14, 2019 02:06
Show Gist options
  • Select an option

  • Save DC3/942be26401906a06a026 to your computer and use it in GitHub Desktop.

Select an option

Save DC3/942be26401906a06a026 to your computer and use it in GitHub Desktop.
/* bling.js */
window.$ = document.querySelectorAll.bind(document)
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn)
}
NodeList.prototype.__proto__ = Array.prototype
NodeList.prototype.on = NodeList.prototype.addEventListener = function (name, fn) {
this.forEach(function (elem, i) {
elem.on(name, fn)
})
}
window.$.Deferred = function() {
var resolver, rejector;
var promise = new Promise(function(resolve, reject) {
resolver = resolve;
rejector = reject;
});
var deferred = {
resolve: resolver,
reject: rejector,
then: function(a, b) { promise = promise.then(a, b); return deferred; },
pipe: function(a, b) { promise = promise.then(a, b); return deferred; },
done: function(a) { promise = promise.then(a, b); return deferred; },
fail: function(a) { promise = promise.catch(a); return deferred; },
always: function(a) { promise = promise.finally(a); return deferred; },
promise: function() { return deferred; }
};
return deferred;
};
Node.prototype.on = window.on = function (name, delegate, fn) {
if(arguments.length !== 3) {
return this.addEventListener(name, arguments[1]);
}
return this.addEventListener(name, function (e) {
if(e.target.matches(delegate)){
return fn.apply(e.target, arguments);
}
})
}
var $ = document.querySelectorAll.bind(document);
NodeList.prototype.get = function(index){
return this[index];
};
Element.prototype.css = function(attr, value){
if((capital = /-([a-z])/g.exec(attr))){
attr = attr.replace(capital[0], capital[1].toUpperCase());
}
this.style[attr] = value;
return this;
};
Event.prototype.delegate = function(elDelegate){
return this.target.localName === elDelegate ? true : false;
};
Element.prototype.on = function(type, callback, elDelegate){
this.addEventListener(type, function(e){
if(e.delegate(elDelegate) && elDelegate!==void(0)){
callback.apply(e.target, arguments);
} else if(!delegate && elDelegate===void(0)) {
callback.apply(this, arguments);
}
});
};
HTMLElement.prototype.find = function(selector){
return this.querySelectorAll(selector);
};
//*********************
$('#list').get(0).on('click', function(e){
this.css('background-color', '#2630a7');
}, 'li');
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function (window, rAF, cAF) {
var lastTime = 0, vendors = ['ms', 'moz', 'webkit', 'o'], x;
for (x = 0; x < vendors.length && !window[rAF]; ++x) {
window[rAF] = window[vendors[x] + 'RequestAnimationFrame'];
window[cAF] = window[vendors[x] + 'CancelAnimationFrame']
|| window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window[rAF]) {
window[rAF] = function (callback) {
var currTime = new Date().getTime(),
timeToCall = Math.max(0, 16 - (currTime - lastTime)),
id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!window[cAF]) {
window[cAF] = function (id) {
window.clearTimeout(id);
};
}
}(this, 'requestAnimationFrame', 'cancelAnimationFrame'));
@DC3
Copy link
Author

DC3 commented Jun 17, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment