Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

test

1492 views
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');
21
rCodeBlocks.each(function() {
22
23
// create a collapsable div to wrap the code in
24
var div = $('<div class="collapse r-code-collapse"></div>');
25
if (show)
26
div.addClass('in');
27
var id = 'rcode-643E0F36' + currentIndex++;
28
div.attr('id', id);
29
$(this).before(div);
30
$(this).detach().appendTo(div);
31
32
// add a show code button right above
33
var showCodeText = $('<span>' + (show ? 'Hide' : 'Code') + '</span>');
34
var showCodeButton = $('<button type="button" class="btn btn-default btn-xs code-folding-btn pull-right"></button>');
35
showCodeButton.append(showCodeText);
36
showCodeButton
37
.attr('data-toggle', 'collapse')
38
.attr('data-target', '#' + id)
39
.attr('aria-expanded', show)
40
.attr('aria-controls', id);
41
42
var buttonRow = $('<div class="row"></div>');
43
var buttonCol = $('<div class="col-md-12"></div>');
44
45
buttonCol.append(showCodeButton);
46
buttonRow.append(buttonCol);
47
48
div.before(buttonRow);
49
50
// update state of button on show/hide
51
div.on('hidden.bs.collapse', function () {
52
showCodeText.text('Code');
53
});
54
div.on('show.bs.collapse', function () {
55
showCodeText.text('Hide');
56
});
57
});
58
59
}
60
61