forked from spring-io/spring-asciidoctor-extensions
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a check for a missing switch value in localStorage
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |