Skip to content

Instantly share code, notes, and snippets.

@justinbmeyer
Created February 16, 2012 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinbmeyer/1844750 to your computer and use it in GitHub Desktop.
Save justinbmeyer/1844750 to your computer and use it in GitHub Desktop.
my$ for Cap
(function(){
var my$ = function(selector){
if(!(this instanceof my$)){
return new my$(selector);
}
if(typeof selector === 'string'){
var arrayOfElements = my$.makeArray( document.querySelectorAll(selector) );
} else if(selector.nodeType){
arrayOfElements = [selector]
} else {
var arrayOfElements = selector;
}
this.push.apply(this, arrayOfElements);
}
my$.prototype = [];
my$.makeArray = function(arr){
var array = [];
for(var i =0; i < arr.length; i++){
array.push(arr[i])
}
return array;
}
my$.prototype.each = function(cb){
for(var i = 0 ; i< this.length; i++){
cb(this[i])
}
return this;
}
my$.prototype.map = function(cb){
var arr = [];
for(var i = 0 ; i< this.length; i++){
arr.push(cb(this[i]))
}
return my$(arr);
}
my$.prototype.val = function(val){
if ( val === undefined ){
return this[0].value
} else {
return this.each(function(el){
el.value = val
});
}
}
my$.prototype.html = function(val){
if ( val === undefined ){
return this[0].innerHTML
} else {
return this.each(function(el){
el.innerHTML = val
});
}
}
my$.prototype.text = function(text){
if ( text === undefined ){
var texts = [];
var getText = function(el){
for(var i =0; i < el.childNodes.length; i++){
var child = el.childNodes[i];
if(child.nodeType == 3){
texts.push(child.nodeValue)
} else {
getText(child)
}
}
}
getText(this[0])
return texts.join("");
} else {
this.html("");
return this.each(function(el){
el.appendChild(document.createTextNode(text))
});
}
}
my$.prototype.find = function(selector){
var elements = []
this.each(function(node){
var arr = node.querySelectorAll(selector);
elements.push.apply(elements, arr)
})
return my$(elements);
};
my$.prototype.parent = function(){
var parents = []
this.each(function(node){
parents.push(node.parentNode)
})
return my$( parents ).unique();
}
var until = function(node, itProp){
var next = node[itProp];
while(next && next.nodeType != 1){
next = next[itProp]
}
return my$(next ? [next] : []);
}
my$.prototype.next = function(){
return until(this[0],"nextSibling")
}
my$.prototype.prev = function(){
return until(this[0],"previousSibling")
}
my$.prototype.unique = function(){
var sorted = this.sort(sortOrder),
cur,
arr = []
for(var i = 0 ; i < sorted.length; i++){
if(sorted[i] !== cur){
arr.push(sorted[i])
cur = sorted[i]
}
}
return my$(arr);
}
var sortOrder = function( a, b ) {
if ( a === b ) {
return 0;
}
return a.compareDocumentPosition(b) & 4 ? -1 : 1;
};
my$.prototype.children = function(){
var arr = [],
childNodes = this[0].childNodes;
for(var i= 0, child; child = childNodes[i++];){
if(child.nodeType ===1){
arr.push(child)
}
}
return my$(arr);
}
my$.prototype.attr = function(attr, value){
if(typeof attr == 'object'){
for(var prop in attr){
if(attr.hasOwnProperty(prop)){
this.attr(prop, attr[prop])
}
}
return this;
} else if(value === undefined) {
return this[0].getAttribute(attr)
} else{
return this.each(function(node){
node.setAttribute(attr, value)
});
}
}
// my$().attr({id: "thing", type: "text"})
my$.prototype.css = function(prop, value){
if(value === undefined) {
prop = prop.replace(/[A-Z]([a-z]+)/g, function(whole, part){
return "-"+whole.toLowerCase();
})
return document
.defaultView
.getComputedStyle( this[0], null )
.getPropertyValue( prop )
} else {
return this.each(function(node){
node.style[prop] = value
});
}
}
my$.prototype.width = function(){
return this[0].clientWidth -
parseInt( this.css('paddingLeft') ) -
parseInt( this.css('paddingRight') ) -
parseInt( this.css('borderRightWidth') ) -
parseInt( this.css('borderLeftWidth') );
}
my$.prototype.offset = function(){
var offset = {
left: 0,
top: 0
}
var current = this[0];
while(current){
offset.left += current.offsetLeft
offset.top += current.offsetTop
current = current.offsetParent
}
return offset;
}
window.my$ = my$
})();
$.fn.tabs = function(){
// hide all infos
var as = this.find('a');
var active = as.splice(0,1)[0],
getTab = function(a){
return $( $(a).attr('href') )
};
as.each(function(i, a){
getTab(a).css("display","none")
})
this.find('a').bind('click', function(ev){
getTab(active).css("display","none")
getTab(this).css("display","")
active = this;
})
}
$('#foods').tabs()
(function(){
var my$ = function(selector){
var arrayOfElements
if(!(this instanceof my$)){
return new my$(selector);
}
if(typeof selector === 'string'){
arrayOfElements = my$.makeArray( document.querySelectorAll(selector) );
} else if(selector.nodeType){
arrayOfElements = [selector]
} else {
arrayOfElements = selector;
}
this.push.apply(this, arrayOfElements);
}
my$.prototype = [];
my$.makeArray = function(arr){
var array = [];
for(var i =0; i < arr.length; i++){
array.push(arr[i])
}
return array;
}
my$.prototype.each = function(cb){
for(var i = 0 ; i< this.length; i++){
cb(this[i])
}
return this;
}
my$.prototype.map = function(cb){
var arr = [];
for(var i = 0 ; i< this.length; i++){
arr.push(cb(this[i]))
}
return my$(arr);
}
my$.prototype.val = function(val){
if ( val === undefined ){
return this[0].value
} else {
return this.each(function(el){
el.value = val
});
}
}
my$.prototype.html = function(val){
if ( val === undefined ){
return this[0].innerHTML
} else {
return this.each(function(el){
el.innerHTML = val
});
}
}
my$.prototype.text = function(text){
if ( text === undefined ){
var texts = [];
var getText = function(el){
for(var i =0; i < el.childNodes.length; i++){
var child = el.childNodes[i];
if(child.nodeType == 3){
texts.push(child.nodeValue)
} else {
getText(child)
}
}
}
getText(this[0])
return texts.join("");
} else {
this.html("");
return this.each(function(el){
el.appendChild(document.createTextNode(text))
});
}
}
my$.prototype.find = function(selector){
var elements = []
this.each(function(node){
var arr = node.querySelectorAll(selector);
elements.push.apply(elements, arr)
})
return my$(elements);
};
my$.prototype.parent = function(){
var parents = []
this.each(function(node){
parents.push(node.parentNode)
})
return my$( parents ).unique();
}
var until = function(node, itProp){
var next = node[itProp];
while(next && next.nodeType != 1){
next = next[itProp]
}
return my$(next ? [next] : []);
}
my$.prototype.next = function(){
return until(this[0],"nextSibling")
}
my$.prototype.prev = function(){
return until(this[0],"previousSibling")
}
my$.prototype.unique = function(){
var sorted = this.sort(sortOrder),
cur,
arr = []
for(var i = 0 ; i < sorted.length; i++){
if(sorted[i] !== cur){
arr.push(sorted[i])
cur = sorted[i]
}
}
return my$(arr);
}
var sortOrder = function( a, b ) {
if ( a === b ) {
return 0;
}
return a.compareDocumentPosition(b) & 4 ? -1 : 1;
};
my$.prototype.children = function(){
var arr = [],
childNodes = this[0].childNodes;
for(var i= 0, child; child = childNodes[i++];){
if(child.nodeType ===1){
arr.push(child)
}
}
return my$(arr);
}
my$.prototype.attr = function(attr, value){
if(typeof attr == 'object'){
for(var prop in attr){
if(attr.hasOwnProperty(prop)){
this.attr(prop, attr[prop])
}
}
return this;
} else if(value === undefined) {
return this[0].getAttribute(attr)
} else{
return this.each(function(node){
node.setAttribute(attr, value)
});
}
}
// my$().attr({id: "thing", type: "text"})
my$.prototype.css = function(prop, value){
if(value === undefined) {
prop = prop.replace(/[A-Z]([a-z]+)/g, function(whole, part){
return "-"+whole.toLowerCase();
})
return document
.defaultView
.getComputedStyle( this[0], null )
.getPropertyValue( prop )
} else {
return this.each(function(node){
node.style[prop] = value
});
}
}
my$.prototype.width = function(){
return this[0].clientWidth -
parseInt( this.css('paddingLeft') ) -
parseInt( this.css('paddingRight') ) -
parseInt( this.css('borderRightWidth') ) -
parseInt( this.css('borderLeftWidth') );
}
my$.prototype.offset = function(){
var offset = {
left: 0,
top: 0
}
var current = this[0];
while(current){
offset.left += current.offsetLeft
offset.top += current.offsetTop
current = current.offsetParent
}
return offset;
}
// addEventListener
my$.prototype.bind = function(eventName, handler){
return this.each(function(el){
el.addEventListener(eventName, handler, false)
})
}
// removeEventListener
my$.prototype.unbind = function(eventName, handler){
return this.each(function(el){
el.removeEventListener(eventName, handler, false)
})
}
// my$('#ul').delegate('li','click', function(){})
my$.prototype.delegate = function(selector, eventName, handler){
this.bind(eventName, function(ev){
if( ev.target.mozMatchesSelector(selector) ){
handler.apply(this, ev)
}
})
}
// my$('#foods').tabs()
window.my$ = my$
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment