turbo: Turbo

Description Super classes Active bindings Methods See Also

Description

Turbo uses complementary techniques to dramatically reduce the amount of custom JavaScript that most web applications will need to write.

Documentation

Super classes

webtools::Configurable -> webtools::Plugin -> Turbo

Active bindings

clients

client IDs for turbo websocket connections

Methods

Public methods

Inherited methods

Method new()

Create a new TurboPlugin

Usage
Turbo$new(
  turbo_root = NULL,
  progress_bar = TRUE,
  pb_height = "5px",
  pb_color = "#008080",
  pb_delay = 500,
  turbo_opt_in = FALSE,
  turbo_socket = FALSE,
  app_version = NA_character_
)
Arguments
turbo_root

(str) path of the turbo root

progress_bar

(flg) whether progress bar is visible

pb_height

(css) height of progress bar (top)

pb_color

(css) color fo the progress bar

pb_delay

(num) milliseconds before progress bar appears

turbo_opt_in

(flg) if TRUE, then turbo is disabled in the page

turbo_socket

(flg) add the turbo web socket

app_version

(str) a string indicating the version, or NA to disable; used to force reload for new version


Method assets()

Generates Turbo Assets

Usage
Turbo$assets(turbo = TRUE)
Arguments
turbo

(opt) include turbo assets

  • TRUE - include assets with global defaults (default)

  • FALSE - do not include turbo assets

  • list() - a list that overrides the default config

    • root (str) path of the turbo root

    • libs (str) path of the libraries

    • force_reload (flg) whether this page will force a reload

    • progress_bar (flg) whether progress bar is visible

    • pb_height (css) height of progress bar (top)

    • pb_delay (int) milliseconds of delay before progress bar appears

    • pb_color (css) color fo the progress bar

    • cache_control (str) whether to avoid previewing or caching the page

    • turbo_opt_in (flg) if TRUE, then turbo is disabled across the page

    • turbo_socket (flg) add the turbo web socket

    • app_version (str) a string indicating the version, or NA to disable; used to force reload when assets change


Method router()

Create a Turbo enabled page

Usage
Turbo$router(path, handler, ..., turbo = TRUE, lang = NULL, fragment = FALSE)
Arguments
path

(str) endpoint

handler

(fun) handler; must have arguments request, response, keys, ... and set the body to html tags

...

(arg) passed to self$router_html()

turbo

(opt) include turbo assets

  • TRUE - include assets with global defaults (default)

  • FALSE - do not include turbo assets

  • list() - a list that overrides the default config

    • root (str) path of the turbo root

    • libs (str) path of the libraries

    • force_reload (flg) whether this page will force a reload

    • progress_bar (flg) whether progress bar is visible

    • pb_height (css) height of progress bar (top)

    • pb_delay (int) milliseconds of delay before progress bar appears

    • pb_color (css) color fo the progress bar

    • cache_control (str) whether to avoid previewing or caching the page

    • turbo_opt_in (flg) if TRUE, then turbo is disabled across the page

    • turbo_socket (flg) add the turbo web socket

    • app_version (str) a string indicating the version, or NA to disable; used to force reload when assets change

lang

(str) language, defaults to en.

fragment

(flg) whether to return a full page or fragment


Method router_stream()

Turbo stream response with defaults

Usage
Turbo$router_stream(
  path,
  handler,
  ...,
  methods = c("get", "post"),
  type = "text/vnd.turbo-stream.html",
  lang = NULL,
  fragment = TRUE
)
Arguments
path

(str) endpoint

handler

(fun) handler; must have arguments request, response, keys, ... and set the body to html tags

...

(arg) passed to self$router_html()

methods

(str) defaults to GET and POST

type

(str) content type, defaults to turbo stream

lang

(str) language, defaults to en.

fragment

(flg) whether to return a full page or fragment


Method send()

Send a turbo websocket message

Usage
Turbo$send(name, data, id = NULL)
Arguments
name

(str) name of the turbo message to send

data

(str) data for the event

id

(str) client ID, leave blank to send to all clients


Method log()

Send a turbo log message

Usage
Turbo$log(type, ...)
Arguments
type

(str) type of the message

...

(chr) components of the message


Method visit()

Sends a visit turbo event from the server

Usage
Turbo$visit(location, action = c("advance", "replace"), id = NULL)
Arguments
location

(url) location (relative path) to visit

action

(str) either:

  • advance (default) - new entry entered into browser history

  • replace - replaces the topmost history entry

id

(str) client ID, leave blank to send to all clients


Method clear()

Clears the Turbo Drive page cache

Usage
Turbo$clear(id = NULL)
Arguments
id

(str) client ID, leave blank to send to all clients


Method listen()

Add a turbo listener

Usage
Turbo$listen(
  event = c("before-visit", "before-render", "before-fetch-request", "click", "visit",
    "submit-start", "before-fetch_response", "submit-end", "before-cache",
    "before-render", "before-stream-render", "render", "load", "frame-render",
    "frame-load"),
  code,
  id = NULL
)
Arguments
event

(str) event to listen for:

  • before-visit - before visiting a location, except when navigating by history.

    • event.detail.url contains the URL

  • before-render - pause rendering and prepare before fetching; useful for animations

  • before-fetch-request - pause rendering and prepare the request; useful for attaching information to request.

    • event.detail.url contains the URL

    • event.detail.fetchOptions contains fetch options

    • event.target - the element that triggered it

    • event.detail.resume() - resumes the request

  • click - when a Turbo-enabled link is clicked

    • event.detail.url contains the URL

    • cancelling will fall through to normal navigation

  • visit - immediately after a visit starts

    • event.detail.url contains the requested location

    • event.detail.action contains the action

  • submit-start - form submission

    • event.detail.formSubmission - contains the FormSubmission

  • before-fetch-response - when the network request completes

    • event.detail - contains fetch options

    • event.target - contains the firing element

  • submit-end - fires after the form submission-initiaited network request completes.

    • event.detail.formSubmission - contains the FormSubmission

    • event.detail - FormSubmissionResult

  • before-cache - before Turbo saves the current page to cache

  • before-render - before rendering the page.

    • event.detail.newBody - new body elemetn

    • event.detail.resume() - canceling and continuing the rendering

  • before-stream-render - fires before a page update is rendered

  • render - fires after Turbo renders the page, if the page is cached then fires both when rendering cached and fresh version.

  • load - fires after the initial page load and every visit

    • event.detail.timing - visit timings

  • frame-render - fires right after a turbo frame renders

    • event.target - the frame

    • event.detail.fetchResponse - the FetchResponse object

  • frame-load - fires when Turbo frame is after frame-render

    • event.target - the frame

code

(str) - JS code to be executed inside the handler.

id

(str) client ID, leave blank to send to all clients


Method stream()

Send a turbo stream

Usage
Turbo$stream(
  action = c("append", "prepend", "replace", "update", "remove", "before", "after"),
  target,
  ...,
  id = NULL,
  multiple = FALSE,
  env = parent.frame()
)
Arguments
action

(str) the action to be taken for this stream element:

  • append - inserted last inside the element

  • prepend - inserted first inside the element

  • replace - replace the whole element

  • update - replace the element but keep handlers intact; only HTML

  • remove - remove the element

  • before - insert before the element

  • after - insert after the element

target

(str) target of any links clicked in this:

  • NULL - the frame itself

  • "_top" - the whole window

  • "<id>" - a specific frame outside tof the current frame

...

(tag) tag content of the stream

id

(str) client ID, leave blank to send to all clients

multiple

(flg) if multiple targets, then target is CSS selector

env

(env) calling environment


Method clone()

The objects of this class are cloneable with this method.

Usage
Turbo$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

Other Turbo: turbo_assets(), turbo_frame(), turbo_link(), turbo_listener(), turbo_modifiers, turbo_stream()


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