Skip to content

Commit 07c4ab5

Browse files
committed
finished the class group loading
1 parent 3d76c2f commit 07c4ab5

File tree

18 files changed

+409
-412
lines changed

18 files changed

+409
-412
lines changed

OpenScript.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -596,20 +596,25 @@ var OpenScript = {
596596

597597
/**
598598
* Asynchronously loads a context
599-
* @param {string} referenceName
599+
* @param {string|Array<string>} referenceName
600600
* @param {string} qualifiedName
601601
* @param {boolean} fetch
602602
*/
603603
load(referenceName, qualifiedName, fetch = false) {
604-
let c = this.map.get(referenceName);
604+
if(!Array.isArray(referenceName)) referenceName = [ referenceName ];
605+
606+
for(let name of referenceName) {
607+
let c = this.map.get(name);
605608

606-
if(!c) {
607-
this.map.set(referenceName, new OpenScript.Context());
609+
if(!c) {
610+
this.map.set(name, new OpenScript.Context());
611+
}
608612
}
609613

614+
610615
this.put(referenceName, qualifiedName, fetch);
611616

612-
return this.map.get(referenceName);
617+
return referenceName.length == 1 ? this.map.get(referenceName[0]) : this.map;
613618
}
614619

615620
/**
@@ -620,7 +625,9 @@ var OpenScript = {
620625
* @param {boolean} load Should this context be loaded automatically
621626
*/
622627
put = async (referenceName, qualifiedName, fetch = false) => {
623-
let c = this.map.get(referenceName);
628+
if(!Array.isArray(referenceName)) referenceName = [referenceName];
629+
630+
let c = this.map.get(referenceName[0]);
624631

625632
let shouldFetch = false;
626633

@@ -643,7 +650,6 @@ var OpenScript = {
643650
}
644651

645652
let counter = 0;
646-
if(!Array.isArray(referenceName)) referenceName = [referenceName];
647653

648654
for(let [k, v] of Context) {
649655

@@ -666,6 +672,9 @@ var OpenScript = {
666672
counter++;
667673
}
668674
}
675+
else {
676+
console.error(`${referenceName[0]} already exists. If you have multiple contexts in the file in ${qualifiedName}, then you can use context('[contextName]Context') to access them.`)
677+
}
669678

670679
return this.context(referenceName);
671680
}

docs/js/OpenScript.js

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -596,20 +596,25 @@ var OpenScript = {
596596

597597
/**
598598
* Asynchronously loads a context
599-
* @param {string} referenceName
599+
* @param {string|Array<string>} referenceName
600600
* @param {string} qualifiedName
601601
* @param {boolean} fetch
602602
*/
603603
load(referenceName, qualifiedName, fetch = false) {
604-
let c = this.map.get(referenceName);
604+
if(!Array.isArray(referenceName)) referenceName = [ referenceName ];
605+
606+
for(let name of referenceName) {
607+
let c = this.map.get(name);
605608

606-
if(!c) {
607-
this.map.set(referenceName, new OpenScript.Context());
609+
if(!c) {
610+
this.map.set(name, new OpenScript.Context());
611+
}
608612
}
609613

614+
610615
this.put(referenceName, qualifiedName, fetch);
611616

612-
return this.map.get(referenceName);
617+
return referenceName.length == 1 ? this.map.get(referenceName[0]) : this.map;
613618
}
614619

615620
/**
@@ -620,7 +625,9 @@ var OpenScript = {
620625
* @param {boolean} load Should this context be loaded automatically
621626
*/
622627
put = async (referenceName, qualifiedName, fetch = false) => {
623-
let c = this.map.get(referenceName);
628+
if(!Array.isArray(referenceName)) referenceName = [referenceName];
629+
630+
let c = this.map.get(referenceName[0]);
624631

625632
let shouldFetch = false;
626633

@@ -643,7 +650,6 @@ var OpenScript = {
643650
}
644651

645652
let counter = 0;
646-
if(!Array.isArray(referenceName)) referenceName = [referenceName];
647653

648654
for(let [k, v] of Context) {
649655

@@ -666,6 +672,9 @@ var OpenScript = {
666672
counter++;
667673
}
668674
}
675+
else {
676+
console.error(`${referenceName[0]} already exists. If you have multiple contexts in the file in ${qualifiedName}, then you can use context('[contextName]Context') to access them.`)
677+
}
669678

670679
return this.context(referenceName);
671680
}
@@ -1364,6 +1373,11 @@ var OpenScript = {
13641373
*/
13651374
AutoLoader: class ClassLoader {
13661375

1376+
/**
1377+
* Keeps track of the files that have been loaded
1378+
*/
1379+
history = new Map();
1380+
13671381
/**
13681382
* The Directory or URL in which all JS files are located
13691383
*/
@@ -1403,32 +1417,15 @@ var OpenScript = {
14031417

14041418
/**
14051419
*
1406-
* @param {string} className script name without the .js.
1420+
* @param {string} fileName script name without the .js.
14071421
*/
1408-
async req(className){
1422+
async req(fileName){
14091423

1410-
let names = className.split(/\./);
1411-
let obj;
1412-
1413-
// check if the object already exists
1414-
for(let n of names){
1415-
1416-
if(!obj) {
1417-
obj = window[n];
1418-
continue;
1419-
}
1420-
1421-
if(!obj) {
1422-
obj = null;
1423-
break;
1424-
}
1425-
1426-
obj = obj[n];
1427-
}
1428-
1429-
if(obj) return obj;
1430-
1431-
let response = await fetch(`${this.dir}/${this.normalize(className)}${this.extension}?v=${this.version}`);
1424+
let names = fileName.split(/\./);
1425+
1426+
if(this.history.has(`${this.dir}.${fileName}`)) return this.history.get(`${this.dir}.${fileName}`);
1427+
1428+
let response = await fetch(`${this.dir}/${this.normalize(fileName)}${this.extension}?v=${this.version}`);
14321429

14331430
let classes = await response.text();
14341431

@@ -1444,7 +1441,6 @@ var OpenScript = {
14441441
let codeMap = new Map();
14451442

14461443
let prefixArray = [...names];
1447-
prefixArray.pop();
14481444

14491445
let prefix = prefixArray.join('.');
14501446
if(prefix.length > 0 && !/^\s+$/.test(prefix)) prefix += '.';
@@ -1498,7 +1494,7 @@ var OpenScript = {
14981494
}
14991495
else {
15001496
let replacement = inheritance[0].replace(original, parent);
1501-
console.log(k, arr[1]);
1497+
//console.log(k, arr[1]);
15021498

15031499
let c = arr[1].replace(inheritance[0], replacement);
15041500
arr[1] = c;
@@ -1516,14 +1512,15 @@ var OpenScript = {
15161512
}
15171513
}
15181514

1515+
this.history.set(`${this.dir}.${fileName}`, codeMap);
15191516

15201517
return codeMap;
15211518
}
15221519

1523-
async include(className){
1520+
async include(fileName){
15241521

15251522
try{
1526-
return await this.req(this.normalize(className));
1523+
return await this.req(fileName);
15271524
} catch(e) {}
15281525

15291526
return null;

docs/js/app.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
ojs(async e => {
2-
2+
// init global components
3+
req('Groups.Common');
4+
req('Groups.Common');
5+
36
// init contexts
4-
let rc = fetchContext('root', 'Root');
5-
let cc = fetchContext('config', 'Config');
7+
let rc = fetchContext(['root', 'data', 'config'], 'Groups.UrgentContexts').get('root');
8+
let cc = context('config');
69

710
cc.states({
811
logo: {},

docs/js/components/App.js

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
class Logo extends OpenScript.Component {
2+
3+
constructor() {
4+
super();
5+
}
6+
7+
render(...args) {
8+
return h.div(
9+
{
10+
class: 'docs-logo-wrapper'
11+
},
12+
13+
h.div(
14+
{ class: 'site-logo' },
15+
h.a(
16+
{
17+
class: 'navbar-brand',
18+
href: 'index.html'
19+
},
20+
21+
h.img(
22+
{
23+
class: 'logo-icon me-2',
24+
src: route.baseUrl('docs/assets/images/coderdocs-logo.svg'),
25+
alt: 'OpenScript Logo',
26+
}
27+
),
28+
29+
h.span(
30+
{ class: 'logo-text' },
31+
v(context('config').logo, (state) => state.value.text)
32+
),
33+
34+
h.span(
35+
{ class: 'text-alt'},
36+
v(context('config').logo, (state) => state.value.alt)
37+
)
38+
)
39+
)
40+
)
41+
}
42+
43+
}
44+
45+
class SocialList extends OpenScript.Component {
46+
47+
async mount() {
48+
await super.mount();
49+
req('Groups.Icons');
50+
}
51+
52+
53+
render(arr, ...args) {
54+
let listItems = [];
55+
56+
for(let obj of arr) {
57+
listItems.push(
58+
h.li(
59+
{ class: 'list-inline-item'},
60+
h.SocialIcon(obj.link, obj.icon)
61+
)
62+
)
63+
}
64+
65+
return h.ul(
66+
{ class: 'social-list list-inline mx-md-3 mx-lg-5 mb-0 d-none d-lg-flex'},
67+
listItems,
68+
...args
69+
)
70+
}
71+
72+
}
73+
74+
class SearchForm extends OpenScript.Component {
75+
constructor() {
76+
super();
77+
78+
this.name = 'Search';
79+
}
80+
81+
render(type, ...args) {
82+
switch(type) {
83+
case 'hero': return this.hero(...args);
84+
85+
default: return this.hero(...args);
86+
}
87+
}
88+
89+
hero(...args) {
90+
return h.form(
91+
{class: 'search-form w-100'},
92+
93+
h.input( {
94+
type: 'text',
95+
placeholder: 'Search the docs...',
96+
class: 'form-control search-input underlined input-data'
97+
}),
98+
99+
h.button(
100+
{
101+
type: 'submit',
102+
class: 'btn search-btn',
103+
value: 's'
104+
},
105+
106+
h.i(
107+
{class: 'fas fa-search'}
108+
)
109+
),
110+
111+
...args
112+
)
113+
}
114+
115+
}

0 commit comments

Comments
 (0)