Created
November 14, 2015 20:52
-
-
Save panayotoff/20251f00c4b4b76a3602 to your computer and use it in GitHub Desktop.
dollar-like.js
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(window) | |
{ | |
var $ = function(expression) | |
{ | |
if(!(this instanceof $)) | |
{ | |
return new $(expression); | |
} | |
var elements; | |
if($.isArray(expression)) | |
{ | |
elements = expression; | |
} | |
else | |
{ | |
elements = $.makeArray(document.querySelectorAll(expression)) | |
} | |
Array.prototype.push.apply(this, elements); | |
}; | |
$.extend = function(target, object) | |
{ | |
for(var prop in object) | |
{ | |
if(object.hasOwnProperty(prop)) | |
{ | |
target[prop] = object[prop]; | |
} | |
} | |
return target; | |
}; | |
$.extend($, { | |
isArray : function(obj) | |
{ | |
return (Object.prototype.toString.call(obj).toLowerCase().indexOf('array') >= 0); | |
}, | |
isArrayLike: function(obj) | |
{ | |
if(obj.length && typeof obj.length === 'number') | |
{ | |
if(obj.length == 0) | |
{ | |
return true; | |
} | |
if(obj.length > 0) | |
{ | |
return (obj.length - 1) in obj; | |
} | |
} | |
return false; | |
}, | |
each : function(collection, func) | |
{ | |
if($.isArrayLike(collection)) | |
{ | |
for(var i = 0, j = collection.length; i < j; i++) | |
{ | |
func.call(collection[i], i, collection[i]); | |
} | |
} | |
else | |
{ | |
for(var prop in collection) | |
{ | |
if(collection.hasOwnProperty(prop)) | |
{ | |
func.call(collection[prop], prop, collection[prop]); | |
} | |
} | |
} | |
return collection; | |
}, | |
makeArray : function(collection) | |
{ | |
var arr = []; | |
$.each(collection, function(index, value) | |
{ | |
arr.push(value); | |
}); | |
return arr; | |
}, | |
proxy : function(func, context) | |
{ | |
return function() | |
{ | |
return func.apply(context, arguments); | |
} | |
}, | |
getText : function(node) | |
{ | |
var text = ''; | |
$.each(node.childNodes, function(index, childNode) | |
{ | |
//Text node | |
if(childNode.nodeType === 3) | |
{ | |
text += childNode.nodeValue; | |
} | |
else if(childNode.nodeType === 1) | |
{ | |
text += $.getText(childNode); | |
} | |
}); | |
return text; | |
}, | |
makeTraverser: function(cb) | |
{ | |
return function() | |
{ | |
var elements = []; | |
for(var i = 0, j = this.length; i < j; i++) | |
{ | |
var result = cb.apply(this[i], arguments); | |
if(result && $.isArrayLike(result)) | |
{ | |
Array.prototype.push.apply(elements, result); | |
} | |
else if(result) | |
{ | |
elements.push(result); | |
} | |
} | |
return $(elements); | |
} | |
}, | |
getCSSProp: function(element, cssProp) | |
{ | |
return element && document.defaultView.getComputedStyle(element).getPropertyValue(cssProp); | |
}, | |
stripUnit : function(unit) | |
{ | |
return parseInt(unit, 10); | |
} | |
}); | |
//-------------------------------------------------------------- | |
// | |
//-------------------------------------------------------------- | |
$.extend($.prototype, { | |
html: function(newHtml) | |
{ | |
if(!arguments.length) | |
{ | |
return this[0].innerHTML; | |
} | |
else | |
{ | |
for(var i = 0; i < this.length; i++) | |
{ | |
this[i].innerHTML = newHtml; | |
} | |
return this; | |
} | |
}, | |
val : function(val) | |
{ | |
if(arguments.length) | |
{ | |
for(var i = 0; i < this.length; i++) | |
{ | |
this[i].value = val; | |
} | |
return this; | |
} | |
else | |
{ | |
return this[0].value; | |
} | |
}, | |
text: function(text) | |
{ | |
if(arguments.length) | |
{ | |
for(var i = 0; i < this.length; i++) | |
{ | |
this[i].innerHTML = ''; | |
this[i].appendChild(document.createTextNode(text)); | |
} | |
} | |
else | |
{ | |
return this[0] && $.getText(this[0]); | |
} | |
}, | |
find: $.makeTraverser(function(selector) | |
{ | |
return this.querySelectorAll(selector); | |
}), | |
previous: $.makeTraverser(function() | |
{ | |
var current = this.previousSibling; | |
while(current && current.nodeType !== 1) | |
{ | |
current = current.previousSibling; | |
} | |
if(current) | |
{ | |
return current; | |
} | |
}), | |
next: $.makeTraverser(function() | |
{ | |
var current = this.nextSibling; | |
while(current && current.nodeType !== 1) | |
{ | |
current = current.nextSibling; | |
} | |
if(current) | |
{ | |
return current; | |
} | |
}), | |
parent: $.makeTraverser(function() | |
{ | |
return this.parentNode; | |
}), | |
children: $.makeTraverser(function() | |
{ | |
return this.children; | |
}), | |
attr: function(attrName, attrVal) | |
{ | |
if(arguments.length > 1) | |
{ | |
for(var i = 0; i < this.length; i++) | |
{ | |
this[i].setAttribute(attrName, attrVal); | |
} | |
return this; | |
} | |
else | |
{ | |
return this[0] && this[0].getAttribute(attrName); | |
} | |
}, | |
css: function(cssProp, cssVal) | |
{ | |
if(arguments.length > 1) | |
{ | |
for(var i = 0; i < this.length; i++) | |
{ | |
this[i].style[cssProp] = cssVal; | |
} | |
} | |
else | |
{ | |
return this[0] && $.getCSSProp(this[0], cssProp); | |
} | |
}, | |
width: function() | |
{ | |
var element = this[0], | |
elementWidth = element.clientWidth, | |
elementPadLeft = $.getCSSProp(element, 'padding-left'), | |
elementPadRight = $.getCSSProp(element, 'padding-right'); | |
return elementWidth - $.stripUnit(elementPadLeft) - $.stripUnit(elementPadRight); | |
}, | |
offset: function() | |
{ | |
var offset = this[0].getBoundingClientRect(); | |
return { | |
top : offset.top + window.pageYOffset, | |
left: offset.left + window.pageXOffset | |
} | |
}, | |
hide : function() | |
{ | |
this.css('display', 'none'); | |
}, | |
show : function() | |
{ | |
this.css('display', ''); | |
} | |
}) | |
; | |
//-------------------------------------------------------------- | |
// tests | |
//-------------------------------------------------------------- | |
window.$ = $; | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment