diff --git a/book.toml b/book.toml index f35932559f0f..bbf28db84256 100644 --- a/book.toml +++ b/book.toml @@ -46,7 +46,12 @@ urlcolor = "red" [output.html] curly-quotes = true -additional-js = ["theme/speaker-notes.js", "theme/redbox.js"] +additional-js = [ + "theme/speaker-notes.js", + "theme/redbox.js", + "theme/save-playgrounds.js", + "theme/instructor-menu.js", +] additional-css = [ "theme/css/svgbob.css", "theme/css/redbox.css", diff --git a/theme/css/redbox.css b/theme/css/redbox.css index 79e9ed990789..633ed37b6b9f 100644 --- a/theme/css/redbox.css +++ b/theme/css/redbox.css @@ -18,3 +18,7 @@ div#aspect-ratio-helper div { of vertical space to borders. */ aspect-ratio: 16/8.5; } + +#instructor-menu-list { + margin-left: 55px; +} diff --git a/theme/instructor-menu.js b/theme/instructor-menu.js new file mode 100644 index 000000000000..7d742e5dcd07 --- /dev/null +++ b/theme/instructor-menu.js @@ -0,0 +1,71 @@ +(function handleInstructor() { + function handleInstructorMenu() { + let leftButtons = document.getElementsByClassName("left-buttons")[0]; + let instructorMenu = document.createElement("button"); + let instructorMenuList = document.createElement("ul"); + let redBoxItem = document.createElement("li"); + let redBoxButton = document.createElement("button"); + let playgroundStateItem = document.createElement("li"); + let playgroundStateButton = document.createElement("button"); + + leftButtons.insertBefore(instructorMenu, leftButtons.lastChild); + leftButtons.insertBefore(instructorMenuList, leftButtons.lastChild); + instructorMenuList.insertBefore(redBoxItem, instructorMenuList.lastChild); + instructorMenuList.insertBefore( + playgroundStateItem, + instructorMenuList.lastChild + ); + redBoxItem.insertBefore(redBoxButton, redBoxItem.lastChild); + playgroundStateItem.insertBefore( + playgroundStateButton, + playgroundStateItem.lastChild + ); + + instructorMenu.title = "Utilities for course instructors"; + instructorMenu.innerHTML = + ''; + redBoxButton.innerHTML = "aspect-ratio box"; + redBoxButton.title = + "Outline the area that fits on one screen while teaching the course."; + playgroundStateButton.innerHTML = "reset all playgrounds"; + playgroundStateButton.title = + "Reset code in all playgrounds to its original value."; + + instructorMenu.className = "icon-button"; + instructorMenuList.className = "theme-popup"; + redBoxButton.className = "theme"; + playgroundStateButton.className = "theme"; + instructorMenuList.style.display = "none"; + + instructorMenuList.role = "menu"; + redBoxItem.role = "none"; + playgroundStateItem.role = "none"; + redBoxButton.role = "menuitem"; + playgroundStateButton.role = "menuitem"; + + redBoxButton.id = "redbox"; + instructorMenuList.id = "instructor-menu-list"; + playgroundStateButton.id = "playground-state"; + + instructorMenu.addEventListener("click", () => { + if (instructorMenuList.style.display === "none") { + instructorMenuList.style.display = "block"; + } else { + instructorMenuList.style.display = "none"; + } + }); + + document.addEventListener("click", (e) => { + if (!instructorMenu.contains(e.target)) { + instructorMenuList.style.display = "none"; + } + }); + } + handleInstructorMenu(); + var redBoxButton = document.getElementById("redbox"); + var playgroundStateButton = document.getElementById("playground-state"); + redBoxButton.addEventListener("click", () => window.redboxButtonClicked()); + playgroundStateButton.addEventListener("click", () => + window.resetPlaygroundsClicked() + ); +})(); diff --git a/theme/redbox.js b/theme/redbox.js index dd8a1c313803..754baffed931 100644 --- a/theme/redbox.js +++ b/theme/redbox.js @@ -11,28 +11,19 @@ var parentElement = document.body; // Change this to your desired parent element // Append the new div to the parent element parentElement.insertBefore(newDiv, parentElement.firstChild); - // Create the button element - var hideShowButton = document.createElement("button"); - hideShowButton.innerHTML = ''; - hideShowButton.className = "icon-button"; - hideShowButton.type = "button"; - hideShowButton.title = - "Outline the area that fits on one screen while teaching the course."; - hideShowButton.id = "Dev"; - var navbarButtons = document.getElementsByClassName("left-buttons"); - navbarButtons[0].insertBefore(hideShowButton, navbarButtons.firstChild); //Default hiding the redbox document.getElementById("aspect-ratio-helper").style.display = "none"; - //Add Event listener to button to perform on click action. - hideShowButton.addEventListener("click", function () { - if ( - document.getElementById("aspect-ratio-helper").style.display === "none" - ) { - document.getElementById("aspect-ratio-helper").style.display = "block"; - hideShowButton.innerHTML = ''; - } else { - document.getElementById("aspect-ratio-helper").style.display = "none"; - hideShowButton.innerHTML = ''; - } - }); })(); + +//Create a function to button to perform on click action. +function redboxButtonClicked() { + var hideShowButton = document.getElementById("redbox"); + if (document.getElementById("aspect-ratio-helper").style.display === "none") { + document.getElementById("aspect-ratio-helper").style.display = "block"; + hideShowButton.innerHTML = "aspect-ratio box"; + } else { + document.getElementById("aspect-ratio-helper").style.display = "none"; + hideShowButton.innerHTML = "aspect-ratio box"; + } +} +window.redboxButtonClicked = redboxButtonClicked; diff --git a/theme/save-playgrounds.js b/theme/save-playgrounds.js new file mode 100644 index 000000000000..bfe45e47be6f --- /dev/null +++ b/theme/save-playgrounds.js @@ -0,0 +1,46 @@ +(function savePlaygrounds() { + function setCodeToPlayground() { + var codes = JSON.parse( + localStorage.getItem(`${window.location.href}₹code`) + ); + if (codes) { + var i = 0; + Array.from(document.querySelectorAll(".playground")).forEach(function ( + pre_block + ) { + let code_block = pre_block.querySelector("code"); + let editor = window.ace.edit(code_block); + editor.setValue(codes[i]); + editor.clearSelection(); + i += 1; + }); + } + } + function getCodeFromPlayground() { + var codes = []; + Array.from(document.querySelectorAll(".playground")).forEach(function ( + pre_block + ) { + let code_block = pre_block.querySelector("code"); + let editor = window.ace.edit(code_block); + let code = editor.getValue(); + codes.push(code); + }); + localStorage.setItem(`${window.location.href}₹code`, JSON.stringify(codes)); + } + setCodeToPlayground(); + addEventListener("pagehide", getCodeFromPlayground); +})(); + +function resetPlaygroundsClicked() { + let keys = []; + for (var i = 0, len = localStorage.length; i < len; i++) { + if (localStorage.key(i).includes("₹code")) { + keys.push(localStorage.key(i)); + } + } + for (let j = 0; j < keys.length; j++) { + localStorage.removeItem(keys[j]); + } +} +window.resetPlaygroundsClicked = resetPlaygroundsClicked;