From 372ef5d3b8ee73c3a40e8f8b7bb07204fd128707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilja=20Karta=C5=A1ov?= Date: Fri, 26 Apr 2019 13:01:22 +0200 Subject: [PATCH] Update main.js to new jStuff prototype --- static/main.js | 140 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 21 deletions(-) diff --git a/static/main.js b/static/main.js index a939c81..4bffd36 100644 --- a/static/main.js +++ b/static/main.js @@ -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(); +}); +