@@ -6,6 +6,7 @@ var codeInput = {
66 usedTemplates : {
77 } ,
88 defaultTemplate : undefined ,
9+ templateQueue : { } , // lists of elements for each unrecognised template
910 CodeInput : class extends HTMLElement { // Create code input element
1011 constructor ( ) {
1112 super ( ) ; // Element
@@ -169,12 +170,34 @@ var codeInput = {
169170 return text . replace ( new RegExp ( "&" , "g" ) , "&" ) . replace ( new RegExp ( "<" , "g" ) , "<" ) ; /* Global RegExp */
170171 }
171172
172- /* Callbacks */
173- connectedCallback ( ) {
174- // Added to document
175- this . template = codeInput . usedTemplates [ this . getAttribute ( "template" ) || codeInput . defaultTemplate ] ;
173+ /* Get the template for this element or add to the unrecognised template queue. */
174+ get_template ( ) {
175+ // Get name of template
176+ let template_name ;
177+ if ( this . getAttribute ( "template" ) == undefined ) {
178+ // Default
179+ template_name = codeInput . defaultTemplate ;
180+ } else {
181+ template_name = this . getAttribute ( "template" ) ;
182+ }
183+ // Get template
184+ if ( template_name in codeInput . usedTemplates ) {
185+ return codeInput . usedTemplates [ template_name ] ;
186+ } else {
187+ // Doesn't exist - add to queue
188+ if ( ! ( template_name in codeInput . templateQueue ) ) {
189+ codeInput . templateQueue [ template_name ] = [ ] ;
190+ }
191+ codeInput . templateQueue [ template_name ] . push ( this ) ;
192+ return undefined ;
193+ }
194+ codeInput . usedTemplates [ codeInput . defaultTemplate ]
195+ }
196+ /* Set up element when a template is added */
197+ setup ( ) {
198+ this . classList . add ( "code-input_registered" ) ; // Remove register message
176199 if ( this . template . preElementStyled ) this . classList . add ( "code-input_pre-element-styled" ) ;
177-
200+
178201 /* Defaults */
179202 let lang = this . getAttribute ( "lang" ) ;
180203 let placeholder = this . getAttribute ( "placeholder" ) || this . getAttribute ( "lang" ) || "" ;
@@ -216,6 +239,13 @@ var codeInput = {
216239 /* Add code from value attribute - useful for loading from backend */
217240 this . update ( value , this ) ;
218241 }
242+
243+ /* Callbacks */
244+ connectedCallback ( ) {
245+ // Added to document
246+ this . template = this . get_template ( ) ;
247+ if ( this . template != undefined ) this . setup ( ) ;
248+ }
219249 static get observedAttributes ( ) {
220250 return [ "value" , "placeholder" , "lang" , "template" ] ; // Attributes to monitor
221251 }
@@ -291,7 +321,25 @@ var codeInput = {
291321 registerTemplate : function ( template_name , template ) {
292322 // Set default class
293323 codeInput . usedTemplates [ template_name ] = template ;
294- codeInput . defaultTemplate = template_name ;
324+ // Add elements w/ template from queue
325+ if ( template_name in codeInput . templateQueue ) {
326+ for ( let i in codeInput . templateQueue [ template_name ] ) {
327+ elem = codeInput . templateQueue [ template_name ] [ i ] ;
328+ elem . template = template ;
329+ elem . setup ( ) ;
330+ }
331+ }
332+ if ( codeInput . defaultTemplate == undefined ) {
333+ codeInput . defaultTemplate = template_name ;
334+ // Add elements w/ default template from queue
335+ if ( undefined in codeInput . templateQueue ) {
336+ for ( let i in codeInput . templateQueue [ undefined ] ) {
337+ elem = codeInput . templateQueue [ undefined ] [ i ] ;
338+ elem . template = template ;
339+ elem . setup ( ) ;
340+ }
341+ }
342+ }
295343 } ,
296344 templates : {
297345 custom ( highlight = function ( ) { } , preElementStyled = true , isCode = true , includeCodeInputInHighlightFunc = false ) {
0 commit comments