diff --git a/html/crafting.js b/html/crafting.js index e8c0b8016..1e0af7eb5 100644 --- a/html/crafting.js +++ b/html/crafting.js @@ -1,10 +1,12 @@ // Constants ////////////////////////////////////////////////////////////////////////////////////////////////////////// +var ERROR_DISPLAY_DURATION = 10000; // ms var FADE_DURATION = 250; // ms -var ERROR_DISPLAY_DURATION = 5000; // ms +var SLIDE_DURATION = 600; // ms // Global Variables /////////////////////////////////////////////////////////////////////////////////////////////////// +var __loadingCount = 0; var __recipebooks = []; var __toolsIncluded = false; @@ -12,32 +14,24 @@ var __toolsIncluded = false; $(function() { $("#recipebook_load_button").click(onRecipeLoadButtonClicked); - $("#crafting_selector").change(onCraftingSelectorChanged); $("#crafting_count").change(onCraftingSelectorChanged); $("#tools_included").change(onIncludeToolsChanged); + $("#crafting_selector").focus(onCraftingSelectorFocused); - loadRecipebook("data/vanilla.json", function() { - loadRecipebook("data/buildcraft.json", function() { - loadRecipebook("data/industrial_craft.json", function() { - parseUrlParameters(); - onCraftingSelectorChanged(); - }); - }); - }); + loadRecipebook("data/vanilla.json"); + loadRecipebook("data/buildcraft.json"); + loadRecipebook("data/industrial_craft.json"); }); function onCraftingSelectorChanged() { - var recipeName = $("#crafting_selector option:selected").val(); + var recipeName = $("#crafting_selector").val(); var count = parseInt($("#crafting_count option:selected").val()); - if (recipeName === undefined) return; if (count === undefined) return; - updatePageState(count, recipeName); var recipe = findRecipe(recipeName); if (recipe === undefined) { - $("#crafting_error").html("Cannot find recipe for " + recipeName); - $("#crafting_error").fadeIn(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).fadeOut(FADE_DURATION); + $("#crafting_output").slideUp(SLIDE_DURATION); } else { var inventory = createManifest(); var craftedItems = createManifest(); @@ -50,7 +44,28 @@ function onCraftingSelectorChanged() { $("#missing_materials").html(missingMaterials.toHtml()); $("#crafted_items").html(craftedItems.toHtml()); $("#leftover_materials").html(inventory.toHtml()); - $("#crafting_output").fadeIn(FADE_DURATION); + $("#crafting_output").slideDown(SLIDE_DURATION); + } + + updatePageState(count, recipeName); +} + +function onCraftingSelectorFocused() { + $("#crafting_selector").autocomplete("search"); +} + +function onIncludeToolsChanged() { + __toolsIncluded = $("#tools_included").is(":checked"); + onCraftingSelectorChanged() +} + +function onRecipeBookLoaded() { + if (__loadingCount > 0 || __recipebooks.length === 0) { + $("#crafting").fadeOut(FADE_DURATION); + } else if (__loadingCount === 0 && __recipebooks.length > 0) { + updateCraftingSelector(); + parseUrlParameters(); + $("#crafting").fadeIn(FADE_DURATION); } } @@ -62,14 +77,10 @@ function onRecipeLoadButtonClicked() { }); } -function onIncludeToolsChanged() { - __toolsIncluded = $("#tools_included").is(":checked"); - onCraftingSelectorChanged() -} - // UI Helper Functions //////////////////////////////////////////////////////////////////////////////////////////////// -function loadRecipebook(recipebookUrl, onSuccess) { +function loadRecipebook(recipebookUrl) { + __loadingCount++; $.ajax({ url: recipebookUrl, dataType: "text", @@ -79,19 +90,18 @@ function loadRecipebook(recipebookUrl, onSuccess) { __recipebooks.push(recipebook); recipebook.asTableRow().insertBefore($("#recipebook_table tr").last()); - - updateCraftingSelector(); - $("#crafting").fadeIn(FADE_DURATION); - - if (onSuccess !== undefined) onSuccess(); } catch (e) { $("#load_error").html(e); $("#load_error").slideDown(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).slideUp(FADE_DURATION); } }, error: function(response, status, error) { - $("#load_error").html(error); + $("#load_error").html(status + ": " + error); $("#load_error").slideDown(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).slideUp(FADE_DURATION); + }, + complete: function() { + __loadingCount--; + onRecipeBookLoaded(); } }); } @@ -113,7 +123,9 @@ function parseUrlParameters() { if (recipeName !== undefined) { $("#crafting_selector").val(recipeName); - if ($("#crafting_selector option:selected").val() === undefined) { + if (findRecipe(recipeName) !== undefined) { + onCraftingSelectorChanged(); + } else if (recipeName.length > 0) { $("#crafting_error").html("Cannot find recipe for " + recipeName); $("#crafting_error").fadeIn(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).fadeOut(FADE_DURATION); } @@ -121,38 +133,37 @@ function parseUrlParameters() { } function updateCraftingSelector() { - var $selector = $("#crafting_selector"); - $selector.html(""); + var $selector = $("crafting_selector"); + var names = []; + $(__recipebooks).each(function(i, recipebook) { + for (var name in recipebook.recipes) { + names.push(name); + } + }); + names.sort(); - if (__recipebooks.length == 0) { - $selector.attr("disabled", "disabled"); - $selector.html(""); - } else { - $(__recipebooks).each(function(i, recipebook) { - var $optgroup = $(""); - $selector.append($optgroup); - - var names = []; - for (var name in recipebook.recipes) { - names.push(name); - } - names.sort(); - - for (var i = 0; i < names.length; i++) { - $optgroup.append(""); - } - }); - $selector.removeAttr("disabled"); - } + $("#crafting_selector").autocomplete({ + source: names, delay: 0, minLength: 0, + change: onCraftingSelectorChanged, + close: onCraftingSelectorChanged, + select: onCraftingSelectorChanged, + }); } function updatePageState(count, recipeName) { - var newTitle = "Crafting Guide: " + count + " " + recipeName; - var newLink = "?count=" + count + "&recipeName=" + encodeURIComponent(recipeName); - newLink = newLink.replace(/%20/g, "+"); + var newTitle = "Crafting Guide"; + var newLink = window.location.pathname; + var state = {} + if (recipeName !== undefined && recipeName.length > 0) { + newTitle = "Crafting Guide: " + count + " " + recipeName; + newLink = "?count=" + count + "&recipeName=" + encodeURIComponent(recipeName); + newLink = newLink.replace(/%20/g, "+"); + state = {count: count, name: recipeName}; + } + console.log("newTitle: " + newTitle + ", newLink: " + newLink); document.title = newTitle; - history.pushState({count: count, name: recipeName}, newTitle, newLink); + history.pushState(state, newTitle, newLink); ga('send', 'pageview'); } diff --git a/html/css/main.css b/html/css/main.css index 126487bd2..94731e199 100644 --- a/html/css/main.css +++ b/html/css/main.css @@ -19,6 +19,7 @@ p { margin-left: 10px; margin-right: 10px; } #crafting_output { margin-left: 10px; width: 920px; } #crafting_output th { background: #f8f8f8; border-left: 1px solid #bbb; font-weight: bold; padding: 10px; } #crafting_output td { background: #f8f8f8; border-left: 1px solid #bbb; padding: 10px; } +#crafting_selector { width: 400px; } #footer { background: #3E1D0A; color: white; padding-top: 10px; }; #footer iframe { display: inline-block; } #footer a { color: gray; } @@ -31,3 +32,4 @@ p { margin-left: 10px; margin-right: 10px; } #recipebook_url { width: 600px; } #recipebooks { margin-top: 32px; margin-bottom: 32px; } +.ui-autocomplete { max-height: 400px; overflow-y: auto; overflow-x: hidden; } diff --git a/html/index.html b/html/index.html index 7fd29ef4f..79a84ef0c 100644 --- a/html/index.html +++ b/html/index.html @@ -2,11 +2,12 @@ Crafting Guide - + + @@ -32,7 +33,7 @@