Skip to content
Snippets Groups Projects
Commit 00fbd1d0 authored by Fredrik Jonsson's avatar Fredrik Jonsson
Browse files

Add JS button to application forms that copy all questions, headers and help text to clipboard.

parent d83dcc32
No related branches found
No related tags found
No related merge requests found
......@@ -59,6 +59,7 @@
<script src="{% static 'js/apply/mailgun-validator.js' %}"></script>
<script src="{% static 'js/apply/file-uploads.js' %}"></script>
<script src="{% static 'js/apply/tinymce-word-count.js' %}"></script>
<script src="{% static 'js/apply/submission-form-copy.js' %}"></script>
{% if not show_all_group_fields %}
<script src="{% static 'js/apply/form-group-toggle.js' %}"></script>
{% endif %}
......
(function ($) {
'use strict';
// Strip html tags from text.
function strip(html) {
var doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent.trim() || '';
}
// Get all questions on the page/form.
function get_questions() {
var questions_text = [];
questions_text.push('# ' + $('.header__title').html());
$('.application-form').find('.form__group, .rich-text').each(function () {
var question_text = '';
var label_text = $(this).find('.form__question').html();
if (label_text) {
// Get the label, i.e. question.
label_text = strip(label_text);
label_text = label_text.replace(/(\r\n|\n|\r)/gm, '');
label_text = label_text.replace(/[ ]+/g, ' ');
question_text = '### ' + label_text;
}
else {
// Get the sub headers and help text.
question_text = strip($(this).html());
if ($(this).find('h2')) {
question_text = '## ' + question_text;
}
}
questions_text.push(question_text);
});
return questions_text.join('\n\n');
}
// Allow users to copy all questions to the clipboard.
if (document.queryCommandSupported && document.queryCommandSupported('copy')) {
var $button = $('<button/>').text('Copy questions to clipboard').addClass('link link--button js-clipboard-button');
$('.application-form').append($button);
$('.js-clipboard-button').on('click', function (e) {
e.preventDefault();
var questions = get_questions();
var $textarea = $('<textarea>').html(questions).addClass('visually-hidden');
$textarea.appendTo('body');
$textarea.select();
document.execCommand('copy');
$textarea.remove();
});
}
})(jQuery);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment