Skip to content

Instantly share code, notes, and snippets.

@mi3tek-amb
Last active August 29, 2015 13:57
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 mi3tek-amb/9366054 to your computer and use it in GitHub Desktop.
Save mi3tek-amb/9366054 to your computer and use it in GitHub Desktop.
//javaScript Library
;
(function (window, undefined) {
//initalize
var Document = document,
Window = window,
Query = function (selector, context) {
return new Query.fn.init(selector, context);
};
Query.extend = function extend(A, B) {
for (var key in A) {
if (!B[key]) {
B[key] = A[key];
}
};
};
//core
Query.extend({
'id': function (a, b) {
if (a && typeof a === 'string') {
var c = b.getElementById(a);
return c !== null ? c : null
} else {
return null
}
},
'className': function (a, b) {
if (a && typeof a === 'string') {
var c = b.getElementsByClassName(a);
return c !== null ? c : null
} else {
return null
}
},
'tagName': function (a, b) {
if (a && typeof a === 'string') {
var c = b.getElementsByTagName(a);
return c !== null ? c : null
} else {
return null
}
},
'createHTML': function (a, b) {
var c = k.createElement('body');
c.innerHTML = a;
var d = c.childNodes;
if (b) {
for (var e in b) {
if (e == 'style') {
var f = b[e];
for (e in f) {
d[0].style[e] = f[e]
}
};
d[0][e] = b[e]
}
};
return Query(d);
c = void 0
}
}, Query);
//methods
Query.fn = Query.prototype = {
//initalize
'init': function (selector, context) {
if (selector && typeof selector === 'string') {
if (/\<("[^"]*"|'[^']*'|[^'">])*>/g.test(selector)) {
return this.createHTML(selector, context);
} else {
var context = context ? context : Document,
elems = [],
self = this,
selector_match = selector.match(/[\.#](.*)/);
if (Document.querySelectorAll) {
elems = context.querySelectorAll(selector);
} else {
if (/#/.test(selector)) {
elems[0] = self.id(selector_match[1], Document);
} else if (/\./.test(selector)) {
elems = self.className(selector_match[1], context);
} else {
elems = self.tagName(selector, context);
}
};
for (var i = 0; i < elems.length; i++) {
self[i] = elems[i];
};
self.context = context,
self.length = elems.length,
self.selector = selector;
elms = void 0;
};
} else if (selector && typeof selector === 'object') {
var self = this;
if (!selector.length) {
self[0] = selector;
self.length = 1;
} else {
for (var i = 0; i < selector.length; i++) {
self[i] = selector[i];
};
self.length = selector.length;
}
self.context = Document;
}
return self;
},
'find': function find(selector) {
var selector_match = selector.match(/[\.#](.*)/),
self = this,
elms, j = 0
,
object = new Query.init();
this.each(function () {
var context = this;
if (Document.querySelectorAll) {
elems = context.querySelectorAll(selector);
for (var i = 0; i < elms.length; i++, j++) {
object[j] = elms[i];
}
} else {
if (/#/.test(selector)) {
elms = self.id(selector_match[1], Document);
} else if (/\./.test(selector)) {
elms = self.className(selector_match[1], context);
} else {
elms = self.tagName(selector, context);
};
for (var i = 0; i < elms.length; i++, j++) {
object[j] = elms[i];
}
}
});
elms = void 0;
object.length = j, object.selector = selector, object.prevObject = context;
return object;
},
'eq': function eq(intSub) {
var self = this,
elm = self[intSub],
object = new Query.init();
object[0] = elm, object.context = self.context, object.length = 1, object.selector = self.selector, object.prevObject = self;
return object;
},
'end': function end() {
return this.prevObject;
},
'each': function each(a) {
var b = this,
i = 0,
elem;
for (;
(elem = b[i]) != null; i++) {
a.call(elem)
};
return this;
},
'fragment': function fragment(elms) {
var fragment = Document.createDocumentFragment(),
i, j;
for (j = 0; j < elms.length; j++) {
var elm = elms[j];
fragment.appendChild(elm[0])
}
return fragment;
},
'domSort': function (elems, callback) {
var array = [],
self = this;
for (var i = 0; i < elems.length; i++) {
array[i] = new this.init(elems[i])
};
if (callback) {
this.each(function () {
callback.call(this, self.fragment(array))
})
} else {
return array
}
},
'append': function append() {
return this.domSort(arguments, function (e) {
this.appendChild(e)
})
},
'prepend': function prepend() {
var self = this,
elems = arguments;
this.domSort(arguments, function (e) {
this.insertBefore(e, this.childNodes[0])
})
},
'before': function before() {
var a = this;
this.domSort(arguments, function (e) {
this.parentNode.insertBefore(e, this)
})
},
'after': function after() {
var a = this;
this.domSort(arguments, function (e) {
this.parentNode.insertBefore(e, this.nextSibling)
})
},
'remove': function remove() {
return this.each(function () {
this.parentNode.removeChild(this);
})
},
'empty': function empty() {
return this.each(function () {
this.innerHTML = ''
})
},
'next': function next() {
var self = this,
nextElm = self.nextSibling;
while (nextElm && nextElm.nodeType != 1) {
nextElm = nextElm.nextSibling
};
return nextElm ? nextElm : null;
},
'prev': function next() {
var self = this,
prevElm = self.prevSibling;
while (prevElm && prevElm.nodeType != 1) {
prevElm = nextElm.prevSibling
};
return prevElm ? prevElm : null;
},
'innerText': function innerText() {
return this[0].innerText || this[0].textContent
},
'style': function style(attr) {
return this.each(function () {
for (key in attr) {
this.style[key] = attr[key]
}
})
},
'data': function detaset(name, value) {
var self = this[0];
if (value != h) {
if (!self.dataset) {
self.dataset = []
};
c.dataset[name] = '' + value;
return self
} else {
var value = self.dataset ? self.dataset[name] : self.getAttribute('data-' + name);
if (/^[0-9]+$/.test(value)) {
value = +value
}
return value
}
},
'on': function on(b, c, d) {
var c = c ? c : null,
d = d ? d : b + 'Event';
return this.each(function () {
var a = this;
if (a.addEventListener) {
a.addEventListener(b, function (e) {
e.stopPropagation();
a[d] = arguments.callee;
if (!c.call(e.currentTarget, e)) {
e.preventDefault()
}
}, false)
} else {
a.attachEvent(b, function (e) {
e.stopPropagation();
a[d] = arguments.callee;
if (!c.call(e.currentTarget, e)) {
e.preventDefault()
}
})
}
})
},
'off': function off(b, c) {
var c = c ? c : b + 'Event';
return this.each(function () {
var a = this;
if (a.removeEventListener) {
a.removeEventListener(b, a[c])
} else {
a.detachEvent(b, a[c])
}
delete a[c]
})
},
'bind': function bind(a, b, c, f) {
var c = c ? c : null,
f = f ? f : b + 'Event',
selector_match = b.match(/[\.#](.*)/)[1];
return this.each(function () {
var d = this;
a[f] = arguments.callee;
if (d.addEventListener) {
d.addEventListener(a, function (e) {
if (e.currentTarget.className.match(selector_match) || e.currentTarget.id.match(selector_match)) {
if (!c.call(e.currentTarget, e)) {
e.preventDefault();
e.stopPropagation();
}
}
}, false)
} else {
d.attachEvent(b, function (e) {
if (e.currentTarget.className.match(selector_match) || e.currentTarget.id.match(selector_match)) {
if (!c.call(e.currentTarget, e)) {
e.preventDefault();
e.stopPropagation();
}
}
})
}
})
},
'unbind': function unbind(b, c) {
var c = c ? c : b + 'Event';
return this.each(function () {
var a = this;
if (a.removeEventListener) {
a.removeEventListener(b, a[c])
} else {
a.detachEvent(b, a[c])
}
delete a[c]
})
},
'fadeIn': function fadeIn(callback) {
return this.each(function () {
var self = this,
value = 0,
style = self.style,
c = setInterval(function () {
if (value >= 1) {
clearInterval(c);
style.display = 'block';
style.opacity = 1;
style.filter = 100;
if (callback) {
callback.call(b)
}
}
style.opacity = value;
style.filter = 'alpha(opacity=' + value * 100 + ")";
value += 0.1
}, 20)
})
},
'fadeOut': function fadeOut(callback) {
return this.each(function () {
var self = this,
value = 1,
style = self.style,
c = setInterval(function () {
if (value <= 0.1) {
clearInterval(c);
style.display = 'none';
style.opacity = 0;
style.filter = 0;
if (callback) {
callback.call(b)
}
}
style.opacity = value;
style.filter = 'alpha(opacity=' + value * 100 + ")";
value -= 0.1
}, 20);
})
},
'hide': function hide(i) {
var self = this.childNodes;
self.each(function () {
if (this.style) {
this.fadeOut(function () {
this.style.visibility = "hidden"
})
}
});
return this;
},
'visible': function visible(i) {
var self = this.childNodes;
self.each(function () {
if (this.style) {
this.style.visibility = "visible";
this.fadeIn()
}
});
return this;
},
'animate': function animate(attributes, duration, callback, easing) {
var self = this,
duration = duration ? duration : 700,
easing = easing ? easing : 'easeInOutQuad';
return self.each(function () {
var _this = this,
i = 0,
style = this.style,
default_status, value_reg, e, time, on_value;
for (var key in attributes) {
on_value = attributes[key];
if (key === 'scrollTop') {
self.scrollTop(on_value, duration, callback, easing);
} else {
if (style[key]) {
default_status = style[key];
} else if (document.defaultView.getComputedStyle) {
default_status = document.defaultView.getComputedStyle(this, null)[key];
} else {
default_status = _this.currentStyle[key];
};
value_ex = default_status.match(/[a-z]+/g), default_status = parseInt(default_status.match(/[0-9]+/g))
}
change_in_value = on_value - default_status;
self.animation = setInterval(function () {
time = 20 * i
style[key] = (Query.fn.easing[easing](time, default_status, change_in_value, duration)) + value_ex;
if (time >= duration) {
clearInterval(_this.animation);
style[key] = on_value + value_ex;
if (callback) {
callback.call(_this);
};
delete _this.animation;
}
i = i + 1
}, 20)
}
})
},
'scrollTop': function (on_value, duration, callback, easing) {
var duration = duration ? duration : 700,
easing = easing ? easing : 'easeInOutQuart';
return this.each(function () {
var _this = this,
i = 0,
value = _this.scrollTop,
change_in_value = on_value - value;
_this.animation = setInterval(function () {
if (20 * i >= duration) {
clearInterval(_this.animation);
scrollTop = on_value;
if (callback) {
callback.call(self)
};
delete _this.animation;
}
_this.scrollTop = Query.fn.easing[easing](20 * i, value, change_in_value, duration);
i = i + 1;
}, 20)
})
},
sort: [].sort,
splice: [].splice
};
Query.fn.easing = {
easeInOutQuad: function (t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t + b;
t--;
return -c / 2 * (t * (t - 2) - 1) + b;
},
easeInOutCubic: function (t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t * t + b;
t -= 2;
return c / 2 * (t * t * t + 2) + b;
},
easeInOutQuart: function (t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * t * t * t * t + b;
t -= 2;
return -c / 2 * (t * t * t * t - 2) + b;
},
easeInOutSine: function (t, b, c, d) {
return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
},
easeInOutExpo: function (t, b, c, d) {
t /= d / 2;
if (t < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
t--;
return c / 2 * (-Math.pow(2, -10 * t) + 2) + b;
}
};
Query.fn.init.prototype = Query.fn;
Window.Query = Window.Q = Query;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment