@@ -32,6 +32,7 @@ ALMCSS.template.dom = function() {
3232 slotLabel . innerHTML = slot . slotId ;
3333 slotElement . appendChild ( slotLabel ) ;
3434 template . htmlElement . appendChild ( slotElement ) ;
35+ slot . htmlElement = slotElement ;
3536 }
3637 } ;
3738
@@ -48,7 +49,9 @@ ALMCSS.template.dom = function() {
4849 // Currently, it is assumed that templates are defined using a single selector per CSS rule
4950 var containerElement = document . querySelector ( template . getSelectorText ( ) ) ;
5051 containerElement . appendChild ( templateElement ) ;
52+ // The HtmlElement object of the DOM **is modified**
5153 containerElement . isTemplate = true ;
54+ containerElement . template = template ;
5255 template . htmlElement = containerElement ;
5356 // Create the slots
5457 createSlotElements ( template ) ;
@@ -61,8 +64,44 @@ ALMCSS.template.dom = function() {
6164 }
6265 } ;
6366
67+ var getTemplateAncestor = function ( element , slotName ) {
68+ var template ;
69+ log ( "Looking for a template ancestor for the slot '" + slotName + "'..." )
70+ while ( element . parentNode ) {
71+ element = element . parentNode ;
72+ if ( element . isTemplate ) {
73+ template = element . template ;
74+ if ( template . hasSlot ( slotName ) ) {
75+ info ( 'A template ancestor was found' ) ;
76+ log ( 'The ancestor template is:\n' + template ) ;
77+ return template ;
78+ } else {
79+ log ( "A template ancestor was found, but it did not contain a slot named '" +
80+ slotName + "', so we continue traversing up the DOM tree..." ) ;
81+ }
82+ }
83+ }
84+ warn ( "No template ancestor with a slot '" + slotName + "' was found" ) ;
85+ } ;
86+
87+ var moveElementsIntoSlots = function ( positionedElements ) {
88+ var i , j , elements , slotName , templateAncestor , slotElement ;
89+ for ( i = 0 ; i < positionedElements . length ; i ++ ) {
90+ slotName = positionedElements [ i ] . slotName ;
91+ elements = document . querySelectorAll ( positionedElements [ i ] . selectorText ) ;
92+ for ( j = 0 ; j < elements . length ; j ++ ) {
93+ templateAncestor = getTemplateAncestor ( elements [ j ] , slotName ) ;
94+ if ( templateAncestor ) {
95+ slotElement = templateAncestor . getSlot ( slotName ) . htmlElement ;
96+ slotElement . appendChild ( elements [ j ] . parentNode . removeChild ( elements [ j ] ) ) ;
97+ }
98+ }
99+ }
100+ } ;
101+
64102 return {
65- createTemplateElements : createTemplateElements
103+ createTemplateElements : createTemplateElements ,
104+ moveElementsIntoSlots : moveElementsIntoSlots
66105 } ;
67106
68107} ( ) ;
0 commit comments