2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-06-14 23:46:18 +02:00
|
|
|
// DOM helper functions
|
|
|
|
|
2019-06-15 17:50:54 +02:00
|
|
|
export function isChildren(children) {
|
2019-06-16 10:52:02 +02:00
|
|
|
// children should be an not-object (that's the attributes), or a domnode, or an array
|
2019-06-15 17:50:54 +02:00
|
|
|
return typeof children !== "object" || !!children.nodeType || Array.isArray(children);
|
|
|
|
}
|
|
|
|
|
2019-06-16 15:12:54 +02:00
|
|
|
export function classNames(obj, value) {
|
|
|
|
return Object.entries(obj).reduce((cn, [name, enabled]) => {
|
|
|
|
if (typeof enabled === "function") {
|
|
|
|
enabled = enabled(value);
|
|
|
|
}
|
|
|
|
if (enabled) {
|
|
|
|
return cn + (cn.length ? " " : "") + name;
|
|
|
|
} else {
|
|
|
|
return cn;
|
|
|
|
}
|
|
|
|
}, "");
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:48:16 +01:00
|
|
|
export function setAttribute(el, name, value) {
|
2019-02-27 23:20:29 +01:00
|
|
|
if (name === "className") {
|
|
|
|
name = "class";
|
|
|
|
}
|
2019-06-14 22:40:18 +02:00
|
|
|
if (value === false) {
|
|
|
|
el.removeAttribute(name);
|
|
|
|
} else {
|
|
|
|
if (value === true) {
|
|
|
|
value = name;
|
|
|
|
}
|
|
|
|
el.setAttribute(name, value);
|
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 17:50:54 +02:00
|
|
|
export function el(elementName, attributes, children) {
|
2020-05-03 01:08:53 +02:00
|
|
|
return elNS(HTML_NS, elementName, attributes, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function elNS(ns, elementName, attributes, children) {
|
2019-06-15 17:50:54 +02:00
|
|
|
if (attributes && isChildren(attributes)) {
|
|
|
|
children = attributes;
|
|
|
|
attributes = null;
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:08:53 +02:00
|
|
|
const e = document.createElementNS(ns, elementName);
|
2019-06-15 17:50:54 +02:00
|
|
|
|
|
|
|
if (attributes) {
|
|
|
|
for (let [name, value] of Object.entries(attributes)) {
|
2019-06-16 15:12:54 +02:00
|
|
|
if (name === "className" && typeof value === "object" && value !== null) {
|
|
|
|
value = classNames(value);
|
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
setAttribute(e, name, value);
|
|
|
|
}
|
|
|
|
}
|
2019-06-15 17:50:54 +02:00
|
|
|
|
2019-02-26 22:45:58 +01:00
|
|
|
if (children) {
|
|
|
|
if (!Array.isArray(children)) {
|
|
|
|
children = [children];
|
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
for (let c of children) {
|
2019-06-14 23:46:18 +02:00
|
|
|
if (!c.nodeType) {
|
2019-02-26 20:49:32 +01:00
|
|
|
c = text(c);
|
|
|
|
}
|
2019-02-20 23:48:16 +01:00
|
|
|
e.appendChild(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function text(str) {
|
|
|
|
return document.createTextNode(str);
|
|
|
|
}
|
|
|
|
|
2020-05-03 01:08:53 +02:00
|
|
|
export const HTML_NS = "http://www.w3.org/1999/xhtml";
|
|
|
|
export const SVG_NS = "http://www.w3.org/2000/svg";
|
|
|
|
|
|
|
|
export const TAG_NAMES = {
|
|
|
|
[HTML_NS]: [
|
|
|
|
"a", "ol", "ul", "li", "div", "h1", "h2", "h3", "h4", "h5", "h6",
|
|
|
|
"p", "strong", "em", "span", "img", "section", "main", "article", "aside",
|
2020-08-14 14:49:22 +02:00
|
|
|
"pre", "button", "time", "input", "textarea", "label"],
|
2020-05-03 01:08:53 +02:00
|
|
|
[SVG_NS]: ["svg", "circle"]
|
|
|
|
};
|
2019-06-13 00:41:45 +02:00
|
|
|
|
2019-06-14 23:46:18 +02:00
|
|
|
export const tag = {};
|
|
|
|
|
2020-05-03 01:08:53 +02:00
|
|
|
|
|
|
|
for (const [ns, tags] of Object.entries(TAG_NAMES)) {
|
|
|
|
for (const tagName of tags) {
|
|
|
|
tag[tagName] = function(attributes, children) {
|
|
|
|
return elNS(ns, tagName, attributes, children);
|
|
|
|
}
|
2019-06-14 23:46:18 +02:00
|
|
|
}
|
|
|
|
}
|