diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 77fde59..46c5157 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -156,18 +156,27 @@ authorize @plan attrs = plan_params - # Save the guidance group selections - guidance_group_ids = params[:guidance_group_ids].blank? ? [] : params[:guidance_group_ids].map(&:to_i).uniq - save_guidance_selections(guidance_group_ids) - respond_to do |format| - if @plan.update_attributes(attrs) - format.html { redirect_to @plan, :editing => false, notice: success_message(_('plan'), _('saved')) } - format.json {render json: {code: 1, msg: success_message(_('plan'), _('saved'))}} - else + begin + # Save the guidance group selections + guidance_group_ids = params[:guidance_group_ids].blank? ? [] : params[:guidance_group_ids].map(&:to_i).uniq + @plan.guidance_groups = GuidanceGroup.where(id: guidance_group_ids) +puts @plan.guidance_groups.collect(&:name).join(', ') + @plan.save + + if @plan.update_attributes(attrs) + format.html { redirect_to @plan, :editing => false, notice: success_message(_('plan'), _('saved')) } + format.json {render json: {code: 1, msg: success_message(_('plan'), _('saved'))}} + else + flash[:alert] = failed_update_error(@plan, _('plan')) + format.html { render action: "edit" } + format.json {render json: {code: 0, msg: flash[:alert]}} + end + + rescue Exception flash[:alert] = failed_update_error(@plan, _('plan')) format.html { render action: "edit" } - format.json {render json: {code: 0, msg: failed_update_error(@plan, _('plan'))}} + format.json {render json: {code: 0, msg: flash[:alert]}} end end end @@ -350,25 +359,6 @@ :data_contact, :data_contact_email, :data_contact_phone, :guidance_group_ids) end - def save_guidance_selections(guidance_group_ids) - all_guidance_groups = @plan.get_guidance_group_options - plan_groups = @plan.guidance_groups - guidance_groups = GuidanceGroup.where(id: guidance_group_ids) - all_guidance_groups.each do |group| - # case where plan group exists but not in selection - if plan_groups.include?(group) && ! guidance_groups.include?(group) - # remove from plan groups - @plan.guidance_groups.delete(group) - end - # case where plan group dosent exist and in selection - if !plan_groups.include?(group) && guidance_groups.include?(group) - # add to plan groups - @plan.guidance_groups << group - end - end - @plan.save - 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/lib/assets/javascripts/views/plans/edit_details.js b/lib/assets/javascripts/views/plans/edit_details.js index f63eb51..b016906 100644 --- a/lib/assets/javascripts/views/plans/edit_details.js +++ b/lib/assets/javascripts/views/plans/edit_details.js @@ -23,29 +23,46 @@ showHideDataContact($('#show_data_contact')); // Keep the modal window's guidance selections in line with selections on the main page - const syncGuidance = () => { - const choices = $('#priority-guidance-orgs, #other-guidance-orgs').find('input[type="checkbox"]:checked') - .map((i, el) => $(el).val()).get() - .filter((v, i, a) => a.indexOf(v) === i); + const syncGuidance = (ctx) => { + const currentList = $(ctx); + const otherList = (currentList.attr('id') === 'priority-guidance-orgs' ? $('#other-guidance-orgs') : $('#priority-guidance-orgs')); + const selections = currentList.find('input[type="checkbox"]:checked').map((i, el) => $(el).val()).get(); - $('#priority-guidance-orgs, #other-guidance-orgs').find('input[type="checkbox"]').each((i, el) => { - const target = $(el); - if (choices.indexOf(target.val()) >= 0) { - target.attr('checked'); + otherList.find('input[type="checkbox"]').each((i, el) => { + const checkbox = $(el); + // Toggle the checked flag to match the current guidance list + if (selections.indexOf(checkbox.val()) >= 0) { + checkbox.attr('checked'); } else { - target.removeAttr('checked'); + checkbox.removeAttr('checked'); + } - // Disable the checkbox if it is unchecked and the user has already selected the max - if (choices.length >= MAX_NUMBER_GUIDANCE_SELECTIONS) { - target.attr('disabled', 'disabled'); - } else { - target.removeAttr('disabled'); - } + // If we have reached the selection limit toggle the disabled flag on the other guidance list + if (selections.length >= MAX_NUMBER_GUIDANCE_SELECTIONS && !checkbox.is(':checked')) { + checkbox.attr('disabled', 'disabled'); + } else { + checkbox.removeAttr('disabled'); } }); + + // If we have reached the selection limit toggle the disabled flag on the current guidance list + if (selections.length >= MAX_NUMBER_GUIDANCE_SELECTIONS) { + currentList.find('input[type="checkbox"]').each((i, el) => { + const checkbox = $(el); + if (el.is(':checked')) { + checkbox.removeAttr('disabled'); + } else { + checkbox.attr('disabled', 'disabled'); + } + }); + } }; - $('#other-guidance-orgs').find('input[type="checkbox"]').click(syncGuidance); - $('#priority-guidance-orgs').find('input[type="checkbox"]').click(syncGuidance); + $('#other-guidance-orgs').find('input[type="checkbox"]').click((e) => { + syncGuidance($(e.target).closest('ul')); + }); + $('#priority-guidance-orgs').find('input[type="checkbox"]').click((e) => { + syncGuidance($(e.target).closest('ul')); + }); });