[#1421] linked dropdwon user view

This commit is contained in:
Frederic Merizen 2018-06-21 16:17:59 +02:00
parent 2f206af0d6
commit dad3bc092b
4 changed files with 117 additions and 1 deletions

View file

@ -0,0 +1,30 @@
document.addEventListener('turbolinks:load', function() {
var masters, i, master, slave, slaveOptions;
masters = document.querySelectorAll('select[data-slave-options]');
for (i = 0; i < masters.length; i++) {
master = masters[i];
slave = document.querySelector('select[data-slave-id="' + master.dataset.masterId + '"]');
slaveOptions = JSON.parse(master.dataset.slaveOptions);
master.addEventListener('change', function(e) {
var option, options, element;
while ((option = slave.firstChild)) {
slave.removeChild(option);
}
options = slaveOptions[e.target.value];
for (i = 0; i < options.length; i++) {
option = options[i];
element = document.createElement("option");
element.textContent = option;
element.value = option;
slave.appendChild(element);
}
slave.selectedIndex = 0;
});
}
});

View file

@ -145,7 +145,7 @@ module NewUser
def champs_params
params.permit(dossier: {
champs_attributes: [
:id, :value, :piece_justificative_file, value: [],
:id, :value, :master_value, :slave_value, :piece_justificative_file, value: [],
etablissement_attributes: Champs::SiretChamp::ETABLISSEMENT_ATTRIBUTES
]
})

View file

@ -0,0 +1,10 @@
- if champ.drop_down_list && champ.drop_down_list.options.any?
- champ_id = champ.object_id
= form.select :master_value,
champ.master_options,
{ required: champ.mandatory? },
{ data: { "slave-options" => champ.slave_options, "master-id" => champ_id } }
= form.select :slave_value,
champ.slave_options[champ.master_value],
{ required: champ.mandatory? },
{ data: { "slave-id" => champ_id } }

View file

@ -0,0 +1,76 @@
require 'spec_helper'
feature 'linked dropdown lists' do
let(:password) { 'secret_password' }
let!(:user) { create(:user, password: password) }
let(:list_items) do
<<~END_OF_LIST
--Master 1--
Slave 1.1
Slave 1.2
--Master 2--
Slave 2.1
Slave 2.2
Slave 2.3
END_OF_LIST
end
let(:drop_down_list) { create(:drop_down_list, value: list_items) }
let(:type_de_champ) { create(:type_de_champ_linked_drop_down_list, libelle: 'linked dropdown', drop_down_list: drop_down_list) }
let!(:procedure) do
p = create(:procedure, :published, :for_individual)
p.types_de_champ << type_de_champ
p
end
let(:user_dossier) { user.dossiers.first }
scenario 'change master value, slave options are updated', js: true do
log_in(user.email, password, procedure)
fill_individual
# Select a master value
select('Master 2', from: master_id_for('linked dropdown'))
# Slave menu reflects chosen master value
expect(page).to have_select(slave_id_for('linked dropdown'), options: ['', 'Slave 2.1', 'Slave 2.2', 'Slave 2.3'])
# Select another master value
select('Master 1', from: master_id_for('linked dropdown'))
# Slave menu gets updated
expect(page).to have_select(slave_id_for('linked dropdown'), options: ['', 'Slave 1.1', 'Slave 1.2'])
end
private
def log_in(email, password, procedure)
visit "/commencer/#{procedure.procedure_path.path}"
expect(page).to have_current_path(new_user_session_path)
fill_in 'user_email', with: email
fill_in 'user_password', with: password
click_on 'Se connecter'
expect(page).to have_current_path(identite_dossier_path(user_dossier))
end
def fill_individual
fill_in('individual_prenom', with: 'prenom')
fill_in('individual_nom', with: 'nom')
check 'dossier_autorisation_donnees'
click_on 'Continuer'
expect(page).to have_current_path(modifier_dossier_path(user_dossier))
end
def master_id_for(libelle)
find(:xpath, ".//label[contains(text()[normalize-space()], '#{libelle}')]")[:for]
end
def slave_id_for(libelle)
master_id = master_id_for(libelle)
link = find("\##{master_id}")['data-master-id']
find("[data-slave-id=\"#{link}\"]")['id']
end
end