Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Accordion state #1544

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions game/static/game/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ ocargo.Game.prototype.setup = function () {
}
}


// Function being called when there is a change in game level.
ocargo.Game.prototype.onLevelChange = function() {
const currentLevelId = LEVEL_ID;
localStorage.setItem('currentEpisode', EPISODE);
}

restoreCmsLogin()
initCustomBlocks()
ocargo.solutionLoaded = false
Expand Down Expand Up @@ -126,6 +133,15 @@ ocargo.Game.prototype.setup = function () {
)
}

// Script used to save and check for episode upon loading of the webpage
document.addEventListener("DOMContentLoaded", function() {
const dataElement = document.getElementById('data');
const currentEpisode = dataElement ? dataElement.getAttribute('data-episode') : null;
if (currentEpisode) {
localStorage.setItem('currentEpisode', currentEpisode);
}
});

ocargo.Game.prototype.clearWorkspaceNameInputInSaveTab = function () {
$('#workspaceNameInput').val('')
}
Expand Down
42 changes: 42 additions & 0 deletions game/static/game/js/level_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,45 @@ function setupCoins() {
}
}
}

document.addEventListener("DOMContentLoaded", function() {
const storedEpisode = localStorage.getItem('currentEpisode');
const allCollapsibles = document.querySelectorAll('.collapse');

// Function to close all except the given ID
function closeAllExcept(openId) {
allCollapsibles.forEach(function(collapsible) {
if (collapsible.id !== openId) {
collapsible.classList.remove('in');
const toggle = document.querySelector(`[data-target="#${collapsible.id}"]`);
toggle.classList.add('collapsed');
toggle.setAttribute('aria-expanded', 'false');
}
});
}

// Open the accordion for the stored episode
const accordionToOpen = storedEpisode ? document.querySelector(`#collapse-${storedEpisode}`) : null;
if (accordionToOpen) {
accordionToOpen.classList.add('in');
const toggle = document.querySelector(`[data-target="#${accordionToOpen.id}"]`);
toggle.classList.remove('collapsed');
toggle.setAttribute('aria-expanded', 'true');
} else {
closeAllExcept(); // Close all of no epissodes are stored
}

// Set up event listeners for accordion toggles
const accordionToggles = document.querySelectorAll('.episode-title');
accordionToggles.forEach(function(toggle) {
toggle.addEventListener('click', function() {
const collapseId = toggle.getAttribute('data-target').replace('#', '');
const isOpened = toggle.getAttribute('aria-expanded') === 'false'; // Notice the change here
localStorage.setItem('currentEpisode', isOpened ? collapseId.split('-')[1] : null);
if (isOpened) {
closeAllExcept(collapseId);
}
});
});

});
1 change: 0 additions & 1 deletion game/templates/game/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
var PYTHON_WORKSPACE = {% if python_workspace == None %}null{% else %}"{{python_workspace|linebreaksbr}}"{% endif %};
</script>


<script defer type="text/javascript" src="{% static 'game/js/utils.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/skulpt/skulpt.min.js' %}"></script>
<script defer type="text/javascript" src="{% static 'game/js/skulpt/skulpt-stdlib.js' %}"></script>
Expand Down
Loading