codefolding.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. @licstart The following is the entire license notice for the JavaScript code in this file.
  3. The MIT License (MIT)
  4. Copyright (C) 1997-2026 by Dimitri van Heesch
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of this software
  6. and associated documentation files (the "Software"), to deal in the Software without restriction,
  7. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all copies or
  11. substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  13. BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  15. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. @licend The above is the entire license notice for the JavaScript code in this file
  18. */
  19. let codefold = {
  20. opened : true,
  21. show_plus : function(el) {
  22. if (el) {
  23. el.classList.remove('minus');
  24. el.classList.add('plus');
  25. }
  26. },
  27. show_minus : function(el) {
  28. if (el) {
  29. el.classList.add('minus');
  30. el.classList.remove('plus');
  31. }
  32. },
  33. // toggle all folding blocks
  34. toggle_all : function() {
  35. if (this.opened) {
  36. const foldAll = document.getElementById('fold_all');
  37. this.show_plus(foldAll);
  38. document.querySelectorAll('div[id^=foldopen]').forEach(el => el.style.display = 'none');
  39. document.querySelectorAll('div[id^=foldclosed]').forEach(el => el.style.display = '');
  40. document.querySelectorAll('div[id^=foldclosed] span.fold').forEach(el => this.show_plus(el));
  41. } else {
  42. const foldAll = document.getElementById('fold_all');
  43. this.show_minus(foldAll);
  44. document.querySelectorAll('div[id^=foldopen]').forEach(el => el.style.display = '');
  45. document.querySelectorAll('div[id^=foldclosed]').forEach(el => el.style.display = 'none');
  46. }
  47. this.opened=!this.opened;
  48. },
  49. // toggle single folding block
  50. toggle : function(id) {
  51. const openEl = document.getElementById('foldopen'+id);
  52. const closedEl = document.getElementById('foldclosed'+id);
  53. if (openEl) {
  54. openEl.style.display = openEl.style.display === 'none' ? '' : 'none';
  55. const nextEl = openEl.nextElementSibling;
  56. if (nextEl) {
  57. nextEl.querySelectorAll('span.fold').forEach(el => this.show_plus(el));
  58. }
  59. }
  60. if (closedEl) {
  61. closedEl.style.display = closedEl.style.display === 'none' ? '' : 'none';
  62. }
  63. },
  64. init : function() {
  65. // add code folding line and global control
  66. document.querySelectorAll('span.lineno').forEach((el, index) => {
  67. el.style.paddingRight = '4px';
  68. el.style.marginRight = '2px';
  69. el.style.display = 'inline-block';
  70. el.style.width = '54px';
  71. el.style.background = 'linear-gradient(var(--fold-line-color),var(--fold-line-color)) no-repeat 46px/2px 100%';
  72. const span = document.createElement('span');
  73. if (index === 0) { // add global toggle to first line
  74. span.className = 'fold minus';
  75. span.id = 'fold_all';
  76. span.onclick = () => codefold.toggle_all();
  77. } else { // add vertical lines to other rows
  78. span.className = 'fold'
  79. }
  80. el.appendChild(span);
  81. });
  82. // add toggle controls to lines with fold divs
  83. document.querySelectorAll('div.foldopen').forEach(el => {
  84. // extract specific id to use
  85. const id = el.getAttribute('id').replace('foldopen','');
  86. // extract start and end foldable fragment attributes
  87. const start = el.getAttribute('data-start');
  88. const end = el.getAttribute('data-end');
  89. // replace normal fold span with controls for the first line of a foldable fragment
  90. const firstFold = el.querySelector('span.fold');
  91. if (firstFold) {
  92. const span = document.createElement('span');
  93. span.className = 'fold minus';
  94. span.onclick = () => codefold.toggle(id);
  95. firstFold.replaceWith(span);
  96. }
  97. // append div for folded (closed) representation
  98. const closedDiv = document.createElement('div');
  99. closedDiv.id = 'foldclosed'+id;
  100. closedDiv.className = 'foldclosed';
  101. closedDiv.style.display = 'none';
  102. el.after(closedDiv);
  103. // extract the first line from the "open" section to represent closed content
  104. const line = el.children[0] ? el.children[0].cloneNode(true) : null;
  105. if (line) {
  106. // remove any glow that might still be active on the original line
  107. line.classList.remove('glow');
  108. if (start) {
  109. // if line already ends with a start marker (e.g. trailing {), remove it
  110. line.innerHTML = line.innerHTML.replace(new RegExp('\\s*'+start+'\\s*$','g'),'');
  111. }
  112. // replace minus with plus symbol
  113. line.querySelectorAll('span.fold').forEach(span => {
  114. codefold.show_plus(span);
  115. // re-apply click handler as it is not copied with cloneNode
  116. span.onclick = () => codefold.toggle(id);
  117. });
  118. // append ellipsis
  119. const ellipsisLink = document.createElement('a');
  120. ellipsisLink.href = "javascript:codefold.toggle('"+id+"')";
  121. ellipsisLink.innerHTML = '…';
  122. line.appendChild(document.createTextNode(' '+start));
  123. line.appendChild(ellipsisLink);
  124. line.appendChild(document.createTextNode(end));
  125. // insert constructed line into closed div
  126. closedDiv.appendChild(line);
  127. }
  128. });
  129. },
  130. };
  131. /* @license-end */