diff options
| author | jpl | 2017-06-14 15:40:52 +0200 |
|---|---|---|
| committer | jpl | 2017-06-14 15:40:59 +0200 |
| commit | 03cc12b4954519bc1a6c6ecf3c1051dceb4fc507 (patch) | |
| tree | a1b3b0c9b04e644c778520a2ffe483e8a39b5c04 | |
| parent | cbdc2ad9f7d51c9a5cf17d76a1025475377a04b8 (diff) | |
| download | chouette-core-03cc12b4954519bc1a6c6ecf3c1051dceb4fc507.tar.bz2 | |
Refs #3487: updating form adding JS management for stop_points_ids
7 files changed, 90 insertions, 38 deletions
diff --git a/app/assets/javascripts/routing_constraint_zones.coffee b/app/assets/javascripts/routing_constraint_zones.coffee index e978cd29a..8575e9f02 100644 --- a/app/assets/javascripts/routing_constraint_zones.coffee +++ b/app/assets/javascripts/routing_constraint_zones.coffee @@ -1,28 +1,61 @@ -fill_stop_points_options = -> - stop_point_select = $('#routing_constraint_zone_stop_point_ids') - stop_point_select.empty() - referential_id = document.location.pathname.match(/\d+/g)[0] - line_id = document.location.pathname.match(/\d+/g)[1] - route_id = $('#routing_constraint_zone_route_id').val() - - if errors_on_form() - stop_point_ids = eval($('#stop_point_ids').val()) - - $.ajax - url: "/referentials/#{referential_id}/lines/#{line_id}/routes/#{route_id}/stop_points" - dataType: 'json' - success: (data, textStatus, jqXHR) -> - for stop_point in data - selected = $.inArray(stop_point.id, stop_point_ids) != -1 - stop_point_select.append "<option value='#{stop_point.id}'" + "#{if selected then ' selected' else ''}" + ">#{stop_point.name}</option>" - error: (jqXHR, textStatus, errorThrown) -> - console.log textStatus - console.log errorThrown - -errors_on_form = -> - document.location.pathname.endsWith('routing_constraint_zones') && $('#new_routing_constraint_zone').length +@ITL_stoppoints = -> + routeID = $('#routing_constraint_zone_route_id').val() + + if (routeID) + origin = window.location.origin + path = window.location.pathname.split('/', 5).join('/') + reqURL = origin + path + '/routes/' + routeID + '/stop_points.json' + + $.ajax + url: reqURL + dataType: 'json' + error: (jqXHR, textStatus, errorThrown) -> + console.log(errorThrown) + success: (collection, textStatus, jqXHR) -> + html = '' + stopAreaBaseURL = origin + window.location.pathname.split('/', 3).join('/') + '/stop_areas/' + + collection.forEach (item, index) -> + html += "<div class='nested-fields'><div class='wrapper'> + <div><a href='" + stopAreaBaseURL + item.stop_area_id + "' class='navlink' title='Voir l'arrêt'><span>" + item.name + "</span></a></div> + <div><span>" + item.city_name + " (" + item.zip_code + ")</span></div> + <div> + <span class='has_radio'> + <input type='checkbox' name='routing_constraint_zone[stop_point_ids][" + index + "]' value='" + item.id + "'> + <span class='radio-label'></span> + </span> + </div> + </div></div>" + + $('#ITL_stoppoints').find('.nested-fields').remove() + $('#ITL_stoppoints').find('.nested-head').after(html) + + # VALIDATION + selection = [] + $('#ITL_stoppoints').on 'click', "input[type='checkbox']", (e) -> + v = $(e.target).val() + + if ( $.inArray(v, selection) != -1 ) + selection.splice(selection.indexOf(v), 1) + else + selection.push(v) + + alertMsg = "<div class='alert alert-danger' style='margin-bottom:15px;'> + <p class='small'>Un ITL doit comporter au moins deux arrêts</p> + </div>" + + $(document).on 'click', "input[type='submit']", (e)-> + inputName = $('#routing_constraint_zone_name').val() + + $('.alert-danger').remove() + + if ( selection.length < 2 && inputName != "") + e.preventDefault() + $('#routing_constraint_zone_name').closest('.form-group').removeClass('has-error').find('.help-block').remove() + $('#ITL_stoppoints').prepend(alertMsg) $ -> - if document.location.pathname.endsWith('routing_constraint_zones/new') || errors_on_form() - fill_stop_points_options() - $('#routing_constraint_zone_route_id').change(fill_stop_points_options) + ITL_stoppoints() + + $('#routing_constraint_zone_route_id').on 'change', -> + ITL_stoppoints() diff --git a/app/controllers/route_stop_points_controller.rb b/app/controllers/route_stop_points_controller.rb index e12acb33b..730bd08a9 100644 --- a/app/controllers/route_stop_points_controller.rb +++ b/app/controllers/route_stop_points_controller.rb @@ -11,8 +11,7 @@ class RouteStopPointsController < ChouetteController def index respond_to do |format| - format.json { render json: referential.lines.find(params[:line_id]).routes.find(params[:route_id]).stop_points.map { |sp| { id: sp.id, name: sp.name } } } + format.json { render json: referential.lines.find(params[:line_id]).routes.find(params[:route_id]).stop_points.map { |sp| { id: sp.id, stop_area_id: sp.stop_area.id, name: sp.name, zip_code: sp.stop_area.zip_code, city_name: sp.stop_area.city_name } } } end end end - diff --git a/app/controllers/routing_constraint_zones_controller.rb b/app/controllers/routing_constraint_zones_controller.rb index b5072f401..7707427b0 100644 --- a/app/controllers/routing_constraint_zones_controller.rb +++ b/app/controllers/routing_constraint_zones_controller.rb @@ -4,7 +4,7 @@ class RoutingConstraintZonesController < ChouetteController defaults resource_class: Chouette::RoutingConstraintZone respond_to :html, :xml, :json - before_action :remove_empty_stop_point, only: [:create, :update] + before_action :check_stoppoint_param, only: [:create, :update] belongs_to :referential do belongs_to :line, parent_class: Chouette::Line @@ -56,7 +56,16 @@ class RoutingConstraintZonesController < ChouetteController ) end - def remove_empty_stop_point - params.require(:routing_constraint_zone)[:stop_point_ids].delete('') + def check_stoppoint_param + spArr = [] + if params.require(:routing_constraint_zone)[:stop_point_ids] and params.require(:routing_constraint_zone)[:stop_point_ids].length >= 2 + params.require(:routing_constraint_zone)[:stop_point_ids].each do |k,v| + spArr << v + end + params.require(:routing_constraint_zone)[:stop_point_ids] = spArr + else + Rails.logger.error("Error: An ITL must have at least two stop points") + end end + end diff --git a/app/views/routing_constraint_zones/_form.html.slim b/app/views/routing_constraint_zones/_form.html.slim index 082e8f7f8..3d4764ef7 100644 --- a/app/views/routing_constraint_zones/_form.html.slim +++ b/app/views/routing_constraint_zones/_form.html.slim @@ -3,12 +3,23 @@ .row .col-lg-12 = form.input :name - = form.input :route_id, collection: @line.routes.select { |route| route.stop_points.count > 2 }, include_blank: false - - stop_points_collection = @routing_constraint_zone.persisted? ? @routing_constraint_zone.route.stop_points : [] - = form.input :stop_point_ids, as: :select, collection: stop_points_collection, selected: @routing_constraint_zone.stop_point_ids, label: Chouette::StopPoint.model_name.human.pluralize.capitalize, label_method: :name, input_html: { 'data-select2ed': 'true', 'data-select2ed-placeholder': 'Sélection des arrêts sur séquence d\'arrêts', 'multiple': 'multiple', style: 'width: 100%' } + .separator - = form.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'itl_form' + #ITL_stoppoints + .subform + .nested-head + .wrapper + div + .form-group + label.control-label Arrêt + div + .form-group + label.control-label Commune + div = hidden_field_tag 'stop_point_ids', @routing_constraint_zone.stop_point_ids.to_s, id: 'stop_point_ids' + + + = form.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'itl_form' diff --git a/config/locales/routing_constraint_zones.en.yml b/config/locales/routing_constraint_zones.en.yml index 8e9dbfb02..59d4484a8 100644 --- a/config/locales/routing_constraint_zones.en.yml +++ b/config/locales/routing_constraint_zones.en.yml @@ -12,6 +12,7 @@ en: objectid: Object ID stop_points_count: Number of stop points route: Associated route + route_id: Associated route errors: models: routing_constraint_zone: diff --git a/config/locales/routing_constraint_zones.fr.yml b/config/locales/routing_constraint_zones.fr.yml index 3e207fac0..4faff6606 100644 --- a/config/locales/routing_constraint_zones.fr.yml +++ b/config/locales/routing_constraint_zones.fr.yml @@ -12,6 +12,7 @@ fr: objectid: Object ID stop_points_count: Nombre d'arrêts route: Itinéraire associé + route_id: Itinéraire associé errors: models: routing_constraint_zone: diff --git a/spec/controllers/route_stop_points_controller_spec.rb b/spec/controllers/route_stop_points_controller_spec.rb index 2f5fa41c7..ac9e2f11b 100644 --- a/spec/controllers/route_stop_points_controller_spec.rb +++ b/spec/controllers/route_stop_points_controller_spec.rb @@ -15,9 +15,7 @@ RSpec.describe RouteStopPointsController, type: :controller do end it 'returns a JSON of stop areas' do - expect(response.body).to eq(route.stop_points.map { |sp| { id: sp.id, name: sp.name } }.to_json) + expect(response.body).to eq(route.stop_points.map { |sp| { id: sp.id, stop_area_id: sp.stop_area.id, name: sp.name, zip_code: sp.stop_area.zip_code, city_name: sp.stop_area.city_name } }.to_json) end end end - - |
