-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb-c-c.js
133 lines (133 loc) · 3.49 KB
/
b-c-c.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { xc } from 'xtal-element/lib/XtalCore.js';
import { upShadowSearch } from 'trans-render/lib/upShadowSearch.js';
const DOMToTemplateMap = new WeakMap();
/**
* Web component that allows basic copying of templates inside Shadow DOM (by default).
* @element b-c-c
*
*/
export class BCC extends HTMLElement {
static is = 'b-c-c';
self = this;
propActions = propActions;
reactor = new xc.Rx(this);
noclear;
from;
copy;
noshadow;
toBeTransformed;
trContext;
templateToClone;
clonedTemplate;
_oldFrom;
_retries = 0;
// /**
// * Replace the b-c-c tag with this tag
// */
// morphInto: string | undefined;
connectedCallback() {
xc.mergeProps(this, slicedPropDefs);
}
onPropChange(name, propDef, newVal) {
this.reactor.addToQueue(propDef, newVal);
}
}
export const linkTemplateToClone = ({ copy, from, self }) => {
if (from === self._oldFrom)
return;
const referencedTemplate = upShadowSearch(self, from);
if (referencedTemplate !== null) {
self._oldFrom = from;
self.templateToClone = referencedTemplate;
}
else if (self._retries === 0) {
self._retries++;
setTimeout(() => linkTemplateToClone(self), 50);
}
else {
console.error('Cannot locate template: ' + from, self);
}
};
export const linkClonedTemplate = ({ templateToClone, self }) => {
let realTemplateToClone = templateToClone;
if (templateToClone.localName !== 'template') {
if (!DOMToTemplateMap.has(templateToClone)) {
const newTemplate = document.createElement('template');
const aTemplateToClone = templateToClone;
newTemplate.innerHTML = aTemplateToClone.getInnerHTML ? aTemplateToClone.getInnerHTML({ includeShadowRoots: true }) : templateToClone.innerHTML;
DOMToTemplateMap.set(templateToClone, newTemplate);
}
realTemplateToClone = DOMToTemplateMap.get(templateToClone);
}
self.clonedTemplate = realTemplateToClone.content.cloneNode(true);
};
export const onClonedTemplate = ({ clonedTemplate, toBeTransformed, trContext: tr, self }) => {
let target = self;
if (!self.noshadow) {
if (target.shadowRoot == null) {
target = self.attachShadow({ mode: 'open' });
}
else {
target = target.shadowRoot;
}
}
if (!self.noclear) {
target.innerHTML = '';
}
if (toBeTransformed && tr === undefined)
return;
if (tr !== undefined) {
tr.transform(clonedTemplate, tr, target);
}
else {
target.appendChild(clonedTemplate);
}
delete self.clonedTemplate;
};
const propActions = [
linkTemplateToClone,
linkClonedTemplate,
onClonedTemplate
];
const bool1 = {
type: Boolean,
dry: true,
async: true,
};
const bool2 = {
...bool1,
stopReactionsIfFalsy: true,
reflect: true,
};
const str1 = {
type: String,
dry: true,
async: true,
};
const str2 = {
...str1,
stopReactionsIfFalsy: true,
};
const obj1 = {
type: Object,
dry: true,
async: true,
};
const obj2 = {
...obj1,
stopReactionsIfFalsy: true,
};
const propDefMap = {
noclear: bool1,
copy: bool2,
from: str2,
noshadow: bool1,
toBeTransformed: bool1,
trContext: obj1,
templateToClone: obj2,
clonedTemplate: obj2,
//morphInto: str1,
};
const slicedPropDefs = xc.getSlicedPropDefs(propDefMap);
xc.letThereBeProps(BCC, slicedPropDefs, 'onPropChange');
xc.define(BCC);