Last active
August 29, 2015 14:20
-
-
Save wilsonpage/d5520bd8d22327633e33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var XFooPrototype = { | |
createdCallback: function() { | |
this.createShadowRoot({ mode: 'open' }); | |
new MutationObserver(() => distribute(this)).observe(this, { childList: true }); | |
distribute(this); | |
} | |
} | |
function distribute(host) { | |
var slots = host.shadowRoot.querySelectorAll('content'); | |
[].forEach.call(slots, empty); // empty slots (required?) | |
distributeCandidates(host, host.children, slots); | |
} | |
function distributeCandidates(root, candidates, slots) { | |
[].forEach.call(candidates, candidate => { | |
if (candidate.tagName === 'CONTENT') { | |
distributeCandidates(candidate.distributedNodes, slots); | |
candidate.ondistributionchanged = () => distribute(root); | |
} else { | |
allocate(candidate, slots); | |
} | |
}); | |
} | |
function allocate(candidate, slots) { | |
for (var i = 0; i < slots.length; i++) { | |
var selector = slots[i].getAttribute('select'); | |
if (!selector || candidate.matches(selector)) { | |
slots[i].insertAt(candidate, slots[i].distributedNodes.length); // append | |
return true; | |
} | |
} | |
} | |
function empty(content) { | |
[].forEach.call(content.distributedNodes, node => content.remove(node)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment