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

Add possibility to select arbitrary elements #125

Open
wants to merge 1 commit into
base: shinyBS3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Imports:
htmltools
URL: https://ebailey78.github.io/shinyBS
BugReports: https://github.com/ebailey78/shinyBS/issues
License: GPL-3
License: GPL-3
RoxygenNote: 7.1.0
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.0): do not edit by hand
# Generated by roxygen2: do not edit by hand

export(addPopover)
export(addTooltip)
Expand Down
4 changes: 2 additions & 2 deletions R/Tooltips_and_Popovers.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ NULL

## These Functions are common to multiple tooltip and popover functions
# Shared functions with really long names...
createTooltipOrPopoverOnServer <- function(session, id, type, options) {
createTooltipOrPopoverOnServer <- function(session, id, type, options, treatAsJQSel) {

data <- list(action = "add", type = type, id = id, options = options)
data <- list(action = "add", type = type, id = id, options = options, treatAsJQSel = treatAsJQSel)
session$sendCustomMessage(type = "updateTooltipOrPopover", data)

}
Expand Down
9 changes: 6 additions & 3 deletions R/addPopover.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@
#'@param trigger What action should cause the popover to appear? (\code{hover},
#'\code{focus}, \code{click}, or \code{manual}). Defaults to \code{hover}.
#'@param options A named list of additional options to be set on the popover.
#'
#'@param treatAsJQSel if \code{TRUE}, \code{id} is treated as a general jQuery selector
#'rather than the id attribute of the element. This allows adding the popover to HTML
#'elements even if they do not have an id attribute attached to it.
#'@templateVar item_name addPopover
#'@templateVar family_name Tooltips_and_Popovers
#'@template item_details
#'@template footer
#'@export
addPopover <- function(session, id, title, content, placement = "bottom", trigger = "hover", options = NULL) {
addPopover <- function(session, id, title, content, placement = "bottom", trigger = "hover",
options = NULL, treatAsJQSel = FALSE) {

options <- buildTooltipOrPopoverOptionsList(title, placement, trigger, options, content)
createTooltipOrPopoverOnServer(session, id, "popover", options)
createTooltipOrPopoverOnServer(session, id, "popover", options, treatAsJQSel)

}
8 changes: 6 additions & 2 deletions R/addTooltip.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
#'@param trigger What action should cause the tooltip to appear? (\code{hover},
#'\code{focus}, \code{click}, or \code{manual}). Defaults to \code{"hover"}.
#'@param options A named list of additional options to be set on the tooltip.
#'@param treatAsJQSel if \code{TRUE}, \code{id} is treated as a general jQuery selector
#'rather than the id attribute of the element. This allows adding the popover to HTML
#'elements even if they do not have an id attribute attached to it.
#'
#'@templateVar item_name addTooltip
#'@templateVar family_name Tooltips_and_Popovers
#'@template item_details
#'@template footer
#'@export
addTooltip <- function(session, id, title, placement = "bottom", trigger = "hover", options = NULL) {
addTooltip <- function(session, id, title, placement = "bottom", trigger = "hover",
options = NULL, treatAsJQSel = FALSE) {

options <- buildTooltipOrPopoverOptionsList(title, placement, trigger, options)
createTooltipOrPopoverOnServer(session, id, "tooltip", options)
createTooltipOrPopoverOnServer(session, id, "tooltip", options, treatAsJQSel)

}
9 changes: 9 additions & 0 deletions inst/examples/TooltipsandPopovers/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ shinyServer(
"This button is pointless too!")
)
})
addPopover(session, "#rdb .radio > label:first",
"Add [Popover|Tooltip] to sub elements",
content = paste0("You can use arbitrary jQuery selectors to add ",
"popovers/tooltips to elements even if they do not ",
"have an 'id' attribute. Use the ", code("treatAsJQSel"),
" flag to select an element by a jQuery selector rather than an ",
"id. Here we used ", code("#rdb .radio > label:first"),
" to select the first label of the radio buttons group."),
treatAsJQSel = TRUE)
addPopover(session, "distPlot", "Data", content = paste0("<p>Waiting time between ",
"eruptions and the duration of the eruption for the Old Faithful geyser ",
"in Yellowstone National Park, Wyoming, USA.</p><p>Azzalini, A. and ",
Expand Down
3 changes: 3 additions & 0 deletions inst/examples/TooltipsandPopovers/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ library(shinyBS)
min = 1,
max = 50,
value = 30),
radioButtons("rdb", "Popover at a sub element",
choices = c("Popover should appear here",
"But not here")),
selectInput("pointlessSelect", "Pointless Select", choices = c("A", "B", "C"), selectize = FALSE),
bsTooltip("pointlessSelect", "This is another pointless input element, its just here to look pretty."),
bsTooltip("bins", "The wait times will be broken into this many equally spaced bins",
Expand Down
23 changes: 14 additions & 9 deletions inst/www/shinyBS.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ Shiny.addCustomMessageHandler("bsAlertClose", function(alertId) {
// tooltips and popovers because there structure is so similar. type="popover"
// will create a popover.

shinyBS.addTooltip = function(id, type, opts) {
var $id = shinyBS.getTooltipTarget(id);
shinyBS.addTooltip = function(id, type, opts, treatAsJQSel) {
var $id = shinyBS.getTooltipTarget(id, treatAsJQSel);
var dopts = {html: true};
opts = $.extend(opts, dopts);

Expand All @@ -210,8 +210,9 @@ shinyBS.addTooltip = function(id, type, opts) {

}

shinyBS.removeTooltip = function(id, type) {
var $id = shinyBS.getTooltipTarget(id);
shinyBS.removeTooltip = function(id, type, treatAsJQSel) {
var $id = shinyBS.getTooltipTarget(id, treatAsJQSel);
console.log($id);
if(type == "tooltip") {
$id.tooltip("destroy");
} else if(type == "popover") {
Expand All @@ -221,9 +222,13 @@ shinyBS.removeTooltip = function(id, type) {

// Makes adjustments to the tooltip and popover targets for specialized
// shiny inputs/outputs
shinyBS.getTooltipTarget = function(id) {

var $id = $("#" + id).closest(".shiny-input-container, .shiny-bound-output, .btn, .shiny-download-link");
shinyBS.getTooltipTarget = function(id, treatAsJQSel) {
var $id;
if(!treatAsJQSel) {
$id = $("#" + id).closest(".shiny-input-container, .shiny-bound-output, .btn, .shiny-download-link");
} else {
$id = $(id);
}

/*
if($id.hasClass("js-range-slider")) {
Expand All @@ -239,9 +244,9 @@ shinyBS.getTooltipTarget = function(id) {

Shiny.addCustomMessageHandler("updateTooltipOrPopover", function(data) {
if(data.action == "add") {
shinyBS.addTooltip(data.id, data.type, data.options);
shinyBS.addTooltip(data.id, data.type, data.options, data.treatAsJQSel);
} else if(data.action == "remove") {
shinyBS.removeTooltip(data.id, data.type)
shinyBS.removeTooltip(data.id, data.type, data.treatAsJQSel)
}
})

Expand Down
40 changes: 22 additions & 18 deletions man/Alerts.Rd

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

42 changes: 22 additions & 20 deletions man/Buttons.Rd

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

Loading