@@ -6,6 +6,7 @@ var codeInput = {
66 usedTemplates : {
77 } ,
88 defaultTemplate : undefined ,
9+ templateQueue : { } , // lists of elements for each unrecognised template
910 plugins : { // Import a plugin from the plugins folder and it will be saved here.
1011 } ,
1112 Plugin : class {
@@ -78,10 +79,32 @@ var codeInput = {
7879 return text . replace ( new RegExp ( "&" , "g" ) , "&" ) . replace ( new RegExp ( "<" , "g" ) , "<" ) ; /* Global RegExp */
7980 }
8081
81- /* Callbacks */
82- connectedCallback ( ) {
83- // Added to document
84- this . template = codeInput . usedTemplates [ this . getAttribute ( "template" ) || codeInput . defaultTemplate ] ;
82+ /* Get the template for this element or add to the unrecognised template queue. */
83+ get_template ( ) {
84+ // Get name of template
85+ let template_name ;
86+ if ( this . getAttribute ( "template" ) == undefined ) {
87+ // Default
88+ template_name = codeInput . defaultTemplate ;
89+ } else {
90+ template_name = this . getAttribute ( "template" ) ;
91+ }
92+ // Get template
93+ if ( template_name in codeInput . usedTemplates ) {
94+ return codeInput . usedTemplates [ template_name ] ;
95+ } else {
96+ // Doesn't exist - add to queue
97+ if ( ! ( template_name in codeInput . templateQueue ) ) {
98+ codeInput . templateQueue [ template_name ] = [ ] ;
99+ }
100+ codeInput . templateQueue [ template_name ] . push ( this ) ;
101+ return undefined ;
102+ }
103+ codeInput . usedTemplates [ codeInput . defaultTemplate ]
104+ }
105+ /* Set up element when a template is added */
106+ setup ( ) {
107+ this . classList . add ( "code-input_registered" ) ; // Remove register message
85108 if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
86109
87110 this . plugin_evt ( "beforeElementsAdded" ) ;
@@ -126,7 +149,14 @@ var codeInput = {
126149 /* Add code from value attribute - useful for loading from backend */
127150 this . update ( value , this ) ;
128151 }
129- get observedAttributes ( ) {
152+
153+ /* Callbacks */
154+ connectedCallback ( ) {
155+ // Added to document
156+ this . template = this . get_template ( ) ;
157+ if ( this . template != undefined ) this . setup ( ) ;
158+ }
159+ static get observedAttributes ( ) {
130160 let attrs = [ "value" , "placeholder" , "lang" , "template" ] ; // Attributes to monitor
131161
132162 /* Add from plugins */
@@ -135,6 +165,7 @@ var codeInput = {
135165 }
136166 return attrs ;
137167 }
168+
138169 attributeChangedCallback ( name , oldValue , newValue ) {
139170 if ( this . isConnected ) {
140171 // This will sometimes be called before the element has been created, so trying to update an attribute causes an error.
@@ -207,7 +238,25 @@ var codeInput = {
207238 registerTemplate : function ( template_name , template ) {
208239 // Set default class
209240 codeInput . usedTemplates [ template_name ] = template ;
210- codeInput . defaultTemplate = template_name ;
241+ // Add elements w/ template from queue
242+ if ( template_name in codeInput . templateQueue ) {
243+ for ( let i in codeInput . templateQueue [ template_name ] ) {
244+ elem = codeInput . templateQueue [ template_name ] [ i ] ;
245+ elem . template = template ;
246+ elem . setup ( ) ;
247+ }
248+ }
249+ if ( codeInput . defaultTemplate == undefined ) {
250+ codeInput . defaultTemplate = template_name ;
251+ // Add elements w/ default template from queue
252+ if ( undefined in codeInput . templateQueue ) {
253+ for ( let i in codeInput . templateQueue [ undefined ] ) {
254+ elem = codeInput . templateQueue [ undefined ] [ i ] ;
255+ elem . template = template ;
256+ elem . setup ( ) ;
257+ }
258+ }
259+ }
211260 } ,
212261 templates : {
213262 custom ( highlight = function ( ) { } , preElementStyled = true , isCode = true , includeCodeInputInHighlightFunc = false , plugins = [ ] ) {
0 commit comments