Last active
April 29, 2016 06:17
-
-
Save BraynStorm/a4a9f3a4e86141ca8deda3487dd3f21e to your computer and use it in GitHub Desktop.
A very very very crude and malformed and plain dumb version of jquery
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
/** | |
* Created by Braynstorm on 9.4.2016 г.. | |
*/ | |
'use strict'; | |
var debugMode = false; | |
function $ (str) { | |
if (str instanceof HTMLElement) | |
return modifySelectedObject(str); | |
return modifySelectedObject(document.querySelectorAll(str)); | |
} | |
function modifySelectedObject (list) { | |
if(list instanceof HTMLElement || list instanceof HTMLDocument || list instanceof SVGElement) { | |
list.toggleClass = function (className) { | |
this.classList.toggle(className); | |
return this; | |
}; | |
list.addClass = function (className) { | |
this.classList.add(className); | |
return this; | |
}; | |
list.hasClass = function (className) { | |
return this.classList.contains(className); | |
}; | |
list.removeClass = function (className) { | |
this.classList.remove(className); | |
return this; | |
}; | |
list.attr = function (name, val){ | |
if(typeof val === 'undefined'){ | |
return list.getAttribute(name); | |
} | |
list.setAttribute(name, val); | |
return list; | |
}; | |
list.html = function (val){ | |
if(typeof val === 'undefined'){ | |
return list.innerHTML; | |
} | |
list.innerHTML = val; | |
return list; | |
}; | |
list.css = function (rules, val) { | |
for (var i in rules) { | |
list.style[i] = rules[i]; | |
} | |
return this; | |
}; | |
// Performance, yo! | |
list.getStyle = function () { | |
return window.getComputedStyle(this, null); | |
}; | |
list.cacheCurrentStyle = function () { | |
this.cachedStyle = this.getStyle(); | |
return this; | |
}; | |
list.getCachedStyle = function () { | |
return this.cachedStyle; | |
}; | |
//Events | |
list.onMouseDown = function (callback) { | |
list.addEventListener("mousedown", callback); | |
return this; | |
}; | |
list.onMouseUp = function (callback) { | |
list.addEventListener("mouseup", callback); | |
return this; | |
}; | |
list.onMouseOver = function (callback) { | |
list.addEventListener("mouseover", callback); | |
return this; | |
}; | |
list.onMouseOut = function (callback) { | |
list.addEventListener("mouseout", callback); | |
return this; | |
}; | |
list.onMouseMove = function (callback) { | |
list.addEventListener("mousemove", callback); | |
return this; | |
}; | |
list.onContextMenu = function (callback) { | |
list.addEventListener("contextmenu", callback); | |
return this; | |
}; | |
list.onMouseWheel = function (callback) { | |
list.addEventListener("wheel", callback); | |
return this; | |
}; | |
return list; | |
} else if (list.length == 1){ | |
return modifySelectedObject(list[0]); | |
} else { | |
var newList = []; | |
for (var i in list) { | |
if(list[i] instanceof HTMLElement|| list[i] instanceof HTMLDocument || list[i] instanceof SVGElement) | |
newList.push(modifySelectedObject(list[i])); | |
} | |
return newList; | |
} | |
} | |
function $$ (str) { | |
return document.querySelectorAll(str); | |
} | |
function $ready(func){ | |
document.addEventListener("ready", func); | |
} | |
function debug (f) { | |
if(debugMode === true) | |
console.log(f); | |
} | |
function $create (tagName, attributes) { | |
var element = document.createElement(tagName); | |
for (var i in attributes) | |
element.attr(i, attributes[i]); | |
return modifySelectedObject([element]); | |
} | |
function $createDiv (attributes) { | |
return $create("div", attributes); | |
} | |
class Animation { | |
constructor (element, property, endValue, time, callback) { | |
if (typeof BoxObject !== "undefined" && element instanceof BoxObject) | |
this.element = element.obj; | |
else | |
this.element = element; | |
this.property = property; | |
this.totalTime = time; | |
this.passedTime = 0; | |
this.suffix = ""; | |
this.callback = callback; | |
this.isRunning = false; | |
this.startValue = this.element.getStyle()[property]; | |
if (typeof this.startValue === "string") { | |
this.startValue = Utils.stripPx(this.startValue); | |
this.suffix = "px"; | |
} | |
if (typeof endValue === "string") { | |
this.endValue = Utils.stripPx(endValue); | |
this.suffix = "px"; | |
} else | |
this.endValue = endValue; | |
this.currentValue = this.startValue; | |
this.operationsPerMs = (this.endValue - this.startValue) / this.totalTime; | |
} | |
go () { | |
if (!this.isRunning) | |
if (this.alreadyWent) | |
this.goBackwards(); | |
else | |
this.goForwards(); | |
} | |
goForwards () { | |
if (this.passedTime < this.totalTime) {// a.k.a Epsilon | |
this.alreadyWent = true; | |
this.isRunning = true; | |
setTimeout(() => { | |
this.passedTime += 10; | |
this.currentValue += this.operationsPerMs * 10; | |
this.element.style[this.property] = Math.round(this.currentValue) + this.suffix; | |
this.goForwards(); | |
}, 10); | |
} else { | |
this.isRunning = false; | |
if (this.callback !== undefined && typeof this.callback === "function") | |
this.callback(); | |
} | |
} | |
goBackwards () { | |
var swap = this.startValue; | |
this.startValue = this.endValue; | |
this.endValue = swap; | |
this.passedTime = 0; | |
this.currentValue = this.startValue; | |
this.operationsPerMs = -this.operationsPerMs; | |
this.alreadyWent = false; | |
this.goForwards(); | |
} | |
} | |
class Utils { | |
/** | |
* 25px => 25. | |
* @param str | |
* @returns {Number} | |
*/ | |
static stripPx (str) { | |
return parseInt(str.substring(0, str.indexOf("p"))); | |
} | |
static sqr (n) { | |
return n * n; | |
} | |
/**@param {Number} val | |
* @param {Number} min | |
* @param {Number} max | |
* @return {Number} */ | |
static clamp (val, min, max) { | |
return val > max ? max : (val < min ? min : val); | |
} | |
static arrayFromIterator(it){ | |
var next; | |
var arr = []; | |
while(true){ | |
next = it.next(); | |
if(next.done === true) | |
break; | |
arr.push(next.value); | |
} | |
return arr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment