Hype CommandPipeline


(Loves Hype) #1

Hype CommandPipeline

As I saw this technic used in simpler ways on the forum. The idea behind it is that one maps different function calls originating from triggering custom behavior onto the same handler function and then uses event.customBehaviorName to run an action.

This code is an early idea and feedback is welcome. So it’s not packed or minified but is fully functional. Just drop the following into your head:

	<script>	
	/**
	* Hype commandPipeline 1.1
	* by Max Ziebell
	*/
	var commandPipeline = function(hypeDocument, element, event) {
		if (event.customBehaviorName.indexOf('|')) {
			var cmd = event.customBehaviorName.split('|');
			try {
				var fnparams = JSON.parse('['+cmd[1]+']');
			} catch (e) {
			    alert("Hype CommandPipeline:\n"+e.name+': '+e.message);
			}
			if (typeof fnparams === "object"){
				if (hypeDocument.hasOwnProperty(cmd[0])) {
					if (typeof hypeDocument[cmd[0]] === "function") {
						hypeDocument[cmd[0]].apply(hypeDocument, fnparams);
					}
				}
			}
		}	
	}
	
	if("HYPE_eventListeners" in window === false) { window.HYPE_eventListeners = Array();}
	window.HYPE_eventListeners.push({"type":"HypeTriggerCustomBehavior", "callback": commandPipeline});
	</script>

This allows you to call functions in the hypeDocument scope using the timeline or actions panel without wrapping them in a Hype function first. This is specially useful for commands that trigger stuff from the timeline as you don’t have to create all these hype function wrappers for slight differences in parameters. Triggering the command …

myFunction|"I am a string", 1

… will call the function hypeDocument.myFunction(“I am a string”, 1);. Although for this example to run properly you would need to define that function first like…

hypeDocument.myFunction = function(name, nr){
    console.log("was here", name, nr);
}

You can call built in functions but mostly that doesn’t make sense as they are already offered in the dropdowns in Hype. Then there are limits in the complexity of arguments as we can’t pass in more then simple objects (so no HTMLDivElement etc.). But at least they can be nested, so the arguments go beyond only strings, as the custom behavior name is actually parsed on the basis of JSON. So you can do this…

myFunction|"I am a string", 1, { "hello": [1,2,3,4,5,6] }

Versionhistory

1.0 (Beta) Initial release under MIT-license
1.1 Fixed error handling and now the errors are detailed


(Jonathan Deutsch) #2

There’s a way to “abuse” export scripts in a similar fashion that I realized when developing could be used in such a manner for direct javascript calls, but I hadn’t thought of this dynamic method! Clever.