From 00fbd1d00f01188a356a4c5c3358aa47e35329c1 Mon Sep 17 00:00:00 2001 From: Fredrik Jonsson <frjo@xdeb.org> Date: Tue, 24 Mar 2020 12:36:19 +0100 Subject: [PATCH] Add JS button to application forms that copy all questions, headers and help text to clipboard. --- .../templates/funds/application_base.html | 1 + .../javascript/apply/submission-form-copy.js | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 hypha/static_src/src/javascript/apply/submission-form-copy.js diff --git a/hypha/apply/funds/templates/funds/application_base.html b/hypha/apply/funds/templates/funds/application_base.html index d3ceb7f7a..c4ecb9976 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 000000000..a7cfb46ff --- /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); -- GitLab