Skip to content

Commit

Permalink
use ES6 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stenders committed Apr 8, 2019
1 parent 3ea372a commit 01a1c96
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions src/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,20 @@ const $ = {};
* @public
*/
$.one = function(selector, contextElement) {
if (contextElement) {
return contextElement.querySelector(selector);
}
return document.querySelector(selector);
return (contextElement || document).querySelector(selector)
}

/**
* get multiple elements
* @public
*/
$.all = function(selector, contextElement) {
let nodeList,
list = [];
if (contextElement) {
nodeList = contextElement.querySelectorAll(selector);
} else {
nodeList = document.querySelectorAll(selector);
}
if (nodeList && nodeList.length > 0) {
list = Array.prototype.slice.call(nodeList);
}
return list;
const nodeList = (contextElement || document).querySelectorAll(selector)
return Array.from(nodeList)
}

/**
* add className to an element
* add className(s) to an or multiple element(s)
* @public
*/
$.addClass = function($el, className) {
Expand All @@ -59,18 +47,18 @@ $.addClass = function($el, className) {
$el = [$el];
}
for (let i=0; i<$el.length; i++) {
let name = $el[i].className || '',
arr = name.split(' ');
if (arr.indexOf(className) > -1) {
continue;
if (isArray(className)) {
className.forEach(name => $el[0].classList.add(name));
} else if (className.split(' ')[1]) {
className.split(' ').forEach(name => $el[0].classList.add(name))
} else {
$el[0].classList.add(className);
}
arr.push(className);
$el[i].className = arr.join(' ');
}
}

/**
* remove className from an element
* remove classNames from an or multiple element(s)
* @public
*/
$.removeClass = function($el, className) {
Expand All @@ -81,13 +69,13 @@ $.removeClass = function($el, className) {
$el = [$el];
}
for (let i=0; i<$el.length; i++) {
let arr = $el[i].className.split(' ');
for (let j=0; j<arr.length; j++) {
if (arr[j] == className) {
arr[j] = '';
}
if (isArray(className)) {
className.forEach(name => $el[0].classList.remove(name));
}} else if (className.split(' ')[1]) {
className.split(' ').forEach(name => $el[0].classList.remove(name))
} else {
$el[0].classList.remove(className);
}
$el[i].className = arr.join(' ').trim();
}
}

Expand All @@ -99,13 +87,7 @@ $.hasClass = function($el, className) {
if (!$el) {
return false;
}
let arr = $el.className.split(' ');
for (let i=0; i<arr.length; i++) {
if (arr[i] == className) {
return true;
}
}
return false;
return $el.classList.contains(className)
}

/**
Expand All @@ -120,15 +102,10 @@ $.bind = function($el, eventType, fn, useCapture) {
if (!$el) {
return;
}
if (useCapture === undefined) {
useCapture = false;
}
if (!isArray($el)) {
$el = [$el];
}
for (let i=0; i<$el.length; i++) {
$el[i].addEventListener(eventType, fn, useCapture);
}
$el.forEach(el => el.addEventListener(eventType, fn, !!useCapture))
}

/**
Expand Down

0 comments on commit 01a1c96

Please sign in to comment.