Skip to content

Commit 7d91c35

Browse files
committed
Merge branch 'feature/visibilityconditions' into feature/structural_edits
2 parents fbe3abb + ccecad1 commit 7d91c35

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed

layouts/shortcodes/dynamic_form_engine.html

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ <h3>{{.title}}</h3>
4141
{{ end }}
4242

4343

44-
<div class="score-card__question" {{if $visibilityCondition}}data-show-when="{{$visibilityCondition}}" {{end}}>
44+
<div class="score-card__question" {{if $visibilityCondition}}data-show-when="{{$visibilityCondition}}" {{end}}
45+
{{if .isOutput }} data-is-output="true" {{end}}>
4546
<h4>
46-
{{ .title }}
47+
<div class="csv-title d-inline">{{ .title }}</div>
4748
{{if .required}}*{{end}}
4849
{{ if .tooltip }}
4950
<button class="remove-on-pdf information-button mt-0" data-toggle="tooltip" data-placement="right"
@@ -53,7 +54,7 @@ <h4>
5354
{{ end }}
5455
</h4>
5556

56-
<p>
57+
<p class="csv-explanation">
5758
{{ .content | markdownify }}
5859
</p>
5960

@@ -98,8 +99,8 @@ <h6 class="d-inline mb-0 ml-2">{{ .title | markdownify }}</h6>
9899
{{ range .options }}
99100
<div class="col-md-4 col-sm-12">
100101
<label>
101-
<input type="radio" id="{{$input.identifier}}_{{.value}}" name={{$input.identifier}} class="card-input-element" value={{.value}} {{if
102-
$input.required}} required {{end}} />
102+
<input type="radio" id="{{$input.identifier}}_{{.value}}" name={{$input.identifier}}
103+
class="card-input-element" value={{.value}} {{if $input.required}} required {{end}} />
103104
<div class="card card-input">
104105
<div class="card-body {{ if not .content }}d-flex justify-content-center{{end}}">
105106
<h6 class="card-title">{{.title}}</h6>
@@ -115,9 +116,9 @@ <h6 class="card-title">{{.title}}</h6>
115116
{{ range .options }}
116117
<div class="col-12">
117118
<label class="my-2">
118-
<input type="radio" id="{{$input.identifier}}_{{.value}}" name={{$input.identifier}} class="" value={{.value}} {{if
119-
$input.required}} required {{end}} />
120-
<h6 class="d-inline ml-1" >{{.title}}</h6>
119+
<input type="radio" id="{{$input.identifier}}_{{.value}}" name={{$input.identifier}} class=""
120+
value={{.value}} {{if $input.required}} required {{end}} />
121+
<h6 class="d-inline ml-1">{{.title}}</h6>
121122
{{ if .content }}
122123
<p class="mb-0">
123124
{{.content | markdownify}}
@@ -144,7 +145,7 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
144145

145146
{{ end }}
146147

147-
<div class="row mt-5">
148+
<div class="row mt-5 formSubmissionSection">
148149
<div class="col-xl col-sm-12">
149150

150151
{{ with .complete_form_options }}
@@ -157,6 +158,9 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
157158
<button id="score-card-print-button" class="btn btn-primary remove-on-pdf"><i class="fas fa-print"></i>
158159
{{ .button_text | markdownify }}
159160
</button>
161+
<button id="score-card-csv-button" class="btn btn-primary remove-on-pdf"><i class="fas fa-file-excel"></i>
162+
{{ .button_text_csv | markdownify }}
163+
</button>
160164
</div>
161165
{{ end }}
162166
{{ end }}
@@ -175,9 +179,13 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
175179
}
176180

177181
var calculateVisibleFields = function () {
182+
183+
{{ if .complete_form_options.onlyShowWithOutput }}
184+
$('#{{.id}} .formSubmissionSection').addClass('d-none');
185+
{{ end }}
178186
// this is what the data attribute looks like: data-show-when=="||&&data1==medium&&data2=medium||&&data1==low"
179187
// This field should be hidden when no input is selected, and shown if either data 1 and data 2 are medium or data 1 is low.
180-
$('.score-card__question').each(function () {
188+
$('#{{.id}} .score-card__question').each(function () {
181189
const showWhen = $(this).attr('data-show-when');
182190
// check if the attribute is there, otherwise always keep it as is.
183191
if (showWhen) {
@@ -203,14 +211,14 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
203211
}
204212

205213
// Check for radio button
206-
let input = document.querySelector(`.score-card__question:not(.d-none) input[name=${name}]:checked`);
214+
let input = document.querySelector(`#{{.id}} .score-card__question:not(.d-none) input[name=${name}]:checked`);
207215
// Check for checkbox
208216
if (!input) {
209-
input = document.querySelector(`.score-card__question:not(.d-none) input[id=${name}]:checked`);
217+
input = document.querySelector(`#{{.id}} .score-card__question:not(.d-none) input[id=${name}]:checked`);
210218
}
211-
if (!input
212-
|| (checkForEquals && !valuesAreEqual(input.value, value))
213-
|| (checkForUnequal && valuesAreEqual(input.value, value))) {
219+
if (!input
220+
|| (checkForEquals && !valuesAreEqual(input.value, value))
221+
|| (checkForUnequal && valuesAreEqual(input.value, value))) {
214222
andShow = false;
215223
}
216224
});
@@ -226,6 +234,10 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
226234
if ($(this).find('textarea').attr('data-required')) {
227235
$(this).find('textarea').prop('required', true);
228236
}
237+
if ($(this).attr('data-is-output')) {
238+
console.log('showing form submission section');
239+
$('#{{.id}} .formSubmissionSection').removeClass('d-none');
240+
}
229241
} else {
230242
$(this).addClass("d-none");
231243
if ($(this).find('input').attr('data-required')) {
@@ -256,7 +268,7 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
256268
})
257269

258270

259-
$('#score-card-print-button').click(function (event) {
271+
$('#{{.id}} #score-card-print-button').click(function (event) {
260272
event.preventDefault();
261273
window.html2canvas = html2canvas;
262274
var element = document.getElementById("{{.id}}");
@@ -280,6 +292,43 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
280292

281293
});
282294
});
295+
296+
$('#{{.id}} #score-card-csv-button').click(function (event) {
297+
event.preventDefault();
298+
const csv = [];
299+
csv.push(['Title/Question', 'Explanation', 'Input', 'Input explanation']);
300+
$('#{{.id}} .score-card__question:not(.d-none)').each(function () {
301+
const question = $(this);
302+
let title = question.find('.csv-title').text();
303+
let explanation = question.find('.csv-explanation').text();
304+
let input = question.find('input:checked').siblings('h6').text() || question.find('input').val() || question.find('textarea').val();
305+
let inputExplanation = question.find('input:checked').siblings('p').text() || question.find('input').siblings('p').text() || question.find('textarea').siblings('p').text();
306+
//exception for checkboxes
307+
if (question.find('input:checked').length > 1) {
308+
input = question.find('input:checked').map(function () {
309+
return $(this).siblings('h6').text();
310+
}).get().join('; ');
311+
312+
inputExplanation = question.find('input:checked').map(function () {
313+
return $(this).siblings('p').text();
314+
}).get().join('; ');
315+
}
316+
title = title?.replaceAll(',', ' ').replaceAll('\r\n', ' ')?.replaceAll('\n', ' ')?.replaceAll(' ', ' ')?.trim();
317+
explanation = explanation?.replaceAll(',', ' ').replaceAll('\r\n', ' ')?.replaceAll('\n', ' ')?.replaceAll(' ', ' ')?.trim();
318+
input = input?.replaceAll(',', ' ').replaceAll('\r\n', ' ')?.replaceAll('\n', ' ')?.replaceAll(' ', ' ')?.trim();
319+
inputExplanation = inputExplanation?.replaceAll(',', ' ').replaceAll('\r\n', ' ')?.replaceAll('\n', ' ')?.replaceAll(' ', ' ')?.trim();
320+
csv.push([title ?? '', explanation ?? '', input ?? '', inputExplanation ?? '']);
321+
});
322+
323+
const csvContent = "data:text/csv;charset=utf-8," + csv.map(e => e.join(",")).join("\n");
324+
const encodedUri = encodeURI(csvContent);
325+
const link = document.createElement("a");
326+
link.setAttribute("href", encodedUri);
327+
link.setAttribute("download", "{{.complete_form_options.file_name}}.csv");
328+
document.body.appendChild(link); // Required for FF
329+
330+
link.click(); // This will download the data file named "my_data.csv".
331+
})
283332
});
284333
</script>
285334

tina/collections/shared/page/building_blocks.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,11 @@ const building_blocks: TinaField[] = [
340340
list: true,
341341
ui: {
342342
itemProps: (item) => {
343-
return { label: `${item?.identifier}=${item?.value}` };
343+
return {
344+
label: `${item?.identifier}${
345+
item?.compareSign ?? "=="
346+
}${item?.value}`,
347+
};
344348
},
345349
},
346350
fields: [
@@ -381,6 +385,13 @@ const building_blocks: TinaField[] = [
381385
},
382386
],
383387
},
388+
{
389+
type: "boolean",
390+
name: "isOutput",
391+
label: "Is output",
392+
description:
393+
"Can be used to hide the save button, the button will show when an output field is shown.",
394+
},
384395
],
385396
},
386397
],
@@ -418,6 +429,11 @@ const building_blocks: TinaField[] = [
418429
description: "",
419430
required: true,
420431
},
432+
{
433+
name: "button_text_csv",
434+
label: "Button text for CSV download",
435+
type: "string",
436+
},
421437
{
422438
name: "backend_link",
423439
label: "Back end link (Required if you want to submit the data)",
@@ -431,6 +447,11 @@ const building_blocks: TinaField[] = [
431447
type: "string",
432448
required: false,
433449
},
450+
{
451+
name: "onlyShowWithOutput",
452+
label: "Only show when an output field is shown",
453+
type: "boolean",
454+
},
434455
],
435456
},
436457
],

tina/tina-lock.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)