From 29e37c53ce1bdf45ca54c8ae25a5839b2cdef530 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Mon, 9 May 2022 22:27:25 +0200 Subject: [PATCH] feat: selection with name and type --- R/selection.R | 11 ++++++++--- js/htmlwidget.ts | 2 +- js/model.ts | 9 ++++++++- js/utils.ts | 14 +++++++++++--- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/R/selection.R b/R/selection.R index 661fbac..02fccb7 100644 --- a/R/selection.R +++ b/R/selection.R @@ -10,7 +10,7 @@ #' #' sets the selection of the chart #' @param upsetjs an object of class \code{upsetjs} or \code{upsetjs_proxy} -#' @param name the name of the set to select +#' @param name the name of the set to select or a list with name and type #' @return the object given as first argument #' @examples #' upsetjs() %>% @@ -20,10 +20,15 @@ setSelection <- function(upsetjs, name = NULL) { checkUpSetCommonArgument(upsetjs) stopifnot(is.null(name) || - (is.character(name) && length(name) >= 1)) + (is.character(name) && length(name) >= 1) || + (is.list(name) && 'name' %in% names(name) && 'type' %in% names(name))) # NULL won't be transmitted - setProperty(upsetjs, "selection", ifelse(is.null(name), "", name)) + if(is.null(name)) { + setProperty(upsetjs, "selection", "") + } else { + setProperty(upsetjs, "selection", name) + } } #' diff --git a/js/htmlwidget.ts b/js/htmlwidget.ts index 9a1304e..210447a 100644 --- a/js/htmlwidget.ts +++ b/js/htmlwidget.ts @@ -41,7 +41,7 @@ function toShinyEventData( if (!set) { return { name: null, - setNames: null, + setNames: [], cardinality: null, isSelected: selected == null, type: null, diff --git a/js/model.ts b/js/model.ts index bb570f8..0e5ed48 100644 --- a/js/model.ts +++ b/js/model.ts @@ -177,12 +177,14 @@ export function fixProps(context: RenderContext, delta: any, append = false) { } else if (typeof delta.selection === 'string' || Array.isArray(delta.selection)) { context.props.selection = resolveSet( delta.selection, + null, context.props.sets, context.props.combinations as ISetCombinations ); } else if (typeof delta.selection?.name === 'string') { context.props.selection = resolveSet( delta.selection.name, + delta.selection.type, context.props.sets, context.props.combinations as ISetCombinations ); @@ -192,7 +194,12 @@ export function fixProps(context: RenderContext, delta: any, append = false) { context.props.queries = delta.queries.map((query: any) => { const base = Object.assign({}, query); if (isSetQuery(query) && (typeof query.set === 'string' || Array.isArray(query.set))) { - base.set = resolveSet(query.set, context.props.sets, context.props.combinations as ISetCombinations)!; + base.set = resolveSet( + query.set, + (query as any).type, + context.props.sets, + context.props.combinations as ISetCombinations + )!; } else if (isElemQuery(query) && typeof query.elems !== 'undefined' && !Array.isArray(query.elems)) { base.elems = [query.elems]; } diff --git a/js/utils.ts b/js/utils.ts index 16ce894..a8e7e85 100644 --- a/js/utils.ts +++ b/js/utils.ts @@ -85,14 +85,22 @@ function toUnifiedCombinationName(c: ISetCombination) { .join('&'); } -export function resolveSet(set: string | string[], sets: ISets, combinations: ISetCombinations) { - const s = sets.find((s) => s.name === set); +export function resolveSet( + set: string | string[], + type: null | undefined | ISetLike['type'], + sets: ISets, + combinations: ISetCombinations +) { + const s = sets.find((s) => s.name === set && type != null && type === s.type); if (s) { return s; } const combinedNames = Array.isArray(set) ? set.slice().sort().join('&') : null; return combinations.find((c) => { - return c.name === set || (combinedNames && combinedNames === toUnifiedCombinationName(c)); + return ( + c.name === set || + (combinedNames && combinedNames === toUnifiedCombinationName(c) && type != null && type === c.type) + ); }); }