aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/app/root.document.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch/app/root.document.zig')
-rw-r--r--src/zilch/app/root.document.zig87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/zilch/app/root.document.zig b/src/zilch/app/root.document.zig
new file mode 100644
index 0000000..6191746
--- /dev/null
+++ b/src/zilch/app/root.document.zig
@@ -0,0 +1,87 @@
+const std = @import("std");
+
+const mod = @import("../root.zig");
+const Element = mod.html.Element;
+const Document = mod.html.Document;
+
+const Options = @import("Options.zig");
+
+pub fn App(comptime options: Options) type {
+ const components: []const Element, const registry: []const u8 = (comp: {
+ var cs: []const Element = &.{};
+ var registry: []const u8 = "const component_registry = {";
+
+ for (options.components) |component| {
+ cs = cs ++ component.toElement();
+ registry = registry ++ std.fmt.comptimePrint("{}: '{s}-{s}',", .{
+ component.id,
+ component.namespace,
+ component.name,
+ });
+ }
+
+ registry = registry ++ "};";
+
+ break :comp .{ cs, registry };
+ });
+
+ const functions: []const u8 = func: {
+ var buf: []const u8 = "ObjectStore.methods = {";
+
+ for (options.functions) |function| {
+ buf = buf ++ std.fmt.comptimePrint("{}: {{await: {}, fn: {s}}},", .{ function.id, function.await, function.content });
+ }
+
+ buf = buf ++ "}";
+
+ break :func buf;
+ };
+
+ return struct {
+ const node: Document = .{
+ .root = .node("html", .{}, &.{
+ .node("head", .{}, &.{
+ .node("title", .{}, &.{.text(options.name)}),
+ .void("link", .{ .rel = "icon", .href = options.favicon }),
+ .void("meta", .{
+ .name = "viewport",
+ .content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0",
+ }),
+ .void("meta", .{ .charset = "utf8" }),
+ .node("style", .{}, &.{.text(options.theme)}),
+ .node("script", .{}, &.{.text(@embedFile("symbols.js"))}),
+ .node("script", .{}, &.{.text(@embedFile("object-store.js"))}),
+ .node("script", .{}, &.{.text(functions)}),
+ .node("script", .{}, &.{.text(@embedFile("event.js"))}),
+ .node("script", .{}, &.{.text(@embedFile("load.js"))}),
+ .node("script", .{}, &.{.text(reg: {
+ var register_code: []const u8 = "";
+
+ for (options.globals, 1..) |field, id| {
+ register_code = register_code ++ std.fmt.comptimePrint(
+ "ObjectStore.registerObject({}, {s});",
+ .{ id, field.name },
+ );
+ }
+
+ break :reg register_code;
+ })}),
+ }),
+ .node(
+ "body",
+ .{},
+ &[_]Element{
+ .node("script", .{}, &.{.text(registry)}),
+ } ++ components ++ options.body.elements,
+ ),
+ }),
+ };
+
+ html: []const u8 = str: {
+ @setEvalBranchQuota(100_000);
+ break :str std.fmt.comptimePrint("{f}", .{node});
+ },
+
+ pub const init: @This() = .{};
+ };
+}