@@ -12,11 +12,6 @@ window.Modulo = class Modulo {
12
12
this . stores = { } ; // Global data store (by default, only used by State)
13
13
}
14
14
15
- start ( elem , callback = null ) { // XXX DEAD CODE
16
- this . loadFromDOM ( elem , null , true ) ;
17
- this . preprocessAndDefine ( callback ) ;
18
- }
19
-
20
15
register ( type , cls , defaults = undefined ) {
21
16
type = ( `${ type } s` in this . registry ) ? `${ type } s` : type ; // pluralize
22
17
if ( type in this . registry . registryCallbacks ) {
@@ -45,6 +40,30 @@ window.Modulo = class Modulo {
45
40
return inst ;
46
41
}
47
42
43
+ instanceParts ( def , extra , parts = { } ) {
44
+ // Loop through all children, instancing each class with configuration
45
+ const allNames = [ def . DefinitionName ] . concat ( def . ChildrenNames ) ;
46
+ for ( const def of allNames . map ( name => this . definitions [ name ] ) ) {
47
+ parts [ def . RenderObj || def . Name ] = this . instance ( def , extra ) ;
48
+ }
49
+ return parts ;
50
+ }
51
+
52
+ lifecycle ( parts , renderObj , lifecycleNames ) {
53
+ for ( const lifecycleName of lifecycleNames ) {
54
+ const methodName = lifecycleName + 'Callback' ;
55
+ for ( const [ name , obj ] of Object . entries ( parts ) ) {
56
+ if ( ! ( methodName in obj ) ) {
57
+ continue ; // Skip if obj has not registered callback
58
+ }
59
+ const result = obj [ methodName ] . call ( obj , renderObj ) ;
60
+ if ( result ) {
61
+ renderObj [ obj . conf . RenderObj || obj . conf . Name ] = result ;
62
+ }
63
+ }
64
+ }
65
+ }
66
+
48
67
preprocessAndDefine ( cb ) {
49
68
this . fetchQueue . wait ( ( ) => {
50
69
this . repeatProcessors ( null , 'DefBuilders' , ( ) => {
@@ -370,20 +389,15 @@ modulo.register('util', function initComponentClass (modulo, def, cls) {
370
389
this . isModulo = true ;
371
390
this . originalHTML = null ;
372
391
this . originalChildren = [ ] ;
373
- this . cparts = { } ;
374
- // Loop through all children, instancing each class with configuration
375
- const allNames = [ def . DefinitionName ] . concat ( def . ChildrenNames ) ;
376
- for ( const def of allNames . map ( name => modulo . definitions [ name ] ) ) {
377
- this . cparts [ def . RenderObj || def . Name ] = modulo . instance ( def , { element : this } ) ;
378
- }
392
+ this . cparts = modulo . instanceParts ( def , { element : this } ) ;
379
393
} ;
380
394
381
395
// Mount the element, optionally "merging" in the modulo-original-html attr
382
396
cls . prototype . parsedCallback = function parsedCallback ( ) {
383
397
const htmlOriginal = this . getAttribute ( 'modulo-original-html' ) ;
384
398
const original = ( ( ! htmlOriginal || htmlOriginal === '' ) ? this :
385
399
modulo . registry . utils . makeDiv ( htmlOriginal ) ) ;
386
- this . cparts . component . lifecycle ( [ 'initialized' ] ) ;
400
+ this . cparts . component . _lifecycle ( [ 'initialized' ] ) ;
387
401
this . rerender ( original ) ; // render and re-mount it's own childNodes
388
402
if ( this . hasAttribute ( 'modulo-original-html' ) ) {
389
403
const { reconciler } = this . cparts . component ;
@@ -419,10 +433,7 @@ modulo.register('processor', function mainRequire (modulo, conf, value) {
419
433
} ) ;
420
434
421
435
modulo . register ( 'cpart' , class Artifact {
422
- // TODO: Refactor Component logic to be shared with Artifact (maybe using
423
- // preprocessors?). Refactor this to use something more generalized for
424
- // children, so it shares code flow with component. Generally, this is a mess!
425
- static build ( modulo , def ) {
436
+ buildCommandCallback ( { modulo, def } ) {
426
437
const finish = ( ) => {
427
438
const { saveFileAs, hash } = modulo . registry . utils ;
428
439
const children = ( def . ChildrenNames || [ ] ) . map ( n => modulo . definitions [ n ] ) ;
@@ -491,6 +502,7 @@ modulo.register('cpart', class Artifact {
491
502
} , {
492
503
Contains : 'cparts' ,
493
504
DefinedAs : 'name' ,
505
+ RenderObj : 'artifact' ,
494
506
DefLoaders : [ 'DefTarget' , 'DefinedAs' , 'Src' , 'Content' ] ,
495
507
} ) ;
496
508
@@ -541,6 +553,7 @@ modulo.register('coreDef', class Component {
541
553
return ${ className } ;
542
554
` ;
543
555
}
556
+
544
557
rerender ( original = null ) {
545
558
if ( original ) {
546
559
if ( this . element . originalHTML === null ) {
@@ -549,28 +562,17 @@ modulo.register('coreDef', class Component {
549
562
this . element . originalChildren = Array . from (
550
563
original . hasChildNodes ( ) ? original . childNodes : [ ] ) ;
551
564
}
552
- this . lifecycle ( [ 'prepare' , 'render' , 'reconcile' , 'update' ] ) ;
565
+ this . _lifecycle ( [ 'prepare' , 'render' , 'reconcile' , 'update' ] ) ;
553
566
}
554
567
555
568
getCurrentRenderObj ( ) {
556
569
return ( this . element . eventRenderObj || this . element . renderObj || this . element . initRenderObj ) ;
557
570
}
558
571
559
- lifecycle ( lifecycleNames , rObj = { } ) {
572
+ _lifecycle ( lifecycleNames , rObj = { } ) {
560
573
const renderObj = Object . assign ( { } , rObj , this . getCurrentRenderObj ( ) ) ;
561
574
this . element . renderObj = renderObj ;
562
- for ( const lifecycleName of lifecycleNames ) {
563
- const methodName = lifecycleName + 'Callback' ;
564
- for ( const [ name , obj ] of Object . entries ( this . element . cparts ) ) {
565
- if ( ! ( methodName in obj ) ) {
566
- continue ; // Skip if obj has not registered callback
567
- }
568
- const result = obj [ methodName ] . call ( obj , renderObj ) ;
569
- if ( result ) {
570
- renderObj [ obj . conf . RenderObj || obj . conf . Name ] = result ;
571
- }
572
- }
573
- }
575
+ this . modulo . lifecycle ( this . element . cparts , renderObj , lifecycleNames ) ;
574
576
//this.element.renderObj = null; // ?rendering is over, set to null
575
577
}
576
578
@@ -645,10 +647,10 @@ modulo.register('coreDef', class Component {
645
647
}
646
648
647
649
handleEvent ( func , payload , ev ) {
648
- this . lifecycle ( [ 'event' ] ) ;
650
+ this . _lifecycle ( [ 'event' ] ) ;
649
651
const { value } = ( ev . target || { } ) ; // Get value if is <INPUT>, etc
650
652
func . call ( null , payload === undefined ? value : payload , ev ) ;
651
- this . lifecycle ( [ 'eventCleanup' ] ) ; // todo: should this go below rerender()?
653
+ this . _lifecycle ( [ 'eventCleanup' ] ) ; // todo: should this go below rerender()?
652
654
if ( this . attrs . rerender !== 'manual' ) {
653
655
this . element . rerender ( ) ; // always rerender after events
654
656
}
@@ -1736,16 +1738,12 @@ modulo.register('engine', class Reconciler {
1736
1738
}
1737
1739
1738
1740
applyPatch ( node , method , arg , arg2 ) { // take that, rule of 3!
1739
- //if (!node || !node[method]) { console.error('NO NODE:', node, method, arg, arg2) } // XXX
1740
1741
if ( method === 'node-value' ) {
1741
1742
node . nodeValue = arg ;
1742
1743
} else if ( method === 'insertBefore' ) {
1743
1744
node . insertBefore ( arg , arg2 ) ; // Needs 2 arguments
1744
- } else if ( method === 'attr-append' ) { // Append string to existing
1745
- node . setAttribute ( arg , ( node . getAttribute ( arg ) || '' ) + arg2 ) ; // TODO: DEAD CODE
1746
1745
} else if ( method . startsWith ( 'directive-' ) ) {
1747
- // TODO: Possibly, remove 'directive-' prefix, unnecessary
1748
- method = method . substr ( 'directive-' . length ) ;
1746
+ method = method . substr ( 'directive-' . length ) ; // TODO: RM prefix (or generalizze)
1749
1747
node [ method ] . call ( node , arg ) ; // invoke directive method
1750
1748
} else {
1751
1749
node [ method ] . call ( node , arg ) ; // invoke method
@@ -1858,9 +1856,22 @@ modulo.register('command', function build (modulo, opts = {}) {
1858
1856
const filter = opts . filter || ( ( { Type } ) => Type === 'Artifact' ) ;
1859
1857
modulo . config . IS_BUILD = true ;
1860
1858
opts . callback = opts . callback || ( ( ) => { } ) ;
1859
+ /*
1860
+ // TODO: Use this to refactor modulo-original-html into Component class
1861
+ for (const elem of document.querySelectorAll('*')) {
1862
+ // Escape hatch for CParts / Scripts to hook in on a per-component basis
1863
+ if (elem.isModulo && elem.cparts && elem.cparts.component) {
1864
+ elem.cparts.component._lifecycle([ 'prepareBuild', 'build' ]);
1865
+ }
1866
+ }
1867
+ */
1861
1868
const artifacts = Object . values ( modulo . definitions ) . filter ( filter ) ;
1862
1869
const buildNext = ( ) => {
1863
- modulo . registry . cparts . Artifact . build ( modulo , artifacts . shift ( ) ) ;
1870
+ const artifact = artifacts . shift ( ) ;
1871
+ const artifactParts = modulo . instanceParts ( artifact , { } ) ;
1872
+ const buildObj = { modulo, def : artifact } ;
1873
+ modulo . lifecycle ( artifactParts , buildObj , [ 'buildCommand' ] ) ;
1874
+ //modulo.repeatProcessors(artifacts, 'ArtifactBuilders');
1864
1875
modulo . fetchQueue . enqueueAll ( artifacts . length > 0 ? buildNext : opts . callback ) ;
1865
1876
} ;
1866
1877
modulo . assert ( artifacts . length , 'Build filter produced no artifacts' ) ;
0 commit comments