async function getCrafts() { const res = await fetch("./data/crafts.json"); const data = await res.json(); return data; }; async function getCollections() { const res = await fetch("./data/collections.json"); const data = await res.json(); return data; }; function updateSelectedCollection (selected = 0) { var previouslySelectedCollection = document.querySelector('#collections nav ul li.selected'); if (previouslySelectedCollection != null) { previouslySelectedCollection.classList.remove('selected'); } var collections = document.querySelectorAll('#collections nav ul li'); console.log(collections); var newlySelectedCollection = collections[selected]; console.log(newlySelectedCollection); newlySelectedCollection.classList.add('selected'); } async function printCollections (crafts, collections) { var collectionsElt = document.getElementById('collections'); var collectionsNavElt = collectionsElt.querySelector('nav ul'); collectionsNavElt.innerHTML = ''; var cssClass = ''; collections.forEach(function (collection, index) { if (collection.items != undefined) { cssClass = ''; collectionsNavElt.innerHTML += '' + '' + '' + '' + ''; } }); displayCollection(crafts, collections); collectionsElt.querySelectorAll('li').forEach(function (elt, index) { elt.addEventListener('click', function () { console.log(index); displayCollection(crafts, collections, index); }); }); } async function displayCollection (crafts, collections, collectionId = 0) { updateSelectedCollection(collectionId); itemsListElt = document.querySelector('#collections .items-list'); itemsListElt.innerHTML = ''; collections[collectionId].items.forEach(function (itemId){ itemsListElt.innerHTML += '
  • ' + '' + '' + '' + '
  • '; }); } async function printItemsList () { crafts = await getCrafts(); itemsListElt = document.getElementById('items-list'); crafts.forEach(function (item, index) { if (item.recipes != undefined) { itemsListElt.innerHTML += '
  • ' + '' + '' + '' + '
  • '; } }); } function padIngredient (nbIngredients, str) { if (nbIngredients <= 1 || nbIngredients == 4 || nbIngredients == 9 || nbIngredients == 16) { return ''; } else if (nbIngredients < 4) { return str.repeat(4 - nbIngredients); } else if (nbIngredients < 9) { return str.repeat(9 - nbIngredients); } else if (nbIngredients < 16) { return str.repeat(16 - nbIngredients); } else if (nbIngredients < 25) { return str.repeat(25 - nbIngredients); } } async function displayRecipe (itemId, recipeId = 0, elementId = 'recipe-main', bc_item = null, bc_recipe = null) { crafts = await getCrafts(); recipeElt = document.getElementById(elementId); ingredientsElt = recipeElt.querySelector('.ingredients'); actionElt = recipeElt.querySelector('.action'); resultElt = recipeElt.querySelector('.result'); itemNameElt = recipeElt.querySelector('.item-name'); otherRecipesElt = recipeElt.querySelector('.other-recipes ul'); detailsElt = recipeElt.querySelector('.recipe-details'); detailsContentElt = detailsElt.querySelector('div'); actionElt.innerHTML = ' '; resultElt.innerHTML = " "; otherRecipesElt.innerHTML = ""; detailsContentElt.innerHTML = ""; recipeElt.style.visibility = 'visible'; detailsElt.style.visibility = 'hidden'; itemNameElt.innerHTML = '' + crafts[itemId].name + ''; resultElt_content = ''; if (elementId == 'recipe-aux' && bc_item != null) { resultElt_content = '' + resultElt_content + ''; } resultElt.innerHTML = resultElt_content; blackHoleStr = '
  • '; if (crafts[itemId].recipes == undefined) { ingredientsElt.innerHTML = blackHoleStr; } else { recipes = crafts[itemId].recipes; ingredients = recipes[recipeId].ingredients; if (ingredients == undefined) { ingredientsElt.innerHTML = blackHoleStr; } else { ingredientsElt.innerHTML = ''; nbIngredients = ingredients.length; ingredientsElt.className = 'ingredients '; if (nbIngredients > 16) { ingredientsElt.className += "grid-25"; } else if (nbIngredients > 9) { ingredientsElt.className += "grid-16"; } else if (nbIngredients > 4) { ingredientsElt.className += "grid-9"; } else if (nbIngredients > 1) { ingredientsElt.className += "grid-4"; } else { ingredientsElt.className += ""; } var i = 1; console.log(ingredients); ingredients.forEach (function (ingredient) { unit = (ingredient.unit != undefined) ? (' ' + ingredient.unit) : ''; qty = (ingredient.qty == undefined) ? '' : '' + ingredient.qty + ''; title = (ingredient.qty == undefined) ? crafts[ingredient.ref].name : ( (ingredient.unit == undefined) ? ingredient.qty + ' ' + crafts[ingredient.ref].name : ingredient.qty + ' ' + ingredient.unit + ' de ' + crafts[ingredient.ref].name ); // displayOn = (elementId == 'recipe-main') ? 'recipe-aux' : 'recipe-main'; displayOn = 'recipe-aux'; if (elementId == 'recipe-aux') { bc_item = itemId; bc_recipe = recipeId; } else { bc_item = null; bc_recipe = null; } ingredientsElt.innerHTML += '
  • ' + qty + '' + '' + '' + '
  • '; i++; }); ingredientsElt.innerHTML += padIngredient(nbIngredients, '
  • '); } action = (recipes[recipeId].action == undefined) ? '' : recipes[recipeId].action; if (recipes[recipeId].details != undefined) { actionElt.className += ' has-details'; } actionElt.innerHTML = ''+ action + ''; if ((details = crafts[itemId].recipes[recipeId].details) != undefined) { detailsContentElt.innerHTML = details; } nbRecipes = recipes.length; if (nbRecipes == 1) { if (recipes[0].name != undefined) { otherRecipesElt.innerHTML += '
  • ' + '' + recipes[0].name + '' + '
  • '; } } else if (nbRecipes > 1) { for (i = 0; i < nbRecipes; i++) { otherRecipeName = (recipes[i].name != undefined) ? recipes[i].name : "recette n°" + (i + 1); if (i == recipeId) { otherRecipesElt.innerHTML += '
  • ' + '' + otherRecipeName + '' + '
  • '; } else { otherRecipesElt.innerHTML += '
  • ' + '' + otherRecipeName + '' '
  • '; } } } } } showDetails = function (elementId) { detailsElt = document.querySelector('#' + elementId + ' .recipe-details'); detailsElt.style.visibility = "visible"; } // printItemsList(); window.addEventListener('load', async function () { const crafts = await getCrafts(); const collections = await getCollections(); printCollections(crafts, collections); });