-
Notifications
You must be signed in to change notification settings - Fork 0
/
be-written.ts
153 lines (136 loc) · 5.73 KB
/
be-written.ts
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
import {config as beCnfg} from 'be-enhanced/config.js';
import {BE, BEConfig} from 'be-enhanced/BE.js';
import {Actions, AllProps, AP, PAP, ProPAP} from './types';
import { Positractions, PropInfo } from 'trans-render/froop/types';
import {IEnhancement, BEAllProps} from 'trans-render/be/types';
import { StreamOrator } from 'stream-orator/StreamOrator.js';
import {EndUserProps as BeBasedEndUserProps} from 'be-based/types';
class BeWritten extends BE implements Actions{
static override config: BEConfig<AP & BEAllProps, Actions & IEnhancement, any> = {
propDefaults:{
to: '.',
beBased: true,
beOosoom: '!defer'
},
propInfo:{
...beCnfg.propInfo as Partial<{[key in keyof AP]: PropInfo}>,
from:{},
},
actions:{
write: {
ifAllOf: ['from', 'to'],
ifNoneOf: ['defer']
}
}
}
//provide hooks for extending decorators like BeRewritten, BeImporting
async getSet(self: this, so: StreamOrator, target: Element){}
async write(self: this): ProPAP {
const {enhancedElement, shadowRootMode, from, to, reqInit, wrapper, beBased, inProgressCss, inserts, between, once} = self;
if(once){
if(alreadyRequested.has(from!)) {
return {
resolved: true,
};
}
alreadyRequested.add(from!);
}
let target = enhancedElement;
if(to !== '.'){
target = enhancedElement.querySelector(to!)!;
}
if(shadowRootMode !== undefined && target.shadowRoot === null){
target.attachShadow({mode: shadowRootMode});
}
//look for bundling. If bundled, we can assume all the links have been properly adjusted.
const linkTest = (<any>globalThis)[from!];
if(linkTest instanceof HTMLLinkElement && linkTest.hasAttribute('onerror')){
const importedID = linkTest.dataset.imported;
if(importedID !== undefined){
const imported = this.importTempl(importedID, shadowRootMode, target);
if(imported){
return {
resolved: true,
};
}
if(document.readyState === 'loading'){
document.addEventListener('readystatechange', e => {
const imported = this.importTempl(importedID, shadowRootMode, target);
if(imported){
self.resolved = true;
}else{
console.error('bW.404');
}
}, {once: true})
}
return {};
}
}
const {resolve} = await import('trans-render/lib/resolve.js');
let finalURL = resolve(from!);
import('be-a-beacon/behivior.js');
if(beBased !== undefined){
const {emc} = await import('be-based/behivior.js');
const base = (<any>target).beEnhanced.whenResolved(emc);
//const {attach} = await import('be-decorated/upgrade.js');
const beBasedEndUserProps = (typeof beBased === 'boolean' ? {} : beBased) as BeBasedEndUserProps;
let bestGuessAtWhatBaseShouldBe = finalURL!;
let fileName = '';
if(!bestGuessAtWhatBaseShouldBe.endsWith('/')){
//this doesn't seem like it will catch all scenarios -- perhaps we should look at the response headers?
//The assumption here is that if the end of the url has a period in it, like *.html or *.aspx, then the base of the path should not include that part
const split = bestGuessAtWhatBaseShouldBe.split('/');
const last = split.at(-1)!;
if(last.indexOf('.') >= -1){ //TODO: check before ? - query string delimiter
split.pop();
fileName = last;
//console.log({fileName});
}
bestGuessAtWhatBaseShouldBe = split.join('/');
}
beBasedEndUserProps.base = bestGuessAtWhatBaseShouldBe;
beBasedEndUserProps.fileName = fileName;
Object.assign(base, beBasedEndUserProps);
}
const {StreamOrator, beginStream} = await import('stream-orator/StreamOrator.js');
const so = new StreamOrator(target, {
shadowRoot: shadowRootMode,
rootTag: wrapper,
between,
inserts,
});
if(!this.getSet(self, so, target)){
return {
resolved: true,
};
};
if(inProgressCss){
enhancedElement.classList.add('be-written-in-progress');
}
await so.fetch(finalURL, reqInit!);
if(inProgressCss){
enhancedElement.classList.remove('be-written-in-progress');
}
if(beBased){
(<any>target).beEnhanced.beBased.disconnect(target);
}
return {
resolved: true,
};
}
importTempl(importedID: string, shadowRoot: 'open' | 'closed' | undefined, target: Element ){
const templ = (<any>globalThis)[importedID!] as HTMLTemplateElement;
if(templ !== undefined){
const fragment = shadowRoot !== undefined ? target.shadowRoot! : target;
fragment.innerHTML = '';
fragment.appendChild(templ.content.cloneNode(true));
return true;
}
return false;
}
}
interface BeWritten extends AP{}
await BeWritten.bootUp();
const lowerCaseRe = /^[a-zA-Z]/;
const alreadyRequested = new Set<string>();
export {BeWritten}