1- import { BaseCustomWebComponentConstructorAppend , BaseCustomWebComponentNoAttachedTemplate , css } from "./BaseCustomWebComponent.js" ;
1+ import { BaseCustomWebComponentNoAttachedTemplate , css } from "./BaseCustomWebComponent.js" ;
22import { WeakArray } from "./WeakArray.js" ;
33
44function camelToDashCase ( text : string ) {
55 return text . replace ( / ( [ A - Z ] ) / g, ( g ) => `-${ g [ 0 ] . toLowerCase ( ) } ` ) ;
66}
77
8- class BaseDeclaritiveWebcomponent extends BaseCustomWebComponentConstructorAppend {
8+ const instanceMap = new Map < string , WeakArray < BaseDeclaritiveWebcomponent > > ( ) ;
9+ class BaseDeclaritiveWebcomponent extends BaseCustomWebComponentNoAttachedTemplate {
10+ #firstConnect = false ;
11+
912 constructor ( ) {
1013 super ( ) ;
11- //@ts -ignore
12- if ( this . constructor . _enableBindings )
13- this . _bindingsParse ( null , true ) ;
1414 }
1515
1616 async connectedCallback ( ) {
17+ if ( ! this . #firstConnect) {
18+ this . #firstConnect = true ;
19+ if ( window [ this . localName ] . template === undefined ) {
20+ window [ this . localName ] . template = window [ this . localName ] . _definingElement . querySelector ( 'template' ) ;
21+ }
22+ //@ts -ignore
23+ this . _rootDocumentFragment = this . constructor . template . content . cloneNode ( true ) ;
24+ //@ts -ignore
25+ if ( this . constructor . _enableBindings )
26+ this . _bindingsParse ( null , true ) ;
27+ this . shadowRoot . appendChild ( this . _rootDocumentFragment ) ;
28+ }
29+
1730 this . _parseAttributesToProperties ( ) ;
1831 //@ts -ignore
1932 if ( this . constructor . _enableBindings )
@@ -32,8 +45,6 @@ class BaseDeclaritiveWebcomponent extends BaseCustomWebComponentConstructorAppen
3245 }
3346}
3447
35- const instanceMap = new Map < string , WeakArray < BaseDeclaritiveWebcomponent > > ( ) ;
36-
3748class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttachedTemplate {
3849
3950 public static style = css `: host {dis play: none;}` ;
@@ -69,21 +80,15 @@ class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttached
6980 }
7081 const name = this . name ;
7182 if ( window [ name ] ) {
72- window [ name ] . template = this . querySelector ( 'template' ) ;
73- //window[name].style = style;
83+ window [ name ] . template = undefined ;
7484 window [ name ] . properties = props ;
7585 window [ name ] . _propertiesDictionary = null ;
7686 window [ name ] . _enableBindings = this . enableBindings ;
7787 window [ name ] . _definingElement = this ;
78- const instanceArray = instanceMap . get ( name ) ;
79- for ( const i of instanceArray )
80- i . _reapplyTemplateAfterUpdate ( ) ;
8188 } else {
8289 const instanceArray = new WeakArray < BaseDeclaritiveWebcomponent > ( ) ;
8390 instanceMap . set ( name , instanceArray ) ;
8491 window [ name ] = function ( ) {
85- if ( window [ name ] . template === undefined )
86- window [ name ] . template = window [ name ] . _definingElement . querySelector ( 'template' ) ;
8792 const instance = Reflect . construct ( BaseDeclaritiveWebcomponent , [ ] , window [ name ] ) ;
8893 instanceArray . add ( instance ) ;
8994
@@ -135,6 +140,13 @@ class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttached
135140 customElements . define ( name , window [ name ] ) ;
136141 }
137142 }
143+
144+ upgradeAllInstances ( template ?: HTMLTemplateElement ) {
145+ window [ this . name ] . template = template ?? this . querySelector ( 'template' ) ;
146+ const instanceArray = instanceMap . get ( this . name ) ;
147+ for ( const i of instanceArray )
148+ i . _reapplyTemplateAfterUpdate ( ) ;
149+ }
138150}
139151
140152customElements . define ( "node-projects-dce" , DeclaritiveBaseCustomWebcomponent ) ;
0 commit comments