R/turbo-modifiers.R

Defines functions turbo_permanent turbo_noeval turbo_target turbo_disable turbo_enable

Documented in turbo_disable turbo_enable turbo_noeval turbo_permanent turbo_target

#' @title Hotwire Turbo Modifiers
#'
#' @description Modify existing tags to imbue (or disimbue) them with specific
#' Turbo functionality.
#'
#' @param tag (tag) to be modified
#'
#' @family Turbo
#' @name turbo_modifiers
NULL

#' @describeIn turbo_modifiers enable Turbo for this tag and its contents
#' @export
turbo_enable <- function(tag) {

  assert_tags(tag)
  tag <- tagQuery(tag)
  tag$removeAttrs("data-turbo")$addAttrs("data-turbo" = "true")
  return(tag$allTags())

}

#' @describeIn turbo_modifiers disable Turbo for this tag and its contents
#' @export
turbo_disable <- function(tag) {

  assert_tags(tag)
  tag <- tagQuery(tag)
  tag$removeAttrs("data-turbo")$addAttrs("data-turbo" = "false")
  return(tag$allTags())

}

#' @describeIn turbo_modifiers targets a different frame for this tag
#' @param target (str) ID of another frame or "_top" for the whole page
#' @export
turbo_target <- function(tag, target) {

  assert_tags(tag)
  assert_string(target, null.ok = TRUE)

  tag <- tagQuery(tag)
  tag$removeAttrs("data-turbo-frame")$addAttrs("data-turbo-frame" = target)
  return(tag$allTags())

}

#' @describeIn turbo_modifiers prevents evaluation on load of a script
#' @export
turbo_noeval <- function(tag) {

  assert_tags(tag)
  assert_true(tag$name == "script")

  tag <- tagQuery(tag)
  tag$removeAttrs("data-turbo-eval")$addAttrs("data-turbo-eval" = "false")
  return(tag$allTags())

}

#' @describeIn turbo_modifiers makes one permanent; tag must have ID
#' @export
turbo_permanent <- function(tag) {

  assert_tags(tag)
  tag <- tagQuery(tag)
  assert_true(tag$hasAttrs("id"))
  tag$removeAttrs("data-turbo-permanent")$addAttrs("data-turbo-permanent" = NA)
  return(tag$allTags())

}
tjpalanca/hotwire.R documentation built on Dec. 23, 2021, 10:59 a.m.