diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index fa94f43..ebea43a 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -12,6 +12,7 @@ # GET /plans/new + # ------------------------------------------------------------------------------------ def new @plan = Plan.new authorize @plan @@ -29,7 +30,8 @@ respond_to :html end - + # GET /plans/possible_templates [JSON] + # ------------------------------------------------------------------------------------ def possible_templates authorize Plan.new @@ -78,101 +80,32 @@ templates << Template.find_by(is_default: true) end - @msg = msg - @templates = templates.sort{|x,y| x.title <=> y.title } if templates.count > 1 + templates = templates.sort{|x,y| x.title <=> y.title } if templates.count > 1 + templates = templates.collect{|t| {id: t.id, title: t.title, org: t.org.name} } - respond_to :json + respond_to do |format| + format.json do + render json: {msg: msg, templates: templates} + end + end end - # we get here either from selecting a funder or if if the first selection - # results in multiple templates, from a template selection screen + # POST /plans + # ------------------------------------------------------------------------------------ def create @plan = Plan.new authorize @plan - message = "" - - # if we have a template_id we've been selcting between templates, otherwise funders - if params[:template_id] - @templates = [ Template.find(params[:template_id] ) ] + @plan.template = Template.find(plan_params["template_id"]) + + if @plan.save + @plan.assign_creator(current_user) + + redirect_to plan_path(@plan), notice: _('Plan was successfully created.') else - funder_id = params[:plan][:funder_id].to_i - - if funder_id.present? && funder_id != -1 - @templates = [] - - # get all funder @templates - funder = Org.find(params[:plan][:funder_id]) - funder_templates = get_most_recent( funder.templates.where(published: true).all ) - - # get org templates and index by customization id - if current_user.org.nil? - orgtemplates = [] - else - orgtemplates = get_most_recent( current_user.org.templates.all ) - end - - orgt_by_customization = orgtemplates.collect{|t| [t.customization_of, t]}.to_h - - # go through funder templates and replace with org cusomizations if needed - funder_templates.each do |ft| - if orgt_by_customization.has_key?(ft.dmptemplate_id) - message = _(" - using template customised by your institution") - @templates << orgt_by_customization[ft.dmptemplate_id] - else - @templates << ft - end - end - - else # either didn't select funder or selected "No Funder" - - # get all org @templates which are not customisations - @templates = get_most_recent( current_user.org.templates.where(customization_of: nil) ) - - message = _(" - choosing default template for your institution") - - # if none of these get the default template - if @templates.blank? - @templates = get_most_recent( Template.where(is_default: true, customization_of: nil) ) - message = _(" - no funder or institution template, choosing default template") - end - end - end - - # if we have more than one template then back to the user - # using the 'create' template - # to choose otherwise just create the plan - # and go to the plan/show template - if @templates.length > 1 - message += _(" - there are more than one to choose from") - flash.notice = message - respond_to do |format| - format.html - end - return - end - - @plan.template = @templates[0] - - @based_on = @plan.base_template() - - @plan.principal_investigator = current_user.name - - @plan.title = _('My plan')+' ('+@plan.template.title+')' # We should use interpolated string since the order of the words from this message could vary among languages - - @all_guidance_groups = @plan.get_guidance_group_options - @selected_guidance_groups = @plan.guidance_groups.pluck(:id) - - respond_to do |format| - if @plan.save - @plan.assign_creator(current_user.id) - flash.notice = _('Plan was successfully created.') + message - format.html { redirect_to({:action => "show", :id => @plan.id, :editing => true }) } - else - flash[:notice] = failed_create_error(@plan, _('plan')) - format.html { render action: "new" } - end + flash[:notice] = failed_create_error(@plan, 'Plan') + render 'new' end end @@ -447,6 +380,9 @@ private + def plan_params + params.require(:plan).permit(:template_id) + end # different versions of the same template have the same dmptemplate_id # but different version numbers so for each set of templates with the diff --git a/app/views/plans/new.html.erb b/app/views/plans/new.html.erb index 053feb7..9d222f2 100644 --- a/app/views/plans/new.html.erb +++ b/app/views/plans/new.html.erb @@ -7,7 +7,7 @@ <%= _("Before you get started, we need to ask a few questions to set you up with the best DMP template for your needs.") %>

-
+ <%= form_for @plan, method: :post do |f| %>

<%= _('Where will the research be conducted?') %>

@@ -55,7 +55,8 @@
- -
+ + + <% end %> diff --git a/app/views/plans/possible_templates.json.erb b/app/views/plans/possible_templates.json.erb deleted file mode 100644 index f0467c6..0000000 --- a/app/views/plans/possible_templates.json.erb +++ /dev/null @@ -1,4 +0,0 @@ -{ - 'msg': '<%= @msg %>', - 'templates': <%= @templates.to_json %> -} \ No newline at end of file diff --git a/lib/assets/javascripts/jquery-accessible-autocomplet-list-aria.js b/lib/assets/javascripts/jquery-accessible-autocomplet-list-aria.js index 60d3b26..dfb04c9 100755 --- a/lib/assets/javascripts/jquery-accessible-autocomplet-list-aria.js +++ b/lib/assets/javascripts/jquery-accessible-autocomplet-list-aria.js @@ -235,7 +235,6 @@ setTimeout(function(){ $input_text.focus(); }, 300); // timeout to avoid problem in suggestions display } } - }) // tab + down management in list of suggestions .on( 'keydown', '.js-suggestion', function( event ) { @@ -346,7 +345,6 @@ $suggestions.empty(); $suggestions_text.empty(); } - }); diff --git a/lib/assets/javascripts/new_plan.js b/lib/assets/javascripts/new_plan.js index 16a0f59..861f0d9 100644 --- a/lib/assets/javascripts/new_plan.js +++ b/lib/assets/javascripts/new_plan.js @@ -11,7 +11,7 @@ }); $(".combobox-container input.js-combobox").change(function(){ - retrieveTemplates(); + toggleSubmit(); }); // Initialize the clear buttons on load @@ -24,16 +24,35 @@ $(this).css("display", 'none'); }); - // If the user clicks the no Organisation checkbox disable the dropdown + // If the user clicks the no Organisation checkbox disable the dropdown and hide clear button $("#plan_no_org").click(function(){ $("#plan_org_name").prop("disabled", this.checked).val(""); - retrieveTemplates(); + displayClearButton($("#plan_org_name")); + toggleSubmit(); }); - // If the user clicks the no Funder checkbox disable the dropdown + // If the user clicks the no Funder checkbox disable the dropdown and hide clear button $("#plan_no_funder").click(function(){ $("#plan_funder_name").prop("disabled", this.checked).val(""); - retrieveTemplates(); + displayClearButton($("#plan_funder_name")); + toggleSubmit(); + }); + + $(".form-submit").click(function(e){ + e.preventDefault(); + + retrieveTemplates(function(hash){ + if(hash['templates']){ + if(hash['templates'].length > 1){ + // Display they template selector + console.log(hash); + }else{ + $("#plan_template_id").val(hash['templates'][0]['id']); + // submit the page + $("form").submit(); + } + } + }); }); }); @@ -62,7 +81,7 @@ } // AJAX call to retrieve the list of available templates -function retrieveTemplates(){ +function retrieveTemplates(callback){ var retrieve = ($("#plan_no_org").prop("checked") || $("#plan_org_name").val().trim().length > 0) && ($("#plan_no_funder").prop("checked") || @@ -70,14 +89,10 @@ if(retrieve){ var args = {org_name: $("#plan_org_name").val(), - funder_name: $("#plan_funder_name").val()}; - -console.log(args); - - $.get("/plans/possible_templates", args, function(data){ - console.log('DATA: ' + data); + funder_name: $("#plan_funder_name").prop("value")}; + + $.getJSON("/plans/possible_templates", args).done(function(json){ + callback(json); }); } - - toggleSubmit(); } \ No newline at end of file diff --git a/lib/assets/stylesheets/dmproadmap.scss b/lib/assets/stylesheets/dmproadmap.scss index 1850394..10f3dfd 100644 --- a/lib/assets/stylesheets/dmproadmap.scss +++ b/lib/assets/stylesheets/dmproadmap.scss @@ -5,7 +5,7 @@ $white: #FFF; $primary-color: #F49700; -$disabled-button-color: #FFE9B3; +$disabled-button-color: #CCC; $reverse-text: #FFF; @@ -53,7 +53,7 @@ } } - input.form-submit { + button.form-submit { background-color: $primary-color; color: $reverse-text; padding: 10px 30px; @@ -63,7 +63,7 @@ -moz-border-radius: 10px; border-radius: 10px; } - input.form-submit:disabled { + button.form-submit:disabled { background-color: $disabled-button-color; } @@ -113,7 +113,8 @@ } .combobox-suggestion:hover, .combobox-suggestion:focus { - color: #000; + color: $white; + background-color: $primary-color; } .combobox-clear-button {