[#21] Add support for linking to a certain recipe
This commit is contained in:
+32
-2
@@ -14,12 +14,18 @@ $(function() {
|
|||||||
$("#crafting_selector").change(onCraftingSelectorChanged);
|
$("#crafting_selector").change(onCraftingSelectorChanged);
|
||||||
$("#crafting_count").change(onCraftingSelectorChanged);
|
$("#crafting_count").change(onCraftingSelectorChanged);
|
||||||
|
|
||||||
loadRecipebook("data/vanilla.json");
|
loadRecipebook("data/vanilla.json", function() {
|
||||||
loadRecipebook("data/buildcraft.json");
|
loadRecipebook("data/buildcraft.json", function() {
|
||||||
|
parseUrlParameters();
|
||||||
|
onCraftingSelectorChanged();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function onCraftingSelectorChanged() {
|
function onCraftingSelectorChanged() {
|
||||||
var recipeName = $("#crafting_selector option:selected").val();
|
var recipeName = $("#crafting_selector option:selected").val();
|
||||||
|
if (recipeName === undefined) return;
|
||||||
|
|
||||||
var recipe = findRecipe(recipeName);
|
var recipe = findRecipe(recipeName);
|
||||||
if (recipe === undefined) {
|
if (recipe === undefined) {
|
||||||
$("#crafting_error").html("Cannot find recipe for " + recipeName);
|
$("#crafting_error").html("Cannot find recipe for " + recipeName);
|
||||||
@@ -77,6 +83,30 @@ function loadRecipebook(recipebookUrl, onSuccess) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseUrlParameters() {
|
||||||
|
var quantity = $.url().param('quantity');
|
||||||
|
if (quantity !== undefined) {
|
||||||
|
$("#crafting_count").val(quantity);
|
||||||
|
|
||||||
|
if ($("#crafting_count option:selected").val() === undefined) {
|
||||||
|
$("#crafting_count").val(1);
|
||||||
|
|
||||||
|
$("#crafting_error").html("Unsupported quantity: " + quantity);
|
||||||
|
$("#crafting_error").fadeIn(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).fadeOut(FADE_DURATION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var recipeName = $.url().param('recipeName');
|
||||||
|
if (recipeName !== undefined) {
|
||||||
|
$("#crafting_selector").val(recipeName);
|
||||||
|
|
||||||
|
if ($("#crafting_selector option:selected").val() === undefined) {
|
||||||
|
$("#crafting_error").html("Cannot find recipe for " + recipeName);
|
||||||
|
$("#crafting_error").fadeIn(FADE_DURATION).delay(ERROR_DISPLAY_DURATION).fadeOut(FADE_DURATION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function updateCraftingSelector() {
|
function updateCraftingSelector() {
|
||||||
var $selector = $("#crafting_selector");
|
var $selector = $("#crafting_selector");
|
||||||
$selector.html("");
|
$selector.html("");
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ p { margin-left: 10px; margin-right: 10px; }
|
|||||||
#border_grass { background: url("../images/grass_side.png"); height: 32px; box-shadow: 2px 0px 4px #444; }
|
#border_grass { background: url("../images/grass_side.png"); height: 32px; box-shadow: 2px 0px 4px #444; }
|
||||||
#border_bedrock { background: url("../images/bedrock.png"); height: 32px; box-shadow: 2px 0px 4px #444; }
|
#border_bedrock { background: url("../images/bedrock.png"); height: 32px; box-shadow: 2px 0px 4px #444; }
|
||||||
#crafting { margin-bottom: 64px; }
|
#crafting { margin-bottom: 64px; }
|
||||||
|
#crafting_error { margin-left: 10px; }
|
||||||
#crafting_output { margin-left: 10px; width: 920px; }
|
#crafting_output { margin-left: 10px; width: 920px; }
|
||||||
#crafting_output th { background: #f8f8f8; border-left: 1px solid #bbb; font-weight: bold; padding: 10px; }
|
#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_output td { background: #f8f8f8; border-left: 1px solid #bbb; padding: 10px; }
|
||||||
|
|||||||
@@ -88,6 +88,7 @@
|
|||||||
</div> <!-- .container_12 -->
|
</div> <!-- .container_12 -->
|
||||||
|
|
||||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
|
||||||
|
<script src="purl.js"></script>
|
||||||
<script src="crafting.js"></script>
|
<script src="crafting.js"></script>
|
||||||
<script>
|
<script>
|
||||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||||
|
|||||||
+268
@@ -0,0 +1,268 @@
|
|||||||
|
/*
|
||||||
|
* Purl (A JavaScript URL parser) v2.3.1
|
||||||
|
* Developed and maintanined by Mark Perkins, [email protected]
|
||||||
|
* Source repository: https://github.com/allmarkedup/jQuery-URL-Parser
|
||||||
|
* Licensed under an MIT-style license. See https://github.com/allmarkedup/jQuery-URL-Parser/blob/master/LICENSE for details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
;(function(factory) {
|
||||||
|
if (typeof define === 'function' && define.amd) {
|
||||||
|
define(factory);
|
||||||
|
} else {
|
||||||
|
window.purl = factory();
|
||||||
|
}
|
||||||
|
})(function() {
|
||||||
|
|
||||||
|
var tag2attr = {
|
||||||
|
a : 'href',
|
||||||
|
img : 'src',
|
||||||
|
form : 'action',
|
||||||
|
base : 'href',
|
||||||
|
script : 'src',
|
||||||
|
iframe : 'src',
|
||||||
|
link : 'href',
|
||||||
|
embed : 'src',
|
||||||
|
object : 'data'
|
||||||
|
},
|
||||||
|
|
||||||
|
key = ['source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'fragment'], // keys available to query
|
||||||
|
|
||||||
|
aliases = { 'anchor' : 'fragment' }, // aliases for backwards compatability
|
||||||
|
|
||||||
|
parser = {
|
||||||
|
strict : /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
|
||||||
|
loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
|
||||||
|
},
|
||||||
|
|
||||||
|
isint = /^[0-9]+$/;
|
||||||
|
|
||||||
|
function parseUri( url, strictMode ) {
|
||||||
|
var str = decodeURI( url ),
|
||||||
|
res = parser[ strictMode || false ? 'strict' : 'loose' ].exec( str ),
|
||||||
|
uri = { attr : {}, param : {}, seg : {} },
|
||||||
|
i = 14;
|
||||||
|
|
||||||
|
while ( i-- ) {
|
||||||
|
uri.attr[ key[i] ] = res[i] || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// build query and fragment parameters
|
||||||
|
uri.param['query'] = parseString(uri.attr['query']);
|
||||||
|
uri.param['fragment'] = parseString(uri.attr['fragment']);
|
||||||
|
|
||||||
|
// split path and fragement into segments
|
||||||
|
uri.seg['path'] = uri.attr.path.replace(/^\/+|\/+$/g,'').split('/');
|
||||||
|
uri.seg['fragment'] = uri.attr.fragment.replace(/^\/+|\/+$/g,'').split('/');
|
||||||
|
|
||||||
|
// compile a 'base' domain attribute
|
||||||
|
uri.attr['base'] = uri.attr.host ? (uri.attr.protocol ? uri.attr.protocol+'://'+uri.attr.host : uri.attr.host) + (uri.attr.port ? ':'+uri.attr.port : '') : '';
|
||||||
|
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAttrName( elm ) {
|
||||||
|
var tn = elm.tagName;
|
||||||
|
if ( typeof tn !== 'undefined' ) return tag2attr[tn.toLowerCase()];
|
||||||
|
return tn;
|
||||||
|
}
|
||||||
|
|
||||||
|
function promote(parent, key) {
|
||||||
|
if (parent[key].length === 0) return parent[key] = {};
|
||||||
|
var t = {};
|
||||||
|
for (var i in parent[key]) t[i] = parent[key][i];
|
||||||
|
parent[key] = t;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse(parts, parent, key, val) {
|
||||||
|
var part = parts.shift();
|
||||||
|
if (!part) {
|
||||||
|
if (isArray(parent[key])) {
|
||||||
|
parent[key].push(val);
|
||||||
|
} else if ('object' == typeof parent[key]) {
|
||||||
|
parent[key] = val;
|
||||||
|
} else if ('undefined' == typeof parent[key]) {
|
||||||
|
parent[key] = val;
|
||||||
|
} else {
|
||||||
|
parent[key] = [parent[key], val];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var obj = parent[key] = parent[key] || [];
|
||||||
|
if (']' == part) {
|
||||||
|
if (isArray(obj)) {
|
||||||
|
if ('' !== val) obj.push(val);
|
||||||
|
} else if ('object' == typeof obj) {
|
||||||
|
obj[keys(obj).length] = val;
|
||||||
|
} else {
|
||||||
|
obj = parent[key] = [parent[key], val];
|
||||||
|
}
|
||||||
|
} else if (~part.indexOf(']')) {
|
||||||
|
part = part.substr(0, part.length - 1);
|
||||||
|
if (!isint.test(part) && isArray(obj)) obj = promote(parent, key);
|
||||||
|
parse(parts, obj, part, val);
|
||||||
|
// key
|
||||||
|
} else {
|
||||||
|
if (!isint.test(part) && isArray(obj)) obj = promote(parent, key);
|
||||||
|
parse(parts, obj, part, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function merge(parent, key, val) {
|
||||||
|
if (~key.indexOf(']')) {
|
||||||
|
var parts = key.split('[');
|
||||||
|
parse(parts, parent, 'base', val);
|
||||||
|
} else {
|
||||||
|
if (!isint.test(key) && isArray(parent.base)) {
|
||||||
|
var t = {};
|
||||||
|
for (var k in parent.base) t[k] = parent.base[k];
|
||||||
|
parent.base = t;
|
||||||
|
}
|
||||||
|
if (key !== '') {
|
||||||
|
set(parent.base, key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseString(str) {
|
||||||
|
return reduce(String(str).split(/&|;/), function(ret, pair) {
|
||||||
|
try {
|
||||||
|
pair = decodeURIComponent(pair.replace(/\+/g, ' '));
|
||||||
|
} catch(e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
var eql = pair.indexOf('='),
|
||||||
|
brace = lastBraceInKey(pair),
|
||||||
|
key = pair.substr(0, brace || eql),
|
||||||
|
val = pair.substr(brace || eql, pair.length);
|
||||||
|
|
||||||
|
val = val.substr(val.indexOf('=') + 1, val.length);
|
||||||
|
|
||||||
|
if (key === '') {
|
||||||
|
key = pair;
|
||||||
|
val = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return merge(ret, key, val);
|
||||||
|
}, { base: {} }).base;
|
||||||
|
}
|
||||||
|
|
||||||
|
function set(obj, key, val) {
|
||||||
|
var v = obj[key];
|
||||||
|
if (typeof v === 'undefined') {
|
||||||
|
obj[key] = val;
|
||||||
|
} else if (isArray(v)) {
|
||||||
|
v.push(val);
|
||||||
|
} else {
|
||||||
|
obj[key] = [v, val];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function lastBraceInKey(str) {
|
||||||
|
var len = str.length,
|
||||||
|
brace,
|
||||||
|
c;
|
||||||
|
for (var i = 0; i < len; ++i) {
|
||||||
|
c = str[i];
|
||||||
|
if (']' == c) brace = false;
|
||||||
|
if ('[' == c) brace = true;
|
||||||
|
if ('=' == c && !brace) return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function reduce(obj, accumulator){
|
||||||
|
var i = 0,
|
||||||
|
l = obj.length >> 0,
|
||||||
|
curr = arguments[2];
|
||||||
|
while (i < l) {
|
||||||
|
if (i in obj) curr = accumulator.call(undefined, curr, obj[i], i, obj);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
return curr;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isArray(vArg) {
|
||||||
|
return Object.prototype.toString.call(vArg) === "[object Array]";
|
||||||
|
}
|
||||||
|
|
||||||
|
function keys(obj) {
|
||||||
|
var key_array = [];
|
||||||
|
for ( var prop in obj ) {
|
||||||
|
if ( obj.hasOwnProperty(prop) ) key_array.push(prop);
|
||||||
|
}
|
||||||
|
return key_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
function purl( url, strictMode ) {
|
||||||
|
if ( arguments.length === 1 && url === true ) {
|
||||||
|
strictMode = true;
|
||||||
|
url = undefined;
|
||||||
|
}
|
||||||
|
strictMode = strictMode || false;
|
||||||
|
url = url || window.location.toString();
|
||||||
|
|
||||||
|
return {
|
||||||
|
|
||||||
|
data : parseUri(url, strictMode),
|
||||||
|
|
||||||
|
// get various attributes from the URI
|
||||||
|
attr : function( attr ) {
|
||||||
|
attr = aliases[attr] || attr;
|
||||||
|
return typeof attr !== 'undefined' ? this.data.attr[attr] : this.data.attr;
|
||||||
|
},
|
||||||
|
|
||||||
|
// return query string parameters
|
||||||
|
param : function( param ) {
|
||||||
|
return typeof param !== 'undefined' ? this.data.param.query[param] : this.data.param.query;
|
||||||
|
},
|
||||||
|
|
||||||
|
// return fragment parameters
|
||||||
|
fparam : function( param ) {
|
||||||
|
return typeof param !== 'undefined' ? this.data.param.fragment[param] : this.data.param.fragment;
|
||||||
|
},
|
||||||
|
|
||||||
|
// return path segments
|
||||||
|
segment : function( seg ) {
|
||||||
|
if ( typeof seg === 'undefined' ) {
|
||||||
|
return this.data.seg.path;
|
||||||
|
} else {
|
||||||
|
seg = seg < 0 ? this.data.seg.path.length + seg : seg - 1; // negative segments count from the end
|
||||||
|
return this.data.seg.path[seg];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// return fragment segments
|
||||||
|
fsegment : function( seg ) {
|
||||||
|
if ( typeof seg === 'undefined' ) {
|
||||||
|
return this.data.seg.fragment;
|
||||||
|
} else {
|
||||||
|
seg = seg < 0 ? this.data.seg.fragment.length + seg : seg - 1; // negative segments count from the end
|
||||||
|
return this.data.seg.fragment[seg];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
purl.jQuery = function($){
|
||||||
|
if ($ != null) {
|
||||||
|
$.fn.url = function( strictMode ) {
|
||||||
|
var url = '';
|
||||||
|
if ( this.length ) {
|
||||||
|
url = $(this).attr( getAttrName(this[0]) ) || '';
|
||||||
|
}
|
||||||
|
return purl( url, strictMode );
|
||||||
|
};
|
||||||
|
|
||||||
|
$.url = purl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
purl.jQuery(window.jQuery);
|
||||||
|
|
||||||
|
return purl;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
Reference in New Issue
Block a user