hide-cell.html 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <script>
  2. /**
  3. Add buttons to hide code cells
  4. */
  5. var setCodeCellVisibility = function (inputField, kind) {
  6. // Update the image and class for hidden
  7. var id = inputField.getAttribute('data-id');
  8. var codeCell = document.querySelector(`#${id} div.highlight`);
  9. if (kind === "visible") {
  10. codeCell.classList.remove('hidden');
  11. inputField.checked = true;
  12. } else {
  13. codeCell.classList.add('hidden');
  14. inputField.checked = false;
  15. }
  16. }
  17. var toggleCodeCellVisibility = function (event) {
  18. // The label is clicked, and now we decide what to do based on the input field's clicked status
  19. if (event.target.tagName === "LABEL") {
  20. var inputField = event.target.previousElementSibling;
  21. } else {
  22. // It is the span inside the target
  23. var inputField = event.target.parentElement.previousElementSibling;
  24. }
  25. if (inputField.checked === true) {
  26. setCodeCellVisibility(inputField, "visible");
  27. } else {
  28. setCodeCellVisibility(inputField, "hidden");
  29. }
  30. }
  31. // Button constructor
  32. const hideCodeButton = id => `<input class="hidebtn" type="checkbox" id="hidebtn${id}" data-id="${id}"><label title="Toggle cell" for="hidebtn${id}" class="plusminus"><span class="pm_h"></span><span class="pm_v"></span></label>`
  33. var addHideButton = function () {
  34. // If a hide button is already added, don't add another
  35. if (document.querySelector('div.tag_hide_input input') !== null) {
  36. return;
  37. }
  38. // Find the input cells and add a hide button
  39. pageElements['inputCells'].forEach(function (inputCell) {
  40. if (!inputCell.classList.contains("tag_hide_input")) {
  41. // Skip the cell if it doesn't have a hidecode class
  42. return;
  43. }
  44. const id = inputCell.getAttribute('id')
  45. // Insert the button just inside the end of the next div
  46. inputCell.querySelector('div.input').insertAdjacentHTML('beforeend', hideCodeButton(id))
  47. // Set up the visibility toggle
  48. hideLink = document.querySelector(`#${id} div.inner_cell + input + label`);
  49. hideLink.addEventListener('click', toggleCodeCellVisibility)
  50. });
  51. }
  52. // Initialize the hide buttos
  53. var initHiddenCells = function () {
  54. // Add hide buttons to the cells
  55. addHideButton();
  56. // Toggle the code cells that should be hidden
  57. document.querySelectorAll('div.tag_hide_input input').forEach(function (item) {
  58. setCodeCellVisibility(item, 'hidden');
  59. item.checked = true;
  60. })
  61. }
  62. initFunction(initHiddenCells);
  63. </script>