Skip to content

Commit

Permalink
Add an is_open field to forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosuav committed Nov 1, 2024
1 parent 7a06076 commit f6ebce3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
8 changes: 5 additions & 3 deletions httpstatic/chan_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ const render_element = {
let editing = null;
function openform(f) {
editing = f.id;
["id", "formtitle"].forEach(key => {
DOM("#editformdlg [name=" + key + "]").value = f[key] || "";
["id", "formtitle", "is_open"].forEach(key => {
const el = DOM("#editformdlg [name=" + key + "]");
if (el.type === "checkbox") el.checked = !!f[key];
else el.value = f[key] || "";
});
set_content("#formelements", (f.elements||[]).map((el, idx) => DIV({class: "element", "data-idx": idx}, [
DIV({class: "header"}, [
Expand All @@ -54,7 +56,7 @@ on("click", ".openform", e => {e.preventDefault(); openform(e.match.form_data);}
on("click", "#createform", e => ws_sync.send({cmd: "create_form"}));
export function sockmsg_openform(msg) {openform(msg.form_data);}

on("change", ".formmeta", e => ws_sync.send({cmd: "form_meta", id: editing, [e.match.name]: e.match.value}));
on("change", ".formmeta", e => ws_sync.send({cmd: "form_meta", id: editing, [e.match.name]: e.match.type === "checkbox" ? e.match.checked : e.match.value}));

on("click", "#addelement", e => {
if (e.match.value != "") ws_sync.send({cmd: "add_element", id: editing, "type": e.match.value});
Expand Down
28 changes: 19 additions & 9 deletions modules/http/chan_form.pike
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ $$formdata$$
";

array formfields = ({
({"id readonly", "Form ID"}), //Note that this won't match a lookup for "id", might be useful(?)
({"formtitle", "Title"}),
({"id", "readonly", "Form ID"}),
({"formtitle", "", "Title"}),
({"is_open", "type=checkbox", "Open form"}),
});

array _element_types = ({
Expand All @@ -54,17 +55,23 @@ array _element_types = ({
({"checkbox", "Check box(es)"}),
});
mapping element_types = (mapping)_element_types;
mapping element_attributes = ([
"simple": (["label": type_string]),
"paragraph": (["label": type_string]),
]);

mapping(string:mixed) http_request(Protocols.HTTP.Server.Request req) {
__async__ mapping(string:mixed) http_request(Protocols.HTTP.Server.Request req) {
if (string nonce = req->variables->nonce) {
//...
}
if (string formid = req->variables->form) {
//...
//If the form is open, anyone may fill it out by providing the form ID.
mapping cfg = await(G->G->DB->load_config(req->misc->channel->userid, "forms"));

}
if (!req->misc->is_mod) return render_template("login.md", req->misc->chaninfo); //Should there be non-privileged info shown?
return render(req, (["vars": (["ws_group": ""]),
"formfields": sprintf("%{* <label>%[1]s: <input class=formmeta name=%[0]s></label>\n> %}", formfields),
"formfields": sprintf("%{* <label>%[2]s: <input class=formmeta name=%[0]s %[1]s></label>\n> %}", formfields),
"elementtypes": sprintf("%{<option value=\"%s\">%s%}", _element_types),
]) | req->misc->chaninfo);
}
Expand Down Expand Up @@ -101,12 +108,15 @@ __async__ void wscmd_delete_form(object channel, mapping(string:mixed) conn, map
}

__async__ void wscmd_form_meta(object channel, mapping(string:mixed) conn, mapping(string:mixed) msg) {
multiset editable = (<"formtitle">);
mapping editable = (["formtitle": "string", "is_open": "bool"]);
await(G->G->DB->mutate_config(channel->userid, "forms") {mapping cfg = __ARGS__[0];
mapping form_data = cfg->forms[?msg->id]; if (!form_data) return;
foreach (editable; string key;) if (mixed val = msg[key]) {
if (stringp(val)) form_data[key] = val;
//TODO: Permit numerics too? Float or just int?
foreach (editable; string key; string type) if (!undefinedp(msg[key])) {
switch (type) {
case "string": form_data[key] = (string)msg[key]; break;
case "bool": form_data[key] = !!msg[key]; break;
//TODO: Permit numerics too? Float or just int?
}
}
});
send_updates_all(channel, "");
Expand Down

0 comments on commit f6ebce3

Please sign in to comment.