@@ -50,8 +50,20 @@ const distillMain = function() {
5050 console . info ( 'Runlevel 1->2.' ) ;
5151 window . distillRunlevel += 1 ;
5252
53- /* 3. Register components */
54- /* Article will register controller which takes control from there */
53+ /* 3. Register Controller listener functions */
54+ /* Needs to happen before components to their connected callbacks have a controller to talk to. */
55+ for ( const [ functionName , callback ] of Object . entries ( Controller . listeners ) ) {
56+ if ( typeof callback === 'function' ) {
57+ document . addEventListener ( functionName , callback ) ;
58+ } else {
59+ console . error ( 'Runlevel 2: Controller listeners need to be functions!' ) ;
60+ }
61+ }
62+ console . info ( 'Runlevel 2: We can now listen to controller events.' ) ;
63+ console . info ( 'Runlevel 2->3.' ) ;
64+ window . distillRunlevel += 1 ;
65+
66+ /* 4. Register components */
5567 const components = [
5668 Abstract , Appendix , Article , Bibliography , Byline , Cite , CitationList , Code ,
5769 Footnote , FootnoteList , FrontMatter , HoverBox , Title , DMath , References , TOC , Figure ,
@@ -71,21 +83,10 @@ const distillMain = function() {
7183 customElements . define ( component . is , component ) ;
7284 }
7385
74- console . info ( 'Runlevel 2: Distill Template finished registering custom elements.' ) ;
75- console . info ( 'Runlevel 2->3.' ) ;
76- window . distillRunlevel += 1 ;
77-
78- /* 4. Register Controller listener functions */
79- for ( const [ functionName , callback ] of Object . entries ( Controller . listeners ) ) {
80- if ( typeof callback === 'function' ) {
81- document . addEventListener ( functionName , callback ) ;
82- } else {
83- console . error ( 'Runlevel 3: Controller listeners need to be functions!' ) ;
84- }
85- }
86- console . info ( 'Runlevel 3: We can now listen to controller events.' ) ;
86+ console . info ( 'Runlevel 3: Distill Template finished registering custom elements.' ) ;
8787 console . info ( 'Runlevel 3->4.' ) ;
8888 window . distillRunlevel += 1 ;
89+
8990 // If template was added after DOMContentLoaded we may have missed that event.
9091 // Controller will check for that case, so trigger the event explicitly:
9192 Controller . listeners . DOMContentLoaded ( ) ;
0 commit comments