-
-
Save panayotoff/313d2af717be23d327dd69c71e4c6672 to your computer and use it in GitHub Desktop.
X.js
This file contains hidden or 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
//-------------------------------------------------------------- | |
// NXT.js | |
// jQuery-like micro-lib for creating banners | |
// ico[at]next-dc[dot]com | |
//-------------------------------------------------------------- | |
;(function (window, document, undefined) { | |
'use strict'; | |
var $ = function (expression) { | |
if ($.isFunction(expression)) { | |
document.addEventListener('DOMContentLoaded', expression); | |
} else { | |
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($, { | |
VERSION: '0.1.0', | |
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; | |
}, | |
isFunction: function (obj) { | |
return typeof obj == 'function' || 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 (callback) { | |
return function () { | |
var elements = []; | |
for (var i = 0, j = this.length; i < j; i++) { | |
var result = callback.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, null).getPropertyValue(cssProp); | |
}, | |
stripUnit: function (unit) { | |
return parseInt(unit, 10); | |
}, | |
classRegex: function (className) { | |
return new RegExp("(^|\\s+)" + className + "(\\s+|$)"); | |
}, | |
supports: { | |
classList: !!( 'classList' in document.documentElement) | |
} | |
}); | |
//-------------------------------------------------------------- | |
// | |
//-------------------------------------------------------------- | |
$.extend($.prototype, { | |
forEach: function (func) { | |
for (var i = 0; i < this.length; i++) { | |
func.call(this[i], this[i], i); | |
} | |
return this; | |
}, | |
on: function (event, callback) { | |
return this.forEach(function (element) { | |
element.addEventListener(event, callback); | |
}); | |
}, | |
off: function (event, callback) { | |
return this.forEach(function (element) { | |
element.removeEventListener(event, callback); | |
}); | |
}, | |
trigger: function (event, data) { | |
return this.forEach(function (element) { | |
element.dispatchEvent(new CustomEvent(event, {detail: data || {}})) | |
}); | |
}, | |
html: function (newHtml) { | |
if (arguments.length) { | |
return this.forEach(function (element) { | |
element.innerHTML = newHtml; | |
}); | |
} | |
return this[0].innerHTML; | |
}, | |
val: function (val) { | |
if (arguments.length) { | |
return this.forEach(function (element) { | |
element.value = val; | |
}); | |
} | |
return this[0].value; | |
}, | |
text: function (text) { | |
if (arguments.length) { | |
return this.forEach(function (element) { | |
element.innerHTML = ''; | |
element.appendChild(document.createTextNode(text)); | |
}); | |
} | |
return this[0] && $.getText(this[0]); | |
}, | |
eq: function (index) { | |
return $([this[index]]); | |
}, | |
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) { | |
return this.forEach(function (element) { | |
element.setAttribute(attrName, attrVal); | |
}); | |
} | |
return this[0] && this[0].getAttribute(attrName); | |
}, | |
css: function (cssProp, cssVal) { | |
if (arguments.length > 1) { | |
return this.forEach(function (element) { | |
element.style[cssProp] = cssVal; | |
}); | |
} | |
return this[0] && $.getCSSProp(this[0], cssProp); | |
}, | |
hasClass: function (className) { | |
for (var i = 0; i < this.length; i++) { | |
var classFound = $.supports.classList ? this[i].classList.contains(className) : $.classRegex(className).test(this[i].className); | |
if (classFound) { | |
return true; | |
} | |
} | |
return false; | |
}, | |
addClass: function (className) { | |
return this.forEach(function (element) { | |
$.supports.classList ? element.classList.add(className) : element.className + ' ' + className; | |
}); | |
}, | |
removeClass: function (className) { | |
return this.forEach(function (element) { | |
$.supports.classList ? element.classList.remove(className) : element.className.replace($.classRegex(className), ' '); | |
}); | |
}, | |
delayedCallbacks: [], | |
delay: function (milliseconds, callback) { | |
if ($.isFunction(callback)) { | |
var funcDelay = setTimeout($.proxy(callback, this), milliseconds); | |
this.delayedCallbacks.push(funcDelay); | |
} | |
return this; | |
}, | |
clearDelays: function () { | |
$.each(this.delayedCallbacks, function (index, timeout) { | |
window.clearTimeout(timeout); | |
}); | |
return this; | |
} | |
}); | |
window.$ = $; | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment