╭ NewRepo.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../event.zig"); 4 const ui = @import("../ui.zig"); 5 const inp = @import("./input.zig"); 6 const xit = @import("xit"); 7 const hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 const wasm = builtin.target.cpu.arch == .wasm32; 16 17 // the new-repo form every page's new repo tab shows. the repo is created for 18 // the logged-in user. 19 pub const View = struct { 20 box: wgt.Box(ui.Widget), 21 session: *ui.Session, 22 23 // `route` is the page's own new-repo route, which the web form posts to 24 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, route: ui.RoutablePage) !View { 25 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 26 errdefer box.deinit(allocator); 27 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{try route.toUrl(session.page_arena)}) }; 28 29 const saved_fields = if (session.formFeedback(.repo)) |saved| saved.fields else null; 30 31 { 32 var name = try wgt.TextInput.init(allocator, .{ .label = " name ", .name = "name", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 33 errdefer name.deinit(allocator); 34 name.getFocus().mode = .all; 35 if (saved_fields) |saved| try name.setContent(allocator, saved.name); 36 try box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 37 box.getFocus().child_id = name.getFocus().id; 38 } 39 40 { 41 var description = try wgt.TextInput.init(allocator, .{ .label = " description ", .name = "description", .visible_width = null, .round_corners = true, .render_content = session.is_terminal }); 42 errdefer description.deinit(allocator); 43 description.getFocus().mode = .all; 44 if (saved_fields) |saved| try description.setContent(allocator, saved.description); 45 try box.children.put(allocator, description.getFocus().id, .{ .widget = .{ .text_input = description }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 46 } 47 48 { 49 var access_radio = try ui.widget.Radio.init(allocator, session, "access", &.{ "private", "public" }, if (saved_fields) |saved| @tagName(saved.access) else "private"); 50 errdefer access_radio.deinit(allocator); 51 try box.children.put(allocator, access_radio.getFocus().id, .{ .widget = .{ .radio = access_radio }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 52 } 53 54 { 55 var hash_radio = try ui.widget.Radio.init(allocator, session, "hash", &.{ "sha1", "sha256" }, if (saved_fields) |saved| @tagName(saved.hash) else "sha1"); 56 errdefer hash_radio.deinit(allocator); 57 try box.children.put(allocator, hash_radio.getFocus().id, .{ .widget = .{ .radio = hash_radio }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 58 } 59 60 { 61 var submit = try ui.widget.SubmitButton.initLabeled(allocator, "submit"); 62 errdefer submit.deinit(allocator); 63 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 64 } 65 66 // absorbs the leftover min-height so the button keeps its natural height 67 { 68 var spacer = try ui.widget.Spacer.init(allocator); 69 errdefer spacer.deinit(allocator); 70 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 71 } 72 73 return .{ .box = box, .session = session }; 74 } 75 76 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 77 self.box.deinit(allocator); 78 } 79 80 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 81 self.clearGrid(); 82 const failure = if (self.session.formFeedback(.repo)) |saved| saved.failure else null; 83 (try formField(&self.box, "name")).options.label = if (failure) |value| switch (value) { 84 .required_name => " name (required) ", 85 .invalid_name => " name (invalid) ", 86 .name_taken => " name (taken) ", 87 } else " name "; 88 // the web form handling finds the inputs by focus id 89 const inputs_arena = self.session.arena.allocator(); 90 for (self.box.children.values()) |*child| switch (child.widget) { 91 .text_input => |*ti| try self.session.text_inputs.put(inputs_arena, ti.getFocus().id, ti), 92 else => {}, 93 }; 94 try self.box.build(allocator, constraint, root_focus); 95 } 96 97 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 98 const cid = self.box.getFocus().child_id orelse return; 99 const cur = self.box.children.getIndex(cid) orelse return; 100 const child = &self.box.children.values()[cur]; 101 102 const on_submit = child.widget == .submit_button; 103 switch (key) { 104 .arrow_up, .back_tab => return if (formStep(&self.box, cur, false)) |i| root_focus.setFocus(self.box.children.keys()[i]), 105 .arrow_down, .tab => return if (formStep(&self.box, cur, true)) |i| root_focus.setFocus(self.box.children.keys()[i]), 106 .enter => if (on_submit) return self.submitForm(allocator), 107 .mouse => |mouse| if (on_submit) { 108 if (inp.leftClickOn(root_focus, child.widget.submit_button.buttonId(), mouse)) return self.submitForm(allocator); 109 }, 110 else => {}, 111 } 112 try child.widget.input(allocator, key, root_focus); 113 } 114 115 // the neighboring form control, skipping the spacer 116 fn formStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 117 var i = cur; 118 while (true) { 119 if (down) { 120 i += 1; 121 if (i >= form.children.count()) return null; 122 } else { 123 if (i == 0) return null; 124 i -= 1; 125 } 126 switch (form.children.values()[i].widget) { 127 .spacer => continue, 128 else => return i, 129 } 130 } 131 } 132 133 fn formRadio(form: *wgt.Box(ui.Widget), name: []const u8) !*ui.widget.Radio { 134 for (form.children.values()) |*child| switch (child.widget) { 135 .radio => |*radio| if (std.mem.eql(u8, radio.name, name)) return radio, 136 else => {}, 137 }; 138 return error.MissingFormField; 139 } 140 141 fn formField(form: *wgt.Box(ui.Widget), name: []const u8) !*wgt.TextInput { 142 for (form.children.values()) |*child| switch (child.widget) { 143 .text_input => |*text_input| if (std.mem.eql(u8, text_input.options.name, name)) return text_input, 144 else => {}, 145 }; 146 return error.MissingFormField; 147 } 148 149 // create the repo and navigate to it. this is the terminal path; the web 150 // posts the form to the new-repo route. 151 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 152 if (comptime wasm) return; 153 const io = self.session.io orelse return; 154 const users_dir = self.session.users_dir orelse return; 155 const admin_repo = self.session.admin_repo orelse return; 156 const user_id = self.session.userId() orelse return; 157 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 158 const user = (try ui.activeUser(moment, self.session.page_arena, user_id)) orelse return; 159 160 const name_input = try formField(&self.box, "name"); 161 const description_input = try formField(&self.box, "description"); 162 const name = try name_input.text(allocator); 163 defer allocator.free(name); 164 const description = try description_input.text(allocator); 165 defer allocator.free(description); 166 const hash_kind = std.meta.stringToEnum(hash.HashKind, (try formRadio(&self.box, "hash")).selected()) orelse unreachable; 167 const access = std.meta.stringToEnum(evt.Repo.Access, (try formRadio(&self.box, "access")).selected()) orelse unreachable; 168 169 _ = evt.createRepo(io, allocator, users_dir, &user_id, .{ .name = user.event.name, .email = user.event.email }, name, description, access, hash_kind) catch |err| { 170 const failure = ui.Session.FormFeedback.RepoFailure.fromError(err) orelse return err; 171 const aa = self.session.arena.allocator(); 172 self.session.data.form_feedback = .{ .repo = .{ .failure = failure, .fields = .{ 173 .name = try aa.dupe(u8, name), 174 .description = try aa.dupe(u8, description), 175 .hash = hash_kind, 176 .access = access, 177 } } }; 178 return; 179 }; 180 181 // wipe the form so a return visit starts fresh 182 name_input.clear(allocator); 183 description_input.clear(allocator); 184 185 const identity = try std.fmt.allocPrint(self.session.page_arena.allocator(), "{s}/{s}", .{ user.event.name, name }); 186 try self.session.navigate(ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0) orelse return); 187 } 188 189 pub fn clearGrid(self: *View) void { 190 self.box.clearGrid(); 191 } 192 193 pub fn getGrid(self: View) ?Grid { 194 return self.box.getGrid(); 195 } 196 197 pub fn getFocus(self: *View) *Focus { 198 return self.box.getFocus(); 199 } 200 201 // up leaves the form from its first control 202 pub fn atTop(self: View) bool { 203 const first = self.box.children.keys()[0]; 204 return self.box.focus.child_id == first; 205 } 206 }; 207