From 804ed1262fa4392967f5aa0c002801d35fda462d Mon Sep 17 00:00:00 2001 From: karatmate Date: Tue, 28 Nov 2023 15:21:12 +0000 Subject: [PATCH 1/2] Changes in accordion state --- game/static/game/js/game.js | 30 ++++++++++++++++ game/templates/game/game.html | 12 +++++++ game/templates/game/level_selection.html | 45 ++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/game/static/game/js/game.js b/game/static/game/js/game.js index 1086ac286..654cd3f98 100644 --- a/game/static/game/js/game.js +++ b/game/static/game/js/game.js @@ -20,6 +20,36 @@ ocargo.Game.prototype.setup = function () { } } +document.addEventListener("DOMContentLoaded", function() { + var dataElement = document.getElementById('data'); + var currentEpisode = dataElement ? dataElement.getAttribute('data-episode') : null; + + if (currentEpisode) { + localStorage.setItem('currentEpisode', currentEpisode); + } + +}); + +ocargo.Game.prototype.onLevelChange = function() { + var currentLevelId = LEVEL_ID; + var currentEpisode = this.determineEpisodeFromLevel(currentLevelId); + localStorage.setItem('currentEpisode', currentEpisode); +} + +ocargo.Game.prototype.determineEpisodeFromLevel = function(levelId) { + if(levelId <= 12) return "1"; + else if(levelId <= 18) return "2"; + else if(levelId <= 28) return "3"; + else if(levelId <= 32) return "4"; + else if(levelId <= 43) return "5"; + else if(levelId <= 50) return "6"; + else if(levelId <= 60) return "7"; + else if(levelId <= 67) return "8"; + else if(levelId <= 79) return "9"; + else if(levelId <= 91) return "10"; + else if(levelId <= 109) return "11"; +} + restoreCmsLogin() initCustomBlocks() ocargo.solutionLoaded = false diff --git a/game/templates/game/game.html b/game/templates/game/game.html index 81a6024e6..7be561745 100644 --- a/game/templates/game/game.html +++ b/game/templates/game/game.html @@ -79,6 +79,18 @@ var PYTHON_WORKSPACE = {% if python_workspace == None %}null{% else %}"{{python_workspace|linebreaksbr}}"{% endif %}; + + diff --git a/game/templates/game/level_selection.html b/game/templates/game/level_selection.html index f3ca0888f..2fcafbb75 100644 --- a/game/templates/game/level_selection.html +++ b/game/templates/game/level_selection.html @@ -41,6 +41,51 @@ {% endfor %} + + {% endblock %} From e6239eadc97e2943336ffdb152997dbec36c7702 Mon Sep 17 00:00:00 2001 From: karatmate Date: Wed, 6 Dec 2023 12:07:35 +0000 Subject: [PATCH 2/2] Updated level_selection and game and uploaded code --- game/static/game/js/game.js | 38 +++++++------------- game/static/game/js/level_selection.js | 42 ++++++++++++++++++++++ game/templates/game/game.html | 13 ------- game/templates/game/level_selection.html | 45 ------------------------ 4 files changed, 54 insertions(+), 84 deletions(-) diff --git a/game/static/game/js/game.js b/game/static/game/js/game.js index 654cd3f98..ad6fdcf26 100644 --- a/game/static/game/js/game.js +++ b/game/static/game/js/game.js @@ -20,34 +20,11 @@ ocargo.Game.prototype.setup = function () { } } -document.addEventListener("DOMContentLoaded", function() { - var dataElement = document.getElementById('data'); - var currentEpisode = dataElement ? dataElement.getAttribute('data-episode') : null; - if (currentEpisode) { - localStorage.setItem('currentEpisode', currentEpisode); - } - -}); - +// Function being called when there is a change in game level. ocargo.Game.prototype.onLevelChange = function() { - var currentLevelId = LEVEL_ID; - var currentEpisode = this.determineEpisodeFromLevel(currentLevelId); - localStorage.setItem('currentEpisode', currentEpisode); -} - -ocargo.Game.prototype.determineEpisodeFromLevel = function(levelId) { - if(levelId <= 12) return "1"; - else if(levelId <= 18) return "2"; - else if(levelId <= 28) return "3"; - else if(levelId <= 32) return "4"; - else if(levelId <= 43) return "5"; - else if(levelId <= 50) return "6"; - else if(levelId <= 60) return "7"; - else if(levelId <= 67) return "8"; - else if(levelId <= 79) return "9"; - else if(levelId <= 91) return "10"; - else if(levelId <= 109) return "11"; + const currentLevelId = LEVEL_ID; + localStorage.setItem('currentEpisode', EPISODE); } restoreCmsLogin() @@ -156,6 +133,15 @@ ocargo.Game.prototype.determineEpisodeFromLevel = function(levelId) { ) } +// 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('') } diff --git a/game/static/game/js/level_selection.js b/game/static/game/js/level_selection.js index dc03425f3..11d723d69 100644 --- a/game/static/game/js/level_selection.js +++ b/game/static/game/js/level_selection.js @@ -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); + } + }); + }); + +}); \ No newline at end of file diff --git a/game/templates/game/game.html b/game/templates/game/game.html index 7be561745..6640298eb 100644 --- a/game/templates/game/game.html +++ b/game/templates/game/game.html @@ -79,19 +79,6 @@ var PYTHON_WORKSPACE = {% if python_workspace == None %}null{% else %}"{{python_workspace|linebreaksbr}}"{% endif %}; - - - diff --git a/game/templates/game/level_selection.html b/game/templates/game/level_selection.html index 2fcafbb75..f3ca0888f 100644 --- a/game/templates/game/level_selection.html +++ b/game/templates/game/level_selection.html @@ -41,51 +41,6 @@ {% endfor %} - - {% endblock %}