Skip to content

Commit

Permalink
Clean up inventory jquery
Browse files Browse the repository at this point in the history
Main thing here is avoided error when `focus.offset()` is undefined.
  • Loading branch information
atz committed Nov 16, 2016
1 parent ff2a5c7 commit 8c1ee66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
36 changes: 14 additions & 22 deletions app/assets/javascripts/inventory.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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 = '<a href="' + itemImages[i].attributes['data-image-link'].value + '"><img alt=\'' + itemImages[i].attributes['data-image-title'].value + '\' title=\'' + itemImages[i].attributes['data-image-title'].value + '\' src="' + itemImages[i].attributes['data-image-url'].value + '"></a>'
}
Expand Down
2 changes: 1 addition & 1 deletion app/views/inventory/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<li>
<ul class="folder-items">
<li class="item-count"><%=t('bassi.show.item', count: folder.children.total_members)%>:</li>
<ul class="item-list">
<ul class="item-list">
<%- folder.children.each do |document| -%>
<li class="item-image-link" data-image-link="<%=solr_document_path(document[:id])%>" data-image-url="<%=show_image(document.images(:square).first)%>" data-image-title="<%=document.title%>">
<noscript>
Expand Down

0 comments on commit 8c1ee66

Please sign in to comment.