From 8c1ee663ae08ef4bce6149da02435aa7c4df9095 Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Wed, 16 Nov 2016 15:17:10 -0800 Subject: [PATCH] Clean up inventory jquery Main thing here is avoided error when `focus.offset()` is undefined. --- app/assets/javascripts/inventory.js | 36 +++++++++++------------------ app/views/inventory/index.html.erb | 2 +- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/inventory.js b/app/assets/javascripts/inventory.js index 700ff18..eedce70 100644 --- a/app/assets/javascripts/inventory.js +++ b/app/assets/javascripts/inventory.js @@ -1,8 +1,7 @@ $(document).ready(function(){ if($("[data-collapse='true']").length > 0) { $("ul", $("[data-collapse='true']")).each(function(){ - var toggle_text = $(this).children("li[data-behavior='toggle-handler']"); - toggle_text.each(function(){ + $(this).children("li[data-behavior='toggle-handler']").each(function(){ var icon = $(this).children("i"); icon.toggle(); var nested_list = $(this).next("li"); @@ -12,46 +11,39 @@ $(document).ready(function(){ nested_list.slideToggle(); expandedType = nested_list.children().attr('class'); if (expandedType == 'folder-items' && icon.is('.glyphicon-minus-sign')) { - imageItems = nested_list.find('.item-image-link'); - loadInventoryImages(imageItems); + loadInventoryImages(nested_list.find('.item-image-link')); } - // Refresh the scrollspy since we've changed the DOM - $('.scrollspy-content').scrollspy("refresh"); + $('.scrollspy-content').scrollspy("refresh"); // Refresh the scrollspy since we've changed the DOM }); }); }); - // Refresh the scrollspy since we've changed the DOM - $('.scrollspy-content').scrollspy("refresh"); + $('.scrollspy-content').scrollspy("refresh"); // Refresh the scrollspy since we've changed the DOM } // When we have an anchor on the inventory page on page load if($(".blacklight-inventory").length > 0 && window.location.hash != '') { var focus = $("[data-reference-id='" + window.location.hash +"']"); // Show the next list item and all its hidden list item parents - nested_list = focus.next("li"); - nested_list.show(); - imageItems = nested_list.find('.item-image-link'); - loadInventoryImages(imageItems); + nested_list = focus.next("li").show(); + loadInventoryImages(nested_list.find('.item-image-link')); focus.children("i").toggleClass("glyphicon-minus-sign"); focus.parents("li:hidden").each(function(){ - $(this).show(); - $(this).prev("li").children("i").toggleClass("glyphicon-minus-sign"); + $(this).show().prev("li").children("i").toggleClass("glyphicon-minus-sign"); }); - // Scroll to the item that we're trying to focus on. - $(window).scrollTop(focus.offset().top); - // Refresh the scrollspy since we've changed the DOM - $('.scrollspy-content').scrollspy("refresh"); + if (focus.offset()) { + $(window).scrollTop(focus.offset().top); // Scroll to the item that we're trying to focus on. + } + $('.scrollspy-content').scrollspy("refresh"); // Refresh the scrollspy since we've changed the DOM } }); +// load all images on the inventory page; not currently used function loadAllInventoryImages() { - // load all images on the inventory page; not currently used - itemImages = $('.item-image-link'); - loadInventoryImages(itemImages); + loadInventoryImages($('.item-image-link')); } +// only load specified images on the inventory page by passing in a JQuery object (dom item) function loadInventoryImages(itemImages) { - // only load specified images on the inventory page by passing in a dom item for (var i = 0 ; i < itemImages.length; i++) { itemImages[i].innerHTML = '\''' } diff --git a/app/views/inventory/index.html.erb b/app/views/inventory/index.html.erb index e89e677..d25c75c 100644 --- a/app/views/inventory/index.html.erb +++ b/app/views/inventory/index.html.erb @@ -42,7 +42,7 @@