Compare commits

...

3 Commits

Author SHA1 Message Date
392c545b8d Increment version number to 0.0.2 2026-01-26 10:29:06 +00:00
49a8834cda Added htmlAddBoxHelpLink function 2026-01-26 10:28:49 +00:00
40b8c8aa12 Increment version number to 0.0.1 2026-01-22 16:00:47 +00:00
4 changed files with 99 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
Package: AVSDevR.HTMLUtils Package: AVSDevR.HTMLUtils
Title: Utility Functions For Modifying R HTML Elements Title: Utility Functions For Modifying R HTML Elements
Version: 0.0.0.9000 Version: 0.0.2
Authors@R: Authors@R:
person("Craig", "Williams", , "craig@avsdev.uk", role = c("aut", "cre")) person("Craig", "Williams", , "craig@avsdev.uk", role = c("aut", "cre"))
Description: Collection of utility functions for modifying HTML markup of Description: Collection of utility functions for modifying HTML markup of
@@ -14,9 +14,11 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE) Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3 RoxygenNote: 7.3.3
Imports: Imports:
htmltools htmltools,
rlang
Suggests: Suggests:
shiny, shiny,
shinydashboard, shinydashboard,
slugify,
testthat (>= 3.0.0) testthat (>= 3.0.0)
Config/testthat/edition: 3 Config/testthat/edition: 3

View File

@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand # Generated by roxygen2: do not edit by hand
export(htmlAddBoxAttributes) export(htmlAddBoxAttributes)
export(htmlAddBoxHelpLink)
export(htmlAddBoxRegionFromTitle) export(htmlAddBoxRegionFromTitle)
export(htmlDisableAutocomplete) export(htmlDisableAutocomplete)
export(htmlFixBoxCollapseButtonAria) export(htmlFixBoxCollapseButtonAria)

View File

@@ -87,3 +87,69 @@ htmlFixBoxCollapseButtonAria <- function(box, context = NULL) {
addAttrs(`aria-label` = paste0("Expand/Collapse ", context))$ addAttrs(`aria-label` = paste0("Expand/Collapse ", context))$
allTags() allTags()
} }
#' Adds a "help" (question mark) icon to a box tools section (right side)
#'
#' The uri to load can have a replacement marker supplied '%box%' which will be
#' substituded with a sanitized copy of the box title. Preference is given to
#' the slugify package, however if unavailable a warning is generated and the
#' fallback of URLEncode is used.
#'
#' @param box The box to add a help icon/link to
#' @param href The help uri to load
#' @param title Optional title for the help link. Defaults to box title
#'
#' @return The modified box
#' @export
#'
#' @examples
#' x <- shinydashboard::box(title = "This is a box")
#' htmlAddBoxHelpLink(x, href = "http://example.com/help-guide#%box%")
htmlAddBoxHelpLink <- function(box, href, title = NULL) {
header_tags <- htmltools::tagQuery(box)$find(".box-header")$selectedTags()
if (length(header_tags) == 0) {
return(box)
}
tools_tags <- htmltools::tagQuery(box)$find(".box-tools")$selectedTags()
if (length(tools_tags) == 0) {
box <- htmltools::tagInsertChildren(
box,
after = 0,
.cssSelector = ".box-header",
htmltools::tags$div(class = "box-tools pull-right")
)
}
box_title <- getBoxTitle(box)
if (is.null(box_title)) {
box_title <- "UNDEFINED"
}
if (is.null(title)) {
title <- box_title
}
if (requireNamespace("slugify")) {
box_title <- slugify::slugify(box_title)
} else {
rlang::warn(
"slugify is not installed, resorting to URLencode",
.frequency = "regularly",
.frequency_id = "htmlAddBoxHelpLink::slugify"
)
box_title <- utils::URLencode(box_title)
}
htmltools::tagInsertChildren(
box,
after = 0,
.cssSelector = ".box-tools",
htmltools::tags$a(
href = gsub("%box%", box_title, href),
target = "_blank",
class = "btn btn-box-tool pl-3 pr-3",
htmlRemoveAttributes(shiny::icon("question"), "aria-label"),
title = paste(trimws(title), " help link")
)
)
}

28
man/htmlAddBoxHelpLink.Rd Normal file
View File

@@ -0,0 +1,28 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/htmlBoxManipulation.R
\name{htmlAddBoxHelpLink}
\alias{htmlAddBoxHelpLink}
\title{Adds a "help" (question mark) icon to a box tools section (right side)}
\usage{
htmlAddBoxHelpLink(box, href, title = NULL)
}
\arguments{
\item{box}{The box to add a help icon/link to}
\item{href}{The help uri to load}
\item{title}{Optional title for the help link. Defaults to box title}
}
\value{
The modified box
}
\description{
The uri to load can have a replacement marker supplied '\%box\%' which will be
substituded with a sanitized copy of the box title. Preference is given to
the slugify package, however if unavailable a warning is generated and the
fallback of URLEncode is used.
}
\examples{
x <- shinydashboard::box(title = "This is a box")
htmlAddBoxHelpLink(x, href = "http://example.com/help-guide#\%box\%")
}