Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support modules in conditionalPanel #1735

Merged
merged 31 commits into from
Jun 23, 2017

Conversation

alandipert
Copy link
Contributor

No description provided.

@alandipert
Copy link
Contributor Author

alandipert commented Jun 8, 2017

Here is Winston's reproduction of the problem modified to work using the new ns argument of conditionalPanel:

library(shiny)

condpanelUI <- function(id) {
  ns <- NS(id)

  tagList(
    checkboxInput(ns("checkbox"), "Make module panel visible"),
    conditionalPanel(condition = "input.checkbox",
                     ns = ns,
                     "This is the module conditional panel")
  )
}

ui <- fluidPage(
  condpanelUI("foo"),
  checkboxInput("checkbox", "Make top-level panel visible"),
  conditionalPanel(condition = "input.checkbox", "This is the top-level conditional panel")
)

server <- function(input, output, session) {
}

shinyApp(ui, server)

// by nsPrefix. Returns a new object with keys removed and renamed as
// necessary.
function narrowScope(scopeComponent, nsPrefix) {
return mapKeys(pickBy(scopeComponent, function(val, key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Feel free to use arrow functions
  2. I found the indentation a little deceptive somehow, i.e. hard to tell whether the second closure belongs to mapKeys or pickBy
  3. Can you make it so the lodash-alike functions are actually accessed via a _ object? So we can swap them out in the future and also to make it obvious that the semantics of these functions must not be changed (unless it's to match lodash even more closely).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The common case by far will be no prefix, exit early in that case?

var show = condFunc(scope);
// The data-ns-prefix attribute is always present, but might be empty. If
// it's empty, no keys are removed or renamed by narrowScope.
var nsPrefix = el.attr('data-ns-prefix');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check for null/undefined here actually? It's possible for existing HTML templates to have conditional panels hard coded. Our support for such code is not clearly defined but in this case it's so trivial to not break them.

srcjs/utils.js Outdated
// pickBy function from lodash.
function pickBy(obj, predicate) {
const newObj = {};
for (let key in obj) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wch Can we use Object.keys? I can't remember.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that should be safe.

@jcheng5
Copy link
Member

jcheng5 commented Jun 10, 2017

Shaping up nicely! It will be a real relief to me when we get this merged!

@alandipert
Copy link
Contributor Author

alandipert commented Jun 15, 2017

Introducing Lodash, even with just the functions we use, increases the size of shiny.min.js.gz from 21kb to 33kb. @wch does not believe the cleaner code justifies the extra code size, and I'm on the fence.

@jcheng5
Copy link
Member

jcheng5 commented Jun 16, 2017

I agree with @wch fwiw...

R/bootstrap.R Outdated
conditionalPanel <- function(condition, ...) {
div('data-display-if'=condition, ...)
conditionalPanel <- function(condition, ..., ns = NS(NULL)) {
div('data-display-if'=condition, 'data-ns-prefix'=ns(""), ...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: backticks here would be more idiomatic R.

return Object.keys(scopeComponent)
.filter(k => k.startsWith(nsPrefix))
.map(k => ({[k.substring(nsPrefix.length)]: scopeComponent[k]}))
.reduce((obj, pair) => Object.assign(obj, pair), {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.assign isn't supported by IE at all, and it's not polyfilled, so we should use $.extend() instead.

@alandipert
Copy link
Contributor Author

I think this is now in an IE9-compatible state, but I still need to test and confirm. Will report back soon.

@wch
Copy link
Collaborator

wch commented Jun 22, 2017

Looks good to me. If it works correctly on IE9, then I think it can be merged.

@alandipert
Copy link
Contributor Author

@wch can confirm: works on IE9. Branch and small demo deployed here: https://beta.rstudioconnect.com/content/2858/

@wch wch merged commit 10db7ad into master Jun 23, 2017
@wch wch deleted the alandipert/feature-namespace-conditional-panel branch June 23, 2017 15:12
@wch wch removed the review label Jun 23, 2017
@jcheng5
Copy link
Member

jcheng5 commented Jun 23, 2017

🎉

maximesunnen added a commit to maximesunnen/flowFate that referenced this pull request Apr 6, 2023
tripartio added a commit to tripartio/ale that referenced this pull request Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants