Skip to content

Commit

Permalink
refactor(core): Use built-in JS methods
Browse files Browse the repository at this point in the history
This also removes some usage of jQuery in favor of pure CSS alternatives
  • Loading branch information
zooley committed Apr 10, 2024
1 parent 9dae080 commit a2f2521
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 270 deletions.
75 changes: 17 additions & 58 deletions app/Modules/Core/Resources/assets/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,6 @@ if (typeof(Halcyon) === 'undefined') {
var Halcyon = {};
}

/**
* Check if an element has the specified class name
*
* @param el The element to test
* @param className The class to test for
* @return bool
*/
Halcyon.hasClass = function (el, className) {
return el.classList ? el.classList.contains(className) : new RegExp('\\b' + className + '\\b').test(el.className);
}

/**
* Add a class to an element
*
* @param el The element to add the class to
* @param className The class to add
* @return bool
*/
Halcyon.addClass = function (el, className) {
if (el.classList) {
el.classList.add(className);
} else if (!Halcyon.hasClass(el, className)) {
el.className += ' ' + className;
}
}

/**
* Remove a class from an element
*
* @param el The element to remove the class from
* @param className The class to remove
* @return bool
*/
Halcyon.removeClass = function (el, className) {
if (el.classList) {
el.classList.remove(className);
} else {
el.className = el.className.replace(new RegExp('\\b' + className + '\\b', 'g'), '');
}
}

/**
* Attach event handlers
*/
Expand All @@ -56,10 +15,10 @@ document.addEventListener('DOMContentLoaded', function () {
summary[i].addEventListener('click', function (e) {
e.preventDefault();

if (Halcyon.hasClass(this.parentNode, 'is-open')) {
Halcyon.removeClass(this.parentNode, 'is-open');
if (this.parentNode.classList.contains('is-open')) {
this.parentNode.classList.remove('is-open');
} else {
Halcyon.addClass(this.parentNode, 'is-open');
this.parentNode.classList.add('is-open');
}
});
}
Expand All @@ -70,32 +29,32 @@ document.addEventListener('DOMContentLoaded', function () {
nodes[i].addEventListener('click', function (e) {
e.preventDefault();

if (Halcyon.hasClass(this.parentNode, 'active')) {
Halcyon.removeClass(this.parentNode, 'active');
if (this.parentNode.classList.contains('active')) {
this.parentNode.classList.remove('active');
} else {
// De-activate any other menu items and sections
var nds = document.getElementsByClassName('node');
for (i = 0; i < nds.length; i++) {
Halcyon.removeClass(nds[i].parentNode, 'active');
nds[i].parentNode.classList.remove('active');
}
var eps = document.getElementsByClassName('docs-collection');
for (i = 0; i < eps.length; i++) {
Halcyon.addClass(eps[i], 'hide');
eps[i].classList.add('hide');
}

Halcyon.addClass(this.parentNode, 'active');
this.parentNode.classList.add('active');

// Set URL hash
window.location.hash = this.getAttribute('href');

// Show the targetted section
var section = document.querySelector(this.getAttribute('href'));
Halcyon.removeClass(section, 'hide');
section.classList.add('hide');

// Close the menu if open (mobile)
var menu = this.parentNode.parentNode;
if (Halcyon.hasClass(menu, 'active')) {
Halcyon.removeClass(menu, 'active');
if (menu.classList.contains('active')) {
menu.classList.remove('active');
}
}
});
Expand All @@ -109,10 +68,10 @@ document.addEventListener('DOMContentLoaded', function () {

var menu = document.querySelector(this.getAttribute('href'));

if (Halcyon.hasClass(menu, 'active')) {
Halcyon.removeClass(menu, 'active');
if (menu.classList.contains('active')) {
menu.classList.remove('active');
} else {
Halcyon.addClass(menu, 'active');
menu.classList.add('active');
}
});
}
Expand All @@ -128,15 +87,15 @@ document.addEventListener('DOMContentLoaded', function () {
if (section) {
var eps = document.getElementsByClassName('docs-collection');
for (i = 0; i < eps.length; i++) {
Halcyon.addClass(eps[i], 'hide');
eps[i].classList.add('hide');
}
Halcyon.removeClass(section, 'hide');
section.classList.remove('hide');
}

var nds = document.getElementsByClassName('node');
for (i = 0; i < nds.length; i++) {
if (nds[i].getAttribute('href') == hash) {
Halcyon.addClass(nds[i].parentNode, 'active');
nds[i].parentNode.classList.add('active');
break;
}
}
Expand Down
Loading

0 comments on commit a2f2521

Please sign in to comment.