dynamic-selection-workflow.js 3.24 KB
(function(dynworkflow, $, undefined) {

	var jsonWF = {};
	var baseUrl = "";
	
	var mainConfig = "";
	var currentSubconfigParent = "";
	var currentSubconfig = "";
	var currentSubConfigName = "";
	var currentStep = 0;
	
	function _preprocessConfig(config) {
		config = config.replace("right-hand", "hand");
		config = config.replace("left-hand", "hand");
		return config;
	};
	
	function _getFirstKey(json) {
	    var first_key = undefined;
	    for (first_key in json) break;
	    return first_key;
	}
	
	function _getAttributes(json) {
		var result = [];
	    for (attr in json) {
	    	result.push(attr);
	    }
	    return result;
	}
	
	function _getNextSubConfig() {
	    var attrs = _getAttributes(currentSubconfigParent);
	    for (var i = 0; i < attrs.length; i++) {
	    	if (attrs[i] == currentSubConfigName && i < attrs.length - 1) {
	    		return attrs[i+1];
	    	}
	    }
	    return -1;
	}
	
	function _showCurrentSubconfig() {
		_showSubconfiguration(currentSubConfigName, currentSubconfig[currentStep]);
	}
	
	function _showSubconfiguration(subconfig, step) {
		var step = typeof step == "undefined" ? "passo-1" : step;
		$(".selection-panel-body").hide();
		dynengine.render(baseUrl, "/" + mainConfig + "/" + subconfig +
				"/" + step + ".html", "#selection-panel", true);
	}
	
	dynworkflow.selectMainConfig = function(config) {
		mainConfig = _preprocessConfig(config);
		currentSubConfigName = _getFirstKey(jsonWF[mainConfig]);
		currentSubconfig = jsonWF[mainConfig][currentSubConfigName];
		currentSubconfigParent = jsonWF[mainConfig];
		currentStep = 0;
		
		_showCurrentSubconfig();
	}
	
	dynworkflow.selectMovement = function(movement) {
		var subconfigJSON = currentSubconfig[movement]
		currentSubConfigName = _getFirstKey(subconfigJSON);
		currentSubconfigParent = subconfigJSON;
		currentSubconfig = subconfigJSON[currentSubConfigName];
		currentStep = 0;
		
		_showCurrentSubconfig();
	}
	
	dynworkflow.selectSubConfig = function(config) {
		currentSubConfigName = config;
		currentSubconfig = currentSubconfigParent[currentSubConfigName];
		currentStep = 0;
		
		_showCurrentSubconfig();
	}
	
	dynworkflow.nextSubConfig = function() {
		currentStep = currentStep + 1;
		
		if (currentStep < currentSubconfig.length) {
			_showCurrentSubconfig();
		} else {
			var nextSubConfig = _getNextSubConfig();
			
			if (nextSubConfig != -1) {
				dynworkflow.selectSubConfig(nextSubConfig);
			} else {
				wikilibras.hideSelectionPanel();
			}
		}
	}
	
	dynworkflow.setupTimeline = function() {
		dynengine.render(baseUrl, "/" + mainConfig + "/timeline.html", "#selection-panel", false, function() {
			$(".subconfiguration-options .icon_container").hide();

			for (var name in currentSubconfigParent) {
				$(".subconfiguration-options .icon_container[json_name=" + name + "]").show();
			}
			if (mainConfig == "hand") {
				$(".subconfiguration-options .icon_container[json_name=movimento]").show();
			}
			
			$(".subconfiguration-panel").show();
		});
	}

	dynworkflow.load = function() {
		baseUrl = $('#server-url').data('url');
		$.get(baseUrl + "/conf/selection-workflow-json", function(result) {
			jsonWF = $.parseJSON(result);
		}).fail(function() {
			console.log("Failed to load the workflow configuration");
		});
	}

}(window.dynworkflow = window.dynworkflow || {}, jQuery));