Skip to content

Commit

Permalink
feat: selection with name and type
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed May 9, 2022
1 parent 49acd43 commit 29e37c5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
11 changes: 8 additions & 3 deletions R/selection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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() %>%
Expand All @@ -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)
}
}

#'
Expand Down
2 changes: 1 addition & 1 deletion js/htmlwidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function toShinyEventData(
if (!set) {
return {
name: null,
setNames: null,
setNames: [],
cardinality: null,
isSelected: selected == null,
type: null,
Expand Down
9 changes: 8 additions & 1 deletion js/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Elem>
);
} 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<Elem>
);
Expand All @@ -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<Elem>)!;
base.set = resolveSet(
query.set,
(query as any).type,
context.props.sets,
context.props.combinations as ISetCombinations<Elem>
)!;
} else if (isElemQuery(query) && typeof query.elems !== 'undefined' && !Array.isArray(query.elems)) {
base.elems = [query.elems];
}
Expand Down
14 changes: 11 additions & 3 deletions js/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,22 @@ function toUnifiedCombinationName(c: ISetCombination<any>) {
.join('&');
}

export function resolveSet(set: string | string[], sets: ISets<any>, combinations: ISetCombinations<any>) {
const s = sets.find((s) => s.name === set);
export function resolveSet(
set: string | string[],
type: null | undefined | ISetLike['type'],
sets: ISets<any>,
combinations: ISetCombinations<any>
) {
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)
);
});
}

Expand Down

0 comments on commit 29e37c5

Please sign in to comment.