Skip to content

Commit

Permalink
Added a check for a missing switch value in localStorage
Browse files Browse the repository at this point in the history
I added a check to see if the blockSwitch value has been set in localStorage and set it to the first value found in the HTML file if not.
  • Loading branch information
Jay Bryant committed Jan 29, 2020
1 parent b900957 commit 723b44e
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions block-switch/src/main/resources/blockSwitch.js
@@ -0,0 +1,68 @@
function addBlockSwitches() {
$('.primary').each(function() {
primary = $(this);
createSwitchItem(primary, createBlockSwitch(primary));
primary.children('.title').remove();
});
$('.secondary').each(function(idx, node) {
secondary = $(node);
primary = findPrimary(secondary);
switchItem = createSwitchItem(secondary, primary.children('.switch'));
switchItem.content.addClass('hidden');
findPrimary(secondary).append(switchItem.content);
secondary.remove();
});
if (window.localStorage.getItem("blockSwitch") === null) {
window.localStorage.setItem("blockSwitch", $( "div.primary" ).find("div.switch--item").first().text())
}
$(".switch--item:contains(" + window.localStorage.getItem("blockSwitch") +")").addClass("selected");
}

function createBlockSwitch(primary) {
blockSwitch = $('<div class="switch"></div>');
primary.prepend(blockSwitch);
return blockSwitch;
}

function findPrimary(secondary) {
candidate = secondary.prev();
while (!candidate.is('.primary')) {
candidate = candidate.prev();
}
return candidate;
}

function createSwitchItem(block, blockSwitch) {
blockName = block.children('.title').text();
content = block.children('.content').first().append(block.next('.colist'));
item = $('<div class="switch--item">' + blockName + '</div>');
item.on('click', '', content, function(e) {
$(this).addClass('selected');
$(this).siblings().removeClass('selected');
e.data.siblings('.content').addClass('hidden');
e.data.removeClass('hidden');
});
blockSwitch.append(item);
return {'item': item, 'content': content};
}

function globalSwitch() {
$('.switch--item').each(function() {
$(this).off('click');
$(this).on('click', function() {
window.localStorage.setItem("blockSwitch", $(this).text());
selectedText = $(this).text()
selectedIndex = $(this).index()
$(".switch--item").filter(function() { return ($(this).text() === selectedText) }).each(function() {
$(this).addClass('selected');
$(this).siblings().removeClass('selected');
selectedContent = $(this).parent().siblings(".content").eq(selectedIndex)
selectedContent.removeClass('hidden');
selectedContent.siblings().addClass('hidden');
});
});
});
}

$(addBlockSwitches);
$(globalSwitch);

0 comments on commit 723b44e

Please sign in to comment.