Skip to content

Commit

Permalink
add reset method & fix init bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xelzs committed Apr 18, 2020
1 parent 2c555a9 commit 5e818df
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/js/core.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const NeuAxentix = (() => {
const neuClickElements = document.querySelectorAll('[data-neu-click]');
const neuFocusElements = document.querySelectorAll('[data-neu-focus]');
const neuClickedElements = document.querySelectorAll('[data-neu-clicked]');
const neuHoverElements = document.querySelectorAll('[data-neu-hover]');
let neuClickElements, neuFocusElements, neuClickedElements, neuHoverElements;
const stateList = ['neu-bordered', 'neu-flat', 'neu-pressed', 'neu-concave', 'neu-convex'];

/**
Expand Down Expand Up @@ -69,6 +66,11 @@ const NeuAxentix = (() => {
* Setup neu-click elements
*/
function setup() {
neuClickElements = document.querySelectorAll('[data-neu-click]');
neuFocusElements = document.querySelectorAll('[data-neu-focus]');
neuClickedElements = document.querySelectorAll('[data-neu-clicked]');
neuHoverElements = document.querySelectorAll('[data-neu-hover]');

neuClickedElements.forEach((el) => {
_setupClasses(el);
el.toggleRef = _toggle.bind(el, 'neuClicked');
Expand Down Expand Up @@ -107,9 +109,54 @@ const NeuAxentix = (() => {
});
}

/**
* Reset all listeners & setup
*/
function reset() {
if (neuClickedElements) {
neuClickedElements.forEach((el) => {
el.removeEventListener('click', el.toggleRef);
el.toggleRef = undefined;
});
}

if (neuClickElements) {
neuClickElements.forEach((el) => {
el.removeEventListener('mousedown', el.toggleRef);
el.removeEventListener('mouseleave', el.toggleRef);
el.removeEventListener('mouseup', el.toggleRef);

if ('ontouchstart' in document.documentElement) {
el.removeEventListener('touchstart', el.toggleRef);
el.removeEventListener('touchend', el.toggleRef);
}
el.toggleRef = undefined;
});
}

if (neuFocusElements) {
neuFocusElements.forEach((el) => {
el.removeEventListener('focus', el.toggleRef);
el.removeEventListener('blur', el.toggleRef);
el.toggleRef = undefined;
});
}

if (neuHoverElements) {
neuHoverElements.forEach((el) => {
el.removeEventListener('mouseenter', el.toggleRef);
el.removeEventListener('mouseleave', el.toggleRef);
el.toggleRef = undefined;
});
}

setup();
}

return {
setup,
reset,
};
})();

NeuAxentix.setup();
document.addEventListener('DOMContentLoaded', NeuAxentix.setup);

0 comments on commit 5e818df

Please sign in to comment.