Debian Codegraph @

inst/rmd/h/navigation-1.1/codefolding.js

raw symbols SCIP
1
2 window.initializeCodeFolding = function(show) {
3
4 // handlers for show-all and hide all
5 $("#rmd-show-all-code").click(function() {
6 $('div.r-code-collapse').each(function() {
7 $(this).collapse('show');
8 });
9 });
10 $("#rmd-hide-all-code").click(function() {
11 $('div.r-code-collapse').each(function() {
12 $(this).collapse('hide');
13 });
14 });
15
16 // index for unique code element ids
17 var currentIndex = 1;
18
19 // select all R code blocks
20 var rCodeBlocks = $('pre.r, pre.python, pre.bash, pre.sql, pre.cpp, pre.stan, pre.julia, pre.foldable');
21 rCodeBlocks.each(function() {
22 // skip if the block has fold-none class
23 if ($(this).hasClass('fold-none')) return;
24
25 // create a collapsable div to wrap the code in
26 var div = $('<div class="collapse r-code-collapse"></div>');
27 var showThis = (show || $(this).hasClass('fold-show')) && !$(this).hasClass('fold-hide');
28 var id = 'rcode-643E0F36' + currentIndex++;
29 div.attr('id', id);
30 $(this).before(div);
31 $(this).detach().appendTo(div);
32
33 // add a show code button right above
34 var showCodeText = $('<span>' + (showThis ? 'Hide' : 'Show') + '</span>');
35 var showCodeButton = $('<button type="button" class="btn btn-default btn-xs btn-secondary btn-sm code-folding-btn pull-right float-right"></button>');
36 showCodeButton.append(showCodeText);
37 showCodeButton
38 .attr('data-toggle', 'collapse')
39 .attr('data-bs-toggle', 'collapse') // BS5
40 .attr('data-target', '#' + id)
41 .attr('data-bs-target', '#' + id) // BS5
42 .attr('aria-expanded', showThis)
43 .attr('aria-controls', id);
44
45 var buttonRow = $('<div class="row"></div>');
46 var buttonCol = $('<div class="col-md-12"></div>');
47
48 buttonCol.append(showCodeButton);
49 buttonRow.append(buttonCol);
50
51 div.before(buttonRow);
52
53 // show the div if necessary
54 if (showThis) div.collapse('show');
55
56 // update state of button on show/hide
57 // * Change text
58 // * add a class for intermediate states styling
59 div.on('hide.bs.collapse', function () {
60 showCodeText.text('Show');
61 showCodeButton.addClass('btn-collapsing');
62 });
63 div.on('hidden.bs.collapse', function () {
64 showCodeButton.removeClass('btn-collapsing');
65 });
66 div.on('show.bs.collapse', function () {
67 showCodeText.text('Hide');
68 showCodeButton.addClass('btn-expanding');
69 });
70 div.on('shown.bs.collapse', function () {
71 showCodeButton.removeClass('btn-expanding');
72 });
73
74 });
75
76 }