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

feat: Change the source of assets. #445

Merged
merged 5 commits into from
Nov 22, 2023
Merged
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
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Change Log
All notable changes to this project will be documented in this file.
## [development]

## Upcoming
- `shiny.semantic` no longer uses CDN as the default source of assets. Instead, `semantic.assets` package was introduced.

## [0.4.2]

Expand Down Expand Up @@ -43,7 +42,7 @@ All notable changes to this project will be documented in this file.

- to modals: `modalDialog`, `removeModal`, `remove_all_modals`

- new STYLEGUIDE introduced
- new STYLEGUIDE introduced

- horizontal menu

Expand Down
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: shiny.semantic
Type: Package
Title: Semantic UI Support for Shiny
Version: 0.4.3.9000
Version: 0.4.3.9001
Authors@R: c(person("Filip", "Stachura", email = "filip@appsilon.com", role = "aut"),
person("Dominik", "Krzeminski", email = "dominik@appsilon.com", role = "aut"),
person("Krystian", "Igras", email = "krystian@appsilon.com", role = "aut"),
Expand Down Expand Up @@ -39,7 +39,8 @@ Imports:
jsonlite,
grDevices,
glue,
R6
R6,
semantic.assets
Suggests:
dplyr,
tibble,
Expand All @@ -53,5 +54,6 @@ Suggests:
rmarkdown,
markdown,
rcmdcheck,
mockery
mockery,
withr
RoxygenNote: 7.2.3
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
export(COLOR_PALETTE)
export(Progress)
export(SIZE_LEVELS)
export(SUPPORTED_THEMES)
export(accordion)
export(actionButton)
export(action_button)
Expand All @@ -12,7 +11,6 @@ export(calendar)
export(card)
export(cards)
export(check_proper_color)
export(check_semantic_theme)
export(checkboxInput)
export(checkbox_input)
export(close_toast)
Expand Down
7 changes: 0 additions & 7 deletions R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ COLOR_PALETTE <- c("#A333C8", "#21BA45", "#2185D0", "#DB2828", "#F2711C", "#FBBD
names(COLOR_PALETTE) <- c("purple", "green", "blue", "red", "orange", "yellow", "olive", "teal",
"violet", "pink", "brown", "grey", "black")

#' Supported semantic themes
#' @export
SUPPORTED_THEMES <- c("cerulean", "darkly", "paper", "simplex", # nolint
"superhero", "flatly", "slate", "cosmo",
"readable", "united", "journal", "solar",
"cyborg", "sandstone", "yeti", "lumen", "spacelab")

#' Allowed sizes
#' @export
SIZE_LEVELS <- c("mini", "tiny", "small", "", "large", "huge", "massive")
4 changes: 2 additions & 2 deletions R/dropdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ updateSelectInput <- function(session, inputId, label = NULL, choices = NULL, se
#' @export
theme_selector <- function(input_id = "theme", label = "Choose theme") {
dropdown_content <- dropdown_input(
"theme", choices = c("default", SUPPORTED_THEMES),
choices_value = c("", SUPPORTED_THEMES),
"theme", choices = c("default", semantic.assets::SUPPORTED_THEMES),
choices_value = c("", semantic.assets::SUPPORTED_THEMES),
type = "selection fluid themes-dropdown"
)
shiny::div(
Expand Down
143 changes: 65 additions & 78 deletions R/semanticPage.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
#' Get CDN path semantic dependencies
#' Get dependencies path
#'
#' Internal function that returns path string from `shiny.custom.semantic.cdn` options.
#'
#' @examples
#' ## Load shiny.semantic dependencies from local domain.
#' options("shiny.custom.semantic.cdn" = "shiny.semantic")
#'
#' @return CDN path of semantic dependencies
#' @return path with css and js files
#' @keywords internal
get_cdn_path <- function() {
getOption("shiny.custom.semantic.cdn", default = "https://d335w9rbwpvuxm.cloudfront.net/2.8.3")
get_dependencies_path <- function() {
if (!is.null(getOption("shiny.custom.semantic"))) {
return(
list(
src = c(file = getOption("shiny.custom.semantic")),
type = "custom"
)
)
}

if (!is.null(getOption("shiny.custom.semantic.cdn"))) {
return(
list(
src = c(href = getOption("shiny.custom.semantic.cdn")),
type = "cdn"
)
)
}

list(
src = c(
file = system.file(
"www",
"shared",
"semantic",
package = "semantic.assets"
)
),
type = "local"
)
}

#' Add dashboard dependencies to html
Expand All @@ -21,85 +43,49 @@ get_cdn_path <- function() {
#' @return Content with appended dependencies.
#' @keywords internal
get_dependencies <- function(theme = NULL) {
minfield <- if (getOption("shiny.minified", TRUE)) "min" else NULL
javascript_file <- paste(c("semantic", minfield, "js"), collapse = ".")
css_files <- c(check_semantic_theme(theme, full_url = FALSE))
dep_src <- get_dependencies_path()

dep_src <- NULL
if (!is.null(getOption("shiny.custom.semantic", NULL))) {
dep_src <- c(file = getOption("shiny.custom.semantic"))
} else if (isTRUE(getOption("shiny.semantic.local", FALSE))) {
if (!is.null(theme)) {
warning("It's not posible use local semantic version with themes. Using CDN")
} else {
dep_src <- c(
file = system.file(
"www",
"shared",
"semantic",
package = "shiny.semantic"
)
)
}
}
minified <- if (getOption("shiny.minified", TRUE)) "min" else NULL
javascript_file <- paste(c("semantic", minified, "js"), collapse = ".")

css_file <- get_css_file(
type = dep_src$type,
theme = theme,
minified = minified
)

if (is.null(dep_src)) {
dep_src <- c(href = get_cdn_path())
}
shiny::tagList(
htmltools::htmlDependency("semantic-ui",
"2.8.3",
dep_src,
script = javascript_file,
stylesheet = css_files
shiny::tagList(
htmltools::htmlDependency(
"semantic-ui",
"2.8.3",
dep_src$src,
script = javascript_file,
stylesheet = css_file
)
)
)
}

#' Get default semantic css
#' Get css file
#'
#' @param full_url define return output filename or full path. Default TRUE
#' @param type define type of dependencies source
#' @param theme define theme
#' @param minified define if minified version should be used
#'
#' @return path to default css semantic file or default filename
#' @return css file name
#' @keywords internal
get_default_semantic_theme <- function(full_url = TRUE) {
minfield <- if (getOption("shiny.minified", TRUE)) "min" else NULL
css_file <- paste(c("semantic", minfield, "css"), collapse = ".")
path <- file.path(get_cdn_path(), css_file, fsep = "/")
return(c(ifelse(full_url, path, css_file)))
}
get_css_file <- function(type, theme = NULL, minified = NULL) {
if (type == "custom") {
return(theme)
}

#' Semantic theme path validator
#'
#' @param theme_css it can be either NULL, character with css path, or theme name
#' @param full_url boolean flag that defines what is returned, either filename, or full path. Default TRUE
#'
#' @return path to theme or filename
#' @export
#'
#' @examples
#' check_semantic_theme(NULL)
#' check_semantic_theme("darkly")
#' check_semantic_theme("darkly", full_url = FALSE)
check_semantic_theme <- function(theme_css, full_url = TRUE) {
minfield <- if (getOption("shiny.minified", TRUE)) "min" else NULL
if (is.null(theme_css)) return(get_default_semantic_theme(full_url))
if (tools::file_ext(theme_css) == "css") return(theme_css)
if (theme_css %in% SUPPORTED_THEMES) {
if (full_url)
return(
file.path(
get_cdn_path(),
paste(c("semantic", theme_css, minfield, "css"), collapse = "."),
fsep = "/"
)
)
else
return(paste(c("semantic", theme_css, minfield, "css"), collapse = "."))
} else {
warning(paste("Theme ", theme_css, "not recognized. Default used instead!"))
return(get_default_semantic_theme(full_url))
if (type == "local" && !(is.null(theme) || theme %in% semantic.assets::SUPPORTED_THEMES)) {
warning(paste("Theme ", theme, "not recognized. Default used instead!"))
theme <- NULL
}

paste(c("semantic", theme, minified, "css"), collapse = ".")
}

#' Semantic UI page
Expand All @@ -121,7 +107,8 @@ check_semantic_theme <- function(theme_css, full_url = TRUE) {
#' wrapper (e.g. style, class etc.)
#' @param title A title to display in the browser's title bar.
#' @param theme Theme name or path. Full list of supported themes you will find in
#' \code{SUPPORTED_THEMES} or at http://semantic-ui-forest.com/themes.
#' \code{\link[semantic.assets:SUPPORTED_THEMES]{semantic.assets::SUPPORTED_THEMES}}
#' or at http://semantic-ui-forest.com/themes.
#' @param suppress_bootstrap boolean flag that supresses bootstrap when turned on
#' @param margin character with body margin size
#' @examples
Expand Down
3 changes: 1 addition & 2 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#' There are a number of global options that affect shiny.semantic as well as
#' Shiny behavior.The options can be set globally with `options()`
#' \describe{
#' \item{shiny.custom.semantic.cdn (defaults is internal CDN)}{This controls from where the css
#' \item{shiny.custom.semantic.cdn (defaults to `NULL`)}{This controls from where the css
#' and javascripts will be downloaded.}
#' \item{shiny.semantic.local (defaults to `FALSE`)}{This allows to use only local dependency.}
#' \item{shiny.custom.semantic (defaults to `NULL`)}{This allows to set custom local path
#' to semantic dependencies.}
#' \item{shiny.minified (defaults to `TRUE`)}{Defines including JavaScript as a minified or
Expand Down
2 changes: 1 addition & 1 deletion R/uirender.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uirender <- function(ui, width = NULL, height = NULL, element_id = NULL) {
# forward options using x
args <- list(
ui = toString(ui),
shiny_custom_semantic = get_cdn_path()
shiny_custom_semantic = get_dependencies_path()$src[[1]]
)

# create widget
Expand Down
Loading