diff --git a/djx/src/main/ts/tsx/traits.ts b/djx/src/main/ts/tsx/traits.ts --- a/djx/src/main/ts/tsx/traits.ts +++ b/djx/src/main/ts/tsx/traits.ts @@ -158,4 +158,49 @@ export function locateNode(node: Node): return next ? [next, "before"] : [node.parentNode, "last"]; +} + +export const placeAt = (node: Node, refNodeOrId: string | Node, position: DojoNodePosition) => { + const collect = (collection: HTMLCollection) => { + const items = []; + for (let i = 0, n = collection.length; i < n; i++) { + items.push(collection[i]); + } + return items; + }; + + const startup = (node: Node) => { + if (node.parentNode) { + const parentWidget = registry.getEnclosingWidget(node.parentNode); + if (parentWidget && parentWidget._started) + return startupWidgets(node); + } + if (isInPage(node)) + startupWidgets(node); + }; + + const ref = typeof refNodeOrId == "string" ? document.getElementById(refNodeOrId) : refNodeOrId; + if (!ref) + return; + + const parent = ref.parentNode; + + if (typeof position == "number") { + + } else { + switch(position) { + case "before": + if (parent) + parent.insertBefore(node,ref); + case "after": + if (parent) + parent.insertBefore(node, ref.nextSibling); + } + } + + const startupPending = isDocumentFragmentNode(node) ? collect(node.children) : [node]; + + dom.place(node, refNodeOrId, position); + + startupPending.forEach(startup); } \ No newline at end of file