diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 3fd4b4e..31ba3f9 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -210,10 +210,41 @@ } } - var selectionInterval = setInterval(function(){ + var selectItemsFromJsonArrayInterval = setInterval(function(){ if(i >= array.length){ - clearInterval(selectionInterval); + clearInterval(selectItemsFromJsonArrayInterval); callback(out); } }, 50); } + +/* --------------------------------------------------------------------------- */ +function selectItemFromJsonArray(array, selector, value){ + var out = null; + + for(var i = 0; i < array.length; i++){ + +console.log(i + ') ' + array[i][selector]); + + if(Array.isArray(array[i][selector])){ + +console.log(selector + ' == ' + value + ' found? ' + (array[i][selector].indexOf(value) >= 0)); + + if(array[i][selector].indexOf(value) >= 0){ + out = array[i][selector]; + } + + }else{ + if(array[i][selector].toString() == value.toString()){ + out = array[i][selector]; + } + } + } + + var selectItemFromJsonArrayInterval = setInterval(function(){ + if(i >= array.length || out != null){ + clearInterval(selectItemFromJsonArrayInterval); + callback(out); + } + }, 50); +} \ No newline at end of file diff --git a/app/assets/javascripts/projects.js b/app/assets/javascripts/projects.js index 59a0e71..332743c 100644 --- a/app/assets/javascripts/projects.js +++ b/app/assets/javascripts/projects.js @@ -1,17 +1,9 @@ $( document ).ready(function() { - $(".select2-container").select2(); -/* - $("#project_funder_id").select2({ - placeholder: "Select a funder" - }); -*/ - /* ------------------------------------------------- */ - $("#project_funder_id").change(function () { - // filter the template and guidance options based on the selected funder - update_template_options(); - update_guidance_options(); + // ---------------------------------------------------------- + $("#project_funder_id").change(function(){ + reloadTemplateData(); if($(this).val().length > 0){ $("#other-funder-name").hide(); @@ -24,47 +16,42 @@ $("#institution-control-group").show(); $("#create-plan-button").show(); $("#confirm-funder").text($(this).val()); - }); - - /* ------------------------------------------------- */ + }); + + // ---------------------------------------------------------- + $("#project_institution_id").change(function(){ + reloadTemplateData(); + + $("#confirm-institution").text($("#project_institution_id").select2('data').text); + }); + + // ---------------------------------------------------------- + $("#project_dmptemplate_id").change(function(){ + reloadGuidanceOptions(); + }); + + // ---------------------------------------------------------- $("#no-funder").click(function(e) { e.preventDefault(); $("#project_funder_id").select2("val", ""); - update_template_options(); - update_guidance_options(); $("#institution-control-group").show(); $("#create-plan-button").show(); $("#other-funder-name").show(); $("#confirm-funder").text(I18n.t("helpers.none")); }); - /* ------------------------------------------------- */ + // ---------------------------------------------------------- $("#project_funder_name").change(function(){ $("#confirm-funder").text($(this).val()); }); - /* ------------------------------------------------- */ - $("#project_institution_id").change(function () { - update_template_options(); - update_guidance_options(); - $("#confirm-institution").text($("#project_institution_id").select2('data').text); - }); - - /* ------------------------------------------------- */ + // ---------------------------------------------------------- $("#no-institution").click(function() { $("#project_institution_id").select2("val", ""); - update_template_options(); - update_guidance_options(); $("#confirm-institution").text(I18n.t("helpers.none")); }); - - /* ------------------------------------------------- */ - $("#project_dmptemplate_id").change(function (f) { - //update_guidance_options(); - $("#confirm-template").text($("#project_dmptemplate_id :selected").text()); - }); - - /* ------------------------------------------------- */ + + // ---------------------------------------------------------- $("#project-confirmation-dialog").on("show", function(){ if ($("#confirm-institution").text() == "") { $("#confirm-institution").text(I18n.t("helpers.none")); @@ -85,73 +72,86 @@ $('.select2-choice').hide(); }); - /* ------------------------------------------------- */ + // ---------------------------------------------------------- $("#new-project-cancelled").click(function (){ $("#project-confirmation-dialog").modal("hide"); $('.select2-choice').show(); }); - /* ------------------------------------------------- */ + // ---------------------------------------------------------- $("#new-project-confirmed").click(function (){ $("#new_project").submit(); }); - /* ------------------------------------------------- */ + // ---------------------------------------------------------- //for the default template alert $("#default-template-confirmation-dialog").on("show", function(){ $('.select2-choice').hide(); }); - /* ------------------------------------------------- */ + // ---------------------------------------------------------- $("#default-template-cancelled").click(function (){ $("#default-template-confirmation-dialog").modal("hide"); $('.select2-choice').show(); }); - /* ------------------------------------------------- */ + // ---------------------------------------------------------- $("#default-template-confirmed").click(function (){ $("#default_tag").val('true'); $("#new_project").submit(); }); - - /* ------------------------------------------------- */ - function update_template_options() { - select_element = $("#project_dmptemplate_id"); - select_element.find("option").remove(); - + + // ---------------------------------------------------------- + function reloadTemplateData(){ var orgs = [$("#project_funder_id").val(), $("#project_institution_id").val()]; - - // select all of the templates available to the funder and/or institution + + var template = $("#project_dmptemplate_id :selected").val(); + +console.log('reloading templates'); + selectItemsFromJsonArray(templates, 'organisation', orgs, function(array){ - for(var i = 0; i < array.length; i++){ - var selected = false - if($("#project_dmptemplate_id").val() == array[i]['id']){ - selected = true; - } + // Clear and reload the contents of the dropdown + $("#project_dmptemplate_id").html("").select2( {data: array} ).val(); - select_element.append(""); - } - - if(array.length > 2){ - $("#template-control-group").show(); - }else{ - $("#template-control-group").hide(); + // If there are no templates, hide the dropdown + if(array.length <= 0){ + $("#template-control-group").hide(); + reloadGuidanceOptions(); + + }else{ + // Select the first item in the list if there was none selected + if(template == undefined){ + $("#project_dmptemplate_id").val(array[0]['id']).trigger('change'); + + }else{ + reloadGuidanceOptions(); + } + + $("#template-control-group").show(); + + // if there is only one template disable the dropdown + if(array.length > 1){ + $("#project_dmptemplate_id").prop('disabled', false); + }else{ + $("#project_dmptemplate_id").prop('disabled', true); + } } }); - - $("#confirm-template").text(""); - $("#project_dmptemplate_id").change(); - } + } - /* ------------------------------------------------- */ - function update_guidance_options() { + // ---------------------------------------------------------- + function reloadGuidanceOptions() { var institution = $("#project_institution_id").select2('val'); var template = $("#project_dmptemplate_id :selected").val(); - var options = null; + var options = null; +console.log('reloading guidance'); + + if(!template){ + template = $("#project_dmptemplate_id :selected").children().first().val(); + } + options_container = $("#guidance-control-group"); options_container = options_container.find(".choices-group"); options_container.empty(); @@ -159,22 +159,25 @@ var orgs = [$("#project_funder_id").val(), $("#project_institution_id").val()]; - // select all of the templates available to the funder and/or institution - selectItemsFromJsonArray(guidance_groups, 'organisation', orgs, function(array){ + // select all of the guidance groups available to the funder and/or institution + selectItemsFromJsonArray(guidance_for_template_or_organisation, 'organisation', + institution, function(array){ + array = guidance_always_available.concat(array); + for(var i = 0; i < array.length; i++){ var selected = false - + options_container.append( "
  • " + "" + + array[i]['text'] + "" + "
  • " ); } - + if(array.length > 0){ $("#guidance-control-group").show(); }else{ diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 3dff207..7158224 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -57,6 +57,7 @@ @funders = orgs_of_type(constant("organisation_types.funder"), true) @templates = get_available_templates @guidance_groups = get_available_guidance + @always_guidance = get_always_available_guidance @institutions = orgs_of_type(constant("organisation_types.institution")) respond_to do |format| @@ -299,18 +300,38 @@ end # ----------------------------------------------------------- - def get_available_guidance + def get_always_available_guidance # Exclude Funders, Institutions, or children of Institutions excluded_orgs = orgs_of_type(constant("organisation_types.funder")) + orgs_of_type(constant("organisation_types.institution")) + Organisation.orgs_with_parent_of_type(constant("organisation_types.institution")) - # Get all guidance that does not belong to a funder or institution - guidance_groups = GuidanceGroup.guidance_groups_excluding(excluded_orgs) + GuidanceGroup.guidance_groups_excluding(excluded_orgs) + end + + # ----------------------------------------------------------- + def get_available_guidance + guidance_groups = [] - (guidance_groups + GuidanceGroup.where(optional_subset: true)).uniq + #subset guidance that belong to an institution + optional_gg = GuidanceGroup.where("optional_subset = ? && organisation_id IS NOT NULL", true) + optional_gg.each do|optional| + guidance_groups << optional.id + + optional.organisation.children.each do |o| + o.guidance_groups.each do |gg| + guidance_groups << gg.id + end + end + end + + #If template belongs to a funder and that funder has subset guidance display then. + optional_gg = GuidanceGroup.where("optional_subset = ? && organisation_id IN (?)", true, orgs_of_type(constant("organisation_types.funder"))) + optional_gg.each do|optional| + guidance_groups << optional.id + end -# (GuidanceGroup.where("guidance_groups.published = ? OR guidance_groups.optional_subset = ?", true, true) + GuidanceGroup.joins(:dmptemplates).where('dmptemplates.id': @templates.collect{|t| t.id})).uniq + GuidanceGroup.where(id: guidance_groups) end end diff --git a/app/models/guidance_group.rb b/app/models/guidance_group.rb index c5d5c24..1468c19 100644 --- a/app/models/guidance_group.rb +++ b/app/models/guidance_group.rb @@ -1,18 +1,18 @@ class GuidanceGroup < ActiveRecord::Base include GlobalHelpers - #associations between tables - belongs_to :organisation + #associations between tables + belongs_to :organisation - has_and_belongs_to_many :guidances, join_table: "guidance_in_group" + has_and_belongs_to_many :guidances, join_table: "guidance_in_group" - has_and_belongs_to_many :projects, join_table: "project_guidance" - has_and_belongs_to_many :dmptemplates, join_table: "dmptemplates_guidance_groups" + has_and_belongs_to_many :projects, join_table: "project_guidance" + has_and_belongs_to_many :dmptemplates, join_table: "dmptemplates_guidance_groups" - accepts_nested_attributes_for :dmptemplates + accepts_nested_attributes_for :dmptemplates - attr_accessible :organisation_id, :name, :optional_subset, :published, :as => [:default, :admin] - attr_accessible :dmptemplate_ids, :as => [:default, :admin] + attr_accessible :organisation_id, :name, :optional_subset, :published, :as => [:default, :admin] + attr_accessible :dmptemplate_ids, :as => [:default, :admin] ## # Converts a guidance group to a string containing the display name diff --git a/app/views/projects/_dropdowns_new_project.html.erb b/app/views/projects/_dropdowns_new_project.html.erb index 3f942ea..23ccf88 100644 --- a/app/views/projects/_dropdowns_new_project.html.erb +++ b/app/views/projects/_dropdowns_new_project.html.erb @@ -11,7 +11,6 @@