<commit_before>/**
 * Custom javascript for calc 
 * 
 *  @author <NAME>
 */

$dataVisualization 	= $( "#data-visualization" );

/**
 * main html container
 */
$container 			= $("#container");

$(document).ready(function() {
	
	/**
	 * Main calc instance
	 */
	Calc = {};
	
	/**
	 * current working section
	 */
	Calc.section = null;
	
	/**
	 * Ui managers
	 */
	Calc.homeCalculationManager 	= null;
	Calc.homeDataManager 			= null;
	Calc.workspaceSettingsManager 	= null;
	
	/**
	 * Ui elements
	 */
	Calc.footer 			= $( "#footer" );
	Calc.footerHomeLinks 	= Calc.footer.find( ".links" );
//	Calc.backHomeBtn 		= Calc.footer.find( "button.back" );
	
	/**
	 * Function to be called when active workspace change
	 */
	Calc.workspaceChange = function( success ) {
		
		WorkspaceManager.getInstance().refreshActiveWorkspace( function(ws){
			// load calc steps to home page
			Calc.homeCalculationManager.updateSteps();
			// load entities and variables for home data query section
			Calc.homeDataManager.refresh();
			// update active workspace home settings ui
			Calc.workspaceSettingsManager.updateActive( ws );
			
			// update ui buttons
			Calc.updateButtonStatus();
			
			if ( success ) {
				success(ws);
			}
		});
			
	};
	
	/**
	 * Enable / disable buttons based on workspace status
	 */
	Calc.updateButtonStatus = function(){
		
		var wsButtons 		= $( document ).find( ".workspace-required" );
		var aoiButtons 		= $( document ).find( ".aoi-required" );
		var sdButtons 		= $( document ).find( ".sampling-design-required" );
		var chainButtons 	= $( document ).find( ".default-chain-completed-required" );
		var stepsButtons 	= $( document ).find( ".calculation-steps-required" );
		
		UI.disable( wsButtons );
		UI.disable( aoiButtons );
		UI.disable( sdButtons );
		UI.disable( chainButtons );
		UI.disable( stepsButtons );
		
		WorkspaceManager.getInstance().activeWorkspace( function(ws){
			if ( ws ) {
				UI.enable( wsButtons );
				if( ws.aoiHierarchies.length > 0 ) {
					UI.enable( aoiButtons );
					
					if( ws.samplingDesign ){
						UI.enable( sdButtons );
						var chain = ws.getDefaultProcessingChain();
						if( chain.status === "COMPLETED" ){
							UI.enable( chainButtons );
						}
					}
				}
				var chain = ws.getDefaultProcessingChain();
				if( chain && chain.calculationSteps.length > 0 ){
					UI.enable( stepsButtons );			
				}
			}
			
		});
	};
	
	/**
	 * Functions used to show rest call errors
	 */
	Calc.error = function( jqXHR , textStatus , errorThrown ) {
		var message = $( "<div class='width100'></div>" );
		
		if( jqXHR.status ) {
			textStatus += " " + jqXHR.status; 
		}
		message.append( "<div class='width100'>" + textStatus + "</div>" );
		
		if( errorThrown ) {
			message.append( "<div class='width100'>" + errorThrown + "</div>" );
		}
		
		if( jqXHR.responseText ) {
			var heightPercent = 80 / 100;
			//div height will be 80% of available space
			var height = heightPercent * ($(window).height() - Calc.footer.height()) + "px";
			message.append( "<div class='width100' style='overflow-y: auto; height: " + height + "'>" + jqXHR.responseText + "</div>" );
		}
		
		UI.showError( message, false );
	};
	
	
	var positionFooter = function() {
		$(".footer-placeholder").css("height", Calc.footer.height());
		Calc.footer.animate({top:$(window).height()- Calc.footer.height() }, 400);
	};
	
	var resizeContainer = function() {
		var containerHeight = $(window).height() - Calc.footer.height();
		$container.css({"height":containerHeight+"px"});
	};
	
	var scrollToSection = function (animate) {
		//calculate the scroll offset
		var scrollTop = Calc.section[0].offsetTop;
		
		if ( animate ) {
			//show all siblings temporarily during scrolling
			Calc.section.siblings().andSelf().visible();
			
			//enable container scrolling during animation
			$container.css('overflow','auto');
			
			var onAnimationComplete = function(){
				//remove scrollbar when animation ends
				$container.css('overflow','hidden');
				//make siblings invisible (block focus of hidden sections)
				Calc.section.siblings().invisible();
			};
			//scroll to the current section offset
			$container.stop().animate({scrollTop: scrollTop}, 800, "easeOutQuart", onAnimationComplete);
		} else {
			//scroll to the current section offset
			$container.css('overflow','auto');
			$container[0].scrollTop = scrollTop;
			$container.css('overflow','hidden');
		}
	};

	Calc.footer.find(".links button").click( function(event){
		event.preventDefault();

		var target = $( $(this).attr("href") );
		
		//set current home section
		if( Calc.section.attr("id") != target.attr("id") ){
		    Calc.section = target ;
		    scrollToSection(true);
		}
		
	});

	// event handler for home button click
	homeButtonClick = function(event){
		event.preventDefault();

		var $button = $(event.currentTarget);
		sectionUrl = $button.attr("href");
		
		//set the current working section (calculation,results,data or settings)
		Calc.section = $button.parents( ".section-home" );
		//home page section (contains the button links to the external pages)
		$homeSection = Calc.section.find( ".page-section" );
		if( !sectionUrl ) {
			var msg = " Calc error. Section url is undefinded";		
			throw msg;
		}
		
		Calc.navigateToSection( sectionUrl );
	};
	
	Calc.navigateToSection = function( sectionUrl ){
		if( $homeSection ){
		
			Calc.pushToHistory( sectionUrl );
			
			$.ajax({
				url		: sectionUrl,<commit_msg>history update.<commit_after>/**
 * Custom javascript for calc 
 * 
 *  @author <NAME>
 */

$dataVisualization 	= $( "#data-visualization" );

/**
 * main html container
 */
$container 			= $("#container");

$(document).ready(function() {
	
	/**
	 * Main calc instance
	 */
	Calc = {};
	
	/**
	 * current working section
	 */
	Calc.section = null;
	
	/**
	 * Ui managers
	 */
	Calc.homeCalculationManager 	= null;
	Calc.homeDataManager 			= null;
	Calc.workspaceSettingsManager 	= null;
	
	/**
	 * Ui elements
	 */
	Calc.footer 			= $( "#footer" );
	Calc.footerHomeLinks 	= Calc.footer.find( ".links" );
//	Calc.backHomeBtn 		= Calc.footer.find( "button.back" );
	
	/**
	 * Function to be called when active workspace change
	 */
	Calc.workspaceChange = function( success ) {
		
		WorkspaceManager.getInstance().refreshActiveWorkspace( function(ws){
			// load calc steps to home page
			Calc.homeCalculationManager.updateSteps();
			// load entities and variables for home data query section
			Calc.homeDataManager.refresh();
			// update active workspace home settings ui
			Calc.workspaceSettingsManager.updateActive( ws );
			
			// update ui buttons
			Calc.updateButtonStatus();
			
			if ( success ) {
				success(ws);
			}
		});
			
	};
	
	/**
	 * Enable / disable buttons based on workspace status
	 */
	Calc.updateButtonStatus = function(){
		
		var wsButtons 		= $( document ).find( ".workspace-required" );
		var aoiButtons 		= $( document ).find( ".aoi-required" );
		var sdButtons 		= $( document ).find( ".sampling-design-required" );
		var chainButtons 	= $( document ).find( ".default-chain-completed-required" );
		var stepsButtons 	= $( document ).find( ".calculation-steps-required" );
		
		UI.disable( wsButtons );
		UI.disable( aoiButtons );
		UI.disable( sdButtons );
		UI.disable( chainButtons );
		UI.disable( stepsButtons );
		
		WorkspaceManager.getInstance().activeWorkspace( function(ws){
			if ( ws ) {
				UI.enable( wsButtons );
				if( ws.aoiHierarchies.length > 0 ) {
					UI.enable( aoiButtons );
					
					if( ws.samplingDesign ){
						UI.enable( sdButtons );
						var chain = ws.getDefaultProcessingChain();
						if( chain.status === "COMPLETED" ){
							UI.enable( chainButtons );
						}
					}
				}
				var chain = ws.getDefaultProcessingChain();
				if( chain && chain.calculationSteps.length > 0 ){
					UI.enable( stepsButtons );			
				}
			}
			
		});
	};
	
	/**
	 * Functions used to show rest call errors
	 */
	Calc.error = function( jqXHR , textStatus , errorThrown ) {
		var message = $( "<div class='width100'></div>" );
		
		if( jqXHR.status ) {
			textStatus += " " + jqXHR.status; 
		}
		message.append( "<div class='width100'>" + textStatus + "</div>" );
		
		if( errorThrown ) {
			message.append( "<div class='width100'>" + errorThrown + "</div>" );
		}
		
		if( jqXHR.responseText ) {
			var heightPercent = 80 / 100;
			//div height will be 80% of available space
			var height = heightPercent * ($(window).height() - Calc.footer.height()) + "px";
			message.append( "<div class='width100' style='overflow-y: auto; height: " + height + "'>" + jqXHR.responseText + "</div>" );
		}
		
		UI.showError( message, false );
	};
	
	
	var positionFooter = function() {
		$(".footer-placeholder").css("height", Calc.footer.height());
		Calc.footer.animate({top:$(window).height()- Calc.footer.height() }, 400);
	};
	
	var resizeContainer = function() {
		var containerHeight = $(window).height() - Calc.footer.height();
		$container.css({"height":containerHeight+"px"});
	};
	
	var scrollToSection = function (animate) {
		//calculate the scroll offset
		var scrollTop = Calc.section[0].offsetTop;
		
		if ( animate ) {
			//show all siblings temporarily during scrolling
			Calc.section.siblings().andSelf().visible();
			
			//enable container scrolling during animation
			$container.css('overflow','auto');
			
			var onAnimationComplete = function(){
				//remove scrollbar when animation ends
				$container.css('overflow','hidden');
				//make siblings invisible (block focus of hidden sections)
				Calc.section.siblings().invisible();
			};
			//scroll to the current section offset
			$container.stop().animate({scrollTop: scrollTop}, 800, "easeOutQuart", onAnimationComplete);
		} else {
			//scroll to the current section offset
			$container.css('overflow','auto');
			$container[0].scrollTop = scrollTop;
			$container.css('overflow','hidden');
		}
	};

	Calc.footer.find(".links button").click( function(event){
		event.preventDefault();

		var target = $( $(this).attr("href") );
		
		//set current home section
		if( Calc.section.attr("id") != target.attr("id") ){
		    Calc.section = target ;
		    scrollToSection(true);
		}
		
	});

	// event handler for home button click
	homeButtonClick = function(event){
		event.preventDefault();

		var $button = $(event.currentTarget);
		sectionUrl = $button.attr("href");
		
		//set the current working section (calculation,results,data or settings)
		Calc.section = $button.parents( ".section-home" );
		//home page section (contains the button links to the external pages)
		$homeSection = Calc.section.find( ".page-section" );
		if( !sectionUrl ) {
			var msg = " Calc error. Section url is undefinded";		
			throw msg;
		}
		
		Calc.navigateToSection( sectionUrl );
	};
	
	Calc.navigateToSection = function( sectionUrl ){
		// not accessing the page directly with section in the url
		if( typeof $homeSection !== 'undefined' ){
		
			Calc.pushToHistory( sectionUrl );
			
			$.ajax({
				url		: sectionUrl,
{'fraction_non_alphanumeric': 0.15463165572574764, 'fraction_numerical': 0.005288110867979577, 'mean_word_length': 3.1850438763830597, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4218601', 'n_tokens_mistral': 4074, 'n_tokens_neox': 3377, 'n_words': 790}