Skip to content

Commit

Permalink
hx_ hx-
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosell committed Aug 4, 2024
1 parent 4eaf5eb commit 96aeb39
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 103 deletions.
59 changes: 54 additions & 5 deletions R/ambhtmx.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ ambhtmx_app <- \(
port <- port %||% Sys.getenv("AMBHTMX_PORT") %||% "8000"
host <- host %||% Sys.getenv("AMBHTMX_HOST") %||% "127.0.0.1"
if (live != "") {
warning("live = TRUE is alpha")
cat(glue::glue("\nRun on the terminal for hot reloading:\nnpx nodemon --signal SIGTERM {live}\n\n\n"))
warning("live = TRUE is alpha")
# ps -ef | grep "nodemon --signal SIGTERM "
args1 <- commandArgs()[1]
cat(glue::glue("\nRun from {args1} on the terminal for hot reloading:\nnpx nodemon --signal SIGTERM {live}\n\n\n"))
}
if (nrow(value) == 1) {
create_table <- !is.null(dbname) && !file.exists(dbname)
Expand Down Expand Up @@ -82,8 +84,8 @@ ambhtmx_app <- \(
error = \(e) stop(e)
)
if (is.null(value)) stop("Value is required")
if (is.null(value$id)) {
value <- value |>
if (is.null(value[["id"]])) {
value <- value |>
dplyr::mutate(id = uwu::new_v4(1))
}
DBI::dbAppendTable(con, name = context$name, value = value)
Expand Down Expand Up @@ -263,8 +265,55 @@ render_page <- \(main = NULL, page_title = NULL) {
}



#' Render a custom page with a custom title and main content
#'
#' @param res response object
#' @param main htmltools object of the body of the html page
#' @param page_title the title tag contents of the page
#' @returns the response page
#' @export
send_page <- \(main, res, ...) {
html <- render_page(main = main, ...)
res$send(html)
}

#' @noRd
replace_hx_attrs <- function(x) {
if (is.list(x)) {
# Check if the element has a named list called 'attribs'
if ("attribs" %in% names(x)) {
# Replace 'hx_' with 'hx-' in attribute names
names(x$attribs) <- gsub("hx_", "hx-", names(x$attribs))
}

# Apply the function recursively to all elements of the list
x <- lapply(x, replace_hx_attrs)
}

return(x)
}

#' @noRd
replace_hx_attrs <- function(x) {
if (is.list(x)) {
# Check if the element has a named list called 'attribs'
if ("attribs" %in% names(x)) {
# Replace 'hx_' with 'hx-' in attribute names
names(x$attribs) <- gsub("hx_", "hx-", names(x$attribs))
}

# Apply the function recursively to all elements of the list
original_class <- class(x)
x <- lapply(x, replace_hx_attrs)
class(x) <- original_class
}
return(x)
}

#' @noRd
render_html <- \(html_tags){
render_html <- \(htmx_tags){
html_tags <- replace_hx_attrs(htmx_tags)
rendered <- htmltools::renderTags(html_tags)
deps <- lapply(rendered$dependencies, function(dep) {
dep <- htmltools::copyDependencyToDir(dep, "lib", FALSE)
Expand Down
16 changes: 16 additions & 0 deletions R/extra-tags.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#' @rdname tags
#' @export
button <- htmltools::tags$button

#' @rdname tags
#' @export
textarea <- htmltools::tags$textarea

#' @rdname tags
#' @export
input <- htmltools::tags$input

#' @rdname tags
#' @export
label <- htmltools::tags$label
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if(!"rlang" %in% installed.packages()){
}
}
rlang::check_installed("remotes")
rlang::check_installed("ambhtmx", action = \(pkg, ...) remotes::install_github("jrosell/ambhtmx"))
remotes::install_github("jrosell/ambhtmx", force = 1)
rlang::check_installed("ambiorix", action = \(pkg, ... ) remotes::install_github("devOpifex/ambiorix"))
rlang::check_installed("scilis", action = \(pkg, ... ) remotes::install_github("devOpifex/scilis"))
rlang::check_installed("signaculum", action = \(pkg, ... ) remotes::install_github("devOpifex/signaculum"))
Expand Down Expand Up @@ -71,6 +71,8 @@ AMBHTMX_PORT=<to change port default 3000>
### [Password protected CRUD (Create, Read, Update, and Delete) example with ambhtmx](https://github.com/jrosell/ambhtmx/blob/main/inst/examples/07-crud.R)
[![](https://raw.githubusercontent.com/jrosell/ambhtmx/main/inst/examples/07.png)](https://github.com/jrosell/ambhtmx/blob/main/inst/examples/07-crud.R)
### [Cleaner and shorter code hx_ to hx- replacement](https://github.com/jrosell/ambhtmx/blob/main/inst/examples/07-hx_attributes.R)
[![](https://raw.githubusercontent.com/jrosell/ambhtmx/main/inst/examples/08.png)](https://github.com/jrosell/ambhtmx/blob/main/inst/examples/08-hx_attributes.R)
## Deployment examples (WIP)
Expand Down
5 changes: 3 additions & 2 deletions inst/examples/01-counter.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ app$get("/", \(req, res){
{
html <- render_page(
page_title = "ambhtmx counter example",
main = withTags(div(style = "margin: 20px", tagList(
main = div(
style = "margin: 20px",
h1("ambhtmx counter example"),
p(id = "counter", glue("Counter is set to {counter}")),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
)
)))
)
)
},
error = \(e) html <- p(e)
Expand Down
9 changes: 5 additions & 4 deletions inst/examples/02-ggplot2.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# library(ambhtmx)
devtools::load_all()
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
library(zeallot)
Expand All @@ -15,14 +15,15 @@ c(app, context, operations) %<-% ambhtmx_app()
app$get("/", \(req, res){
html <- render_page(
page_title = "ambhtmx ggplot2 example",
main = withTags(div(style = "margin: 20px", tagList(
main = div(
style = "margin: 20px",
h1("ambiorix + htmx example"),
p(id = "counter", glue("Counter is set to {counter}")),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
)
)))
)
)
res$send(html)
})
Expand Down
14 changes: 8 additions & 6 deletions inst/examples/03-slider.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ app$get("/", \(req, res){
rexp_plot <- generate_plot()
html <- render_page(
page_title = "ambhtmx slider example",
main = withTags(div(style = "margin: 20px", tagList(
main = div(
style = "margin: 20px",
h1("ambiorix + htmx example"),
div(id = "counter", withTags(tagList(
p(glue("Counter is set to {counter}")),
render_plot(rexp_plot)
))),
div(
id = "counter",
p(glue("Counter is set to {counter}")),
render_plot(rexp_plot)
),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
Expand All @@ -44,7 +46,7 @@ app$get("/", \(req, res){
`hx-target`="#counter",
`hx-swap`="innerHTML"
)
)))
)
)
res$send(html)
})
Expand Down
39 changes: 19 additions & 20 deletions inst/examples/04-todo.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library(ambhtmx)
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(scilis)
Expand All @@ -19,23 +19,21 @@ data_delete <- operations$delete_row

#' Some todo functions
todo_page <- \(items) {
tagList(
tags$div(
class = "container small-container",
create_card(
class = "my-3",
title = "Get Things Done!",
title_class = "text-center",
todo_form()
),
create_card(
class = "my-3",
tags$div(
id = "todo_items",
`hx-target` = "this",
`hx-swap` = "innerHTML",
create_todo_list(items)
)
div(
class = "container small-container",
create_card(
class = "my-3",
title = "Get Things Done!",
title_class = "text-center",
todo_form()
),
create_card(
class = "my-3",
tags$div(
id = "todo_items",
`hx-target` = "this",
`hx-swap` = "innerHTML",
create_todo_list(items)
)
)
)
Expand Down Expand Up @@ -202,10 +200,11 @@ app$get("/", \(req, res){
todos <- data_read(context = context)
html <- render_page(
page_title = "ambhtmx todo example",
main = withTags(div(style = "margin: 20px", tagList(
main = div(
style = "margin: 20px",
h1("ambhtmx todo example"),
todo_page(items = data_read(context = context))
)))
)
)
res$send(html)
})
Expand Down
19 changes: 10 additions & 9 deletions inst/examples/05-live.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library(ambhtmx)
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
Expand All @@ -19,14 +19,15 @@ c(app, context, operations) %<-% ambhtmx_app(live = live_path)
app$get("/", \(req, res){
html <- render_page(
page_title = "ambiorix + htmx example",
main = withTags(div(style = "margin: 100px", tagList(
h1("ambhtmx live hot realoading example"),
p(id = "counter", glue("Counter is set to {counter}")),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
)
)))
main = div(
style = "margin: 100px",
h1("ambhtmx live hot realoading example"),
p(id = "counter", glue("Counter is set to {counter}")),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
)
)
)
res$send(html)
})
Expand Down
10 changes: 5 additions & 5 deletions inst/examples/06-basic-auth.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library(ambhtmx)
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
Expand Down Expand Up @@ -58,18 +58,18 @@ app$get("/", \(req, res){
rexp_plot <- generate_plot()
html <- render_page(
page_title = "ambhtmx basic authentication example",
main = withTags(div(style = "margin: 20px", tagList(
main = div(style = "margin: 20px",
div(style ="float:right", id = "logout", button("Logout", onclick = "void(location.href='/logout')")),
h1(hello),
div(id = "counter", withTags(tagList(
div(id = "counter",
p(glue("Counter is set to {counter}")),
render_plot(rexp_plot)
))),
),
button(
"+1",
`hx-post`="/increment", `hx-target`="#counter", `hx-swap`="innerHTML"
)
)))
)
)
res$send(html)
})
Expand Down
20 changes: 10 additions & 10 deletions inst/examples/07-crud.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# library(ambhtmx)
devtools::load_all()
library(ambhtmx)
# devtools::load_all()
library(ambiorix)
library(tidyverse)
library(zeallot)
Expand Down Expand Up @@ -36,7 +36,7 @@ render_index <- \() {
)
})
}
main <- withTags(div(id = "page", style = "margin: 50px",
main <- div(id = "page", style = "margin: 50px",
div(style ="float:right", id = "logout", button("Logout", onclick = "void(location.href='/logout')")),
h1(page_title),
div(id = "main", style = "margin-top: 20px", tagList(
Expand All @@ -50,7 +50,7 @@ render_index <- \() {
`hx-swap` = "innerHTML"
)
))
))
)
},
error = \(e) print(e)
)
Expand Down Expand Up @@ -170,7 +170,7 @@ app$get("/items/new", \(req, res){
return(res$redirect("/login", status = 302L))
}
errors <- process_error_get(req, res)
html <- render_tags(withTags(tagList(
html <- render_tags(tagList(
h2("New item"),
div(label("Name", p(input(name = "name")))),
div(label("Content", p(textarea(name = "content")))),
Expand All @@ -193,7 +193,7 @@ app$get("/items/new", \(req, res){
`hx-include` = "[name='name'], [name='content']",
),
errors
)))
))
res$send(html)
})

Expand All @@ -205,7 +205,7 @@ app$get("/items/:id", \(req, res){
}
item_id <- req$params$id %||% ""
item <- items$read_row(id = item_id)
html <- render_tags(withTags(tagList(
html <- render_tags(tagList(
h2("Show item details"),
items$render_row(item),
a(
Expand Down Expand Up @@ -233,7 +233,7 @@ app$get("/items/:id", \(req, res){
`hx-target` = "#main",
`hx-swap` = "innerHTML"
)
)))
))
res$send(html)
})

Expand All @@ -244,7 +244,7 @@ app$get("/items/:id/edit", \(req, res){
}
item_id <- req$params$id %||% ""
item <- items$read_row(id = item_id)
html <- render_tags(withTags(tagList(
html <- render_tags(tagList(
h2("Edit item"),
input(type = "hidden", name = "id", value = item$id),
div(label("Name", p(input(name = "name", value = item$name)))),
Expand All @@ -267,7 +267,7 @@ app$get("/items/:id/edit", \(req, res){
`hx-swap` = "outerHTML",
`hx-include` = "[name='name'], [name='content']",
)
)))
))
res$send(html)
})

Expand Down
Loading

0 comments on commit 96aeb39

Please sign in to comment.