|
@@ -1,4 +1,46 @@
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+// TODO: search for .childNodes
|
|
|
|
|
+export function queryChildren(parent: HTMLElement, selector?: string): HTMLElement[] {
|
|
|
|
|
+ let childNodes = parent.childNodes
|
|
|
|
|
+ let a = []
|
|
|
|
|
+
|
|
|
|
|
+ for (let i = 0; i < childNodes.length; i++) {
|
|
|
|
|
+ let childNode = childNodes[i]
|
|
|
|
|
+ if (
|
|
|
|
|
+ childNode.nodeType === 1 && // an element
|
|
|
|
|
+ (!selector || elementMatches(childNode as HTMLElement, selector))
|
|
|
|
|
+ ) {
|
|
|
|
|
+ a.push(childNode)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return a
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function queryChild(parent: HTMLElement, selector?: string): HTMLElement | null {
|
|
|
|
|
+ let childNodes = parent.childNodes
|
|
|
|
|
+
|
|
|
|
|
+ for (let i = 0; i < childNodes.length; i++) {
|
|
|
|
|
+ let childNode = childNodes[i]
|
|
|
|
|
+ if (
|
|
|
|
|
+ childNode.nodeType === 1 && // an element
|
|
|
|
|
+ (!selector || elementMatches(childNode as HTMLElement, selector))
|
|
|
|
|
+ ) {
|
|
|
|
|
+ return childNode as HTMLElement
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return null
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// TODO: util for animation end
|
|
|
|
|
+// TODO: get straight the distinction between HTMLElement and Element
|
|
|
|
|
+
|
|
|
// TODO: use in other places
|
|
// TODO: use in other places
|
|
|
// TODO: rename to createElement
|
|
// TODO: rename to createElement
|
|
|
export function makeElement(tagName, attrs, content?): HTMLElement {
|
|
export function makeElement(tagName, attrs, content?): HTMLElement {
|
|
@@ -122,6 +164,10 @@ const closestMethod = Element.prototype.closest || function(selector) {
|
|
|
return null
|
|
return null
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+export function elementClosest(el: HTMLElement, selector: string) {
|
|
|
|
|
+ return closestMethod.call(el, selector)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export function elementMatches(el: HTMLElement, selector: string) {
|
|
export function elementMatches(el: HTMLElement, selector: string) {
|
|
|
return matchesMethod.call(el, selector)
|
|
return matchesMethod.call(el, selector)
|
|
|
}
|
|
}
|
|
@@ -133,7 +179,7 @@ export function listenBySelector(
|
|
|
handler: (ev: Event, matchedTarget: HTMLElement) => void
|
|
handler: (ev: Event, matchedTarget: HTMLElement) => void
|
|
|
) {
|
|
) {
|
|
|
function realHandler(ev: Event) {
|
|
function realHandler(ev: Event) {
|
|
|
- let matchedChild = closestMethod.call(ev.target, selector)
|
|
|
|
|
|
|
+ let matchedChild = elementClosest(ev.target as HTMLElement, selector)
|
|
|
if (matchedChild) {
|
|
if (matchedChild) {
|
|
|
handler.call(matchedChild, ev, matchedChild)
|
|
handler.call(matchedChild, ev, matchedChild)
|
|
|
}
|
|
}
|
|
@@ -172,6 +218,7 @@ export function listenToHoverBySelector(
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: user new signature in other places
|
|
// TODO: user new signature in other places
|
|
|
|
|
+// this is only func that accepts array :(
|
|
|
export function findElsWithin(containers: HTMLElement[] | HTMLElement, selector: string): HTMLElement[] {
|
|
export function findElsWithin(containers: HTMLElement[] | HTMLElement, selector: string): HTMLElement[] {
|
|
|
if (containers instanceof HTMLElement) {
|
|
if (containers instanceof HTMLElement) {
|
|
|
containers = [ containers ]
|
|
containers = [ containers ]
|