-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelepathic-element.js
361 lines (338 loc) · 14.8 KB
/
telepathic-element.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//import {Marked} from '../marked/lib/marked-class.mjs';
export class TelepathicElement extends HTMLElement{
static describe(){return `TelepathicElement provides the base class for all telepathic-elements. It is responsible for all templating and binding operations.`};
constructor(fileName,noshadow,delayRender){
super();
this.initialized = false;
this.promises = []; //Helps speed up loading to defer things to init in derived constructors
if(noshadow){
this.$ = this;
}else{
try{
this.$ = this.attachShadow({mode: 'open'});
}catch(err){
//Firefox and some others don't support shadow dom completely or at all.
console.debug(err);
this.$ = this;
}
}
if(!this.$){
this.$ = this;
}
this.delayRender = delayRender;
this.templateBindings = {};
this.templatePropertyNames = {};
if(fileName){
this.templateFileName = fileName;
}
}
sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms));
}
async connectedCallback(){
if(!this.initialized){
this.className = this.constructor.name;
await Promise.all(this.promises)
.then(async ()=>{
await this.prepareTemplate();
if(this['init']){
await this.init();
}
if(!this.delayRender){
await this.render();
if(this.onReady){
this.onReady();
}
}
});
}else{
try{
await this.render();
}catch(err){
console.error(`Error rendering: ${this.constructor.name}`);
console.error(err);
}
}
}
async loadFile(fileName){
console.debug("Loading: ",fileName);
let response = await fetch(fileName);
if(response.ok){
return await response.text();
}else{
throw(`${response.status} : ${response.statusText}`);
}
}
async loadFileJSON(fileName){
return await(await(fetch(fileName))).json();
}
async loadTemplate(fileName){
let file;
/*
let marked = new Marked();
if(fileName){
if(!window[fileName]){
file = await this.loadFile(fileName);
this.templateStr = marked.parse(file);
window[fileName] = this.templateStr;
}else{
this.templateStr = window[fileName];
}
this.templateFileName = fileName;
}else{*/
let path = window[this.className];
let tagName = this.tagName.toLowerCase();
//We're still going to try and parse any markdown we find in the template whether it's .md or .html
let htmlFile = `${path}/${tagName}/${tagName}.html`;
file = await this.loadFile(htmlFile);
this.templateStr = file;
this.templateFileName = htmlFile;
// }
console.debug("this.templateFileName: ",this.templateFileName);
//console.debug("file: ",file);
//console.debug("this.templateStr: ",this.templateStr);
}
async prepareTemplate(fileName){
if(!this.templateStr){
if(fileName){
console.debug("Template not yet loaded for ",fileName);
}
await this.loadTemplate(fileName);
console.debug("Loaded ",this.templateFileName);
}
//console.debug(`Preparing ${this.templateFileName}`);
let templateStr = this.templateStr;
this.template = document.createElement("template");
this.template.innerHTML = templateStr;
this.$.appendChild(this.template.content.cloneNode(true));
//Need loader to inject here and load submodules that were hidden previously
window.TelepathicLoader.Load(this.$);
}
async render(){
if(this.templateStr){
let tags = await uniq(this.templateStr.match(TelepathicElement.templateRegex));
await this.compileTemplate(tags);
await this.setIDs();
}
console.debug(`${this.templateFileName} is rendered`);
}
async setIDs(){
let elements = this.$.querySelectorAll("*");
elements.forEach((element)=>{
let id = element.id;
if(id){
let varname = id.replaceAll("-","_");
this[varname] = element;
this[varname].owner = this;
console.debug(`setting ${varname} on ${this.localName}`);
}
});
}
compileTemplate(tags){
console.debug("tags: ",tags);
this.propertyNames = {};
for(let tag of tags){
let property = tag.replaceAll("${","").replaceAll("}","").replaceAll("this.","");
let object = this;
if(property.includes(".")){
let properties = property.split(".");
let props = [];
for(let i = 0; i <= properties.length -1; i++){
let prop = properties[i];
props.push(prop);
if(object[prop] === undefined){
console.debug("Found undeclared property ",props.join('.')," in template ",this);
object[prop] = 'undeclared';
}
try{
this.templateBindings[props.join(".")] = new DataBind({object: object, property: prop});
}catch(err){
console.debug(`Looks like you tried to bind a readonly property somewhere, if so disregard this ${err}`);
}
object = object[prop];
}
}else{
try{
console.debug("About to bind "+property+": ",this[property]," to ",this.templateBindings);
if(this[property]=== undefined){
console.debug(property+" was undefined");
this[property] = "undefined";
}
this.templateBindings[property] = new DataBind({object: this, property: property});
}catch(err){
console.debug(`Looks like you tried to bind a readonly property somewhere, if so disregard this ${err}`);
}
}
this.templatePropertyNames[tag] = property;
let root = this.$;
let iter = document.createNodeIterator(root, NodeFilter.SHOW_TEXT);
let textnode;
while (textnode = iter.nextNode()) {
let txt = textnode.textContent;
if(txt.includes(tag)){
let newNode = document.createElement("span");
//console.debug(`Replacing ${tag} with <span data-bind='${tag}'></span>`);
if(typeof tag !== HTMLElement){
newNode.innerHTML = txt.replaceAll(tag,`<span data-bind='${tag}'></span>`);
}else{
newNode.appendChild(tag);
}
//console.debug("After replacement: ",newNode.innerHTML);
let parentNode = textnode.parentNode;
parentNode.replaceChild(newNode,textnode);
//console.debug("Parent is now: ",parentNode);
}
}
};
for(let tag of tags){
let property = this.templatePropertyNames[tag];
for(let node of this.$.querySelectorAll("*")){
//console.debug("compiling: "+tag+" : "+property+" against ",node);
this.compileNodeAttributes(node, tag, property);
}
}
}
compileNodeAttributes(node,tag,property){
if(node.hasAttributes()){
let attrs = node.attributes;
for(var i = attrs.length - 1; i >= 0; i--) {
let attr = attrs[i];
if(attr.value == tag){
if(attr.name == "data-bind"){
node.removeAttribute("data-bind");
if(this.templateBindings[property]){
//console.debug("removing data-bind =",tag," on ",node," setting bind to innerHTML property is ",property);
this.templateBindings[property] = this.templateBindings[property].bindElement(node,"innerHTML");
}else{
throw("Couldn't find "+property+" on ",this.templateBindings);
}
}else{
if(this[property] == tag && node.getAttribute(attr.name) == tag){
if(attr.name != "value"){
//console.debug("Clearing "+attr.name+" on ",node);
node.setAttribute(attr.name,"");
}else{
//console.debug("Clearing value for "+attr.name+" on ",node);
node.value ="";
}
}else{
//console.debug("Setting "+attr.name+" to ",this[property]+" on ",node);
node.setAttribute(attr.name,this[property]);
}
if(this.templateBindings[property]){
this.templateBindings[property] = this.templateBindings[property].bindElement(node,attr.name,"change");
}else{
throw("Couldn't find "+property+" on ",this.templateBindings);
}
}
}
};
}
}
}
TelepathicElement.templateRegex = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
export class DataBind {
constructor(source) {
let _this = this;
this.elementBindings = [];
this.subscribeFuncs = [];
this.value = source.object[source.property];
this.valueGetter = function () {
//console.debug("DataBind.valueGetter: ",_this);
if(typeof _this === HTMLElement){
return _this;
}else{
return _this.value;
}
};
this.valueSetter = function (val) {
let oldval = _this.value;
_this.value = val;
for (let i = 0; i < _this.elementBindings.length; i++) {
let binding = _this.elementBindings[i];
try{
//console.debug(binding.element," @ ",binding.attribute," was ",oldval," now ",val," type is ",(typeof val));
if(binding.element[binding.attribute] !== val){
if(binding.attribute == "class"){
if(binding.element.classList.contains(oldval)){
binding.element.classList.remove(oldval);
binding.element.classList.add(val);
}
}else{
if(binding.attribute){
if(binding.attribute == "innerHTML"){// && val instanceof HTMLElement){
////console.debug(binding.element," @ ",binding.attribute," = val.innerHTML: ",val.innerHTML.toString());
if(val instanceof HTMLElement){
let oldNode = binding.element.firstChild;
binding.element.replaceChild(val,oldNode);
}else{
binding.element.innerHTML = val;
}
//binding.element.innerHTML = val.innerHTML;
////console.debug("afterwards - binding.element[binding.attribute] : ",binding.element[binding.attribute]);
}else{
if(binding.attribute !== "value"){
//console.debug(binding.element," @ ",binding.attribute," = ",val);
binding.element.setAttribute(binding.attribute,val);
}else{
//console.debug(binding.element," = ",val);
binding.element.value = val;
}
}
}else{
throw("Trying to update value on empty attribute for ",binding.element," with ",_this);
}
}
}
}catch(error){
//console.error(error);
}
}
};
this.bindElement = function (element, attribute, event) {
let binding = {
element: element,
attribute: attribute
};
if (event) {
element.addEventListener(event, function (event) {
_this.valueSetter(element[attribute]);
});
binding.event = event;
}
this.elementBindings.push(binding);
if(_this instanceof HTMLElement){
//console.error("_this is HTMLElement ",_this);
}
if(element instanceof HTMLElement && _this.value instanceof HTMLElement){
//console.error(" element is HTMLElement ",element);
//console.error("_this.value is ",_this.value);
let oldNode = element.firstChild;
//console.debug("oldNode: ",oldNode);
if(oldNode){
element.replaceChild(_this.value,oldNode);
}else{
element.appendChild(_this.value);
}
}else{
element[attribute] = _this.value;
}
if(!event){
event = "*"
}
//console.debug("Binding ",element," @ ",attribute," : ",event," to ",this);
return _this;
};
Object.defineProperty(source.object, source.property, {
get: this.valueGetter,
set: this.valueSetter
});
source.object[source.property] = this.value;
}
}
//Adding this here because there's no other good place to put it
String.prototype.replaceAll = function(search, replacement) {
let target = this;
return target.split(search).join(replacement);
};
const uniq = (a) => Array.from(new Set(a));