diff --git a/app/assets/stylesheets/dmpopidor.scss b/app/assets/stylesheets/dmpopidor.scss index 46cfcd6..d2479fc 100644 --- a/app/assets/stylesheets/dmpopidor.scss +++ b/app/assets/stylesheets/dmpopidor.scss @@ -574,6 +574,7 @@ .answer-section { flex: 8; position: relative; + min-height: 300px; .toggle-guidance-section { position: absolute; top: 0; @@ -585,6 +586,10 @@ text-orientation: mixed; writing-mode: vertical-rl; + &.disabled { + background-color: $light-grey; + cursor: not-allowed; + } } .question-form { padding-top: 10px; diff --git a/app/controllers/madmp_fragments_controller.rb b/app/controllers/madmp_fragments_controller.rb index d795dc4..3c90039 100644 --- a/app/controllers/madmp_fragments_controller.rb +++ b/app/controllers/madmp_fragments_controller.rb @@ -434,64 +434,65 @@ plan = fragment.plan template = plan.template run_parameters = fragment.madmp_schema.extract_run_parameters + editable = plan.editable_by?(current_user) - return { - "fragment_id" => fragment.id, - "answer" => { - "id" => answer&.id - }, - "question" => { - "id" => question&.id, - "answer_lock_version" => answer&.lock_version, - "locking" => stale_fragment ? - render_to_string(partial: "madmp_fragments/locking", locals: { - question: question, - answer: answer, - fragment: stale_fragment, - research_output: research_output, - user: answer&.user - }, formats: [:html]) : - nil, - "form" => render_to_string(partial: "madmp_fragments/edit", locals: { - template: template, - question: question, - answer: answer, - fragment: fragment, - madmp_schema: fragment.madmp_schema, - research_output: research_output, - dmp_id: fragment.dmp_id, - parent_id: fragment.parent_id, - pickable_schemas: MadmpSchema.where(classname: fragment.classname).order(:label), - readonly: false, - base_template_org: template.base_org - }, formats: [:html]), - "form_run" => run_parameters.present? ? - render_to_string(partial: "shared/dynamic_form/codebase/show", locals: { - fragment: fragment, - parameters: run_parameters, - template_locale: template.locale - }, formats: [:html]) : nil, - "answer_status" => answer.present? ? - render_to_string(partial: "answers/status", locals: { - answer: answer - }, formats: [:html]) : - nil - }, - "section" => { - "id" => section&.id - }, - "plan" => { - "id" => plan.id, - "progress" => section.present? ? - render_to_string(partial: "plans/progress", locals: { - plan: plan, - current_phase: section.phase - }, formats: [:html]) : - nil - }, - "research_output" => { - "id" => research_output&.id - } + { + "fragment_id" => fragment.id, + "answer" => { + "id" => answer&.id + }, + "question" => { + "id" => question&.id, + "answer_lock_version" => answer&.lock_version, + "locking" => stale_fragment ? + render_to_string(partial: "madmp_fragments/locking", locals: { + question: question, + answer: answer, + fragment: stale_fragment, + research_output: research_output, + user: answer&.user + }, formats: [:html]) : + nil, + "form" => render_to_string(partial: "madmp_fragments/edit", locals: { + template: template, + question: question, + answer: answer, + fragment: fragment, + madmp_schema: fragment.madmp_schema, + research_output: research_output, + dmp_id: fragment.dmp_id, + parent_id: fragment.parent_id, + pickable_schemas: MadmpSchema.where(classname: fragment.classname).order(:label), + readonly: !editable, + base_template_org: template.base_org + }, formats: [:html]), + "form_run" => run_parameters.present? ? + render_to_string(partial: "shared/dynamic_form/codebase/show", locals: { + fragment: fragment, + parameters: run_parameters, + template_locale: template.locale + }, formats: [:html]) : nil, + "answer_status" => answer.present? ? + render_to_string(partial: "answers/status", locals: { + answer: answer + }, formats: [:html]) : + nil + }, + "section" => { + "id" => section&.id + }, + "plan" => { + "id" => plan.id, + "progress" => section.present? ? + render_to_string(partial: "plans/progress", locals: { + plan: plan, + current_phase: section.phase + }, formats: [:html]) : + nil + }, + "research_output" => { + "id" => research_output&.id + } }.to_json end diff --git a/app/javascript/views/madmp_fragments/edit.js b/app/javascript/views/madmp_fragments/edit.js index ebe97cf..3efae03 100644 --- a/app/javascript/views/madmp_fragments/edit.js +++ b/app/javascript/views/madmp_fragments/edit.js @@ -15,11 +15,36 @@ $('.panel-collapse').on('shown.bs.collapse reload.form', (e) => { const target = $(e.target); + const fragmentId = target.find('.fragment-id').val(); if (!target.hasClass('fragment-content')) { return; } - const form = target.find('form'); - if (form.hasClass('new-fragment')) { + if (fragmentId) { + $.ajax({ + method: 'get', + url: `/madmp_fragments/load_form/${fragmentId}`, + beforeSend: () => { + showLoadingOverlay(target); + }, + complete: () => { + hideLoadingOverlay(target); + }, + }).done((data) => { + doneCallback(data, target); + $(`#runs-${data.question.id}-research-output-${data.research_output.id} .run-zone`).html(data.question.form_run); + Tinymce.init({ + selector: `#research_output_${data.research_output.id}_section_${data.section.id} .note`, + toolbar, + }); + target.find('.toggle-guidance-section').removeClass('disabled'); + Select2.init(`#answer-form-${data.question.id}-research-output-${data.research_output.id}`); + }).fail((error) => { + failCallback(error, target); + }); + } else { + const form = target.find('.new-fragment'); + if (form.length === 0) return; + $.ajax({ method: 'get', url: '/madmp_fragments/load_new_form', @@ -40,40 +65,19 @@ }); Select2.init(`#answer-form-${data.question.id}-research-output-${data.research_output.id}`); target.find('.schema_picker').data('fragment-id', data.fragment_id); - }).fail((error) => { - failCallback(error, target); - }); - } else { - const fragmentId = target.find('.fragment-id').val(); - $.ajax({ - method: 'get', - url: `/madmp_fragments/load_form/${fragmentId}`, - beforeSend: () => { - showLoadingOverlay(target); - }, - complete: () => { - hideLoadingOverlay(target); - }, - }).done((data) => { - doneCallback(data, target); - $(`#runs-${data.question.id}-research-output-${data.research_output.id} .run-zone`).html(data.question.form_run); - Tinymce.init({ - selector: `#research_output_${data.research_output.id}_section_${data.section.id} .note`, - toolbar, - }); - Select2.init(`#answer-form-${data.question.id}-research-output-${data.research_output.id}`); + target.find('.toggle-guidance-section').removeClass('disabled'); }).fail((error) => { failCallback(error, target); }); } }); - $('.panel-collapse').on('hide.bs.collapse', (e) => { + + $('.panel-collapse').on('hide.bs.collapse.fragment-content', (e) => { const target = $(e.target); - if (!target.hasClass('fragment-content')) { - return; + if (target.find('.guidance-section').is(':visible')) { + target.find('.toggle-guidance-section').trigger('click'); } - const fragmentId = target.find('.fragment-id').val(); - target.find('.answer-form').html(``); + target.find('.toggle-guidance-section').addClass('disabled'); }); $('body').on('click', '.question .heading-button', (e) => { diff --git a/app/javascript/views/shared/dynamic_form.js b/app/javascript/views/shared/dynamic_form.js index 5bbad2a..a0a0083 100644 --- a/app/javascript/views/shared/dynamic_form.js +++ b/app/javascript/views/shared/dynamic_form.js @@ -41,7 +41,7 @@ } }); - $(document).on('click', '.toggle-guidance-section', (e) => { + $(document).on('click', '.toggle-guidance-section:not(.disabled)', (e) => { const target = $(e.currentTarget); target.parents('.question-body').find('.guidance-section').toggle(); target.find('span.fa-chevron-right, span.fa-chevron-left') diff --git a/app/policies/madmp_fragment_policy.rb b/app/policies/madmp_fragment_policy.rb index 5bc554a..445edbd 100644 --- a/app/policies/madmp_fragment_policy.rb +++ b/app/policies/madmp_fragment_policy.rb @@ -1,4 +1,7 @@ +# frozen_string_literal: true + class MadmpFragmentPolicy < ApplicationPolicy + def initialize(user, fragment) raise Pundit::NotAuthorizedError, _("must be logged in") unless user @user = user @@ -6,7 +9,7 @@ end def create? - @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner + @fragment.plan.readable_by?(@user.id) || @user == @answer.plan.owner end def update? @@ -14,7 +17,7 @@ end def load_form? - @fragment.plan.editable_by?(@user.id) || @user == @answer.plan.owner + @fragment.plan.readable_by?(@user.id) || @user == @answer.plan.owner end def change_schema? diff --git a/app/views/branded/answers/_status.html.erb b/app/views/branded/answers/_status.html.erb index 8481695..d9405d4 100644 --- a/app/views/branded/answers/_status.html.erb +++ b/app/views/branded/answers/_status.html.erb @@ -7,4 +7,8 @@ <%= _('Answered')%> <%= _(' by %{user_name}') %{ :user_name => answer.user.name } if answer.user.present? %> +<% else %> + + <%= _('Question not answered.') %> + <% end %> \ No newline at end of file diff --git a/app/views/branded/madmp_fragments/_new.html.erb b/app/views/branded/madmp_fragments/_new.html.erb index 831455f..d52b67c 100644 --- a/app/views/branded/madmp_fragments/_new.html.erb +++ b/app/views/branded/madmp_fragments/_new.html.erb @@ -1,12 +1,12 @@ <%# locals: { template, question, madmp_schema, research_output, readonly, locking } %> -<%= form_for :madmp_fragment, url: load_new_form_madmp_fragments_path, - html: { - method: :get, - 'data-autosave': question.id, - class: "form-answer madmp-fragment new-fragment", - id: nil - } do |f| %> - <% if !readonly %> +<% unless readonly %> + <%= form_for :madmp_fragment, url: load_new_form_madmp_fragments_path, + html: { + method: :get, + 'data-autosave': question.id, + class: "form-answer madmp-fragment new-fragment", + id: nil + } do |f| %> <%= f.fields_for(:answer, answer) do |af| %> <%= af.hidden_field :plan_id, id: nil %> <%= af.hidden_field :question_id, id: nil %> @@ -15,19 +15,19 @@ <%= af.hidden_field :is_common, class: 'ans_is_common' if research_output.main? %> <%= af.hidden_field :id, id: nil, class: 'answer_id' %> <% end %> - <% end %> - <%= f.hidden_field :schema_id, :value => madmp_schema.id, id: nil, class: "schema_id" %> - <%= f.hidden_field :dmp_id, :value => dmp_id, id: nil %> - <%= f.hidden_field :parent_id, :value => parent_id, id: nil %> - <%= f.hidden_field :property_name, :value => madmp_schema.property_name_from_classname, id: nil %> - <%= f.hidden_field "source", :value => "form" %> -
-
- +
+
+ <% end %> <% end %> \ No newline at end of file diff --git a/app/views/branded/phases/_edit_plan_answers.html.erb b/app/views/branded/phases/_edit_plan_answers.html.erb index 33996b3..a034ea1 100644 --- a/app/views/branded/phases/_edit_plan_answers.html.erb +++ b/app/views/branded/phases/_edit_plan_answers.html.erb @@ -92,9 +92,11 @@ aria-labelledby="heading-<%= consolidated_id %>">
-
- <%= d_('dmpopidor','Comments & Guidance') %> -
+ <% unless question.question_format.structured? && answer.nil? %> +
+ <%= d_('dmpopidor','Comments & Guidance') %> +
+ <% end %>
"