Repository navigation
Expand file tree
/
Copy pathhtml-code-view.js
More file actions
150 lines (139 loc) · 4.13 KB
/
Copy pathhtml-code-view.js
File metadata and controls
150 lines (139 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const Workflow = require("@saltcorn/data/models/workflow");
const Table = require("@saltcorn/data/models/table");
const Form = require("@saltcorn/data/models/form");
const { freeVariables } = require("@saltcorn/data/models/expression");
const Handlebars = require("handlebars");
const { getState, features } = require("@saltcorn/data/db/state");
const { div } = require("@saltcorn/markup/tags");
const {
stateFieldsToWhere,
readState,
add_free_variables_to_joinfields,
} = require("@saltcorn/data/plugin-helper");
const { mergeIntoWhere } = require("@saltcorn/data/utils");
const vm = require("vm");
const configuration_workflow = () =>
new Workflow({
steps: [
{
name: "Code",
form: async (context) => {
const table = await Table.findOne({ id: context.table_id });
const fields = table.fields;
return new Form({
fields: [
{
name: "row_count",
label: "Row count",
type: "String",
required: true,
attributes: { options: ["Single", "Many"] },
},
{
name: "code",
label: "HTML Code",
input_type: "code",
attributes: { mode: "text/html" },
},
{
input_type: "section_header",
label: " ",
sublabel: div(
"Use handlebars to access table rows. Example: <code>{{#each rows}}<h1>{{this.name}}</h1>{{/each}}</code>"
),
showIf: { row_count: "Many" },
},
{
input_type: "section_header",
label: " ",
sublabel: div(
"Use handlebars to access rows. Example: <code><h1>{{name}}</h1></code>. Variables in scope: " +
fields.map((f) => `<code>${f.name}</code>`).join(", ")
),
showIf: { row_count: "Single" },
},
],
});
},
},
],
});
const get_state_fields = () => [
{
name: "id",
type: "Integer",
required: false,
},
];
const run = async (
table_id,
viewname,
{ code, row_count },
state,
extraArgs,
queriesObj
) => {
const rows = queriesObj?.get_rows_query
? await queriesObj.get_rows_query(state)
: await getRowsImpl(table_id, { code }, state);
const template = Handlebars.compile(code || "");
if (row_count === "Many") return template({ rows });
else {
if (rows.length === 0) return "";
const row = rows[0];
return template({ ...row, row });
}
};
const extractVariables = (template) => {
const ast = Handlebars.parse(template);
const variables = new Set();
const extractFromNode = (node) => {
if (node.type === "MustacheStatement" || node.type === "BlockStatement") {
variables.add(node.path.original);
}
if (node.program) {
node.program.body.forEach(extractFromNode);
}
if (node.inverse) {
node.inverse.body.forEach(extractFromNode);
}
};
ast.body.forEach(extractFromNode);
return variables;
};
const getRowsImpl = async (table_id, { code }, state) => {
const table = await Table.findOne(table_id);
const fields = await table.getFields();
readState(state, fields);
const qstate = await stateFieldsToWhere({ fields, state });
const joinFields = {};
const freeVars = new Set([]);
// Extract Handlebars variables
const hbVars = extractVariables(code);
hbVars.forEach((hbVar) => {
if (reserved.has(hbVar)) return;
try {
freeVariables(hbVar).forEach((fv) => freeVars.add(fv));
} catch {
//ignore
}
});
add_free_variables_to_joinfields(freeVars, joinFields, fields);
return await table.getJoinedRows({
where: qstate,
joinFields,
});
};
const reserved = new Set(["if"]);
module.exports = {
name: "HtmlCodeView",
display_state_form: false,
run,
get_state_fields,
configuration_workflow,
queries: ({ table_id, configuration: { code } }) => ({
async get_rows_query(state) {
return await getRowsImpl(table_id, { code }, state);
},
}),
};