<commit_before>'use strict';

var ingredients;

function get_ingredients(callback) {
	var xhr = new XMLHttpRequest();
	xhr.addEventListener(
		'load',
		function(event) {
			if(event.target.status === 200) {
				ingredients = event.target.response;
				callback(ingredients);
			}
			else {
				alert('Unable to retrieve ingredients');
			}
		}
	);
	xhr.open('GET', 'api/ingredient', true);
	xhr.send();
	xhr.responseType = 'json';
}

window.addEventListener(
	'load',
	function() {
		get_ingredients(function() {

			function provide_ingredient(text) {
				return ingredients.filter(function(ingredient) {
					return ingredient.name.nocaseIncludes(text);
				}).slice(0, 10);
			}

			function draw_ingredient(ingredient, value) {
				//prepare regexp to highlight part of ingredient matching the search
				var regexp = new RegExp('('+ value + ')', 'gi');
				var ingredient_li = document.createElement('li');
				ingredient_li.appendChild(document.createFullElement('img', {src : 'images/ingredients/' + ingredient.id + '.png'}));
				//label
				var ingredient_label = document.createElement('span');
				ingredient_label.innerHTML = ingredient.name.replace(regexp, '<span class="highlight">$1</span>');
				ingredient_li.appendChild(ingredient_label);
				return ingredient_li;
			}

			Forms.Autocomplete(
				document.getElementById('recipe')['ingredient'],
				document.getElementById('ingredients'),
				provide_ingredient,
				draw_ingredient,
				Router.SelectIngredient,
				undefined,
				Ingredients.Close
			);

			document.getElementById('recipe').addEventListener(
				'submit',
				function(event) {
					Event.stop(event);
					var ingredient = ingredients.find(Array.objectFilter({'name' : this.value}));
					if(ingredient) {
						Router.SelectIngredient(ingredient);
					}
				}
			);

			//try to restore selected node
			var event = new UIEvent('hashchange', {bubbles : true, cancelable : true, detail : 1});
			window.dispatchEvent(event);
		});
	}
);<commit_msg>Reorganize Javascript code<commit_after>'use strict';

var ingredients;

(function() {

	function get_ingredients(callback) {
		var xhr = new XMLHttpRequest();
		xhr.addEventListener(
			'load',
			function(event) {
				if(event.target.status === 200) {
					ingredients = event.target.response;
					callback(ingredients);
				}
				else {
					alert('Unable to retrieve ingredients');
				}
			}
		);
		xhr.open('GET', 'api/ingredient', true);
		xhr.send();
		xhr.responseType = 'json';
	}

	function provide_ingredient(text) {
		return ingredients.filter(function(ingredient) {
			return ingredient.name.nocaseIncludes(text);
		}).slice(0, 10);
	}

	function draw_ingredient(ingredient, value) {
		//prepare regexp to highlight part of ingredient matching the search
		var regexp = new RegExp('('+ value + ')', 'gi');
		var ingredient_li = document.createElement('li');
		ingredient_li.appendChild(document.createFullElement('img', {src : 'images/ingredients/' + ingredient.id + '.png'}));
		//label
		var ingredient_label = document.createElement('span');
		ingredient_label.innerHTML = ingredient.name.replace(regexp, '<span class="highlight">$1</span>');
		ingredient_li.appendChild(ingredient_label);
		return ingredient_li;
	}

	window.addEventListener(
		'load',
		function() {
			get_ingredients(function() {

				Forms.Autocomplete(
					document.getElementById('recipe')['ingredient'],
					document.getElementById('ingredients'),
					provide_ingredient,
					draw_ingredient,
					Router.SelectIngredient,
					undefined,
					Ingredients.Close
				);

				document.getElementById('recipe').addEventListener(
					'submit',
					function(event) {
						Event.stop(event);
						var ingredient = ingredients.find(Array.objectFilter({'name' : this.value}));
						if(ingredient) {
							Router.SelectIngredient(ingredient);
						}
					}
				);

				//try to restore selected node
				var event = new UIEvent('hashchange', {bubbles : true, cancelable : true, detail : 1});
				window.dispatchEvent(event);
			});
		}
	);
})();
{'fraction_non_alphanumeric': 0.1416521954849913, 'fraction_numerical': 0.003969238402381543, 'mean_word_length': 3.8932038834951457, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4483569', 'n_tokens_mistral': 1575, 'n_tokens_neox': 1241, 'n_words': 229}