2017-04-19 21:19:33 +02:00
|
|
|
//General helper functions
|
2018-07-04 06:33:55 +02:00
|
|
|
import {isString} from "./helpers/isString";
|
2018-07-05 06:38:00 +02:00
|
|
|
import { createElement } from "./uiHelpers/createElement";
|
2017-04-19 21:19:33 +02:00
|
|
|
|
|
|
|
//Returns the size (number of keys) of an object
|
|
|
|
function sizeOfObject(obj) {
|
|
|
|
var size = 0, key;
|
|
|
|
for (key in obj) {
|
|
|
|
if (obj.hasOwnProperty(key)) size++;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-05-02 19:38:11 +02:00
|
|
|
function clearObject(obj) {
|
|
|
|
for (var key in obj) {
|
|
|
|
if (obj.hasOwnProperty(key)) {
|
|
|
|
delete obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 23:27:35 +02:00
|
|
|
//Given an element by its Id(usually an 'a' element), removes all event listeners
|
|
|
|
//from that element by cloning and replacing. Then returns the new cloned element
|
|
|
|
function clearEventListeners(elemId) {
|
|
|
|
var elem = document.getElementById(elemId);
|
|
|
|
if (elem == null) {console.log("ERR: Could not find element for: " + elemId); return null;}
|
|
|
|
var newElem = elem.cloneNode(true);
|
|
|
|
elem.parentNode.replaceChild(newElem, elem);
|
2017-05-06 08:24:01 +02:00
|
|
|
return newElem;
|
2017-06-02 06:15:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 23:27:31 +02:00
|
|
|
//Same as clearEventListeners except it takes a DOM element object rather than an ID
|
|
|
|
function clearEventListenersEl(el) {
|
|
|
|
if (el == null) {console.log("ERR: element passed into clearEventListenersEl is null"); return null;}
|
|
|
|
var newElem = el.cloneNode(true);
|
|
|
|
el.parentNode.replaceChild(newElem, el);
|
|
|
|
return newElem;
|
|
|
|
}
|
|
|
|
|
2018-02-09 22:11:43 +01:00
|
|
|
function removeElement(elem) {
|
2018-05-02 19:38:11 +02:00
|
|
|
if (elem == null || !(elem instanceof Element)) {return;}
|
2018-02-09 22:11:43 +01:00
|
|
|
while(elem.firstChild) {elem.removeChild(elem.firstChild);}
|
|
|
|
elem.parentNode.removeChild(elem);
|
|
|
|
}
|
|
|
|
|
2017-12-02 04:50:08 +01:00
|
|
|
function removeChildrenFromElement(el) {
|
|
|
|
if (isString(el)) {
|
|
|
|
el = document.getElementById(el);
|
|
|
|
}
|
|
|
|
if (el == null) {return;}
|
|
|
|
if (el instanceof Element) {
|
|
|
|
while(el.firstChild) {
|
|
|
|
el.removeChild(el.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Creates both the header and panel element of an accordion and sets the click handler
|
|
|
|
function createAccordionElement(params) {
|
|
|
|
var li = document.createElement("li"),
|
|
|
|
hdr = document.createElement("button"),
|
|
|
|
panel = document.createElement("div");
|
|
|
|
hdr.classList.add("accordion-header");
|
|
|
|
panel.classList.add("accordion-panel");
|
|
|
|
|
|
|
|
if (params.id) {
|
|
|
|
hdr.id = params.id + "-hdr";
|
|
|
|
panel.id = params.id + "-panel";
|
|
|
|
}
|
|
|
|
if (params.hdrText) {hdr.innerHTML = params.hdrText;}
|
|
|
|
if (params.panelText) {panel.innerHTML = params.panelText;}
|
|
|
|
li.appendChild(hdr);
|
|
|
|
li.appendChild(panel);
|
|
|
|
//Click handler
|
|
|
|
hdr.onclick = function() {
|
|
|
|
this.classList.toggle("active");
|
|
|
|
var tmpPanel = this.nextElementSibling;
|
|
|
|
if (tmpPanel.style.display === "block") {
|
|
|
|
tmpPanel.style.display = "none";
|
|
|
|
} else {
|
|
|
|
tmpPanel.style.display = "block";
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 22:11:43 +01:00
|
|
|
return [li, hdr, panel];
|
2017-12-02 04:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearSelector(selector) {
|
|
|
|
for (var i = selector.options.length - 1; i >= 0; --i) {
|
|
|
|
selector.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
//Returns true if all elements are equal, and false otherwise
|
|
|
|
//Assumes both arguments are arrays and that there are no nested arrays
|
|
|
|
function compareArrays(a1, a2) {
|
|
|
|
if (a1.length != a2.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
for (var i = 0; i < a1.length; ++i) {
|
|
|
|
if (a1[i] != a2[i]) {return false;}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-03 05:35:12 +02:00
|
|
|
export {sizeOfObject,
|
|
|
|
clearObject,
|
|
|
|
clearEventListeners,
|
|
|
|
compareArrays,
|
|
|
|
clearEventListenersEl,
|
|
|
|
removeElement,
|
|
|
|
createAccordionElement,
|
|
|
|
removeChildrenFromElement,
|
2018-07-05 19:23:12 +02:00
|
|
|
clearSelector};
|