From 07fe8bf9665c2a81ad34d305100af3c15ce0e498 Mon Sep 17 00:00:00 2001 From: Ashley Blewer Date: Sat, 13 Jul 2019 18:41:44 -0400 Subject: [PATCH] Move repeated JS into functions --- js/js.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/js/js.js b/js/js.js index 8719618..647f2a5 100644 --- a/js/js.js +++ b/js/js.js @@ -1,28 +1,37 @@ $(document).ready(function() { - // open recipe window if a hash is found in URL - if(window.location.hash) { - id = window.location.hash - document.getElementById(id.substring(1)).checked = true; - $('html, body').animate({ scrollTop: $(id).offset().top}, 1000); + function appendLink(id) { + console.log(id) $(id).closest('div').find('.link').empty(); - $(id).closest('div').find('.link').append("Link to this command: https://amiaopensource.github.io/ffmprovisr/index.html"+window.location.hash+""); + $(id).closest('div').find('.link') + .append("Link to this command: https://amiaopensource.github.io/ffmprovisr/index.html" + id + ""); + + } + + function moveToRecipe(id) { + document.getElementById(id.substring(1)).checked = true; + $('html, body').animate({ scrollTop: $(id).offset().top }, 1000); + appendLink(id) + } + + // open recipe window if a hash is found in URL + if (window.location.hash) { + id = window.location.hash + moveToRecipe(id) } // add hash URL when recipe is opened $('label[class="recipe"]').on("click", function(){ id = $(this).attr("for"); window.location.hash = ('#' + id) - $('#' + id).closest('div').find('.link').empty(); - $('#' + id).closest('div').find('.link').append("Link to this command: https://amiaopensource.github.io/ffmprovisr/index.html"+window.location.hash+""); - }); + appendLink('#' + id) + }) // open recipe when clicked $('a').on("click", function(){ intralink = $(this).attr("href") if (intralink[0] == "#") { - document.getElementById(intralink.substring(1)).checked = true; - $('html, body').animate({ scrollTop: $(intralink).offset().top }, 1000); + moveToRecipe(intralink) } })