2019-08-28 15:33:33 +00:00
|
|
|
|
|
|
|
window.initializeCodeFolding = function(show) {
|
|
|
|
|
|
|
|
// handlers for show-all and hide all
|
|
|
|
$("#rmd-show-all-code").click(function() {
|
|
|
|
$('div.r-code-collapse').each(function() {
|
|
|
|
$(this).collapse('show');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
$("#rmd-hide-all-code").click(function() {
|
|
|
|
$('div.r-code-collapse').each(function() {
|
|
|
|
$(this).collapse('hide');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// index for unique code element ids
|
|
|
|
var currentIndex = 1;
|
|
|
|
|
|
|
|
// select all R code blocks
|
2021-01-06 15:52:57 +00:00
|
|
|
var rCodeBlocks = $('pre.r, pre.python, pre.bash, pre.sql, pre.cpp, pre.stan, pre.julia, pre.foldable');
|
2019-08-28 15:33:33 +00:00
|
|
|
rCodeBlocks.each(function() {
|
|
|
|
|
|
|
|
// create a collapsable div to wrap the code in
|
|
|
|
var div = $('<div class="collapse r-code-collapse"></div>');
|
2021-01-06 15:52:57 +00:00
|
|
|
var showThis = (show || $(this).hasClass('fold-show')) && !$(this).hasClass('fold-hide');
|
|
|
|
if (showThis) div.addClass('in');
|
2019-08-28 15:33:33 +00:00
|
|
|
var id = 'rcode-643E0F36' + currentIndex++;
|
|
|
|
div.attr('id', id);
|
|
|
|
$(this).before(div);
|
|
|
|
$(this).detach().appendTo(div);
|
|
|
|
|
|
|
|
// add a show code button right above
|
2021-01-06 15:52:57 +00:00
|
|
|
var showCodeText = $('<span>' + (showThis ? 'Hide' : 'Code') + '</span>');
|
2019-08-28 15:33:33 +00:00
|
|
|
var showCodeButton = $('<button type="button" class="btn btn-default btn-xs code-folding-btn pull-right"></button>');
|
|
|
|
showCodeButton.append(showCodeText);
|
|
|
|
showCodeButton
|
|
|
|
.attr('data-toggle', 'collapse')
|
|
|
|
.attr('data-target', '#' + id)
|
2021-01-06 15:52:57 +00:00
|
|
|
.attr('aria-expanded', showThis)
|
2019-08-28 15:33:33 +00:00
|
|
|
.attr('aria-controls', id);
|
|
|
|
|
|
|
|
var buttonRow = $('<div class="row"></div>');
|
|
|
|
var buttonCol = $('<div class="col-md-12"></div>');
|
|
|
|
|
|
|
|
buttonCol.append(showCodeButton);
|
|
|
|
buttonRow.append(buttonCol);
|
|
|
|
|
|
|
|
div.before(buttonRow);
|
|
|
|
|
|
|
|
// update state of button on show/hide
|
|
|
|
div.on('hidden.bs.collapse', function () {
|
|
|
|
showCodeText.text('Code');
|
|
|
|
});
|
|
|
|
div.on('show.bs.collapse', function () {
|
|
|
|
showCodeText.text('Hide');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|