Skip to content

Commit ff2b33e

Browse files
committed
work on decl. wc
1 parent 21500be commit ff2b33e

3 files changed

Lines changed: 86 additions & 42 deletions

File tree

package.json

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
{
2-
"description": "Base Custom Webcomponent",
3-
"name": "@node-projects/base-custom-webcomponent",
4-
"version": "0.25.6",
5-
"type": "module",
6-
"main": "./dist/index.js",
7-
"author": "",
8-
"license": "BSD-3-Clause",
9-
"scripts": {
10-
"tsc": "tsc",
11-
"start": "web-dev-server --node-resolve --open sample/index.html"
12-
},
13-
"devDependencies": {
14-
"typescript": "^5.4.3",
15-
"@web/dev-server": "^0.4.3"
16-
},
17-
"repository": {
18-
"type": "git",
19-
"url": "git+https://github.com/node-projects/base-custom-webcomponent.git"
20-
},
21-
"bugs": {
22-
"url": "https://github.com/node-projects/base-custom-webcomponent/issues"
23-
},
24-
"homepage": "https://github.com/node-projects/base-custom-webcomponent#readme"
25-
}
1+
{
2+
"description": "Base Custom Webcomponent",
3+
"name": "@node-projects/base-custom-webcomponent",
4+
"version": "0.25.7",
5+
"type": "module",
6+
"main": "./dist/index.js",
7+
"author": "",
8+
"license": "BSD-3-Clause",
9+
"scripts": {
10+
"tsc": "tsc",
11+
"start": "web-dev-server --node-resolve --open sample/index.html"
12+
},
13+
"devDependencies": {
14+
"typescript": "^5.4.3",
15+
"@web/dev-server": "^0.4.3"
16+
},
17+
"repository": {
18+
"type": "git",
19+
"url": "git+https://github.com/node-projects/base-custom-webcomponent.git"
20+
},
21+
"bugs": {
22+
"url": "https://github.com/node-projects/base-custom-webcomponent/issues"
23+
},
24+
"homepage": "https://github.com/node-projects/base-custom-webcomponent#readme"
25+
}

sample/index.html

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,51 @@ <h1>DSD Demo</h1>
3737
</my-element>
3838
<br><br>
3939
<h1>DCE Demo</h1>
40-
<node-projects-dce name="simple-dce-demo" enable-bindings properties='{"list":"Array","ctx":{"type":"String","reflect":true}}' >
40+
<node-projects-dce name="simple-dce-demo" enable-bindings
41+
properties='{"list":"Array","ctx":{"type":"String","reflect":true}}'>
4142
<template>
42-
<style>h1 {color: blue}</style>
43+
<style>
44+
h1 {
45+
color: blue
46+
}
47+
</style>
4348
<h1>Hello World</h1>
4449
<div>[[this.ctx]]</div>
4550
<template repeat:myitem="[[this.list]]">
46-
<button>[[myitem]] - [[index]]</button>
51+
<button @click="{{this.ctx = myitem}}"
52+
$aaaonclick="[['this.getRootNode().host.ctx = \'' + myitem + '\'']]">[[myitem]] - [[index]]</button>
53+
</template>
54+
</template>
55+
</node-projects-dce>
56+
57+
<simple-dce-demo list='["aa","bb"]' ctx="test">
58+
</simple-dce-demo>
59+
60+
<br><br>
61+
62+
<node-projects-dce id="a" name="simple-dce-demo" enable-bindings
63+
properties='{"list":"Array","ctx":{"type":"String","reflect":true}}'>
64+
<template>
65+
<style>
66+
h1 {
67+
color: red
68+
}
69+
</style>
70+
<h1>Hello World</h1>
71+
<div>[[this.ctx]]</div>
72+
<template repeat:myitem="[[this.list]]">
73+
<button @click="{{this.ctx = myitem}}"
74+
$aaaonclick="[['this.getRootNode().host.ctx = \'' + myitem + '\'']]">[[myitem]] - [[index]]</button>
4775
</template>
4876
</template>
4977
</node-projects-dce>
5078

5179
<simple-dce-demo list='["aa","bb"]' ctx="test">
5280
</simple-dce-demo>
81+
82+
<script>
83+
setTimeout(() => a.upgradeAllInstances(), 2000);
84+
</script>
5385
</body>
5486

5587
</html>

src/DeclaritiveBaseCustomWebcomponent.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
import { BaseCustomWebComponentConstructorAppend, BaseCustomWebComponentNoAttachedTemplate, css } from "./BaseCustomWebComponent.js";
1+
import { BaseCustomWebComponentNoAttachedTemplate, css } from "./BaseCustomWebComponent.js";
22
import { WeakArray } from "./WeakArray.js";
33

44
function 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-
3748
class DeclaritiveBaseCustomWebcomponent extends BaseCustomWebComponentNoAttachedTemplate {
3849

3950
public static style = css`:host{display: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

140152
customElements.define("node-projects-dce", DeclaritiveBaseCustomWebcomponent);

0 commit comments

Comments
 (0)