Skip to content

Commit

Permalink
feat: Change the source of assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubnowicki committed Nov 15, 2023
1 parent 4b00b7c commit 279f35f
Show file tree
Hide file tree
Showing 65 changed files with 204 additions and 109,983 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
@@ -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
10 changes: 6 additions & 4 deletions DESCRIPTION
@@ -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
RoxygenNote: 7.2.1
mockery,
withr
RoxygenNote: 7.2.3
1 change: 0 additions & 1 deletion NAMESPACE
Expand Up @@ -12,7 +12,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
140 changes: 63 additions & 77 deletions R/semanticPage.R
@@ -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 Down
7 changes: 3 additions & 4 deletions R/shiny.R
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 All @@ -29,8 +28,8 @@ NULL
#' @keywords internal
.onLoad <- function(libname, pkgname) { # nolint
# Add directory for static resources
file <- system.file("www", package = "shiny.semantic", mustWork = TRUE)
shiny::addResourcePath("shiny.semantic", file)
file <- system.file("www", package = "semantic.assets", mustWork = TRUE)
shiny::addResourcePath("semantic.assets", file)
}

#' Create universal Shiny input binding
Expand Down
2 changes: 1 addition & 1 deletion R/uirender.R
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

0 comments on commit 279f35f

Please sign in to comment.