Riyyi
4 weeks ago
8 changed files with 88 additions and 14 deletions
@ -0,0 +1,30 @@
|
||||
import Elementary |
||||
import ElementaryHTMX |
||||
import ElementaryHTMXSSE |
||||
import ElementaryHTMXWS |
||||
import Fluent |
||||
import Vapor |
||||
import VaporElementary |
||||
|
||||
struct TestController: RouteCollection { |
||||
|
||||
func boot(routes: RoutesBuilder) throws { |
||||
routes.group("test") { test in |
||||
test.get("toast", use: toast) |
||||
} |
||||
} |
||||
|
||||
@Sendable |
||||
func toast(req: Request) async throws -> HTMLResponse { |
||||
|
||||
let state = try getState(request: req) |
||||
state.toast = ToastState(message: "Wow!", |
||||
title: "This is my title", |
||||
level: ToastState.Level.success) |
||||
|
||||
throw Abort(.badRequest, headers: ["HX-Trigger": "toast"]) |
||||
|
||||
// return HTMLResponse { } |
||||
} |
||||
|
||||
} |
@ -0,0 +1,35 @@
|
||||
import Elementary |
||||
|
||||
struct ScriptAfterLoad: HTML { |
||||
|
||||
var initial: Bool = false |
||||
var js: String = "" |
||||
|
||||
init(initial: Bool = false, js: () -> String) { |
||||
self.initial = initial |
||||
self.js = js() |
||||
} |
||||
|
||||
// ------------------------------------- |
||||
|
||||
var content: some HTML { |
||||
if initial { |
||||
script { |
||||
""" |
||||
document.addEventListener("DOMContentLoaded", function() { |
||||
\(js) |
||||
}); |
||||
""" |
||||
} |
||||
} else { |
||||
script { |
||||
""" |
||||
web.afterLoad(function () { |
||||
\(js) |
||||
}); |
||||
""" |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue