|
|
@@ -1,11 +1,15 @@
|
|
|
+import { assignTo } from '../util/object'
|
|
|
|
|
|
// TODO: use in other places
|
|
|
+// TODO: rename to createElement
|
|
|
export function makeElement(tagName, attrs, content?): HTMLElement {
|
|
|
let el: HTMLElement = document.createElement(tagName)
|
|
|
|
|
|
if (attrs) {
|
|
|
for (let attrName in attrs) {
|
|
|
- if (attrName === 'className' || attrName === 'colSpan' || attrName === 'rowSpan') {
|
|
|
+ if (attrName === 'style') {
|
|
|
+ assignTo(el.style, attrs[attrName])
|
|
|
+ } else if (attrName === 'className' || attrName === 'colSpan' || attrName === 'rowSpan') { // TODO: do hash
|
|
|
el[attrName] = attrs[attrName]
|
|
|
} else {
|
|
|
el.setAttribute(attrName, attrs[attrName])
|
|
|
@@ -13,22 +17,19 @@ export function makeElement(tagName, attrs, content?): HTMLElement {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- appendContentTo(el, content)
|
|
|
+ if (content != null) {
|
|
|
+ appendContentTo(el, content)
|
|
|
+ }
|
|
|
|
|
|
return el
|
|
|
}
|
|
|
|
|
|
-export type ElementContent = string | Node | Node[]
|
|
|
+export type ElementContent = string | Node | NodeList | Node[]
|
|
|
|
|
|
export function appendContentTo(el: HTMLElement, content: ElementContent) {
|
|
|
- if (typeof content === 'string') {
|
|
|
- el.innerHTML = content
|
|
|
- } else if (content instanceof Node) {
|
|
|
- el.appendChild(content)
|
|
|
- } else if (content && content.length) {
|
|
|
- for (let i = 0; i < content.length; i++) {
|
|
|
- el.appendChild(content[i])
|
|
|
- }
|
|
|
+ let childNodes = normalizeContent(content)
|
|
|
+ for (let i = 0; i < childNodes.length; i++) {
|
|
|
+ el.appendChild(childNodes[i])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -44,6 +45,7 @@ export function htmlToElements(htmlString): HTMLElement {
|
|
|
return Array.prototype.slice.call(div.childNodes)
|
|
|
}
|
|
|
|
|
|
+// TODO: rename to listenByClassName
|
|
|
export function listenViaDelegation(container: HTMLElement, eventType, childClassName, handler) {
|
|
|
container.addEventListener(eventType, function(ev: Event) {
|
|
|
for (let node = ev.target as HTMLElement; node !== container; node = node.parentNode as HTMLElement) {
|
|
|
@@ -55,10 +57,22 @@ export function listenViaDelegation(container: HTMLElement, eventType, childClas
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-export function findElsWithin(container: HTMLElement, selector: string): HTMLElement[] {
|
|
|
- return Array.prototype.slice.call(
|
|
|
- container.querySelectorAll(selector)
|
|
|
- )
|
|
|
+// TODO: user new signature in other places
|
|
|
+export function findElsWithin(containers: HTMLElement[] | HTMLElement, selector: string): HTMLElement[] {
|
|
|
+ if (containers instanceof HTMLElement) {
|
|
|
+ containers = [ containers ]
|
|
|
+ }
|
|
|
+ let allChildEls: HTMLElement[] = []
|
|
|
+
|
|
|
+ for (let i = 0; i < containers.length; i++) {
|
|
|
+ let childEls = containers[i].querySelectorAll(selector)
|
|
|
+
|
|
|
+ for (let j = 0; j < childEls.length; j++) {
|
|
|
+ allChildEls.push(childEls[j] as HTMLElement)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return allChildEls
|
|
|
}
|
|
|
|
|
|
export function removeElement(el: HTMLElement) {
|
|
|
@@ -66,3 +80,33 @@ export function removeElement(el: HTMLElement) {
|
|
|
el.parentNode.removeChild(el)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+export function prependWithinEl(parent: HTMLElement, content: ElementContent) {
|
|
|
+ let newEls = normalizeContent(content)
|
|
|
+ let afterEl = parent.firstChild || null // if no firstChild, will append to end, but that's okay, b/c there were no children
|
|
|
+
|
|
|
+ for (let i = 0; i < newEls.length; i++) {
|
|
|
+ parent.insertBefore(newEls[i], afterEl)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function insertAfterEl(refEl: HTMLElement, content: ElementContent) {
|
|
|
+ let newEls = normalizeContent(content)
|
|
|
+ let afterEl = refEl.nextSibling || null
|
|
|
+
|
|
|
+ for (let i = 0; i < newEls.length; i++) {
|
|
|
+ refEl.parentNode.insertBefore(newEls[i], afterEl)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function normalizeContent(content: ElementContent): NodeList | Node[] {
|
|
|
+ let els
|
|
|
+ if (typeof content === 'string') {
|
|
|
+ els = htmlToElements(content) // TODO: optimization, htmlToNodeList
|
|
|
+ } else if (content instanceof Node) {
|
|
|
+ els = [ content ]
|
|
|
+ } else { // assumed to be HTMLElement[]
|
|
|
+ els = content
|
|
|
+ }
|
|
|
+ return els
|
|
|
+}
|