aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Florisson2014-04-04 07:32:04 +0200
committerMarc Florisson2014-04-04 07:32:04 +0200
commit2921684d388c738dcf29b3059b40f6348bc0edfc (patch)
tree189045d99b464c1982e1184a1d4fa1ebec964908
parentcb8083bfaab30ad168e3ace50ab77b30f78754fe (diff)
downloadchouette-core-2921684d388c738dcf29b3059b40f6348bc0edfc.tar.bz2
fix stop point add and remove
-rw-r--r--app/views/routes/_form.html.erb74
-rw-r--r--app/views/routes/_stop_point_fields.html.erb21
2 files changed, 93 insertions, 2 deletions
diff --git a/app/views/routes/_form.html.erb b/app/views/routes/_form.html.erb
index 152fe6d97..d909da12d 100644
--- a/app/views/routes/_form.html.erb
+++ b/app/views/routes/_form.html.erb
@@ -25,4 +25,78 @@
<%= form.action :cancel, :as => :link %>
<% end %>
<% end %>
+<script>
+// Sorting the list
+
+ $(document).ready(function(){
+ $('.stop_points.content').on('cocoon:after-insert', function(e, insertedItem) {
+ // ... do something
+ console.log("insertedItem="+insertedItem);
+ var total = $( "input.position" ).length;
+ var input_position = insertedItem.find( "input.position" );
+ $(input_position).attr( "value", total - 1);
+ console.log( "position set = " + $(input_position).val());
+ });
+
+ $('#stop_points').sortable({
+ axis: 'y',
+ dropOnEmpty: false,
+ handle: '.handle',
+ cursor: 'crosshair',
+ items: '.stop_points.nested-fields',
+ opacity: 0.4,
+ scroll: true,
+ update: function(){
+ $('.stop_points input.position').map(function(index,element){
+// var stop_point_id =$('#sortable_stop_points .stop_point')[index].id.replace("stop_point_","");
+// var before = $( element).attr("value");
+// $( element).attr("value",stop_point_id);
+// var after = $( element).attr("value");
+
+// change stop_point stop_area_id
+// trouver le stop_area correspond à l'index
+ var stopAreaInitialList = <%= @route.stop_points.map(&:stop_area_id).compact.to_json %>;
+
+ // list added stop area ids
+ var addedStopAreaIds = [];
+ $('input.new_stop_point').each ( function(index,element) { addedStopAreaIds.push( parseInt( $(element).val())); });
+ console.log(addedStopAreaIds);
+
+ // list added stop area positions
+ var addedStopAreaPositions = [];
+ $('input.position.added').each ( function(index,element) {
+ console.log( "parseInt( $( element ).val())="+parseInt( $( element ).val()) );
+ console.log( " stopAreaInitialList.length="+ stopAreaInitialList.length );
+ var position = parseInt( $( element ).val()) - stopAreaInitialList.length;
+ addedStopAreaPositions.push( position);
+ });
+ console.log(addedStopAreaPositions);
+
+ if (addedStopAreaIds.length != addedStopAreaPositions.length) {
+ console.log( "Erreur, arrays should have same length: addedStopAreaIds.length="+insertedItem.length+", addedStopAreaPositions.length = "+ addedStopAreaPositions.length)
+ }
+ for ( var i=0; i<addedStopAreaIds.length; i++ ) {
+ stopAreaInitialList.push( addedStopAreaIds[ addedStopAreaPositions[ i ]]);
+ }
+ console.log(stopAreaInitialList);
+
+ // si la position de l'élément a été modifiée
+ // autrement dit le stop_area_id occupe la même position
+ var stopPointPosition = parseInt( $( element ).attr("value"));
+ console.log( "index = " + index + ", position = " + stopPointPosition +
+ ", stop_area_id old id = "+
+ $( '#route_stop_points_attributes_'+index+'_stop_area_id' ).attr("value") +
+ ", new id = " + stopAreaInitialList[ stopPointPosition]
+ );
+ // maj de la référence au stop area id
+ $( '#route_stop_points_attributes_'+index+'_stop_area_id' ).attr( "value", stopAreaInitialList[ stopPointPosition]);
+
+ // rectifier le stop_point associé à la suppression
+ $($('.remove_fields.existing').prev("input[type=hidden]")[index]).attr("name","route[stop_points_attributes]["+index+"][_destroy]");
+
+ });
+ }
+ });
+ });
+</script>
diff --git a/app/views/routes/_stop_point_fields.html.erb b/app/views/routes/_stop_point_fields.html.erb
index af388bc47..de4f50e5f 100644
--- a/app/views/routes/_stop_point_fields.html.erb
+++ b/app/views/routes/_stop_point_fields.html.erb
@@ -1,9 +1,26 @@
<%= f.inputs :class => 'stop_points nested-fields' do %>
- <%= f.input :stop_area_id, :as => :hidden %>
+ <div class="handle" alt="<%= t('stop_points.index.move') %>" title="<%= t('stop_points.index.move') %>" ><%= image_tag "icons/move.png" %></div>
<% if f.object.stop_area.nil? %>
- <p>nouveau formulaire</p>
+ <%= f.input :position, :as => :hidden, :input_html => { :class => "position added" } %>
+ <%= f.input :stop_area_id, :label => t('.select_area'),:input_html => { :class => "new_stop_point", :"data-pre" => [].to_json } %>
<% else %>
+ <%= f.input :position, :as => :hidden, :input_html => { :class => "position" } %>
+ <%= f.input :stop_area_id, :as => :hidden %>
<%= f.object.stop_area.name %>
<% end %>
<%= link_to_remove_association t('actions.destroy'), f %>
<% end %>
+
+<script>
+ $(function() {
+ $( ".new_stop_point" ).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>