PageSourceSearch

https://www.geraldp.net/deckstacker_docs/codefolding.js

js geraldp.net collected 2026-09-25 03:04:13 UTC 5,923 bytes, 143 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.