assets/js/main.js in another-theme-0.0.1 vs assets/js/main.js in another-theme-1.0.0
- old
+ new
@@ -9,5 +9,46 @@
};
const theme = localStorage.getItem("theme") || "light";
setTheme(theme);
initTheme("light", "dark");
initTheme("dark", "light");
+
+// Exelent little functions to use any time when class modification is needed
+function hasClass(ele, cls) {
+ return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
+}
+
+function addClass(ele, cls) {
+ if (!hasClass(ele, cls)) ele.className += "" + cls;
+}
+
+function removeClass(ele, cls) {
+ if (hasClass(ele, cls)) {
+ var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
+ ele.className = ele.className.replace(reg, '');
+ }
+}
+
+// Add event from js the keep the markup clean
+function init() {
+ let elementsArray = document.querySelectorAll(".menu-toggle");
+ elementsArray.forEach(function(element) {
+ element.addEventListener("click", toggleMenu);
+ });
+}
+
+// The actual function
+function toggleMenu() {
+ var ele = document.getElementsByTagName('body')[0];
+ if (!hasClass(ele, "open")) {
+ addClass(ele, "open");
+ } else {
+ removeClass(ele, "open");
+ }
+}
+
+// Prevent the function to run before the document is loaded
+document.addEventListener('readystatechange', function() {
+ if (document.readyState === "complete") {
+ init();
+ }
+});