MC/website/web/imperial-recipe-book.js

134 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { itemLoreCard } from './item-lore-view.mjs';
const get = id => document.getElementById(id);
const node = (tag, text) => Object.assign(document.createElement(tag), { textContent: text });
const link = (recipe, name = recipe.output.name) =>
Object.assign(node('a', name), { href: '?recipe=' + encodeURIComponent(recipe.id) });
try {
const response = await fetch('/recipe-catalogue.json');
if (!response.ok) throw Error('Recipe catalogue unavailable');
const recipes = await response.json();
const items = new Map();
for (const recipe of recipes) {
if (!items.has(recipe.output.id)) items.set(recipe.output.id, recipe);
}
const indexRecipes = [...items.values()].sort((a, b) => a.output.name.localeCompare(b.output.name));
const search = get('recipe-search');
let selected;
function renderIndex() {
const query = search.value.trim().toLowerCase();
const index = get('recipe-index');
index.replaceChildren();
let previous;
for (const recipe of indexRecipes.filter(entry => entry.output.name.toLowerCase().includes(query))) {
const letter = recipe.output.name[0].toUpperCase();
if (previous !== letter) index.append(node('h2', letter));
previous = letter;
const anchor = link(recipe);
if (recipe.output.id === selected.output.id) anchor.setAttribute('aria-current', 'page');
index.append(anchor);
}
if (!index.childElementCount) index.append(node('p', 'No recipes match your search.'));
}
function label(item) {
const recipe = items.get(item.id);
return recipe && item.id !== selected.output.id ? link(recipe, item.name) : node('span', item.name);
}
function icon(item) {
const image = document.createElement('img');
image.src = item.icon ?? '/assets/minecraft-items/' + item.material + '.png';
image.alt = '';
image.addEventListener('error', () => image.remove(), { once: true });
return image;
}
function render(id) {
selected = recipes.find(recipe => recipe.id === id) ?? indexRecipes[0];
if (!selected) throw Error('No recipes');
renderIndex();
const ingredients = new Map();
for (const item of selected.inputs) {
const row = ingredients.get(item.id) ?? { item, count: 0 };
row.count++;
ingredients.set(item.id, row);
}
get('recipe-ingredients').replaceChildren(...[...ingredients.values()].map(({ item, count }) => {
const row = node('li', '');
const artwork = node('span', ''); artwork.className = 'ingredient-icon'; artwork.append(icon(item));
const quantity = node('span', count + ' ×'); quantity.className = 'ingredient-quantity';
row.append(quantity, label(item), artwork);
return row;
}));
get('recipe-name').textContent = selected.output.name;
const station = { 'Crafting table': 'crafting_table', Furnace: 'furnace', Smoker: 'smoker', 'Brewing stand': 'brewing_stand' }[selected.station];
get('recipe-station').replaceChildren(icon({ material: station }), node('span', 'Use a ' + selected.station.toLowerCase() + '.'));
get('recipe-result').replaceChildren(icon(selected.output));
get('recipe-result').setAttribute('aria-label', selected.output.name);
const card = itemLoreCard({ ...selected.output, nativeMetadata: true });
if (selected.output.icon) card.querySelector('.item-lore-name img').src = selected.output.icon;
get('recipe-item-summary').textContent = selected.output.summary;
get('recipe-item-card').replaceChildren(card);
get('recipe-output').textContent = (selected.possible ? 'Possible result' : 'Makes') + ' · ' + selected.amount + ' × ' + selected.output.name;
const crafting = selected.station === 'Crafting table';
const cooking = selected.station === 'Furnace' || selected.station === 'Smoker';
get('recipe-station-layout').classList.toggle('recipe-station--crafting', crafting);
get('recipe-station-layout').classList.toggle('recipe-station--cooking', cooking);
const slots = crafting ? selected.grid ?? Array.from({ length: 9 }, (_, index) => selected.inputs[index]) : selected.inputs;
get('recipe-grid').replaceChildren(...slots.map(item => {
const slot = node('li', '');
if (item) {
slot.title = item.name;
slot.setAttribute('aria-label', item.name);
const source = items.get(item.id);
if (source && item.id !== selected.output.id) {
const anchor = link(source, '');
anchor.setAttribute('aria-label', item.name); anchor.append(icon(item)); slot.append(anchor);
} else slot.append(icon(item));
}
else slot.setAttribute('aria-label', 'Empty slot');
return slot;
}));
if (cooking) {
const heat = node('li', '');
heat.className = 'recipe-heat';
heat.setAttribute('aria-hidden', 'true');
const fuel = node('li', '');
fuel.title = 'Add any valid fuel';
fuel.setAttribute('aria-label', 'Fuel slot');
get('recipe-grid').append(heat, fuel);
}
const directions = crafting
? selected.shaped ? 'Arrange the ingredients exactly as shown.' : 'Place one ingredient in each occupied slot. The arrangement can vary.'
: selected.station === 'Brewing stand' ? 'Add the vial or potion below, place the spice above, and fuel the stand with blaze powder.'
: 'Place the ingredient in the input slot and supply fuel.';
get('recipe-directions').textContent = [directions, selected.notes].filter(Boolean).join(' ');
get('recipe-variants').replaceChildren(...recipes.filter(entry => entry.output.id === selected.output.id && entry.id !== selected.id).map(variant => {
const row = node('li', '');
row.append(link(variant, variant.station + ' · ' + variant.inputs.map(item => item.name).join(', ')));
return row;
}));
}
const recipeId = () => new URLSearchParams(location.search).get('recipe');
search.addEventListener('input', renderIndex);
get('main').addEventListener('click', event => {
const anchor = event.target.closest('a[href^="?recipe="]');
if (!anchor || event.button || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
event.preventDefault();
history.pushState(null, '', anchor.href);
render(recipeId());
get('recipe-name').focus({ preventScroll: true });
});
window.addEventListener('popstate', () => render(recipeId()));
render(recipeId());
get('recipe-status').hidden = true;
get('recipe').hidden = false;
} catch {
get('recipe-status').textContent = 'The recipe catalogue could not be loaded. Refresh to try again.';
}