Update main.js to new jStuff prototype

This commit is contained in:
Ilja Kartašov 2019-04-26 13:01:22 +02:00
parent 97cc087e23
commit 372ef5d3b8
1 changed files with 119 additions and 21 deletions

View File

@ -1,3 +1,109 @@
'use strict';
// jStuff
function jStuff(query)
{
this.nodes = null;
if (typeof query === 'string' || query instanceof String) {
this.nodes = document.querySelectorAll(query);
} else {
this.nodes = [ query ];
}
this.all = function(func, arg) {
var i, result;
for (i = 0; i < this.nodes.length; i++) {
result = func(this.nodes[i], arg);
}
return result;
};
this.attribute = function(name, value) {
return this.all(function(node, attr) {
if (attr.v)
node.setAttribute(attr.k, attr.v);
return node.getAttribute(attr.k);
}, {k: name, v: value});
};
this.onClick = function(handler) {
this.all(function(n, f) {n.addEventListener('click', f, false)}, handler);
return this;
};
this.addClass = function(className) {
this.all(function(node, cn) {
if (node.classList)
node.classList.add(className);
}, className);
return this;
}
this.removeClass = function(className) {
this.all(function(node, cn) {
if (node.classList)
node.classList.remove(className);
}, className);
return this;
}
this.replaceClass = function(oldClass, newClass) {
this.all(function(node, obj) {
if (node.classList) {
node.classList.remove(obj.ocl);
node.classList.add(obj.ncl);
}
}, {ocl: oldClass, ncl: newClass});
return this;
}
this.switchClass = function(className) {
this.all(function(node, obj) {
if (node.classList) {
node.classList.toggle(className);
}
}, className);
return this;
}
this.hasClass = function(className) {
return this.all(function(node, cn) {
if (node.classList) {
return node.classList.contains(cn);
} else {
return (node.className.indexOf(cn) != -1);
}
}, className);
}
}
jStuff.create = function(tagName, childNode) {
var n = document.createElement(tagName);
if (childNode)
n.appendChild(childNode);
return new jStuff(n);
}
jStuff.createLabel = function(text) {
return new jStuff(document.createTextNode(text));
}
jStuff.main = function(func) {
if (window.addEventListener) {
addEventListener("load", func, false);
} else if (window.attachEvent) {
attachEvent("onload", func);
} else {
onload = func;
}
}
window.$ = jStuff;
// SVG fix
/*! code below is based on svg4everybody v2.1.9
* github.com/jonathantneal/svg4everybody */
@ -234,6 +340,9 @@ function getSVGAncestor(node) {
return svg;
}
// Main
function main()
{
/*
@ -278,27 +387,16 @@ function main()
});
*/
document.getElementById('menu__switch').addEventListener('click', function(event) {
var menu = document.getElementById('menu'),
targ = event.currentTarget;
if (menu.className=='menu')
{
menu.className='menu menu--visible';
targ.className='menu__switch menu__switch--opened';
}
else
{
menu.className='menu';
targ.className='menu__switch';
}
});
svg4everybody();
}
window.addEventListener ?
addEventListener("load", main, false) :
window.attachEvent ? attachEvent("onload", main) : (onload = main);
$.main(function(){
var sw = $('#menu__switch').onClick(function(evt) {
var menu = $('#menu'),
targ = $(evt.currentTarget);
menu.switchClass('menu--visible');
targ.switchClass('menu__switch--opened');
});
svg4everybody();
});