Skip to content

Commit bd3b9b1

Browse files
v1.0.5
1 parent d889d99 commit bd3b9b1

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

HypeReactiveContent.js

+53-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
Hype Reactive Content 1.0.4
2+
Hype Reactive Content 1.0.5
33
copyright (c) 2022 Max Ziebell, (https://maxziebell.de). MIT-license
44
*/
55
/*
@@ -9,15 +9,16 @@ copyright (c) 2022 Max Ziebell, (https://maxziebell.de). MIT-license
99
* 1.0.2 This version is being released to get JsDelivr to update
1010
* 1.0.3 Changed listener syntax to sentence structure
1111
* 1.0.4 Fixed falsy values not being updated
12+
* 1.0.5 Added Hype Action Events support, running code through triggerAction
1213
*/
1314
if("HypeReactiveContent" in window === false) window['HypeReactiveContent'] = (function () {
1415
/**
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) {
2122
const handler = {
2223
get(target, key, receiver) {
2324
const result = Reflect.get(target, key, receiver);
@@ -57,41 +58,65 @@ if("HypeReactiveContent" in window === false) window['HypeReactiveContent'] = (f
5758
};
5859
}
5960

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+
6075
/**
6176
* @function HypeDocumentLoad
6277
* @param {object} hypeDocument - the hypeDocument
6378
* @param {object} element - the element
6479
* @param {object} event - the event
6580
*/
66-
function HypeDocumentLoad(hypeDocument, element, event){
81+
function HypeDocumentLoad(hypeDocument, element, event) {
82+
6783
hypeDocument.updateContent = function(key, value) {
6884
if (key!==undefined && value!==undefined) hypeDocument.triggerCustomBehaviorNamed(key + ' equals ' + (typeof value === 'string' ? '"' + value + '"' : value))
6985
if (key!==undefined) hypeDocument.triggerCustomBehaviorNamed(key + ' was updated')
7086
let sceneElm = document.getElementById(hypeDocument.currentSceneId());
7187
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+
}
78109
})
79110
}
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+
88112
hypeDocument.customData = enableReactiveObject(hypeDocument.customData, debounceByRequestFrame(hypeDocument.updateContent));
89113
if (hypeDocument.functions().HypeReactiveContent) hypeDocument.functions().HypeReactiveContent(hypeDocument, element, event);
90114
}
91115

92116
function HypeTriggerCustomBehavior(hypeDocument, element, event) {
117+
if ("HypeActionEvents" in window !== false) return;
93118
var code = event.customBehaviorName;
94-
if (/[;=()]/.test(code)) hypeDocument.runCode(code);
119+
if (/[;=()]/.test(code)) runCode(code, hypeDocument);
95120
}
96121

97122
function HypeScenePrepareForDisplay(hypeDocument, element, event) {
@@ -100,10 +125,12 @@ if("HypeReactiveContent" in window === false) window['HypeReactiveContent'] = (f
100125

101126
if ("HYPE_eventListeners" in window === false) { window.HYPE_eventListeners = Array(); }
102127
window.HYPE_eventListeners.push({ "type": "HypeDocumentLoad", "callback": HypeDocumentLoad });
103-
window.HYPE_eventListeners.push({ "type": "HypeTriggerCustomBehavior", "callback": HypeTriggerCustomBehavior });
104128
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+
106133
return {
107-
version: '1.0.4'
134+
version: '1.0.5'
108135
};
109136
})();

0 commit comments

Comments
 (0)