diff --git a/hypha/apply/funds/templates/funds/application_base.html b/hypha/apply/funds/templates/funds/application_base.html index d3ceb7f7a9ffc3cf8281fba6d8a119c9e6b311d9..c4ecb9976d1082f6a35ecbbf40b8c61e8c6e18c0 100644 --- a/hypha/apply/funds/templates/funds/application_base.html +++ b/hypha/apply/funds/templates/funds/application_base.html @@ -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 %} diff --git a/hypha/static_src/src/javascript/apply/submission-form-copy.js b/hypha/static_src/src/javascript/apply/submission-form-copy.js new file mode 100644 index 0000000000000000000000000000000000000000..a7cfb46ff4fa2390c00ccb43bfef639f238959ea --- /dev/null +++ b/hypha/static_src/src/javascript/apply/submission-form-copy.js @@ -0,0 +1,53 @@ +(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);