1
1
/*!
2
- Hype Reactive Content 1.0.4
2
+ Hype Reactive Content 1.0.5
3
3
copyright (c) 2022 Max Ziebell, (https://maxziebell.de). MIT-license
4
4
*/
5
5
/*
@@ -9,15 +9,16 @@ copyright (c) 2022 Max Ziebell, (https://maxziebell.de). MIT-license
9
9
* 1.0.2 This version is being released to get JsDelivr to update
10
10
* 1.0.3 Changed listener syntax to sentence structure
11
11
* 1.0.4 Fixed falsy values not being updated
12
+ * 1.0.5 Added Hype Action Events support, running code through triggerAction
12
13
*/
13
14
if ( "HypeReactiveContent" in window === false ) window [ 'HypeReactiveContent' ] = ( function ( ) {
14
15
/**
15
- * @function enableReactiveObject
16
- * @param {object } obj - the object to be made reactive.
17
- * @param {function } callback - the function to be called when a property of the object is changed.
18
- * @returns {object } - the reactive object.
19
- */
20
- function enableReactiveObject ( obj , callback ) {
16
+ * @function enableReactiveObject
17
+ * @param {object } obj - the object to be made reactive.
18
+ * @param {function } callback - the function to be called when a property of the object is changed.
19
+ * @returns {object } - the reactive object.
20
+ */
21
+ function enableReactiveObject ( obj , callback ) {
21
22
const handler = {
22
23
get ( target , key , receiver ) {
23
24
const result = Reflect . get ( target , key , receiver ) ;
@@ -57,41 +58,65 @@ if("HypeReactiveContent" in window === false) window['HypeReactiveContent'] = (f
57
58
} ;
58
59
}
59
60
61
+ /**
62
+ * @function runCode
63
+ * This is used if there Hype Action Events isn't found
64
+ * @param {string } code JavaScript to run
65
+ * @param {HYPE.documents.API } hypeDocument for context
66
+ */
67
+ function runCode ( code , hypeDocument ) {
68
+ try {
69
+ return new Function ( 'hypeDocument' , 'customData' , 'with(customData){ ' + code + '}' ) ( hypeDocument , hypeDocument . customData ) ;
70
+ } catch ( e ) {
71
+ console . error ( e )
72
+ }
73
+ }
74
+
60
75
/**
61
76
* @function HypeDocumentLoad
62
77
* @param {object } hypeDocument - the hypeDocument
63
78
* @param {object } element - the element
64
79
* @param {object } event - the event
65
80
*/
66
- function HypeDocumentLoad ( hypeDocument , element , event ) {
81
+ function HypeDocumentLoad ( hypeDocument , element , event ) {
82
+
67
83
hypeDocument . updateContent = function ( key , value ) {
68
84
if ( key !== undefined && value !== undefined ) hypeDocument . triggerCustomBehaviorNamed ( key + ' equals ' + ( typeof value === 'string' ? '"' + value + '"' : value ) )
69
85
if ( key !== undefined ) hypeDocument . triggerCustomBehaviorNamed ( key + ' was updated' )
70
86
let sceneElm = document . getElementById ( hypeDocument . currentSceneId ( ) ) ;
71
87
sceneElm . querySelectorAll ( '[data-content], [data-visibility]' ) . forEach ( function ( elm ) {
72
- var content = elm . getAttribute ( 'data-content' ) ;
73
- var contentReturn = hypeDocument . runCode ( content , true ) ;
74
- if ( content ) elm . innerHTML = contentReturn !== undefined ? contentReturn : '' ;
75
- var visibility = elm . getAttribute ( 'data-visibility' ) ;
76
- if ( visibility ) elm . style . visibility = hypeDocument . runCode ( visibility , true ) ? 'visible' : 'hidden' ;
77
-
88
+ let content = elm . getAttribute ( 'data-content' ) ;
89
+ let visibility = elm . getAttribute ( 'data-visibility' ) ;
90
+ if ( "HypeActionEvents" in window === false ) {
91
+ if ( content ) {
92
+ let contentReturn = runCode ( 'return ' + content , hypeDocument ) ;
93
+ elm . innerHTML = contentReturn !== undefined ? contentReturn : '' ;
94
+ }
95
+ if ( visibility ) {
96
+ let visibilityReturn = runCode ( 'return ' + visibility , hypeDocument ) ;
97
+ elm . style . visibility = visibilityReturn ? 'visible' : 'hidden' ;
98
+ }
99
+ } else {
100
+ if ( content ) {
101
+ let contentReturn = hypeDocument . triggerAction ( 'return ' + content , { element : elm , event : { type :'HypeReactiveContent' } } ) ;
102
+ elm . innerHTML = contentReturn !== undefined ? contentReturn : '' ;
103
+ }
104
+ if ( visibility ) {
105
+ let visibilityReturn = hypeDocument . triggerAction ( 'return ' + visibility , { element : elm , event : { type :'HypeReactiveContent' } } ) ;
106
+ elm . style . visibility = visibilityReturn ? 'visible' : 'hidden' ;
107
+ }
108
+ }
78
109
} )
79
110
}
80
- hypeDocument . runCode = function ( code , ret ) {
81
- try {
82
- if ( ret ) code = 'return ' + code ;
83
- return new Function ( 'hypeDocument' , 'customData' , 'with(customData){ ' + code + '}' ) ( hypeDocument , hypeDocument . customData ) ;
84
- } catch ( e ) {
85
- console . error ( e )
86
- }
87
- }
111
+
88
112
hypeDocument . customData = enableReactiveObject ( hypeDocument . customData , debounceByRequestFrame ( hypeDocument . updateContent ) ) ;
89
113
if ( hypeDocument . functions ( ) . HypeReactiveContent ) hypeDocument . functions ( ) . HypeReactiveContent ( hypeDocument , element , event ) ;
90
114
}
91
115
92
116
function HypeTriggerCustomBehavior ( hypeDocument , element , event ) {
117
+ if ( "HypeActionEvents" in window !== false ) return ;
93
118
var code = event . customBehaviorName ;
94
- if ( / [ ; = ( ) ] / . test ( code ) ) hypeDocument . runCode ( code ) ;
119
+ if ( / [ ; = ( ) ] / . test ( code ) ) runCode ( code , hypeDocument ) ;
95
120
}
96
121
97
122
function HypeScenePrepareForDisplay ( hypeDocument , element , event ) {
@@ -100,10 +125,12 @@ if("HypeReactiveContent" in window === false) window['HypeReactiveContent'] = (f
100
125
101
126
if ( "HYPE_eventListeners" in window === false ) { window . HYPE_eventListeners = Array ( ) ; }
102
127
window . HYPE_eventListeners . push ( { "type" : "HypeDocumentLoad" , "callback" : HypeDocumentLoad } ) ;
103
- window . HYPE_eventListeners . push ( { "type" : "HypeTriggerCustomBehavior" , "callback" : HypeTriggerCustomBehavior } ) ;
104
128
window . HYPE_eventListeners . push ( { type : "HypeScenePrepareForDisplay" , callback : HypeScenePrepareForDisplay } ) ;
105
-
129
+ if ( "HypeActionEvents" in window === false ) {
130
+ window . HYPE_eventListeners . push ( { "type" : "HypeTriggerCustomBehavior" , "callback" : HypeTriggerCustomBehavior } ) ;
131
+ }
132
+
106
133
return {
107
- version : '1.0.4 '
134
+ version : '1.0.5 '
108
135
} ;
109
136
} ) ( ) ;
0 commit comments