Skip to content

Commit 86ad0f0

Browse files
committed
Merge branch 'develop' of https://github.com/levizwannah/js-markup
2 parents 105337f + 30665cb commit 86ad0f0

File tree

1 file changed

+142
-48
lines changed

1 file changed

+142
-48
lines changed

OpenScript.js

Lines changed: 142 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,23 @@ var OpenScript = {
267267
}
268268

269269

270+
/**
271+
* Returns the current URL
272+
* @returns {URL}
273+
*/
274+
url(){
275+
return new URL(window.location.href);
276+
}
277+
278+
/**
279+
* Gets the value after hash in the url
280+
* @returns {string}
281+
*/
282+
hash(){
283+
return this.url().hash.replace('#', '');
284+
}
285+
286+
270287
/**
271288
* Allows Grouping of routes
272289
*/
@@ -349,12 +366,14 @@ var OpenScript = {
349366

350367
// Fire the event
351368
emit(eventName, ...args) {
369+
352370
let fns = this.listeners[eventName];
353371
if (!fns) return false;
354372
fns.forEach((f) => {
355373
f(...args);
356374
});
357375
return true;
376+
358377
}
359378

360379
listenerCount(eventName) {
@@ -375,6 +394,18 @@ var OpenScript = {
375394
*/
376395
Component: class {
377396

397+
/**
398+
* List of events that a component emits
399+
*/
400+
EVENTS = {
401+
rendered: 'rendered', // component is visible on the dom
402+
rerendered: 'rerendered', // component was rerendered
403+
premount: 'premount', // component is ready to register
404+
mounted: 'mounted', // the component is now registered
405+
prebind: 'prebind', // the component is ready to bind
406+
bound: 'bound', // the component has bound
407+
markupBound: 'markup-bound' // a temporary markup has bound
408+
}
378409
/**
379410
* Name of the component
380411
*/
@@ -412,24 +443,20 @@ var OpenScript = {
412443
* @emits pre-mount
413444
*/
414445
async mount(){
415-
this.emit('pre-mount');
416-
417-
h.component(this.name, this);
418-
419-
this.bind();
420-
421446
this.claimListeners();
422-
423-
this.emit('mounted');
447+
this.emit(this.EVENTS.premount);
448+
h.component(this.name, this);
449+
await this.bind();
450+
this.emit(this.EVENTS.mounted);
424451
}
425452

426453
/**
427454
* Emits an event
428455
* @param {string} event
429456
* @param {Array<*>} args
430457
*/
431-
emit(event, ...args) {
432-
this.emitter.emit(event, this, ...args);
458+
emit(event, args = []) {
459+
this.emitter.emit(event, this, event, ...args);
433460
}
434461

435462
/**
@@ -440,23 +467,21 @@ var OpenScript = {
440467
*/
441468
async bind() {
442469

443-
this.emit('pre-bind');
470+
this.emit(this.EVENTS.prebind);
444471

445472
let all = h.dom.querySelectorAll(`ojs-${this.name.toLowerCase()}-tmp`);
446473

447-
448-
449474
for(let elem of all) {
450475

451476
let hId = elem.getAttribute('ojs-key');
452477
let args = [...h.compArgs.get(hId)];
453478

454-
this.wrap(...args, {parent: elem});
479+
this.wrap(...args, {parent: elem, replaceParent: true});
455480

456-
this.emit('markup-bound', [elem, args]);
481+
this.emit(this.EVENTS.markupBound, [elem, args]);
457482
}
458483

459-
this.emit('bound');
484+
this.emit(this.EVENTS.bound);
460485

461486
return true;
462487
}
@@ -465,7 +490,9 @@ var OpenScript = {
465490
* Gets all the listeners for itself and adds them to itself
466491
*/
467492
claimListeners() {
468-
if(h.eventsMap.has(this.name)) return;
493+
494+
if(!h.eventsMap.has(this.name)) return;
495+
469496
let events = h.eventsMap.get(this.name);
470497

471498
for(let event in events) {
@@ -496,7 +523,7 @@ var OpenScript = {
496523
* @returns
497524
*/
498525
getParentAndListen(args){
499-
let final = {index: -1, parent: null, states: [], resetParent: false };
526+
let final = {index: -1, parent: null, states: [], resetParent: false, replaceParent: false };
500527

501528
for(let i in args){
502529

@@ -513,6 +540,11 @@ var OpenScript = {
513540
delete args[i].resetParent;
514541
}
515542

543+
if(args[i].replaceParent) {
544+
final.replaceParent = args[i].replaceParent;
545+
delete args[i].replaceParent;
546+
}
547+
516548
delete args[i].parent;
517549
}
518550

@@ -534,7 +566,7 @@ var OpenScript = {
534566
wrap(...args) {
535567

536568
const lastArg = args[args.length - 1];
537-
let { index, parent, resetParent, states } = this.getParentAndListen(args);
569+
let { index, parent, resetParent, states, replaceParent } = this.getParentAndListen(args);
538570

539571
// check if the render was called due to a state change
540572
if(lastArg && lastArg['called-by-state-change']) {
@@ -550,9 +582,7 @@ var OpenScript = {
550582

551583
let arg = this.argsMap.get(e.getAttribute("uuid"));
552584

553-
this.render(...arg, { parent: e });
554-
555-
this.emit('re-rendered', arg);
585+
this.render(...arg, { parent: e, component: this, event: this.EVENTS.rerendered, eventParams: [] });
556586
});
557587

558588
return;
@@ -562,13 +592,16 @@ var OpenScript = {
562592

563593
this.argsMap.set(uuid, args ?? []);
564594

565-
let attr = {uuid, parent, resetParent};
595+
let attr = {uuid, parent, resetParent, replaceParent};
566596

567597
states.forEach((s) => {
568598
attr[`s-${s.id}`] = s.id;
569599
});
570600

571-
return h[`ojs-${this.name}`](attr, this.render(...args));
601+
return h[`ojs-${this.name}`](attr, this.render(...args), {
602+
component: this,
603+
event: this.EVENTS.rendered,
604+
eventParams: []});
572605
}
573606

574607
/**
@@ -735,7 +768,7 @@ var OpenScript = {
735768
}
736769
}
737770
else {
738-
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.`)
771+
console.log(`${referenceName[0]} already exists. If you have multiple contexts in the file in ${qualifiedName}, then you can use context('[contextName]Context') to access them.`)
739772
}
740773

741774
return this.context(referenceName);
@@ -1169,10 +1202,18 @@ var OpenScript = {
11691202
return this.compMap.get(name).wrap(...args);
11701203
}
11711204

1205+
let component;
1206+
let event = '';
1207+
let eventParams = [];
1208+
1209+
/**
1210+
* @type {DocumentFragment|HTMLElement}
1211+
*/
11721212
let parent = null;
1213+
11731214
let emptyParent = false;
1215+
let replaceParent = false;
11741216
let rootFrag = new DocumentFragment();
1175-
let finalRoot = new DocumentFragment();
11761217

11771218
const isUpperCase = (string) => /^[A-Z]*$/.test(string);
11781219
let isComponent = isUpperCase(name[0]);
@@ -1209,8 +1250,28 @@ var OpenScript = {
12091250
}
12101251

12111252
if(k === "resetParent" && typeof v === "boolean") {
1212-
emptyParent = v;
1213-
// console.log(k, name, emptyParent);
1253+
emptyParent = v;
1254+
continue;
1255+
}
1256+
1257+
if(k === "event" && typeof v === "string") {
1258+
event = v;
1259+
continue;
1260+
}
1261+
1262+
if(k === "replaceParent" && typeof v === "boolean") {
1263+
replaceParent = v;
1264+
continue;
1265+
}
1266+
1267+
if(k === "eventParams") {
1268+
if(!Array.isArray(v)) v = [v];
1269+
eventParams = v;
1270+
continue;
1271+
}
1272+
1273+
if(k === "component" && v instanceof OpenScript.Component){
1274+
component = v;
12141275
continue;
12151276
}
12161277

@@ -1225,7 +1286,6 @@ var OpenScript = {
12251286
}
12261287
}
12271288

1228-
// console.log(`empty parent is: ${name}`, emptyParent);
12291289

12301290
for(let arg of args){
12311291

@@ -1257,20 +1317,24 @@ var OpenScript = {
12571317
}
12581318

12591319
root.append(rootFrag);
1260-
finalRoot.append(root);
12611320

12621321
if(parent) {
1263-
// console.log('parent found', `${parent}`, emptyParent);
1322+
12641323
if(emptyParent){
12651324
parent.textContent = '';
1266-
// console.log('emptied parent');
12671325
}
12681326

1269-
parent.append(finalRoot);
1270-
return finalRoot;
1327+
if(replaceParent) {
1328+
parent.replaceWith(root);
1329+
}
1330+
else {
1331+
parent.append(root);
1332+
}
1333+
if(component) component.emit(event, eventParams);
1334+
return root;
12711335
}
12721336

1273-
return finalRoot;
1337+
return root;
12741338
}
12751339

12761340
/**
@@ -1314,28 +1378,46 @@ var OpenScript = {
13141378

13151379
/**
13161380
* Adds an event listener to a component
1317-
* @param {string} component component name
1381+
* @param {string|Array<string>} component component name
13181382
* @param {string} event event name
13191383
* @param {...function} listeners listeners
13201384
*/
13211385
on = (component, event, ...listeners) =>{
1386+
let components = component;
13221387

1323-
if(this.compMap.has(component)) {
1388+
if(!Array.isArray(component)) components = [component];
13241389

1325-
if(!this.emitter(component).listeners[event]) {
1326-
this.emitter(component).listeners[event] = [];
1390+
for(let component of components) {
1391+
1392+
if(/\./.test(component)){
1393+
let tmp = component.split('.').filter(e => e);
1394+
component = tmp[0];
1395+
listeners.push(event);
1396+
event = tmp[1];
13271397
}
13281398

1329-
return this.emitter(component).listeners[event].push(...listeners);
1330-
}
1399+
if(this.compMap.has(component)) {
1400+
1401+
if(!this.emitter(component).listeners[event]) {
1402+
this.emitter(component).listeners[event] = [];
1403+
}
13311404

1332-
if(!this.eventsMap.has(component)){
1333-
this.eventsMap.set(component, {});
1334-
this.eventsMap.get(component)[event] = listeners;
1335-
return;
1405+
this.emitter(component).listeners[event].push(...listeners);
1406+
continue;
1407+
}
1408+
1409+
if(!this.eventsMap.has(component)){
1410+
this.eventsMap.set(component, {});
1411+
this.eventsMap.get(component)[event] = listeners;
1412+
continue;
1413+
}
1414+
1415+
if(!this.eventsMap.get(component)[event]) {
1416+
this.eventsMap.get(component)[event] = [];
1417+
}
1418+
1419+
this.eventsMap.get(component)[event].push(...listeners);
13361420
}
1337-
1338-
return this.eventsMap.get(component)[event].push(...listeners);
13391421
}
13401422

13411423
/**
@@ -1378,6 +1460,8 @@ var OpenScript = {
13781460
return value;
13791461
}
13801462

1463+
if(value?.length === 0) return this.dom.createTextNode('');
1464+
13811465
let tmp = this.dom.createElement("ojs-group");
13821466
tmp.innerHTML = value;
13831467

@@ -1669,6 +1753,11 @@ var OpenScript = {
16691753
*/
16701754
loader = new OpenScript.AutoLoader();
16711755

1756+
/**
1757+
* Used to Import any File
1758+
*/
1759+
autoload = new OpenScript.AutoLoader();
1760+
16721761
/**
16731762
* Create a namespace if it doesn't exists and returns it.
16741763
*/
@@ -1904,7 +1993,12 @@ const {
19041993
/**
19051994
* The router object
19061995
*/
1907-
route
1996+
route,
1997+
1998+
/**
1999+
* Used to Autoload Files
2000+
*/
2001+
autoload
19082002

19092003
} = new OpenScript.Initializer();
19102004

0 commit comments

Comments
 (0)