diff options
author | Joris | 2020-02-13 18:54:18 +0100 |
---|---|---|
committer | Joris | 2020-02-15 10:59:25 +0100 |
commit | 7a01b001ccc4a7bda3da92486903540e3f9754fd (patch) | |
tree | fbe269faa319fd7f21049612f8a62c0c169f369e /src/dom.ts | |
parent | 8328c11ac6706e3adf9b09877c02897132b8927a (diff) | |
download | cooking-7a01b001ccc4a7bda3da92486903540e3f9754fd.tar.gz cooking-7a01b001ccc4a7bda3da92486903540e3f9754fd.tar.bz2 cooking-7a01b001ccc4a7bda3da92486903540e3f9754fd.zip |
Set up bucklescript
Diffstat (limited to 'src/dom.ts')
-rw-r--r-- | src/dom.ts | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/src/dom.ts b/src/dom.ts deleted file mode 100644 index 6b1c803..0000000 --- a/src/dom.ts +++ /dev/null @@ -1,54 +0,0 @@ -type Attribute = string | boolean | ((e: Event) => void) - -type Child = Element | string - -export function h(tag: string, attrs: {[key: string]: Attribute}, children: Array<Child> = []): Element { - let element = document.createElement(tag) - - for (let name in attrs) { - let value = attrs[name] - if (typeof value === 'boolean') { - if (value) { - element.setAttribute(name, name) - } - } else if (typeof value === 'function') { - (element as any)[name] = (e: Event) => { - (value as ((e: Event) => void))(e) - } - } else { - element.setAttribute(name, value) - } - } - - children.forEach(child => { - if (typeof child === 'string') { - element.appendChild(document.createTextNode(child)) - } else { - element.appendChild(child) - } - }) - - return element -} - -export function toggleClassName(node: Element, className: string) { - if (node.className === className) { - node.className = '' - } else { - node.className = className - } -} - -export function nodeListToArray(nodeList: NodeListOf<HTMLElement>): HTMLElement[] { - const xs: HTMLElement[] = []; - nodeList.forEach(node => xs.push(node)) - return xs -} - -export function replace(node: Node, replacement: Node) { - const parentNode = node.parentNode - - if (parentNode) { - parentNode.replaceChild(replacement, node) - } -} |