@@ -8,9 +8,16 @@ var OpenScript = {
88 Router : class {
99 /**
1010 * Current Prefix
11+ * @type {Array<string> }
12+ */
13+ __prefix = [ "" ] ;
14+
15+ /**
16+ * Prefix to append
17+ * To all the runtime URL changes
1118 * @type {string }
1219 */
13- __prefix = "" ;
20+ __runtimePrefix = "" ;
1421
1522 /**
1623 * The routes Map
@@ -60,6 +67,15 @@ var OpenScript = {
6067 } ) ;
6168 }
6269
70+ /**
71+ * Sets the global runtime prefix
72+ * to use when resolving routes
73+ * @param {string } prefix
74+ */
75+ runtimePrefix ( prefix ) {
76+ this . __runtimePrefix = prefix ;
77+ }
78+
6379 /**
6480 * Sets the default path
6581 * @param {string } path
@@ -84,7 +100,9 @@ var OpenScript = {
84100 * @param {function } action action to perform
85101 */
86102 on ( path , action ) {
87- const paths = `${ this . path } /${ this . __prefix } /${ path } ` . split ( "/" ) ;
103+ const paths = `${ this . path } /${ this . __prefix . join (
104+ "/"
105+ ) } /${ path } `. split ( "/" ) ;
88106
89107 let key = null ;
90108 let map = this . map ;
@@ -124,7 +142,7 @@ var OpenScript = {
124142 * @param {string } name
125143 */
126144 prefix ( name ) {
127- this . __prefix = name ;
145+ this . __prefix . push ( name ) ;
128146
129147 return new this . PrefixRoute ( this ) ;
130148 }
@@ -165,6 +183,8 @@ var OpenScript = {
165183
166184 this . reset . value = false ;
167185
186+ broker . send ( "routeChanged" ) ;
187+
168188 return this ;
169189 }
170190
@@ -174,7 +194,7 @@ var OpenScript = {
174194 * @param {object } qs Query string
175195 */
176196 to ( path , qs = { } ) {
177- path = `${ this . path } /${ path } ` . trim ( ) ;
197+ path = `${ this . path } /${ this . __runtimePrefix } / ${ path } ` . trim ( ) ;
178198
179199 let paths = path . split ( "/" ) ;
180200
@@ -305,7 +325,7 @@ var OpenScript = {
305325 group ( func = ( ) => { } ) {
306326 func ( ) ;
307327
308- this . router . __prefix = "" ;
328+ this . router . __prefix . pop ( ) ;
309329
310330 return this . router ;
311331 }
@@ -394,6 +414,11 @@ var OpenScript = {
394414 * The Broker Class
395415 */
396416 Broker : class Broker {
417+ /**
418+ * Should the events be logged as they are fired?
419+ */
420+ #shouldLog = false ;
421+
397422 /**
398423 * TIME DIFFERENCE BEFORE GARBAGE
399424 * COLLECTION
@@ -464,6 +489,7 @@ var OpenScript = {
464489
465490 this . #logs[ event ] = this . #logs[ event ] ?? [ ] ;
466491 this . #logs[ event ] . push ( { timestamp : currentTime ( ) , args : args } ) ;
492+ if ( this . #shouldLog) console . log ( `fired ${ event } : args` , args ) ;
467493
468494 return this . #emitter. emit ( event , ...args ) ;
469495 }
@@ -499,6 +525,14 @@ var OpenScript = {
499525 removeStaleEvents ( ) {
500526 setInterval ( this . clearLogs . bind ( this ) , this . CLEAR_LOGS_AFTER ) ;
501527 }
528+
529+ /**
530+ * If the broker should display events as they are fired
531+ * @param {boolean } shouldLog
532+ */
533+ withLogs ( shouldLog ) {
534+ this . #shouldLog = shouldLog ;
535+ }
502536 } ,
503537
504538 /**
@@ -686,11 +720,11 @@ var OpenScript = {
686720
687721 /**
688722 * JSON.parse
689- * @param {string } string
723+ * @param {string } str
690724 * @returns {EventData }
691725 */
692- static decode ( string ) {
693- return JSON . parse ( string ) ;
726+ static decode ( str ) {
727+ return JSON . parse ( str ) ;
694728 }
695729 /**
696730 * Parse and Event Data
@@ -700,9 +734,36 @@ var OpenScript = {
700734 static parse ( eventData ) {
701735 let ed = OpenScript . EventData . decode ( eventData ) ;
702736
737+ if ( ! "_meta" in ed ) ed . _meta = { } ;
738+ if ( ! "_message" in ed ) ed . _message = { } ;
739+
703740 return {
704- meta : ed ?. _meta ,
705- message : ed ?. _message ,
741+ meta : {
742+ ...ed . _meta ,
743+ has : function ( key ) {
744+ return key in this ;
745+ } ,
746+ get : function ( key , def = null ) {
747+ return this [ key ] ?? def ;
748+ } ,
749+ put : function ( key , value ) {
750+ this [ key ] = value ;
751+ return this ;
752+ } ,
753+ } ,
754+ message : {
755+ ...ed . _message ,
756+ has : function ( key ) {
757+ return key in this ;
758+ } ,
759+ get : function ( key , def = null ) {
760+ return this [ key ] ?? def ;
761+ } ,
762+ put : function ( key , value ) {
763+ this [ key ] = value ;
764+ return this ;
765+ } ,
766+ } ,
706767 encode : function ( ) {
707768 return eData ( this . meta , this . message ) ;
708769 } ,
@@ -1418,11 +1479,19 @@ var OpenScript = {
14181479
14191480 if ( ! this . visible ) attr . style = "display: none;" ;
14201481
1421- const markup = this . render ( ...args ) ;
1482+ let markup = this . render ( ...args , { withCAttr : true } ) ;
1483+ let cAttributes = { } ;
1484+
1485+ if ( markup instanceof HTMLElement ) {
1486+ cAttributes = JSON . parse (
1487+ markup ?. getAttribute ( "c-attr" ) ?? "{}"
1488+ ) ;
1489+ markup . setAttribute ( "c-attr" , "" ) ;
1490+ }
14221491
14231492 attr = { ...attr , component : this , event, eventParams : [ markup ] } ;
14241493
1425- return h [ `ojs-${ this . kebab ( this . name ) } ` ] ( attr , markup ) ;
1494+ return h [ `ojs-${ this . kebab ( this . name ) } ` ] ( attr , markup , cAttributes ) ;
14261495 }
14271496
14281497 /**
@@ -2179,6 +2248,9 @@ var OpenScript = {
21792248 let isComponent = isUpperCase ( name [ 0 ] ) ;
21802249 let root = null ;
21812250
2251+ let componentAttribute = { } ;
2252+ let withCAttr = false ;
2253+
21822254 /**
21832255 * When dealing with a component
21842256 * save the argument for async rendering
@@ -2241,6 +2313,16 @@ var OpenScript = {
22412313 continue ;
22422314 }
22432315
2316+ if ( k === "c_attr" ) {
2317+ componentAttribute = v ;
2318+ continue ;
2319+ }
2320+
2321+ if ( k === "withCAttr" ) {
2322+ withCAttr = true ;
2323+ continue ;
2324+ }
2325+
22442326 let val = `${ v } ` ;
22452327 if ( Array . isArray ( v ) ) val = `${ v . join ( " " ) } ` ;
22462328
@@ -2303,6 +2385,10 @@ var OpenScript = {
23032385
23042386 root . append ( rootFrag ) ;
23052387
2388+ if ( withCAttr ) {
2389+ root . setAttribute ( "c-attr" , JSON . stringify ( componentAttribute ) ) ;
2390+ }
2391+
23062392 root . toString = function ( ) {
23072393 return this . outerHTML ;
23082394 } ;
@@ -2501,8 +2587,12 @@ var OpenScript = {
25012587 * @param {Array<string> } attribute attribute path
25022588 * @returns
25032589 */
2504- $anonymous = ( state , callback = ( state ) => state . value ) => {
2505- return h [ OpenScript . Component . anonymous ( ) ] ( state , callback ) ;
2590+ $anonymous = ( state , callback = ( state ) => state . value , ...args ) => {
2591+ return h [ OpenScript . Component . anonymous ( ) ] (
2592+ state ,
2593+ callback ,
2594+ ...args
2595+ ) ;
25062596 } ;
25072597
25082598 /**
@@ -2989,10 +3079,11 @@ var OpenScript = {
29893079 * @param {OpenScript.State } state
29903080 * @param {Function<OpenScript.State> } callback the function that returns
29913081 * the value to put in the anonymous markup created
3082+ * @param {... } args
29923083 * @returns
29933084 */
2994- v = ( state , callback = ( state ) => state . value ) =>
2995- h . $anonymous ( state , callback ) ;
3085+ v = ( state , callback = ( state ) => state . value , ... args ) =>
3086+ h . $anonymous ( state , callback , ... args ) ;
29963087 /**
29973088 * The markup engine for OpenScript.Js
29983089 */
0 commit comments