diff options
| author | Marc Florisson | 2014-05-07 19:35:58 +0200 |
|---|---|---|
| committer | Marc Florisson | 2014-05-07 19:35:58 +0200 |
| commit | f3dc45b3f62e55cf1d060ab67c2fdaea6443b13c (patch) | |
| tree | 864d0b55b80a32aa96d2738a20966a7603742347 | |
| parent | c088a3b8d8c355191d4eed33e55be599124c5e0d (diff) | |
| download | chouette-core-f3dc45b3f62e55cf1d060ab67c2fdaea6443b13c.tar.bz2 | |
fix error message on route's stop points edition
| -rw-r--r-- | app/views/routes/_form.html.erb | 93 | ||||
| -rw-r--r-- | app/views/routes/_stop_point_fields.html.erb | 15 |
2 files changed, 47 insertions, 61 deletions
diff --git a/app/views/routes/_form.html.erb b/app/views/routes/_form.html.erb index 091f68278..b11e94f2c 100644 --- a/app/views/routes/_form.html.erb +++ b/app/views/routes/_form.html.erb @@ -23,21 +23,15 @@ <%= form.action :cancel, :as => :link %> <% end %> <% end %> - <script> var stop_point_ids = []; -var order_position; -order_position = function() { +var order_position = function() { $('#stop_points input[type="hidden"][id$="position"]').each(function(index) { $(this).val(index); }); }; -var read_stop_point_ids = function(){ - $('#stop_points input[type="hidden"][class="stop_point_id"]').map(function() { - return $(this).val(); - }); -} + var write_stop_point_ids = function(){ $('#stop_points input[type="hidden"][class~="stop_point_id"]').each(function(index,element) { @@ -51,7 +45,32 @@ var write_stop_point_ids = function(){ }); } +var empty_stop_point_ids = function() { + while(stop_point_ids.length > 0) { + stop_point_ids.pop(); + } + $('#stop_points input[type="hidden"][class="stop_point_id"]').map(function() { + stop_point_ids.push( $(this).val()); + }); + // console.log( "before-remove"); + // console.log( stop_point_ids); +}; +var stop_area_selection_token_input = function( element ) { + element.tokenInput('<%= referential_stop_point_areas_path(@referential, :format => :json) %>', + { crossDomain: false, + prePopulate: $(element).data('pre'), + tokenLimit: 1, + minChars: 3, + hintText: '<%= t('search_hint') %>', + noResultsText: '<%= t('no_result_text') %>', + searchingText: '<%= t('searching_term') %>' } + ); +}; +$(document).ready( function() { + $( ".new_stop_point" ).each( function(index, element){ + stop_area_selection_token_input( $(element)) + }); $('#stop_points').sortable({ axis: 'y', dropOnEmpty: false, @@ -61,58 +80,38 @@ var write_stop_point_ids = function(){ opacity: 0.4, scroll: true, start: function( event, ui ) { - while(stop_point_ids.length > 0) { - stop_point_ids.pop(); - } - $('#stop_points input[type="hidden"][class="stop_point_id"]').map(function() { - stop_point_ids.push( $(this).val()); - }); - console.log( stop_point_ids ); + empty_stop_point_ids(); }, update: function( event, ui ) { - $('#stop_points input[type="hidden"][class~="stop_point_id"]').each( function( index,element) { - if ( $(element).hasClass("added_stop_point") ){ - $(this).attr("value", ""); - } else { - $(this).attr("value", stop_point_ids.shift()); - } - }); + write_stop_point_ids(); order_position(); } }); + $('#stop_points').bind("cocoon:after-insert", function(event, insertedItem) { + stop_area_selection_token_input( insertedItem.find( ".new_stop_point" )); -$('#stop_points').bind("cocoon:after-insert", function(event, insertedItem) { - //$( ".stop_point" ).last().val( ); + var new_stop_count = $('div.nested-fields.stop_point').size(); + var cocoonId = insertedItem.find("input.new_stop_point").attr("id").match( /route_stop_points_attributes_(\d+)_stop_area_id/)[1]; + insertedItem.find('input').each( function(index,e){ + var old = $(e).attr("name"); + if (old!=undefined) { + $(e).attr("name", old.replace( cocoonId, new_stop_count - 1)); + } + }); - var new_stop_count = $('div.nested-fields.stop_point').size(); - var cocoonId = insertedItem.find("input.new_stop_point").attr("id").match( /route_stop_points_attributes_(\d+)_stop_area_id/)[1]; - insertedItem.find('input').each( function(index,e){ - var old = $(e).attr("name"); - if (old!=undefined) { - $(e).attr("name", old.replace( cocoonId, new_stop_count - 1)); - } + //console.log( "cocoonId="+cocoonId+", new_stop_count="+new_stop_count); + order_position(); }); - console.log( "cocoonId="+cocoonId+", new_stop_count="+new_stop_count); - order_position(); -}); + $('#stop_points').on("cocoon:before-remove", empty_stop_point_ids); -$('#stop_points').on("cocoon:before-remove", function() { - while(stop_point_ids.length > 0) { - stop_point_ids.pop(); - } - $('#stop_points input[type="hidden"][class="stop_point_id"]').map(function() { - stop_point_ids.push( $(this).val()); + $('#stop_points').on("cocoon:after-remove", function() { + write_stop_point_ids(); + order_position(); }); - //var stop_point_ids = read_stop_point_ids(); - console.log( "before-remove"); - console.log( stop_point_ids); }); -$('#stop_points').on("cocoon:after-remove", function() { - write_stop_point_ids(); - order_position(); -}); + </script> diff --git a/app/views/routes/_stop_point_fields.html.erb b/app/views/routes/_stop_point_fields.html.erb index e36ee460c..72790b519 100644 --- a/app/views/routes/_stop_point_fields.html.erb +++ b/app/views/routes/_stop_point_fields.html.erb @@ -5,20 +5,7 @@ <% if f.object.stop_area.nil? %> <%= f.input :id, :as => :hidden, :input_html => { :class => "stop_point_id added_stop_point", :value => "" } %> <%= f.input :position, :as => :hidden, :input_html => { :class => "position" } %> - <%= f.input :stop_area_id, :label => false, :wrapper_html => { :class => (f.object.valid? ? "" : "error") }, :input_html => { :class => "new_stop_point stop_area_id", :"data-pre" => [].to_json } %> - <script> - $(function() { - $( ".new_stop_point" ).last().tokenInput('<%= referential_stop_point_areas_path(@referential, :format => :json) %>', - { crossDomain: false, - prePopulate: $('#stop_area_id').data('pre'), - tokenLimit: 1, - minChars: 3, - hintText: '<%= t('search_hint') %>', - noResultsText: '<%= t('no_result_text') %>', - searchingText: '<%= t('searching_term') %>' } - ); - }); - </script> + <%= f.input :stop_area_id, :label => false, :input_html => { :class => "new_stop_point stop_area_id", :"data-pre" => [].to_json } %> <% else %> <%= f.input :id, :as => :hidden, :input_html => { :class => "stop_point_id" } %> <%= f.input :position, :as => :hidden, :input_html => { :class => "position" } %> |
