@@ -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 "
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
0 commit comments