Skip to content

Instantly share code, notes, and snippets.

@tarranjones
Last active February 4, 2016 12:40
Show Gist options
  • Save tarranjones/cf841fd48940e2c01268 to your computer and use it in GitHub Desktop.
Save tarranjones/cf841fd48940e2c01268 to your computer and use it in GitHub Desktop.
Node.prototype.addBefore = function(newElement) {
this.parentNode.insertBefore(newElement,this);
}
Node.prototype.addAfter = function (newElement) {
this.parentNode.insertBefore(newElement, this.nextSibling);
}
Node.prototype.setProperties = function(props){
var i,p,c,k,j;
for(i in props) {
p = props[i];
c = p.constructor;
if(c === Object) {
for(k in p) this[i][k] = p[k];
} else if(c === Array) {
for(j = 0, l = p.length; j < l; j++) this[i][j] = p[j];
} else {
this[i] = p;
}
}
}
Document.prototype.createElementProps = function(tagName, props){
var element = this.createElement(tagName)
element.setProperties(props);
return element;
}
var e = 'Event', e = window.addEventListener ? {a:'add'+(e+='Listener'),r:'remove'+e,p:''} : {a:'attach'+e,r:'detach'+e,p:'on'};
Node.prototype.EVENT_ADD = e.a;
Node.prototype.EVENT_REMOVE = e.r;
Node.prototype.EVENT_PREFIX = e.p;
e = null;
Node.prototype.on =function(type, listener){
on = this[this.EVENT_ADD];
if(on){
on(this.EVENT_PREFIX+type,listener);
} else if(this['on'+type]){
this['on' + type] = listener;
}
}
Node.prototype.off =function(type, listener){
var off = this[this.EVENT_REMOVE];
if(off){
off(this.EVENT_PREFIX+type,listener);
} else if(this['on'+type]){
this['on' + type] = null;
}
}
Node.prototype.once = function(eventTarget, type, listener){
this.on(type, var newlistener = function(){
listener.call(this, type);
this.off(type, newlistener);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment