Skip to content

Commit

Permalink
add support for bookmarking the expanded/collapsed state of the whole…
Browse files Browse the repository at this point in the history
… sidebar
  • Loading branch information
bborgesr committed Apr 21, 2017
1 parent 9b88a79 commit e71c93f
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 12 deletions.
13 changes: 10 additions & 3 deletions R/dashboardSidebar.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,19 @@ dashboardSidebar <- function(..., disable = FALSE, width = NULL, collapsed = FAL
'))))
}

# If we're restoring a bookmarked app, this holds the value of whether or not the
# sidebar was collapsed. If this is not the case, the default is whatever the user
# specified in the `collapsed` argument.
dataValue <- shiny::restoreInput(id = "sidebarCollapsed",
default = if (collapsed) "true" else "false")

# The expanded/collapsed state of the sidebar is actually set by adding a
# class to the body (not to the sidebar). However, it makes sense for the
# `collapsed` argument to belong in this function. So this information is
# just passed through (also as a class) to the `dashboardPage()` function
tags$aside(class = paste("main-sidebar", if (collapsed) "start-collapsed"),
custom_css,
# just passed through (as the `data-collapsed` attribute) to the
# `dashboardPage()` function
tags$aside(
class = "main-sidebar", `data-collapsed` = dataValue, custom_css,
tags$section(
class = "sidebar",
`data-disable` = if (disable) 1 else NULL,
Expand Down
57 changes: 54 additions & 3 deletions inst/shinydashboard.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inst/shinydashboard.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/shinydashboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion inst/shinydashboard.min.js.map

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions srcjs/input_binding_sidebarCollapsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* global Shiny */

// sidebarCollapsedInputBinding
// ------------------------------------------------------------------
// This keeps tracks of whether the sidebar is expanded (default)
// or collapsed
var sidebarCollapsedInputBinding = new Shiny.InputBinding();
$.extend(sidebarCollapsedInputBinding, {
find: function(scope) {
return $(scope).find('.main-sidebar').first();
},
getId: function(el) {
return "sidebarCollapsed";
},
getValue: function(el) {
return $(el).attr("data-collapsed");
},
setValue: function(el, value) {
$(el).attr("data-collapsed", value);
},
toggleValue: function(el) {
var current = this.getValue(el);
var newVal = (current === "true") ? "false" : "true";
this.setValue(el, newVal);
},
receiveMessage: function(el, data) {
if (data.hasOwnProperty('value'))
this.setValue(el, data.value);
},
subscribe: function(el, callback) {
$(el).on('change.sidebarCollapsedInputBinding', function() {
callback();
});
},
unsubscribe: function(el) {
$(el).off('.sidebarCollapsedInputBinding');
}
});
Shiny.inputBindings.register(sidebarCollapsedInputBinding,
'shinydashboard.sidebarCollapsedInputBinding');
13 changes: 10 additions & 3 deletions srcjs/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// Optionally disable sidebar
// Optionally disable sidebar (set through the `disable` argument
// to the `dashboardSidebar` function)
if ($("section.sidebar").data("disable")) {
$("body").addClass("sidebar-collapse");
$(".navbar > .sidebar-toggle").hide();
}

// Trigger the resize event when the sidebar is collapsed/expanded
// (this allows images to be responsive and resize themselves)
// Whenever the sidebar expand/collapse button is clicked:
$(document).on("click", ".sidebar-toggle", function() {
// 1) Trigger the resize event (so images are responsive and resize)
$(window).trigger("resize");

// 2) Update the value for the sidebar's input binding
var $obj = $('.main-sidebar.shiny-bound-input');
var inputBinding = $obj.data('shiny-input-binding');
inputBinding.toggleValue($obj);
$obj.trigger('change');
});

$(document).on("click", ".treeview > a", function() {
Expand Down
2 changes: 2 additions & 0 deletions tools/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = function(grunt) {
srcdirjs + 'sidebar.js',
srcdirjs + 'output_binding_menu.js',
srcdirjs + 'input_binding_tabItem.js',
srcdirjs + 'input_binding_sidebarCollapsed.js',
srcdirjs + '_end.js'
],
dest: destdirjs + 'shinydashboard.js'
Expand Down Expand Up @@ -108,6 +109,7 @@ module.exports = function(grunt) {
srcdirjs + 'sidebar.js',
srcdirjs + 'output_binding_menu.js',
srcdirjs + 'input_binding_tabItem.js',
srcdirjs + 'input_binding_sidebarCollapsed.js',
]
},

Expand Down

0 comments on commit e71c93f

Please sign in to comment.