Last active
August 29, 2015 14:06
-
-
Save L8D/82591e13322c1208ca8e to your computer and use it in GitHub Desktop.
This file contains 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
;(function(root, factory) { | |
if (typeof define === 'function') { | |
define(factory); | |
} else if (typeof exports === 'object') { | |
module.exports = factory(); | |
} else { | |
root.Zync = root.$ = factory(); | |
} | |
})(this, function() { | |
function fromMarkup(markup) { | |
var div = document.createElement('div'); | |
div.innerHTML = markup; | |
return div.childNodes[0]; | |
} | |
function select(selector) { | |
if (typeof selector === 'string') { | |
if (selector[0] === '<') { | |
return fromMarkup(selector); | |
} else { | |
return document.querySelector(selector); | |
} | |
} else if (selector instanceof Zync) { | |
return selector.el; | |
} else { | |
return selector; | |
} | |
} | |
function Zync(selector) { | |
if (!(this instanceof Zync)) { | |
return new Zync(selector); | |
} | |
this.el = select(selector); | |
} | |
Zync.fn = Zync; | |
var proto = Zync.prototype; | |
proto.toString = function() { | |
return '' + this.el; | |
}; | |
proto.valueOf = function() { | |
return +this.el; | |
}; | |
proto.on = function(event, callback) { | |
this.el.addEventListener(event, callback); | |
}; | |
proto.off = function(event, callback) { | |
this.el.removeEventListener(event, callback); | |
}; | |
proto.emit = function(event, value) { | |
return this.el.dispatchEvent(new Event(event, value)); | |
}; | |
proto.find = function(selector) { | |
return this.el.querySelector(selector); | |
}; | |
proto.html = function(html) { | |
if (html == null) { | |
return this.el.innerHTML; | |
} else { | |
return this.el.innerHTML = html; | |
} | |
}; | |
proto.css = function(attr, value) { | |
if (value == null) { | |
return this.el.style[attr]; | |
} else { | |
return this.el.style[attr] = value; | |
} | |
}; | |
proto.attr = function(attr, value) { | |
if (value == null) { | |
return this.el.getAttribute(attr); | |
} else { | |
return this.el.setAttribute(attr, value); | |
} | |
}; | |
return Zync; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment